diff --git a/404.html b/404.html index 8f61a753a..dae4e3fd9 100644 --- a/404.html +++ b/404.html @@ -134,4 +134,4 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/404/index.html b/404/index.html index d480abe1a..4cbfce118 100644 --- a/404/index.html +++ b/404/index.html @@ -134,4 +134,4 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/alla-uppgifter/index.html b/alla-uppgifter/index.html index a9285ce8f..c5c635b67 100644 --- a/alla-uppgifter/index.html +++ b/alla-uppgifter/index.html @@ -136,4 +136,4 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/bedomningar-och-prov/index.html b/bedomningar-och-prov/index.html index 9a1812425..270a5371d 100644 --- a/bedomningar-och-prov/index.html +++ b/bedomningar-och-prov/index.html @@ -138,7 +138,7 @@ } } }) -

Bedömning och prov

Grund för bedömningen

Bedömningen baseras på både poäng från gjorda uppgifter och tenten. Poäng ges för alla uppgifter i varje modul. Du hittar modulens alla uppgifter genom att välja "Uppgifter i den här modulen" under respektive modul, och du kan hålla koll på dina egna uppgiftspoäng under rubriken "Uppgiftspoäng totalt" i poängvisualiseraren som finns i kursmaterialets nedre högra hörn (blå cirkel).

Tentamen

Att delta i tenten

Du kan delta i vilken tent som helst då du har gjort minimiantalet uppgifter. Det betyder i praktiken att du ska ha samlat minst 25 % av alla poäng som hör till de kapitel som ingår i tenten senast på tentdagens morgon klockan 8.00. Ifall du gör fortsättningskursens tent, ska modul 14 vara helt slutförd. Ifall poängen saknas, bedöms inte din tentprestation.

Poängen granskas skilt för intoduktionskursen (del 1-7) och fortsättningskursen (del 8-14).

Om du vill får du delta i flera tenttillfällen. Den bästa prestationen lagras i studieregistret.

Tentarrangemang

    +

    Bedömning och prov

    Grund för bedömningen

    Bedömningen baseras på både poäng från gjorda uppgifter och tenten. Poäng ges för alla uppgifter i varje modul. Du hittar modulens alla uppgifter genom att välja "Uppgifter i den här modulen" under respektive modul, och du kan hålla koll på dina egna uppgiftspoäng under rubriken "Uppgiftspoäng totalt" i poängvisualiseraren som finns i kursmaterialets nedre högra hörn (blå cirkel).

    Tentamen

    Att delta i tenten

    Du kan delta i vilken tent som helst då du har gjort minimiantalet uppgifter. Det betyder i praktiken att du ska ha samlat minst 25 % av alla poäng som hör till de kapitel som ingår i tenten senast på tentdagens morgon klockan 8.00. Ifall du gör fortsättningskursens tent, ska modul 14 vara helt slutförd. Ifall poängen saknas, bedöms inte din tentprestation.

    Poängen granskas skilt för intoduktionskursen (del 1-7) och fortsättningskursen (del 8-14).

    Om du vill får du delta i flera tenttillfällen. Den bästa prestationen lagras i studieregistret.

    Tentarrangemang

    • Du behöver inte anmäla dig till tenten på förhand.
    • Du kan utföra tenten på den valda tentdagen mellan kl 10:00-22:00
    • Du har fyra timmar på dig att göra tenten. Ifall du har fått särskilt lov för extratid till tenter, är maxtiden fem timmar.
    • @@ -181,4 +181,4 @@ underkänt -

    Bedömningen baseras på uppgifterna (utgör 50 % av totalpoängen) och tenten (utgör 50 % av totalpoängen). Detta innebär att vitsordsskalans totalpoäng fås genom att addera ihop poängen från kursuppgifterna (som syns i poängvisualiseraren) och tenten.

    Minimikrav

    För att komma igenom kursen krävs minst 25 % av uppgiftspoängen i varje modul och minst 50 % i tenten.

\ No newline at end of file +

Bedömningen baseras på uppgifterna (utgör 50 % av totalpoängen) och tenten (utgör 50 % av totalpoängen). Detta innebär att vitsordsskalans totalpoäng fås genom att addera ihop poängen från kursuppgifterna (som syns i poängvisualiseraren) och tenten.

Minimikrav

För att komma igenom kursen krävs minst 25 % av uppgiftspoängen i varje modul och minst 50 % i tenten.

\ No newline at end of file diff --git a/credits/index.html b/credits/index.html index 55629ba29..bb6786dc9 100644 --- a/credits/index.html +++ b/credits/index.html @@ -134,4 +134,4 @@ } } }) -

Kiitokset ja materiaalista

Kurssin on tehnyt Helsingin yliopiston Agile Education Research -tutkimusryhmä.

Kurssimateriaali

Kurssimateriaalin ja tehtävien tekijät ovat Erkki Kaila, Antti Laaksonen ja Matti Luukkainen. Muutama kurssin tehtävistä on Arto Hellaksen (né Vihavainen) käsialaa.

Kurssin materiaali on lisensoitu Creative Commons BY-NC-SA 4.0 -lisenssillä, joten voit käyttää ja levittää sitä vapaasti, kunhan alkuperäisten tekijöiden nimiä ei poisteta. Jos teet muutoksia materiaaliin ja haluat levittää muunneltua versiota, se täytyy lisensoida samalla lisenssillä. Materiaalien käyttö kaupalliseen tarkoitukseen on ilman erillistä lupaa kielletty.

Kurssilla käytössä oleva teknologia

Kurssisivuston ovat tehneet Henrik Nygren ja Antti Leinonen. Helsingin yliopiston Agile Education Research -tutkimusryhmä on luonut kurssilla käytetyn ohjelmointitehtävien palautusympäristön (Test My Code) ja sen liitännäiset ohjelmointiympäristöihin, kurssimateriaalissa olevan kyselyjärjestelmän ja muut toiminnot.

\ No newline at end of file +

Kiitokset ja materiaalista

Kurssin on tehnyt Helsingin yliopiston Agile Education Research -tutkimusryhmä.

Kurssimateriaali

Kurssimateriaalin ja tehtävien tekijät ovat Erkki Kaila, Antti Laaksonen ja Matti Luukkainen. Muutama kurssin tehtävistä on Arto Hellaksen (né Vihavainen) käsialaa.

Kurssin materiaali on lisensoitu Creative Commons BY-NC-SA 4.0 -lisenssillä, joten voit käyttää ja levittää sitä vapaasti, kunhan alkuperäisten tekijöiden nimiä ei poisteta. Jos teet muutoksia materiaaliin ja haluat levittää muunneltua versiota, se täytyy lisensoida samalla lisenssillä. Materiaalien käyttö kaupalliseen tarkoitukseen on ilman erillistä lupaa kielletty.

Kurssilla käytössä oleva teknologia

Kurssisivuston ovat tehneet Henrik Nygren ja Antti Leinonen. Helsingin yliopiston Agile Education Research -tutkimusryhmä on luonut kurssilla käytetyn ohjelmointitehtävien palautusympäristön (Test My Code) ja sen liitännäiset ohjelmointiympäristöihin, kurssimateriaalissa olevan kyselyjärjestelmän ja muut toiminnot.

\ No newline at end of file diff --git a/felanmalningar/index.html b/felanmalningar/index.html index 7e2db9c51..89b8cb26c 100644 --- a/felanmalningar/index.html +++ b/felanmalningar/index.html @@ -136,7 +136,7 @@ } } }) -

Vanliga felmeddelanden

På den här sidan informerar vi om vanliga felmeddelanden som du kan stöta på när du programmerar.

+

Vanliga felmeddelanden

På den här sidan informerar vi om vanliga felmeddelanden som du kan stöta på när du programmerar.

Uppgiften går inte igenom, trots att utskriften är identisk med exempelutskriften

Granska att ditt program inte skriver ut extra mellanslag. Observera att i print-funktionen skapar ett kommatecken automatiskt ett mellanslag mellan strängar.

    print("Hello","World!")    # Skriver ut: Hello World!
@@ -166,4 +166,4 @@

print(alder//2) # fel: input läser in data som strängar, och här har variabeln alder inte omvandlats till ett heltal

TypeError: cannot concatenate 'str' and 'int' objects on line [radnummer]

-

Se ovanstående fel.

\ No newline at end of file +

Se ovanstående fel.

\ No newline at end of file diff --git a/index.html b/index.html index 4c57e3a1d..71b3d90ed 100644 --- a/index.html +++ b/index.html @@ -145,7 +145,7 @@ } } }) -

Info om kursen

Kursen börjar onsdagen den 4.9.2024

+

Info om kursen

Kursen börjar onsdagen den 4.9.2024

Välkommen till kurssidorna för Introduktion till programmering (TKT10002, 5 sp) och Fortsättningskurs i programmering (TKT10003, 5 sp)!

Kursens upplägg

Du genomför kurserna genom att göra uppgifter och i slutet av vardera kurs delta i en obligatorisk tentamen. Mer information om bedömningen finns här.

@@ -256,4 +256,4 @@

Kursöversikt1 + 1 poäng tilläggsprestationer

Ifall du tidigare har slutfört motsvarande kurser i Java vid Helsingfors universitet, kan du nu genomföra kurserna i Python och få tilläggsstudiepoäng (1 + 1 sp). Du hittar mer info om detta under rubriken Bedömning och tentamen.

Tidsgräns

-

Kursen är öppen från september 2024 till mars 2025. Du kan göra uppgifterna i egen takt fram tills dess.

\ No newline at end of file +

Kursen är öppen från september 2024 till mars 2025. Du kan göra uppgifterna i egen takt fram tills dess.

\ No newline at end of file diff --git a/ofta-stallda-fragar/index.html b/ofta-stallda-fragar/index.html index 0719b8e67..23e5cadc5 100644 --- a/ofta-stallda-fragar/index.html +++ b/ofta-stallda-fragar/index.html @@ -140,7 +140,7 @@ } } }) -

Ofta ställda frågor

Innehåll

+

Ofta ställda frågor

Innehåll

Jag hittade ett fel i materialet eller uppgifterna

Så bra att du märkte! Meddela gärna handledaren i kurskanalen eller per epost via adressen ohjelmoinnin-mooc@helsinki.fi. Tack!

Varför syns inte alla kursens moduler i TMC? Hur får jag alla modulers uppgifter nedladdade?

@@ -165,4 +165,4 @@

FPA vill ha bevis på mina studier

Du kan ge FPA följande information:

Introduktion till programmering är en webbkurs som är gratis och öppen för alla intresserade. Att delta i kursen kan liknas med att låna en bok från ett bibliotek. Den bok du lånar på biblioteket kan vara relaterad till någon kurs som hör till dina universitetsstudier. Precis som en bok som lånas på biblioteket är vår onlinekurs tillgänglig för läsning för nöjes skull och självutbildning eller för att främja dina universitetsstudier.

-

Introduktion till programmering motsvarar en kurs värd 5 studiepoäng vid Helsingfors universitet. Den som genomför och avlägger kursen är däremot inte automatiskt studerande vid Helsingfors universitet, och vi tvingar inte heller någon att slutföra kursen i ett studiepoängsgivande format. Kursens genomförande kan dock vid önskan ge studiepoäng. Deltagande i kursen ger inte rätt till studiestöd.

\ No newline at end of file +

Introduktion till programmering motsvarar en kurs värd 5 studiepoäng vid Helsingfors universitet. Den som genomför och avlägger kursen är däremot inte automatiskt studerande vid Helsingfors universitet, och vi tvingar inte heller någon att slutföra kursen i ett studiepoängsgivande format. Kursens genomförande kan dock vid önskan ge studiepoäng. Deltagande i kursen ger inte rätt till studiestöd.

\ No newline at end of file diff --git a/onlinetent-ohja/index.html b/onlinetent-ohja/index.html index d32c6ba31..02490caed 100644 --- a/onlinetent-ohja/index.html +++ b/onlinetent-ohja/index.html @@ -134,7 +134,7 @@ } } }) -

Fortsättningskurs i programmerings nättentamen 16.12.

    +

    Fortsättningskurs i programmerings nättentamen 16.12.

    • Tentfrågorna uppenbarar sig på courses.mooc.fi sidan på tentdagen kl 10:00.
    • Länkar till tentuppgifterna hittas på denna sidas del "Utgivning av uppgifter och påbörjande av tentamen"

    Deltagande i tenten

      @@ -192,4 +192,4 @@
    • Tentsvaren granskas med plagieringsigenkänningsprogram. Svindling leder alltid minst till att tentprestationen underkänns, möjligen även till avlägsning ur kursen.
    • Tentfrågor påminner om kursmaterialets uppgifter - som förberedning till tenten lönar det alltså att gå igenom materialet
    • Kursens resultat kommer efter provet till eposten med instruktioner för tillgodoräknande
    • -
    \ No newline at end of file +
\ No newline at end of file diff --git a/onlinetent-ohpe/index.html b/onlinetent-ohpe/index.html index 3d7657daa..7f86f43c6 100644 --- a/onlinetent-ohpe/index.html +++ b/onlinetent-ohpe/index.html @@ -134,7 +134,7 @@ } } }) -

Introduktionskurs i programmerings nättentamen 17.08.

    +

    Introduktionskurs i programmerings nättentamen 17.08.

    • Tentfrågorna uppenbarar sig på courses.mooc.fi sidan på tentdagen kl 10:00.
    • Länkar till tentuppgifterna hittas på denna sidas del "Utgivning av uppgifter och påbörjande av tentamen"

    Deltagande i tenten

      @@ -192,4 +192,4 @@
    • Tentsvaren granskas med plagieringsigenkänningsprogram. Svindling leder alltid minst till att tentprestationen underkänns, möjligen även till avlägsning ur kursen.
    • Tentfrågor påminner om kursmaterialets uppgifter - som förberedning till tenten lönar det alltså att gå igenom materialet
    • Kursens resultat kommer efter provet till eposten med instruktioner för tillgodoräknande
    • -
    \ No newline at end of file +
\ No newline at end of file diff --git a/onlinetent/index.html b/onlinetent/index.html index 6a3c59f2e..5cae13b36 100644 --- a/onlinetent/index.html +++ b/onlinetent/index.html @@ -20,7 +20,9 @@ data-styled.g7[id="TreeViewItem__ListItemLabel-sc-1jcweii-2"]{content:"RPRFr,"}/*!sc*/ .hSetgm{border-left:0.5em solid white;width:100%;background-color:white;}/*!sc*/ .hSetgm:hover{-webkit-text-decoration:none;text-decoration:none;color:black;background-color:#f5ebeb;border-color:#f5ebeb;}/*!sc*/ -data-styled.g8[id="TreeViewItem__NavigationLink-sc-1jcweii-3"]{content:"hSetgm,"}/*!sc*/ +.ddReQ{border-left:0.5em solid white;width:100%;background-color:white;border-color:#f75b4b !important;background-color:#edeaea;}/*!sc*/ +.ddReQ:hover{-webkit-text-decoration:none;text-decoration:none;color:black;background-color:#f5ebeb;border-color:#f5ebeb;}/*!sc*/ +data-styled.g8[id="TreeViewItem__NavigationLink-sc-1jcweii-3"]{content:"hSetgm,ddReQ,"}/*!sc*/ .iTrTgJ{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-align-items:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;}/*!sc*/ .iTrTgJ a{color:black;-webkit-text-decoration:none;text-decoration:none;}/*!sc*/ .iTrTgJ.active-section{background-color:#ffdfdf;border-top-left-radius:5px;border-bottom-left-radius:5px;}/*!sc*/ @@ -134,7 +136,7 @@ } } }) -

Nättentamen 4.6.

Fortsättningskurs i programmering

    +

    Nättentamen 4.6.

    Fortsättningskurs i programmering

    • Tentfrågorna uppenbarar sig på courses.mooc.fi sidan på tentdagen kl 10:00.
    • Länkar till tentuppgifterna hittas på denna sidas del "Utgivning av uppgifter och påbörjande av tentamen"

    Deltagande i tenten

      @@ -192,4 +194,4 @@
    • Tentsvaren granskas med plagieringsigenkänningsprogram. Svindling leder alltid minst till att tentprestationen underkänns, möjligen även till avlägsning ur kursen.
    • Tentfrågor påminner om kursmaterialets uppgifter - som förberedning till tenten lönar det alltså att gå igenom materialet
    • Kursens resultat kommer efter provet till eposten med instruktioner för tillgodoräknande
    • -
    \ No newline at end of file +
\ No newline at end of file diff --git a/osa-1/1-introduktion/index.html b/osa-1/1-introduktion/index.html index 22ecc4ee6..680e114db 100644 --- a/osa-1/1-introduktion/index.html +++ b/osa-1/1-introduktion/index.html @@ -206,7 +206,7 @@ } } }) -
Osa 1

Introduktion

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-1/2-info-fran-anvandaren/index.html b/osa-1/2-info-fran-anvandaren/index.html index b9e0236e0..8eceb0bab 100644 --- a/osa-1/2-info-fran-anvandaren/index.html +++ b/osa-1/2-info-fran-anvandaren/index.html @@ -206,7 +206,7 @@ } } }) -
Osa 1

Information från användaren

Du hittar ett repetitionsquiz för denna del här:

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-1/3-mer-om-variabler/index.html b/osa-1/3-mer-om-variabler/index.html index 568af7538..32a6da2b0 100644 --- a/osa-1/3-mer-om-variabler/index.html +++ b/osa-1/3-mer-om-variabler/index.html @@ -206,11 +206,11 @@ } } }) -
Osa 1

Mer om variabler

Vänligen fyll i den här enkäten före du börjar med den här delen. Du får ett poäng efter att du har fyllt i enkäten.

Loading...
:
Loading...

Log in to view the quiz

Variabler har olika användningsområden inom programmering. Du kan använda variabler för att lagra vilken som helst typ av information som kan behövas senare medan ett program körs.

I Python skapas variabler på följande sätt:

variabelns_namn = ...

där ... syftar till värdet som sparas i variabeln.

Till exempel när du använde input för att läsa in en sträng från användaren, sparade du strängen i en variabel och använde variabeln senare i ditt program.

namn = input("Ange ditt namn: ")
 print("Hejsan " + namn)
Exempelutskrift

Ange ditt namn: Konstantin Hejsan Konstantin

När man ger en variabel ett värde säger man att man tilldelar den ett värde. I koden ovan tilldelar vi alltså variabeln namn det värde som användaren matar in som svar på frågan "Ange ditt namn:".

Värdet som lagras i variabeln kan också definieras med hjälp av andra variabler:

fornamn = "Gabrielle"
 efternamn = "Gullholm"
@@ -266,4 +266,4 @@
 siffra3 = 3.62
 
 medeltal = (siffra1 + siffra2 + siffra3) / 3
-print(f"Medelvärde: {medeltal}")
Exempelutskrift

Medelvärde: 1.6233333333333333

Du hittar ett repetitionsquiz för denna del här:

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file +print(f"Medelvärde: {medeltal}")
Exempelutskrift

Medelvärde: 1.6233333333333333

Du hittar ett repetitionsquiz för denna del här:

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:
4. Räkneoperationer

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-1/4-rakneoperationer/index.html b/osa-1/4-rakneoperationer/index.html index 8fa4d584c..0ad71ac22 100644 --- a/osa-1/4-rakneoperationer/index.html +++ b/osa-1/4-rakneoperationer/index.html @@ -208,7 +208,7 @@ } } }) -
Osa 1

Räkneoperationer

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-1/5-villkorssatser/index.html b/osa-1/5-villkorssatser/index.html index e0857752e..7052bfb40 100644 --- a/osa-1/5-villkorssatser/index.html +++ b/osa-1/5-villkorssatser/index.html @@ -201,7 +201,7 @@ } } }) -
Osa 1

If-satser

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-1/index.html b/osa-1/index.html index 9e634dd10..2ca08d571 100644 --- a/osa-1/index.html +++ b/osa-1/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-10/1-klasshierarkier/index.html b/osa-10/1-klasshierarkier/index.html index ed3a39b54..e72c75715 100644 --- a/osa-10/1-klasshierarkier/index.html +++ b/osa-10/1-klasshierarkier/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 10

Klasshierarkier

Loading
Loading
Loading
Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-10/2-atkamstmodifierare/index.html b/osa-10/2-atkamstmodifierare/index.html index 6bc7ef8b7..5bdc0e18c 100644 --- a/osa-10/2-atkamstmodifierare/index.html +++ b/osa-10/2-atkamstmodifierare/index.html @@ -185,7 +185,7 @@ } } }) -
Osa 10

Åtkomstmodifierare

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-10/3-objektorienterade-programmeringstekniker/index.html b/osa-10/3-objektorienterade-programmeringstekniker/index.html index 210341450..44aed5114 100644 --- a/osa-10/3-objektorienterade-programmeringstekniker/index.html +++ b/osa-10/3-objektorienterade-programmeringstekniker/index.html @@ -185,7 +185,7 @@ } } }) -
Osa 10

Objektorienterade programmeringstekniker

Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-10/4-storre-applikation/index.html b/osa-10/4-storre-applikation/index.html index 47ddb7721..c3e492640 100644 --- a/osa-10/4-storre-applikation/index.html +++ b/osa-10/4-storre-applikation/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 10

Att utveckla en större applikation

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-10/index.html b/osa-10/index.html index 6ff3749f5..5b336b20c 100644 --- a/osa-10/index.html +++ b/osa-10/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-11/1-list-comprehension/index.html b/osa-11/1-list-comprehension/index.html index 123fe3a00..a4f7d544f 100644 --- a/osa-11/1-list-comprehension/index.html +++ b/osa-11/1-list-comprehension/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 11

List comprehension

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-11/2-mer-om-comprehensions/index.html b/osa-11/2-mer-om-comprehensions/index.html index 293386869..6d579dffd 100644 --- a/osa-11/2-mer-om-comprehensions/index.html +++ b/osa-11/2-mer-om-comprehensions/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 11

Mer om comprehensions

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-11/3-rekursion/index.html b/osa-11/3-rekursion/index.html index 9853dc02c..c5357edeb 100644 --- a/osa-11/3-rekursion/index.html +++ b/osa-11/3-rekursion/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 11

Rekursion

Funktionen binar_sokning tar fyra argument: mållistan, det objekt som söks samt vänster och höger kant på sökområdet. När funktionen anropas första gången täcker sökområdet hela mållistan. Den vänstra kanten ligger på index 0 och den högra kanten ligger på index len(lista)-1. Funktionen beräknar det centrala indexet och kontrollerar den positionen på listan. Antingen har objektet hittats eller så fortsätter sökningen till den mindre eller större halvan av mållistan.

Låt oss jämföra detta med en enkel linjär sökning. Vid en linjär sökning är sökområdet från början och framåt, tills antingen objektet hittas eller sökområdet tar slut. Antalet steg som behövs för att täcka hela sökområdet växer linjärt i samma takt som sökområdets storlek. Varje söksteg täcker endast en sökkandidat från början av sökområdet. Låt oss anta att det sökta objektet inte hittas. Om sökområdet är en miljon objekt långt måste vi ta en miljon söksteg för att försäkra oss om att objektet inte finns i sökområdet.

Vid en binär sökning växer däremot antalet steg som behövs logaritmiskt. Låt oss återigen anta att det sökta objektet inte hittas. Sökområdet halveras för varje steg, eftersom vi vet att objektet antingen är mindre eller större än den aktuella sökkandidaten i mitten. 2 gånger 20 (2^20) är redan långt över 1 miljon, så det tar som mest 20 steg att täcka hela sökområdet med en binär sökning. När vi har att göra med sorterade sökområden, vilket ofta är fallet när vi har att göra med datorer och material som ska bearbetas automatiskt, är en binär sökning alltså mycket effektivare än en linjär sökning.

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-11/4-lisaa-esimerkkeja/index.html b/osa-11/4-lisaa-esimerkkeja/index.html index 453a9301f..ceb77d889 100644 --- a/osa-11/4-lisaa-esimerkkeja/index.html +++ b/osa-11/4-lisaa-esimerkkeja/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 11

Fler exempel på rekursion

\ No newline at end of file +
Loading

Besök till tiden innan rekursion

Låt oss avsluta denna del av materialet med en lite större övning som koncentrerar sig på objektorienterade programmeringsprinciper. Vi rekommenderar inte att du använder rekursion i denna serie av uppgifter, men tekniker för list comprehension kommer att vara användbara.

Loading
Loading

Svara slutligen på en snabb enkät:

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-11/index.html b/osa-11/index.html index 03ffedd24..4e56cf6cd 100644 --- a/osa-11/index.html +++ b/osa-11/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-12/1-funktioner-som-argument/index.html b/osa-12/1-funktioner-som-argument/index.html index 9c0e198e6..57c5dfd8d 100644 --- a/osa-12/1-funktioner-som-argument/index.html +++ b/osa-12/1-funktioner-som-argument/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 12

Funktioner som argument

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-12/2-generatorer/index.html b/osa-12/2-generatorer/index.html index c1e2b56c6..4076f98ae 100644 --- a/osa-12/2-generatorer/index.html +++ b/osa-12/2-generatorer/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 12

Generatorer

\ No newline at end of file +jkl

Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:
3. Funktionell programmering

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-12/3-funktionell-programmering/index.html b/osa-12/3-funktionell-programmering/index.html index 74a8e7cd0..b7523df51 100644 --- a/osa-12/3-funktionell-programmering/index.html +++ b/osa-12/3-funktionell-programmering/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 12

Funktionell programmering

Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file +print(talens_summa)

Ifall ett ursprungligt värde lämnas bort tar reduce det första föremålet i listan som sitt ursprungsvärde och börjar reducera från det andra föremålet framåt.

OBS: Om föremålen i serien är av en annan typ än det avsedda reducerade resultatet, är det tredje argumentet obligatoriskt. Exemplet med bankkontona skulle inte fungera utan det ursprungliga värdet. Det vill säga att prova detta

saldon_totalt = reduce(saldo_summa_hjalpare, konton)

Skulle producera ett fel:

TypeError: unsupported operand type(s) for +: 'Bankkonto' and 'int'

I ovanstående fall, när reduce försöker utföra funktionen saldo_summa_hjalpare för första gången, är de argument som används de två första föremålen i listan, som båda är av typen Bankkonto. Specifikt är det värde som tilldelats parametern saldo_summa det första föremålet i listan. Funktionen saldo_summa_hjalpare försöker lägga till ett heltalsvärde till den, men att lägga till ett heltal direkt till ett Bankkonto-objekt är inte en åtgärd som stöds.

Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:
4. Reguljära uttryck

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-12/4-reguljara-uttryck/index.html b/osa-12/4-reguljara-uttryck/index.html index ea8d31746..4750417b0 100644 --- a/osa-12/4-reguljara-uttryck/index.html +++ b/osa-12/4-reguljara-uttryck/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 12

Reguljära uttryck

\ No newline at end of file +Hittades inte.

Loading

Den stora finalen

Som avslutning på denna del av materialet ska vi arbeta lite mer med objekt och klasser genom att bygga ett lite mer omfattande program. Denna övning innefattar inte nödvändigtvis reguljära uttryck, men avsnitten om Funktioner som argument och list comprehension kommer sannolikt att vara användbara.

Du kan också ha nytta av de exempel som finns i modul 10.

Loading

Svara till sist på en snabb enkät:

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-12/index.html b/osa-12/index.html index f626247f7..fa769256f 100644 --- a/osa-12/index.html +++ b/osa-12/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-13/1-pygame/index.html b/osa-13/1-pygame/index.html index 1ffb96d70..110bcbe1a 100644 --- a/osa-13/1-pygame/index.html +++ b/osa-13/1-pygame/index.html @@ -175,7 +175,7 @@ } } }) -
Osa 13

Pygame

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-13/2-animation/index.html b/osa-13/2-animation/index.html index 2d81859cc..a32574897 100644 --- a/osa-13/2-animation/index.html +++ b/osa-13/2-animation/index.html @@ -175,7 +175,7 @@ } } }) -
Osa 13

Animation

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-13/3-handelser/index.html b/osa-13/3-handelser/index.html index fa29c63b2..a9b0ec7d9 100644 --- a/osa-13/3-handelser/index.html +++ b/osa-13/3-handelser/index.html @@ -175,7 +175,7 @@ } } }) -
Osa 13

Händelser

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-13/4-fler-pygame-tekniker/index.html b/osa-13/4-fler-pygame-tekniker/index.html index 973a09c72..7e36f7a3d 100644 --- a/osa-13/4-fler-pygame-tekniker/index.html +++ b/osa-13/4-fler-pygame-tekniker/index.html @@ -189,7 +189,7 @@ } } }) -
Osa 13

Fler pygame-tekniker

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-13/index.html b/osa-13/index.html index 8be44fed7..de9fd146b 100644 --- a/osa-13/index.html +++ b/osa-13/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-14/1-spelprojekt/index.html b/osa-14/1-spelprojekt/index.html index 9ce138330..d7346d066 100644 --- a/osa-14/1-spelprojekt/index.html +++ b/osa-14/1-spelprojekt/index.html @@ -167,7 +167,7 @@ } } }) -
Osa 14

Spelprojekt

I den här modulen kommer vi att använda pygame för att skapa ett lite större spel. Det är en variant av det klassiska Sokoban spelet, där spelaren flyttar en robot på ett rutnät och skjuter lådor till rätt platser med så få drag som möjligt.

+
\ No newline at end of file +

OBS: koordinaterna x och y används på två olika sätt i spelet. När man hanterar index i en tvådimensionell lista är det logiskt att ange y-koordinaten först, eftersom y hänvisar till numret på raden medan x är numret på kolumnen. Å andra sidan, när man använder pygame-metoder, skickas x vanligtvis först, vilket det ganska ofta gör när man arbetar med grafik och även i matematiska sammanhang.

Du har nått slutet av den här delen! Fortsätt till nästa del:
2. Robot och lådor

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-14/2-robot-och-ladar/index.html b/osa-14/2-robot-och-ladar/index.html index 2a53cdd6a..7f6834acd 100644 --- a/osa-14/2-robot-och-ladar/index.html +++ b/osa-14/2-robot-och-ladar/index.html @@ -167,7 +167,7 @@ } } }) -
Osa 14

Robot och lådor

Det svåraste att implementera i ett Sokoban-spel brukar vara att flytta roboten så att den kan skjuta lådor i önskad riktning. Spelet ska kunna avgöra när roboten kan röra sig i en angiven riktning och kunna hantera alla situationer där en låda också ska röra sig. Låt oss ta an den här utmaningen nu.

+
Osa 14

Robot och lådor

Det svåraste att implementera i ett Sokoban-spel brukar vara att flytta roboten så att den kan skjuta lådor i önskad riktning. Spelet ska kunna avgöra när roboten kan röra sig i en angiven riktning och kunna hantera alla situationer där en låda också ska röra sig. Låt oss ta an den här utmaningen nu.

Att hantera viktiga händelser

Spelaren styr roboten med de fyra piltangenterna, så vår händelsehanterare ska också kunna reagera på lämpliga tangenthändelser:

    def granska_handelser(self):
@@ -255,4 +255,4 @@ 

Omfaktorisering?3. Färdigställande av spelet

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file +

Omfaktorisering och kodkvalitet är ett ämne för en del efterföljande kurser, t.ex. Software Development Methods och Software Engineering.

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-14/3-fardigstallande-av-spelet/index.html b/osa-14/3-fardigstallande-av-spelet/index.html index 4df8e9774..a698b2ccc 100644 --- a/osa-14/3-fardigstallande-av-spelet/index.html +++ b/osa-14/3-fardigstallande-av-spelet/index.html @@ -167,7 +167,7 @@ } } }) -
Osa 14

Färdigställande av spelet

Vårt spel är redan ganska fungerande, så det är dags att lägga till några sista detaljer. Vi lägger till en räknare för att visa antalet drag, en möjlighet att starta ett nytt spel och stänga spelet med tangentbordsinmatning samt ett meddelande när spelaren lyckas vinna spelet.

+
Osa 14

Färdigställande av spelet

Vårt spel är redan ganska fungerande, så det är dags att lägga till några sista detaljer. Vi lägger till en räknare för att visa antalet drag, en möjlighet att starta ett nytt spel och stänga spelet med tangentbordsinmatning samt ett meddelande när spelaren lyckas vinna spelet.

Räknare för mängden drag

Dragräknaren i nedre kanten av spelfönstret visar antalet drag som spelaren har gjort hittills. Detta kan användas för att hitta den lösning som kräver minst antal drag.

Räknaren kräver några ändringar i koden. Först ändrar vi konstruktorn så att det finns tillräckligt med utrymme för räknaren och att vi har ett lämpligt teckensnitt till vårt förfogande för att rita texten:

@@ -248,4 +248,4 @@

Ditt spel på GitHub?GitHub är en populär plats för många typer av programmeringsprojekt. Det kan användas för att lagra källkoden och annat material för alla dina egna programmeringsprojekt också, och ditt program kommer då att underhållas genom git-versionskontroll, och det kan enkelt delas med andra. Du kommer att bli mycket bekant med git och GitHub om du fortsätter med andra programmeringskurser på mooc.fi.

Hur många drag krävs?

Rutnätet i det här spelet är ganska litet, men spelet är inte så lätt. Den första utmaningen är att helt enkelt klara spelet, men nästa steg är att försöka göra det med så få drag som möjligt. Hur kort är den kortaste vägen till en lösning?

-

Att leta efter den kortaste möjliga lösningen är inte alls en lätt uppgift, men det finns beräkningslösningar för detta också. Detta är ett av ämnena i kursen Datastrukturer och algoritmer.

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file +

Att leta efter den kortaste möjliga lösningen är inte alls en lätt uppgift, men det finns beräkningslösningar för detta också. Detta är ett av ämnena i kursen Datastrukturer och algoritmer.

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-14/4-ditt-eget-sepl/index.html b/osa-14/4-ditt-eget-sepl/index.html index 04ab496cb..6058d3f44 100644 --- a/osa-14/4-ditt-eget-sepl/index.html +++ b/osa-14/4-ditt-eget-sepl/index.html @@ -181,7 +181,7 @@ } } }) -
Osa 14

Ditt eget spel

Den sista programmeringsövningen

+
Osa 14

Ditt eget spel

Den sista programmeringsövningen

Den sista programmeringsövningen på den här kursen är att skapa ett eget litet spel med pygame. Övningsmallen innehåller några bilder som du kan använda. Använd inte andra bilder i ditt spel för då kommer andra inte att kunna testa ditt spel enbart baserat på källkoden.

Vad spelet handlar om är upp till dig, men det bör vara ungefär lika komplicerat som Sokoban-exemplet i denna del av materialet. Spelet bör innehålla följande funktioner:

En bra recension brukar peka ut både bra funktioner och några förslag till förbättringar.

-
Loading...
:
Loading...

Log in to view the quiz

+
Loading...
:
Loading...

Log in to view the quiz

Frågeformulär för att avsluta

Först ber vi dig svara på en snabb enkät om den här delen av kursen.

-
Loading...
:
Loading...

Log in to view the quiz

+
Loading...
:
Loading...

Log in to view the quiz

Vänligen svara också på kursens feedbackformulär. Enkätens svar hjälper oss att förbättra kursen.

-
Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file +
Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-14/index.html b/osa-14/index.html index 81aa619ee..b8e2970fd 100644 --- a/osa-14/index.html +++ b/osa-14/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-2/1-terminologi/index.html b/osa-2/1-terminologi/index.html index 58b1d66d3..1003016c5 100644 --- a/osa-2/1-terminologi/index.html +++ b/osa-2/1-terminologi/index.html @@ -208,7 +208,7 @@ } } }) -
Osa 2

Programmeringsterminologi

\ No newline at end of file +Lön 552.0 euro

När programmet fungerar som det ska, är det viktigt att ta bort print-satser och annan kod som använts för att debugga.

Det här var ett ganska enkelt exempel och i fall som det här kan man eventuellt hitta buggar genom att läsa igenom koden med omtanke. Att använda print-satser för att debugga är ändå ofta ett snabbt sätt att få en ledtråd för var problemet kan ligga. print-satser kan också användas för att fastställa vilka delar av koden som fungerar korrekt. Då kan man fokusera på andra ställen där buggar med större sannolikhet gömmer sig.

print-satser är bara ett sätt att debugga program. Vi återkommer till det här ämnet senare under kursen. Nu ska du bli van vid att debugga, med hjälp av print-instruktioner, för att hitta problematiska delar i din kod. Proffs klarar sig inte utan print-satser i debuggningssyfte – det är alltså en viktig resurs redan som nybörjare.

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-2/2-else-elif/index.html b/osa-2/2-else-elif/index.html index ff79f69a6..61db9e237 100644 --- a/osa-2/2-else-elif/index.html +++ b/osa-2/2-else-elif/index.html @@ -206,7 +206,7 @@ } } }) -
Osa 2

Mera om if-satser

\ No newline at end of file +Tack och hej.

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-2/3-kombinera-villkor/index.html b/osa-2/3-kombinera-villkor/index.html index f52414d80..569cce37b 100644 --- a/osa-2/3-kombinera-villkor/index.html +++ b/osa-2/3-kombinera-villkor/index.html @@ -208,7 +208,7 @@ } } }) -
Osa 2

Kombinera villkor

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-2/4-loopar/index.html b/osa-2/4-loopar/index.html index a5cf58a78..4255be8fa 100644 --- a/osa-2/4-loopar/index.html +++ b/osa-2/4-loopar/index.html @@ -199,7 +199,7 @@ } } }) -
Osa 2

Enkla loopar

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-2/index.html b/osa-2/index.html index 558c80b73..94af7d8ba 100644 --- a/osa-2/index.html +++ b/osa-2/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-3/1-loopar-med-villkor/index.html b/osa-3/1-loopar-med-villkor/index.html index e21769713..4fc27de2c 100644 --- a/osa-3/1-loopar-med-villkor/index.html +++ b/osa-3/1-loopar-med-villkor/index.html @@ -206,7 +206,7 @@ } } }) -
Osa 3

Loopar med villkor

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-3/2-behandla-strangar/index.html b/osa-3/2-behandla-strangar/index.html index a178b33ef..258dddd64 100644 --- a/osa-3/2-behandla-strangar/index.html +++ b/osa-3/2-behandla-strangar/index.html @@ -206,7 +206,7 @@ } } }) -
Osa 3

Behandla strängar

\ No newline at end of file +...

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:
3. Mera om loopar

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-3/3-mer-om-loopar/index.html b/osa-3/3-mer-om-loopar/index.html index a9028bf00..9d42bc514 100644 --- a/osa-3/3-mer-om-loopar/index.html +++ b/osa-3/3-mer-om-loopar/index.html @@ -206,7 +206,7 @@ } } }) -
Osa 3

Mera om loopar

\ No newline at end of file +0

I programmet använder den yttre loopen hjälpvariabeln siffra som minskar med ett tills det når till noll. Hjälpvariabeln i tilldelas värdet 0 före man fortsätter till den inre loopen – varje gång den yttre loopen upprepas.

Den inre loopen använder sig av hjälpvariabeln i som ökar med talet 1 för varje iteration av den inre loopen. Den inre loopen fortsätter tills i är lika med siffra, och skriver ut varje värde hos i med mellanslag emellan. När loopen avslutas skapar print-instruktionen i den yttre loopen en ny rad.

I och med att värdet på siffra minskar för varje iteration av den yttre loopen, kommer antalet iterationer hos den inre loopen att minska. Vid varje upprepning blir sifferraden kortare, vilket bildar "pyramiden".

Kapslade loopar kan vara svårtolkade på en första titt, men det är viktigt att förstå hur de fungerar. Du kan använda dig av Python Tutors visualiseringsverktyg för att bättre förstå hur ovanstående exempel fungerar. Kopiera koden ovan till kodfönstret och följ hur utskriften formar sig och hur hjälpvariablernas värden ändras medan programmet körs.

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:
4. Definiera funktioner

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-3/4-definiera-funktioner/index.html b/osa-3/4-definiera-funktioner/index.html index 344353135..30e99205a 100644 --- a/osa-3/4-definiera-funktioner/index.html +++ b/osa-3/4-definiera-funktioner/index.html @@ -199,7 +199,7 @@ } } }) -
Osa 3

Definiera funktioner

Oavsett vilka argument vi anropar funktionen med skrivs värdet "Beatrice" från den globala variabeln ut.

Loading...
:
Loading...

Log in to view the quiz

Vänligen svara på en kort enkät gällande den här veckans material.

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-3/index.html b/osa-3/index.html index a0b8dd34f..6af492eab 100644 --- a/osa-3/index.html +++ b/osa-3/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-4/1-vscode/index.html b/osa-4/1-vscode/index.html index 07f7c2ec7..343ebb775 100644 --- a/osa-4/1-vscode/index.html +++ b/osa-4/1-vscode/index.html @@ -175,7 +175,7 @@ } } }) -
Osa 4

Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget

\ No newline at end of file diff --git a/osa-4/2-mer-om-funktioner/index.html b/osa-4/2-mer-om-funktioner/index.html index 41fe7dc60..ddebe895d 100644 --- a/osa-4/2-mer-om-funktioner/index.html +++ b/osa-4/2-mer-om-funktioner/index.html @@ -206,7 +206,7 @@ } } }) -
Osa 4

Mer om funktioner

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-4/3-listor/index.html b/osa-4/3-listor/index.html index 296623d02..4e8180b46 100644 --- a/osa-4/3-listor/index.html +++ b/osa-4/3-listor/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 4

Listor

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-4/4-iteration/index.html b/osa-4/4-iteration/index.html index 215f5b3cd..a4f2fdeca 100644 --- a/osa-4/4-iteration/index.html +++ b/osa-4/4-iteration/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 4

Iteration

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-4/5-utskrift/index.html b/osa-4/5-utskrift/index.html index 4b6643597..12dada5ab 100644 --- a/osa-4/5-utskrift/index.html +++ b/osa-4/5-utskrift/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 4

Formatera utskrift

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-4/6-strangar-listor/index.html b/osa-4/6-strangar-listor/index.html index feac25392..92c275920 100644 --- a/osa-4/6-strangar-listor/index.html +++ b/osa-4/6-strangar-listor/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 4

Mera strängar och listor

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-4/index.html b/osa-4/index.html index 0baee5df1..b15de7739 100644 --- a/osa-4/index.html +++ b/osa-4/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-5/1-mer-om-listor/index.html b/osa-5/1-mer-om-listor/index.html index 406f39bd1..b7b7107ae 100644 --- a/osa-5/1-mer-om-listor/index.html +++ b/osa-5/1-mer-om-listor/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 5

Mer om listor

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-5/2-referenser/index.html b/osa-5/2-referenser/index.html index 56afc1c20..c30d322e9 100644 --- a/osa-5/2-referenser/index.html +++ b/osa-5/2-referenser/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 5

Referenser

Funktionen sorted returnerar en ny ordnad kopia av listan, så vi behöver inte mera "sabotera" den ursprungliga listan när vi söker efter det näst minsta värdet.

Det är en bra vana att undvika sidoeffekter i funktioner. Sidoeffekter kan göra det svårare att säkerställa att programmet fungerar som det ska i alla situationer.

Funktioner som saknar sidoeffekter kallas rena funktioner. Då man arbetar med funktionell programmering är rena funktioner speciellt viktiga. Vi dyker djupare i det här under fortsättningskursen i programmering.

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-5/3-lexikon/index.html b/osa-5/3-lexikon/index.html index 54afb58e2..cec374b88 100644 --- a/osa-5/3-lexikon/index.html +++ b/osa-5/3-lexikon/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 5

Lexikon

Loading
Loading
Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-5/4-tuple/index.html b/osa-5/4-tuple/index.html index 0f8927bdd..248486d1d 100644 --- a/osa-5/4-tuple/index.html +++ b/osa-5/4-tuple/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 5

Tuple

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-5/index.html b/osa-5/index.html index bbb4bff02..ba74cf4a1 100644 --- a/osa-5/index.html +++ b/osa-5/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-6/1-lasa-filer/index.html b/osa-6/1-lasa-filer/index.html index c33904d51..07632907c 100644 --- a/osa-6/1-lasa-filer/index.html +++ b/osa-6/1-lasa-filer/index.html @@ -212,7 +212,7 @@ } } }) -
Osa 6

Läsa filer

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-6/2-skriva-filer/index.html b/osa-6/2-skriva-filer/index.html index df00350d1..9e3608adb 100644 --- a/osa-6/2-skriva-filer/index.html +++ b/osa-6/2-skriva-filer/index.html @@ -212,7 +212,7 @@ } } }) -
Osa 6

Skriva filer

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-6/3-fel/index.html b/osa-6/3-fel/index.html index cd4f28117..bab14d0fa 100644 --- a/osa-6/3-fel/index.html +++ b/osa-6/3-fel/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 6

Förbered dig på fel

\ No newline at end of file +ValueError: Negativt värde: -1
Loading
Loading
Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:
4. Lokala och globala variabler

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-6/4-lokala-globala-variabler/index.html b/osa-6/4-lokala-globala-variabler/index.html index 93e26c722..a6e0894a0 100644 --- a/osa-6/4-lokala-globala-variabler/index.html +++ b/osa-6/4-lokala-globala-variabler/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 6

Lokala och globala variabler

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-6/index.html b/osa-6/index.html index 114a342da..33dc30d31 100644 --- a/osa-6/index.html +++ b/osa-6/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-7/1-moduler/index.html b/osa-7/1-moduler/index.html index 11a0f16b3..f115f56c5 100644 --- a/osa-7/1-moduler/index.html +++ b/osa-7/1-moduler/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 7

Moduler

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-7/2-slumpmassighet/index.html b/osa-7/2-slumpmassighet/index.html index a8cd83e90..6c0824f4a 100644 --- a/osa-7/2-slumpmassighet/index.html +++ b/osa-7/2-slumpmassighet/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 7

Slumpmässighet

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-7/3-tid-datum/index.html b/osa-7/3-tid-datum/index.html index 485c7b9fc..ddeb63eca 100644 --- a/osa-7/3-tid-datum/index.html +++ b/osa-7/3-tid-datum/index.html @@ -206,7 +206,7 @@ } } }) -
Osa 7

Tid och datum

Loading
Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-7/4-behandla-data/index.html b/osa-7/4-behandla-data/index.html index 3e6784823..8f345b226 100644 --- a/osa-7/4-behandla-data/index.html +++ b/osa-7/4-behandla-data/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 7

Behandla data

\ No newline at end of file diff --git a/osa-7/5-egna-moduler/index.html b/osa-7/5-egna-moduler/index.html index 2685d500d..95c6fe91c 100644 --- a/osa-7/5-egna-moduler/index.html +++ b/osa-7/5-egna-moduler/index.html @@ -204,7 +204,7 @@ } } }) -
Osa 7

Skapa dina egna moduler

I uppgifterna under den här kursen har du flera gånger ombetts att ha dina test under ett if __name__ == "__main__" -block. Nu vet du varför.

Loading
Loading...
:
Loading...

Log in to view the quiz

Vänligen svara på en kort enkät om materialet för den här veckan.

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-7/6-funktionaliteter-python/index.html b/osa-7/6-funktionaliteter-python/index.html index 53644fe88..2f0d90226 100644 --- a/osa-7/6-funktionaliteter-python/index.html +++ b/osa-7/6-funktionaliteter-python/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 7

Flera funktionaliteter i Python

Loading

Vänligen svara på kursfeedbacksenkäten här nedan. Enkätens resultat hjälper oss att utveckla och förbättra den här kursen.

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-7/index.html b/osa-7/index.html index 362ce56df..5802d958e 100644 --- a/osa-7/index.html +++ b/osa-7/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-8/1-objekt-och-metoder/index.html b/osa-8/1-objekt-och-metoder/index.html index 5c542a1f1..5e835347c 100644 --- a/osa-8/1-objekt-och-metoder/index.html +++ b/osa-8/1-objekt-och-metoder/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 8

Objekt och metoder

Loading
Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-8/2-klasser-och-objekt/index.html b/osa-8/2-klasser-och-objekt/index.html index 86df68061..266543b30 100644 --- a/osa-8/2-klasser-och-objekt/index.html +++ b/osa-8/2-klasser-och-objekt/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 8

Klasser och objekt

\ No newline at end of file +TypeError: 'int' object is not callable
Loading
Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:
3. Egna klasser

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-8/3-egna-klasser/index.html b/osa-8/3-egna-klasser/index.html index a0c8f33ad..2102fb422 100644 --- a/osa-8/3-egna-klasser/index.html +++ b/osa-8/3-egna-klasser/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 8

Egna klasser

Loading
Loading
Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-8/4-metoder-i-egna-klasser/index.html b/osa-8/4-metoder-i-egna-klasser/index.html index 179fd23b5..e0869da15 100644 --- a/osa-8/4-metoder-i-egna-klasser/index.html +++ b/osa-8/4-metoder-i-egna-klasser/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 8

Metoder i egna klasser

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-8/5-fler-exempel/index.html b/osa-8/5-fler-exempel/index.html index d05879ca2..69ccdfdf1 100644 --- a/osa-8/5-fler-exempel/index.html +++ b/osa-8/5-fler-exempel/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 8

Fler exempel

\ No newline at end of file +0

Loading

Vänligen svara på en snabb enkät om veckans material:

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-8/index.html b/osa-8/index.html index 70f910558..427f2841f 100644 --- a/osa-8/index.html +++ b/osa-8/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/osa-9/1-objekt-och-referenser/index.html b/osa-9/1-objekt-och-referenser/index.html index fad3b4c20..843770fde 100644 --- a/osa-9/1-objekt-och-referenser/index.html +++ b/osa-9/1-objekt-och-referenser/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 9

Objekt och referenser

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-9/2-objekt-som-attribut/index.html b/osa-9/2-objekt-som-attribut/index.html index e0e6c1ed5..9896ef9a8 100644 --- a/osa-9/2-objekt-som-attribut/index.html +++ b/osa-9/2-objekt-som-attribut/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 9

Objekt som attribut

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-9/3-inkapsling/index.html b/osa-9/3-inkapsling/index.html index ddf5087a5..c6a6545c1 100644 --- a/osa-9/3-inkapsling/index.html +++ b/osa-9/3-inkapsling/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 9

Inkapsling

\ No newline at end of file +
Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-9/4-metodernas-rackvidd/index.html b/osa-9/4-metodernas-rackvidd/index.html index 24b90c5ad..c32a39e20 100644 --- a/osa-9/4-metodernas-rackvidd/index.html +++ b/osa-9/4-metodernas-rackvidd/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 9

Metodernas räckvidd

Privata metoder är i allmänhet mindre vanliga än privata attribut. En tumregel är att en metod ska döljas när klienten inte har något behov av att direkt komma åt den. Detta är särskilt fallet när det är möjligt att klienten kan påverka objektets integritet negativt genom att anropa metoden.

Loading
Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-9/5-klassattribut/index.html b/osa-9/5-klassattribut/index.html index 5535e46ec..5513c3dd3 100644 --- a/osa-9/5-klassattribut/index.html +++ b/osa-9/5-klassattribut/index.html @@ -183,7 +183,7 @@ } } }) -
Osa 9

Klassattribut

Du har nått slutet av den här delen! Fortsätt till nästa del:

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-9/6-fler-exempel-med-klasser/index.html b/osa-9/6-fler-exempel-med-klasser/index.html index c316ac181..c4a1cd1ae 100644 --- a/osa-9/6-fler-exempel-med-klasser/index.html +++ b/osa-9/6-fler-exempel-med-klasser/index.html @@ -197,7 +197,7 @@ } } }) -
Osa 9

Fler exempel med klasser

Den stora finalen

Fastän följande övning avslutar den här delen av materialet, så har de tekniker som krävs för att lösa den redan behandlats i avsnittet som heter Objekt som attribut. Du behöver inte använda @property-dekoratorn eller standardvärden för parametrar i den här övningen. Den här övningen är mycket lik övningarna "en presentask" och "den kortaste personen i rummet".

Loading

Svara avslutningsvis på följande frågeformulär:

Loading...
:
Loading...

Log in to view the quiz

Du har nått slutet av den här delen!

Se dina poäng genom att klicka på cirkeln nere till höger av sidan.

\ No newline at end of file diff --git a/osa-9/index.html b/osa-9/index.html index 0db74e61b..44d44d86e 100644 --- a/osa-9/index.html +++ b/osa-9/index.html @@ -151,5 +151,5 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/page-data/alla-uppgifter/page-data.json b/page-data/alla-uppgifter/page-data.json index 6d066f999..090f22ce4 100644 --- a/page-data/alla-uppgifter/page-data.json +++ b/page-data/alla-uppgifter/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-info-template-js", "path": "/alla-uppgifter", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-all-sections","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/kaikki-tehtavat.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide","overview":true,"hidden":true},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion","overview":null,"hidden":false},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren","overview":null,"hidden":false},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler","overview":null,"hidden":false},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer","overview":null,"hidden":false},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier","overview":null,"hidden":false},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare","overview":null,"hidden":false},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker","overview":null,"hidden":false},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation","overview":null,"hidden":false},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension","overview":null,"hidden":false},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion","overview":null,"hidden":false},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion","overview":null,"hidden":false},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument","overview":null,"hidden":false},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer","overview":null,"hidden":false},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering","overview":null,"hidden":false},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck","overview":null,"hidden":false},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame","overview":null,"hidden":false},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation","overview":null,"hidden":false},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker","overview":null,"hidden":false},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel","overview":null,"hidden":false},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi","overview":null,"hidden":null},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar","overview":null,"hidden":false},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar","overview":null,"hidden":false},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar","overview":null,"hidden":false},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner","overview":null,"hidden":false},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner","overview":null,"hidden":false},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration","overview":null,"hidden":false},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift","overview":null,"hidden":false},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon","overview":null,"hidden":false},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple","overview":null,"hidden":false},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer","overview":null,"hidden":false},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer","overview":null,"hidden":false},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel","overview":null,"hidden":false},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler","overview":null,"hidden":false},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet","overview":null,"hidden":false},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data","overview":null,"hidden":false},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler","overview":null,"hidden":false},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python","overview":null,"hidden":false},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder","overview":null,"hidden":false},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt","overview":null,"hidden":false},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut ","overview":null,"hidden":false},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling","overview":null,"hidden":false},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd","overview":null,"hidden":false},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut","overview":null,"hidden":false},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel","overview":null,"hidden":false},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-all-sections","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/kaikki-tehtavat.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide","overview":true,"hidden":true},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion","overview":null,"hidden":false},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren","overview":null,"hidden":false},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler","overview":null,"hidden":false},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer","overview":null,"hidden":false},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier","overview":null,"hidden":false},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare","overview":null,"hidden":false},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker","overview":null,"hidden":false},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation","overview":null,"hidden":false},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension","overview":null,"hidden":false},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion","overview":null,"hidden":false},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion","overview":null,"hidden":false},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument","overview":null,"hidden":false},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer","overview":null,"hidden":false},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering","overview":null,"hidden":false},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck","overview":null,"hidden":false},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame","overview":null,"hidden":false},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation","overview":null,"hidden":false},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker","overview":null,"hidden":false},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel","overview":null,"hidden":false},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi","overview":null,"hidden":null},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar","overview":null,"hidden":false},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar","overview":null,"hidden":false},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar","overview":null,"hidden":false},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner","overview":null,"hidden":false},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner","overview":null,"hidden":false},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration","overview":null,"hidden":false},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift","overview":null,"hidden":false},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor","overview":null,"hidden":false},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon","overview":null,"hidden":false},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple","overview":null,"hidden":false},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer","overview":null,"hidden":false},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer","overview":null,"hidden":false},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel","overview":null,"hidden":false},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler","overview":null,"hidden":false},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet","overview":null,"hidden":false},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data","overview":null,"hidden":false},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler","overview":null,"hidden":false},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python","overview":null,"hidden":false},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder","overview":null,"hidden":false},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt","overview":null,"hidden":false},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel","overview":null,"hidden":false},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut ","overview":null,"hidden":false},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling","overview":null,"hidden":false},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd","overview":null,"hidden":false},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut","overview":null,"hidden":false},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser","overview":null,"hidden":false},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9","overview":true,"hidden":false},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-1/1-introduktion/page-data.json b/page-data/osa-1/1-introduktion/page-data.json index b01223b88..49412382b 100644 --- a/page-data/osa-1/1-introduktion/page-data.json +++ b/page-data/osa-1/1-introduktion/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-1/1-introduktion", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"har du skrivit och kört ditt första Python-program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man använder print för att få utskrifter på skärmen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du utföra räkneoperationer genom att programmera."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datorprogram består av instruktioner eller kommandon. Varje kommando instruerar datorn att göra en viss sak. Datorn utför varje instruktion en i taget. Instruktionerna kan till exempel användas för att utföra räkneoperationer, jämföra saker i datorns minne, göra ändringar i hur programmet fungerar, förmedla meddelanden eller fråga något av programmets användare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja programmera genom att bekanta oss med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" som skriver ut (print) text. I praktiken betyder det att programmet visar text på skärmen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande programmet skriver ut texten \"Hej!\":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, blir resultatet följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet fungerar inte om koden inte skrivs exakt som den är ovan. Om man till exempel kör programmet utan citattecken, på följande sätt…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Hej!"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…så kommer texten \"Hej!\" inte att skrivas ut. Istället får vi ett felmeddelande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"File \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1\n print(Hej!)\n ^\nSyntaxError: invalid syntax\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sammanfattningsvis: För att skriva ut text, måste den vara inom citattecken för att Python ska kunna tolka den korrekt. Vi återkommer nedan till varför det är så här."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Leende","tmcname":"osa01-01_leende","height":"300px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som skriver ut ett leende: :-)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ett-program-med-flera-instruktioner","style":"position:relative;"},"children":[{"type":"text","value":"Ett program med flera instruktioner"},{"type":"element","tagName":"a","properties":{"href":"#ett-program-med-flera-instruktioner","ariaLabel":"ett program med flera instruktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flera instruktioner som skrivs efter varandra körs i ordning från den första till den sista. Till exempel skriver följande program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Välkommen till vår programmeringskurs!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"För att börja ska vi testa print.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här programmet skriver ut tre rader text.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"… ut följande textrader på skärmen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Välkommen till vår programmeringskurs!\nFör att börja ska vi testa print.\nDet här programmet skriver ut tre rader text."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Sju bröder","tmcname":"osa01-03_sju_broder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet borde skriva ut namnet på sju bröder i alfabetisk ordning. Det finns ändå några fel i programmet. Korrigera dem, så att namnen skrivs ut i korrekt ordning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Simeoni\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Juhani\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lauri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Aapo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tuomas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Timo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Gubben Noak","tmcname":"osa01-02_gubben_noak"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som skriver ut följande textrader (exakt som de står nedan):"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gubben Noak, gubben Noak var en hedersman\nNär han gick ur arken plantera han på marken\nGubben Noak, gubben Noak var en hedersman."}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"rakneoperationer","style":"position:relative;"},"children":[{"type":"text","value":"Räkneoperationer"},{"type":"element","tagName":"a","properties":{"href":"#rakneoperationer","ariaLabel":"rakneoperationer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också utföra räkneoperationer som sedan skrivs ut med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":". När instruktionen körs, kommer resultatet av operationen att skrivas ut på skärmen. Till exempel skriver detta program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"… ut följande textrader:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n9\n22"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att citattecknen fattas från instruktionerna med räkneoperationer. Citattecknen används för att markera strängar. Inom programmering är strängar en sekvens av tecken. Strängar kan innehålla bokstäver, siffror och alla andra typer av tecken – till exempel skiljetecken. Strängar är inte nödvändigtvis bara enskilda tecken eller ord utan kan vara flera meningar långa. Strängar skrivs vanligtvis ut exakt så som de är skrivna. Därmed ger dessa två instruktioner mycket olika resultat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"2 + 2 * 10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"22\n2 + 2 * 10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I instruktionen på den andra raden utför Python inte några räkneoperationer utan skriver ut operationen som sådan, en sträng. En sträng skrivs alltså alltid exakt som den ser ut – det vill säga allt som finns mellan citattecknena."}]},{"type":"element","tagName":"h2","properties":{"id":"kommentarer","style":"position:relative;"},"children":[{"type":"text","value":"Kommentarer"},{"type":"element","tagName":"a","properties":{"href":"#kommentarer","ariaLabel":"kommentarer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en rad börjar med tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":", tolkas raden som en kommentar. Det innebär att en rad som börjar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":" inte påverkar programmets funktion på något sätt – Python ignorerar helt enkelt hela raden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kommentarer kan användas för att beskriva hur ett program fungerar – både för programmeraren och för andra personer som läser koden. I det här programmet finns en kommentar som beskriver räkneoperationen som utförs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antal timmar i ett år:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ett år består av 365 dagar och varje dag av 24 timmar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"365"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, kommer kommentaren inte att synas för användaren:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Antal timmar i ett år:\n8760"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korta kommentarer kan också skrivas i slutet på en rad, på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antal timmar i ett år:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"365"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 365 dagar, 24 timmar per dag"}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Minuter i ett år","tmcname":"osa01-04_minuter_per_ar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som skriver ut antalet minuter i ett år. Låt Python utföra räkneoperationen som i exemplet ovan."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Kod som skriver ut kod","tmcname":"osa01-05_skriv_ut_kod"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Istället för dubbla citattecken ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\""}]},{"type":"text","value":") kan man i Python också använda enkla citattecken ( "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"'"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kan vara nyttigt till exempel i situationer där man vill skriva ut citattecken:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'\"Kom tillbaka direkt!\", vrålade polisen.'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Kom tillbaka direkt!\", vrålade polisen."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som skriver ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"print(\"Hej!\")"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du hittar ett repetitionsquiz för denna del här:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"4b736942-71b8-5171-8427-508b57d1776b"},"children":[]}]}]},"html":"

Efter den här delen

Datorprogram består av instruktioner eller kommandon. Varje kommando instruerar datorn att göra en viss sak. Datorn utför varje instruktion en i taget. Instruktionerna kan till exempel användas för att utföra räkneoperationer, jämföra saker i datorns minne, göra ändringar i hur programmet fungerar, förmedla meddelanden eller fråga något av programmets användare.

Låt oss börja programmera genom att bekanta oss med print som skriver ut (print) text. I praktiken betyder det att programmet visar text på skärmen.

Det följande programmet skriver ut texten \"Hej!\":

print(\"Hej!\")

När programmet körs, blir resultatet följande:

Hej!

Programmet fungerar inte om koden inte skrivs exakt som den är ovan. Om man till exempel kör programmet utan citattecken, på följande sätt…

print(Hej!)

…så kommer texten \"Hej!\" inte att skrivas ut. Istället får vi ett felmeddelande:

\nFile \"\", line 1\n  print(Hej!)\n                   ^\nSyntaxError: invalid syntax\n

Sammanfattningsvis: För att skriva ut text, måste den vara inom citattecken för att Python ska kunna tolka den korrekt. Vi återkommer nedan till varför det är så här.

Skriv ett program som skriver ut ett leende: :-)

Ett program med flera instruktioner

Flera instruktioner som skrivs efter varandra körs i ordning från den första till den sista. Till exempel skriver följande program…

print(\"Välkommen till vår programmeringskurs!\")\nprint(\"För att börja ska vi testa print.\")\nprint(\"Det här programmet skriver ut tre rader text.\")

… ut följande textrader på skärmen:

Välkommen till vår programmeringskurs!\nFör att börja ska vi testa print.\nDet här programmet skriver ut tre rader text.

Det här programmet borde skriva ut namnet på sju bröder i alfabetisk ordning. Det finns ändå några fel i programmet. Korrigera dem, så att namnen skrivs ut i korrekt ordning.

print(\"Simeoni\")\nprint(\"Juhani\")\nprint(\"Eero\")\nprint(\"Lauri\")\nprint(\"Aapo\")\nprint(\"Tuomas\")\nprint(\"Timo\")

Skriv ett program som skriver ut följande textrader (exakt som de står nedan):

Gubben Noak, gubben Noak var en hedersman\nNär han gick ur arken plantera han på marken\nGubben Noak, gubben Noak var en hedersman.

Räkneoperationer

Du kan också utföra räkneoperationer som sedan skrivs ut med hjälp av print. När instruktionen körs, kommer resultatet av operationen att skrivas ut på skärmen. Till exempel skriver detta program…

print(2 + 5)\nprint(3 * 3)\nprint(2 + 2 * 10)

… ut följande textrader:

7\n9\n22

Observera att citattecknen fattas från instruktionerna med räkneoperationer. Citattecknen används för att markera strängar. Inom programmering är strängar en sekvens av tecken. Strängar kan innehålla bokstäver, siffror och alla andra typer av tecken – till exempel skiljetecken. Strängar är inte nödvändigtvis bara enskilda tecken eller ord utan kan vara flera meningar långa. Strängar skrivs vanligtvis ut exakt så som de är skrivna. Därmed ger dessa två instruktioner mycket olika resultat:

print(2 + 2 * 10)\nprint(\"2 + 2 * 10\")

Programmet skriver ut:

22\n2 + 2 * 10

I instruktionen på den andra raden utför Python inte några räkneoperationer utan skriver ut operationen som sådan, en sträng. En sträng skrivs alltså alltid exakt som den ser ut – det vill säga allt som finns mellan citattecknena.

Kommentarer

Om en rad börjar med tecknet #, tolkas raden som en kommentar. Det innebär att en rad som börjar med # inte påverkar programmets funktion på något sätt – Python ignorerar helt enkelt hela raden.

Kommentarer kan användas för att beskriva hur ett program fungerar – både för programmeraren och för andra personer som läser koden. I det här programmet finns en kommentar som beskriver räkneoperationen som utförs:

print(\"Antal timmar i ett år:\")\n# ett år består av 365 dagar och varje dag av 24 timmar\nprint(365*24)

När programmet körs, kommer kommentaren inte att synas för användaren:

Antal timmar i ett år:\n8760

Korta kommentarer kan också skrivas i slutet på en rad, på följande sätt:

print(\"Antal timmar i ett år:\")\nprint(365*24) # 365 dagar, 24 timmar per dag

Skriv ett program som skriver ut antalet minuter i ett år. Låt Python utföra räkneoperationen som i exemplet ovan.

Istället för dubbla citattecken (\") kan man i Python också använda enkla citattecken ( ').

Det här kan vara nyttigt till exempel i situationer där man vill skriva ut citattecken:

print('\"Kom tillbaka direkt!\", vrålade polisen.')

\"Kom tillbaka direkt!\", vrålade polisen.

Skapa ett program som skriver ut följande:

print(\"Hej!\")

Du hittar ett repetitionsquiz för denna del här:

","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/1-introduktion.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"har du skrivit och kört ditt första Python-program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man använder print för att få utskrifter på skärmen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du utföra räkneoperationer genom att programmera."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datorprogram består av instruktioner eller kommandon. Varje kommando instruerar datorn att göra en viss sak. Datorn utför varje instruktion en i taget. Instruktionerna kan till exempel användas för att utföra räkneoperationer, jämföra saker i datorns minne, göra ändringar i hur programmet fungerar, förmedla meddelanden eller fråga något av programmets användare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja programmera genom att bekanta oss med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" som skriver ut (print) text. I praktiken betyder det att programmet visar text på skärmen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande programmet skriver ut texten \"Hej!\":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, blir resultatet följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet fungerar inte om koden inte skrivs exakt som den är ovan. Om man till exempel kör programmet utan citattecken, på följande sätt…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Hej!"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…så kommer texten \"Hej!\" inte att skrivas ut. Istället får vi ett felmeddelande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"File \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1\n print(Hej!)\n ^\nSyntaxError: invalid syntax\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sammanfattningsvis: För att skriva ut text, måste den vara inom citattecken för att Python ska kunna tolka den korrekt. Vi återkommer nedan till varför det är så här."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Leende","tmcname":"osa01-01_leende","height":"300px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som skriver ut ett leende: :-)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ett-program-med-flera-instruktioner","style":"position:relative;"},"children":[{"type":"text","value":"Ett program med flera instruktioner"},{"type":"element","tagName":"a","properties":{"href":"#ett-program-med-flera-instruktioner","ariaLabel":"ett program med flera instruktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flera instruktioner som skrivs efter varandra körs i ordning från den första till den sista. Till exempel skriver följande program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Välkommen till vår programmeringskurs!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"För att börja ska vi testa print.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här programmet skriver ut tre rader text.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"… ut följande textrader på skärmen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Välkommen till vår programmeringskurs!\nFör att börja ska vi testa print.\nDet här programmet skriver ut tre rader text."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Sju bröder","tmcname":"osa01-03_sju_broder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet borde skriva ut namnet på sju bröder i alfabetisk ordning. Det finns ändå några fel i programmet. Korrigera dem, så att namnen skrivs ut i korrekt ordning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Simeoni\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Juhani\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lauri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Aapo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tuomas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Timo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Gubben Noak","tmcname":"osa01-02_gubben_noak"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som skriver ut följande textrader (exakt som de står nedan):"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gubben Noak, gubben Noak var en hedersman\nNär han gick ur arken plantera han på marken\nGubben Noak, gubben Noak var en hedersman."}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"rakneoperationer","style":"position:relative;"},"children":[{"type":"text","value":"Räkneoperationer"},{"type":"element","tagName":"a","properties":{"href":"#rakneoperationer","ariaLabel":"rakneoperationer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också utföra räkneoperationer som sedan skrivs ut med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":". När instruktionen körs, kommer resultatet av operationen att skrivas ut på skärmen. Till exempel skriver detta program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"… ut följande textrader:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n9\n22"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att citattecknen fattas från instruktionerna med räkneoperationer. Citattecknen används för att markera strängar. Inom programmering är strängar en sekvens av tecken. Strängar kan innehålla bokstäver, siffror och alla andra typer av tecken – till exempel skiljetecken. Strängar är inte nödvändigtvis bara enskilda tecken eller ord utan kan vara flera meningar långa. Strängar skrivs vanligtvis ut exakt så som de är skrivna. Därmed ger dessa två instruktioner mycket olika resultat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"2 + 2 * 10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"22\n2 + 2 * 10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I instruktionen på den andra raden utför Python inte några räkneoperationer utan skriver ut operationen som sådan, en sträng. En sträng skrivs alltså alltid exakt som den ser ut – det vill säga allt som finns mellan citattecknena."}]},{"type":"element","tagName":"h2","properties":{"id":"kommentarer","style":"position:relative;"},"children":[{"type":"text","value":"Kommentarer"},{"type":"element","tagName":"a","properties":{"href":"#kommentarer","ariaLabel":"kommentarer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en rad börjar med tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":", tolkas raden som en kommentar. Det innebär att en rad som börjar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":" inte påverkar programmets funktion på något sätt – Python ignorerar helt enkelt hela raden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kommentarer kan användas för att beskriva hur ett program fungerar – både för programmeraren och för andra personer som läser koden. I det här programmet finns en kommentar som beskriver räkneoperationen som utförs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antal timmar i ett år:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ett år består av 365 dagar och varje dag av 24 timmar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"365"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, kommer kommentaren inte att synas för användaren:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Antal timmar i ett år:\n8760"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korta kommentarer kan också skrivas i slutet på en rad, på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antal timmar i ett år:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"365"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 365 dagar, 24 timmar per dag"}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Minuter i ett år","tmcname":"osa01-04_minuter_per_ar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som skriver ut antalet minuter i ett år. Låt Python utföra räkneoperationen som i exemplet ovan."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Kod som skriver ut kod","tmcname":"osa01-05_skriv_ut_kod"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Istället för dubbla citattecken ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\""}]},{"type":"text","value":") kan man i Python också använda enkla citattecken ( "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"'"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kan vara nyttigt till exempel i situationer där man vill skriva ut citattecken:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'\"Kom tillbaka direkt!\", vrålade polisen.'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Kom tillbaka direkt!\", vrålade polisen."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som skriver ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"print(\"Hej!\")"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du hittar ett repetitionsquiz för denna del här:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"4b736942-71b8-5171-8427-508b57d1776b"},"children":[]}]}]},"html":"

Efter den här delen

Datorprogram består av instruktioner eller kommandon. Varje kommando instruerar datorn att göra en viss sak. Datorn utför varje instruktion en i taget. Instruktionerna kan till exempel användas för att utföra räkneoperationer, jämföra saker i datorns minne, göra ändringar i hur programmet fungerar, förmedla meddelanden eller fråga något av programmets användare.

Låt oss börja programmera genom att bekanta oss med print som skriver ut (print) text. I praktiken betyder det att programmet visar text på skärmen.

Det följande programmet skriver ut texten \"Hej!\":

print(\"Hej!\")

När programmet körs, blir resultatet följande:

Hej!

Programmet fungerar inte om koden inte skrivs exakt som den är ovan. Om man till exempel kör programmet utan citattecken, på följande sätt…

print(Hej!)

…så kommer texten \"Hej!\" inte att skrivas ut. Istället får vi ett felmeddelande:

\nFile \"\", line 1\n  print(Hej!)\n                   ^\nSyntaxError: invalid syntax\n

Sammanfattningsvis: För att skriva ut text, måste den vara inom citattecken för att Python ska kunna tolka den korrekt. Vi återkommer nedan till varför det är så här.

Skriv ett program som skriver ut ett leende: :-)

Ett program med flera instruktioner

Flera instruktioner som skrivs efter varandra körs i ordning från den första till den sista. Till exempel skriver följande program…

print(\"Välkommen till vår programmeringskurs!\")\nprint(\"För att börja ska vi testa print.\")\nprint(\"Det här programmet skriver ut tre rader text.\")

… ut följande textrader på skärmen:

Välkommen till vår programmeringskurs!\nFör att börja ska vi testa print.\nDet här programmet skriver ut tre rader text.

Det här programmet borde skriva ut namnet på sju bröder i alfabetisk ordning. Det finns ändå några fel i programmet. Korrigera dem, så att namnen skrivs ut i korrekt ordning.

print(\"Simeoni\")\nprint(\"Juhani\")\nprint(\"Eero\")\nprint(\"Lauri\")\nprint(\"Aapo\")\nprint(\"Tuomas\")\nprint(\"Timo\")

Skriv ett program som skriver ut följande textrader (exakt som de står nedan):

Gubben Noak, gubben Noak var en hedersman\nNär han gick ur arken plantera han på marken\nGubben Noak, gubben Noak var en hedersman.

Räkneoperationer

Du kan också utföra räkneoperationer som sedan skrivs ut med hjälp av print. När instruktionen körs, kommer resultatet av operationen att skrivas ut på skärmen. Till exempel skriver detta program…

print(2 + 5)\nprint(3 * 3)\nprint(2 + 2 * 10)

… ut följande textrader:

7\n9\n22

Observera att citattecknen fattas från instruktionerna med räkneoperationer. Citattecknen används för att markera strängar. Inom programmering är strängar en sekvens av tecken. Strängar kan innehålla bokstäver, siffror och alla andra typer av tecken – till exempel skiljetecken. Strängar är inte nödvändigtvis bara enskilda tecken eller ord utan kan vara flera meningar långa. Strängar skrivs vanligtvis ut exakt så som de är skrivna. Därmed ger dessa två instruktioner mycket olika resultat:

print(2 + 2 * 10)\nprint(\"2 + 2 * 10\")

Programmet skriver ut:

22\n2 + 2 * 10

I instruktionen på den andra raden utför Python inte några räkneoperationer utan skriver ut operationen som sådan, en sträng. En sträng skrivs alltså alltid exakt som den ser ut – det vill säga allt som finns mellan citattecknena.

Kommentarer

Om en rad börjar med tecknet #, tolkas raden som en kommentar. Det innebär att en rad som börjar med # inte påverkar programmets funktion på något sätt – Python ignorerar helt enkelt hela raden.

Kommentarer kan användas för att beskriva hur ett program fungerar – både för programmeraren och för andra personer som läser koden. I det här programmet finns en kommentar som beskriver räkneoperationen som utförs:

print(\"Antal timmar i ett år:\")\n# ett år består av 365 dagar och varje dag av 24 timmar\nprint(365*24)

När programmet körs, kommer kommentaren inte att synas för användaren:

Antal timmar i ett år:\n8760

Korta kommentarer kan också skrivas i slutet på en rad, på följande sätt:

print(\"Antal timmar i ett år:\")\nprint(365*24) # 365 dagar, 24 timmar per dag

Skriv ett program som skriver ut antalet minuter i ett år. Låt Python utföra räkneoperationen som i exemplet ovan.

Istället för dubbla citattecken (\") kan man i Python också använda enkla citattecken ( ').

Det här kan vara nyttigt till exempel i situationer där man vill skriva ut citattecken:

print('\"Kom tillbaka direkt!\", vrålade polisen.')

\"Kom tillbaka direkt!\", vrålade polisen.

Skapa ett program som skriver ut följande:

print(\"Hej!\")

Du hittar ett repetitionsquiz för denna del här:

","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/1-introduktion.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-1/2-info-fran-anvandaren/page-data.json b/page-data/osa-1/2-info-fran-anvandaren/page-data.json index cca144ce4..26b69cc59 100644 --- a/page-data/osa-1/2-info-fran-anvandaren/page-data.json +++ b/page-data/osa-1/2-info-fran-anvandaren/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-1/2-info-fran-anvandaren", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man skriver ett program som kan läsa in information av användaren"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man använder variabler för att lagra indata och skriva ut motsvarande värden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du kombinera strängar."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Indata (input) syftar till information som programmet läser in från omvärlden. Det kan handla om allt från indata från tangentbordet eller musklick till sensorer. I Python används "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" för att läsa in en rad text som användaren matar in. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" kan också användas för att skriva ut ett meddelande till användaren för att be om någon specifik information."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet läser in användarens namn med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":". Därefter skriver programmet ut namnet med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej på dig, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, kan det se ut så här (indata från användaren markerat med rött):"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Pauline Python"}]},{"type":"text","value":"\nHej på dig, Pauline Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad programmet skriver ut beror här på den information som användaren ger. Utskriften kommer alltså att variera beroende på vad användaren matar in:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Kira Kodare"}]},{"type":"text","value":"\nHej på dig, Kira Kodare"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" i programmet ovan är en variabel. Inom programmering är en variabel ett ställe för att lagra ett värde – till exempel en sträng eller ett nummer. Värdet kan användas senare och det kan också ändras."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Att namnge variabler"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I princip kan variabler namnges relativt fritt, men Python har några begränsningar som måste tas i beaktande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är vanligt att namnge variabler på engelska, men du kan stöta på variabler som namngetts på andra språk – till exempel på programmerarens eget modersmål. Variabelns namn påverkar inte dess värde, så det har inte en direkt påverkan på programmets funktion. Däremot kan variabler namngivna på engelska förtydliga kodens funktion för läsaren – förutsatt att variablerna har logiska namn. Det viktigaste är att använda variabelnamn som gör koden lättläst och förståelig både för dig som programmerar och för andra som till exempel behöver granska koden."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Namn, två gånger","tmcname":"osa01-06_namn_tva_ganger"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör ett program som frågar efter användarens förnamn. Programmet ska sedan skriva ut namnet två gånger på varandra följande rader."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Pauline"}]},{"type":"text","value":"\nPauline\nPauline"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"hanvisa-till-en-variabel","style":"position:relative;"},"children":[{"type":"text","value":"Hänvisa till en variabel"},{"type":"element","tagName":"a","properties":{"href":"#hanvisa-till-en-variabel","ariaLabel":"hanvisa till en variabel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En viss variabel kan hänvisas till flera gånger i ett program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" är ett ganska fint namn.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren ger namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Paulus Python"}]},{"type":"text","value":", skriver programmet ut följande rader:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paulus Python"}]},{"type":"text","value":"\nHej, Paulus Python!\nPaulus Python är ett ganska fint namn."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss undersöka närmare hur "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" används ovan. Mellan parenteserna i instruktionen finns det både text inom citattecken och namn på variabler, som hänvisar till indata från användaren. De har kombinerats med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-operatorn, som kombinerar två strängar till en sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar och variabler kan kombineras ganska fritt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"! Ditt namn var alltså \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren ger namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Ellen Exempel"}]},{"type":"text","value":", skrivs följande ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ellen Exempel"}]},{"type":"text","value":"\nHej Ellen Exempel! Ditt namn var alltså Ellen Exempel?"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Namn med utropstecken","tmcname":"osa01-07_namn_utropstecken"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som frågar efter användarens förnamn. Programmet ska skriva ut det angivna namnet två gånger på samma rad, så att det finns ett utropstecken mellan namnen och i början och slutet av raden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Sandrine"}]},{"type":"text","value":"\n!Sandrine!Sandrine!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"samla-mera-data","style":"position:relative;"},"children":[{"type":"text","value":"Samla mera data"},{"type":"element","tagName":"a","properties":{"href":"#samla-mera-data","ariaLabel":"samla mera data permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett program kan be användaren att mata in data flera gånger. Observera att vi då behöver flera variabler för att lagra de olika värdena som användaren matar in via "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nepost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange din e-postadress: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsmeknamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt smeknamn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vi kollar ännu att alla uppgifter är korrekta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ditt namn: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Din e-postadress: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ditt smeknamn: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" smeknamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet kunde till exempel fungera så här när det körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Per-Pierre Påhittad"}]},{"type":"text","value":"\nAnge din e-postadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"mailto:perpierre01@example.com","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"perpierre01@example.com"}]}]},{"type":"text","value":"\nAnge ditt smeknamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"PP"}]},{"type":"text","value":"\nVi kollar ännu att alla uppgifter är korrekta\nDitt namn: Per-Pierre Påhittad\nDin e-postadress: "},{"type":"element","tagName":"a","properties":{"href":"mailto:perpierre01@example.com","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"perpierre01@example.com"}]},{"type":"text","value":"\nDitt smeknamn: PP"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en och samma variabel används för att lagra indata flera gånger kommer det nyaste värdet alltid att ersätta det föregående värdet. Till exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilken är din gatuadress? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du bor alltså på \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" gata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ngata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange en ny gatuadress: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gatuadressen är nu \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" gata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vilken är din gatuadress? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Pythonparken 7"}]},{"type":"text","value":"\nDu bor alltså på Pythonparken 7\nAnge en ny gatuadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Gredelingatan 17 B 231"}]},{"type":"text","value":"\nGatuadressen är nu Gredelingatan 17 B 231"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här betyder att om samma variabel används för att lagra indata två gånger i rad så kommer det första värdet inte längre att vara tillgängligt efter att det har ersatts:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilken är din gatuadress? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange en ny gatuadress: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gatuadressen är nu \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" gata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vilken är din gatuadress? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Slitagestigen 2"}]},{"type":"text","value":"\nAnge en ny gatuadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Värjan 1"}]},{"type":"text","value":"\nGatuadressen är nu Värjan 1"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Namn och adress","tmcname":"osa01-08_namn_adress"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter användarens namn och adress. Skriv sedan ut dem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Cecilia"}]},{"type":"text","value":"\nEfternamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Citronlik"}]},{"type":"text","value":"\nGatuadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Bulevarden 12"}]},{"type":"text","value":"\nPostnummer och adressort: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"00120 HELSINGFORS"}]},{"type":"text","value":"\nCecilia Citronlik\nBulevarden 12\n00120 HELSINGFORS"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Uttryck","tmcname":"osa01-09_uttryck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften finns ett program som borde fråga användaren efter tre ord och skriva dem ut på följande sätt."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ord 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ärtan"}]},{"type":"text","value":"\nGe ord 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pärtan"}]},{"type":"text","value":"\nGe ord 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"puff"}]},{"type":"text","value":"\närtan-pärtan-puff!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns några problem i koden som du nu borde lösa, så att programmet fungerar korrekt."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Berättelse","tmcname":"osa01-10_berattelse"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som skriver ut den följande berättelsen. Namnet och årtalet användaren anger ska användas i texten."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Amelie"}]},{"type":"text","value":"\nAnge år: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1993"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Amelie är kommunens bästa läkare. När Amelie, född 1993, vaknar till ett telefonsamtal klockan 4 på morgonen, blir det brådis. Amelie behövs till akuten för att rädda livet hos en cyklist som krockat med en bil."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berättelsen ska ändras beroende på de värden användaren anger."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du hittar ett repetitionsquiz för denna del här:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14"},"children":[]}]}]},"html":"

Efter den här delen

Indata (input) syftar till information som programmet läser in från omvärlden. Det kan handla om allt från indata från tangentbordet eller musklick till sensorer. I Python används input för att läsa in en rad text som användaren matar in. input kan också användas för att skriva ut ett meddelande till användaren för att be om någon specifik information.

Det här programmet läser in användarens namn med hjälp av input. Därefter skriver programmet ut namnet med print:

namn = input(\"Ange ditt namn: \")\nprint(\"Hej på dig, \" + namn)

När programmet körs, kan det se ut så här (indata från användaren markerat med rött):

Ange ditt namn: Pauline Python\nHej på dig, Pauline Python

Vad programmet skriver ut beror här på den information som användaren ger. Utskriften kommer alltså att variera beroende på vad användaren matar in:

Ange ditt namn: Kira Kodare\nHej på dig, Kira Kodare

Ordet namn i programmet ovan är en variabel. Inom programmering är en variabel ett ställe för att lagra ett värde – till exempel en sträng eller ett nummer. Värdet kan användas senare och det kan också ändras.

I princip kan variabler namnges relativt fritt, men Python har några begränsningar som måste tas i beaktande.

Det är vanligt att namnge variabler på engelska, men du kan stöta på variabler som namngetts på andra språk – till exempel på programmerarens eget modersmål. Variabelns namn påverkar inte dess värde, så det har inte en direkt påverkan på programmets funktion. Däremot kan variabler namngivna på engelska förtydliga kodens funktion för läsaren – förutsatt att variablerna har logiska namn. Det viktigaste är att använda variabelnamn som gör koden lättläst och förståelig både för dig som programmerar och för andra som till exempel behöver granska koden.

Gör ett program som frågar efter användarens förnamn. Programmet ska sedan skriva ut namnet två gånger på varandra följande rader.

Så här ska programmet fungera:

Ange ditt namn: Pauline\nPauline\nPauline

Hänvisa till en variabel

En viss variabel kan hänvisas till flera gånger i ett program:

namn = input(\"Ange ditt namn: \")\n\nprint(\"Hej, \" + namn + \"!\")\nprint(namn + \" är ett ganska fint namn.\")

Om användaren ger namnet Paulus Python, skriver programmet ut följande rader:

Ange ditt namn: Paulus Python\nHej, Paulus Python!\nPaulus Python är ett ganska fint namn.

Låt oss undersöka närmare hur print används ovan. Mellan parenteserna i instruktionen finns det både text inom citattecken och namn på variabler, som hänvisar till indata från användaren. De har kombinerats med +-operatorn, som kombinerar två strängar till en sträng.

Strängar och variabler kan kombineras ganska fritt:

namn = input(\"Ange ditt namn: \")\n\nprint(\"Hej \" + namn + \"! Ditt namn var alltså \" + namn + \"?\")

Om användaren ger namnet Ellen Exempel, skrivs följande ut:

Ange ditt namn: Ellen Exempel\nHej Ellen Exempel! Ditt namn var alltså Ellen Exempel?

Skriv ett program som frågar efter användarens förnamn. Programmet ska skriva ut det angivna namnet två gånger på samma rad, så att det finns ett utropstecken mellan namnen och i början och slutet av raden.

Så här ska programmet fungera:

Ange ditt namn: Sandrine\n!Sandrine!Sandrine!

Samla mera data

Ett program kan be användaren att mata in data flera gånger. Observera att vi då behöver flera variabler för att lagra de olika värdena som användaren matar in via input.

namn = input(\"Ange ditt namn: \")\nepost = input(\"Ange din e-postadress: \")\nsmeknamn = input(\"Ange ditt smeknamn: \")\n\nprint(\"Vi kollar ännu att alla uppgifter är korrekta\")\nprint(\"Ditt namn: \" + namn)\nprint(\"Din e-postadress: \" + epost)\nprint(\"Ditt smeknamn: \" + smeknamn)

Programmet kunde till exempel fungera så här när det körs:

Ange ditt namn: Per-Pierre Påhittad\nAnge din e-postadress: perpierre01@example.com\nAnge ditt smeknamn: PP\nVi kollar ännu att alla uppgifter är korrekta\nDitt namn: Per-Pierre Påhittad\nDin e-postadress: perpierre01@example.com\nDitt smeknamn: PP

Om en och samma variabel används för att lagra indata flera gånger kommer det nyaste värdet alltid att ersätta det föregående värdet. Till exempel:

gata = input(\"Vilken är din gatuadress? \")\nprint(\"Du bor alltså på \" + gata)\n\ngata = input(\"Ange en ny gatuadress: \")\nprint(\"Gatuadressen är nu \" + gata)

Så här kan det se ut när programmet körs:

Vilken är din gatuadress? Pythonparken 7\nDu bor alltså på Pythonparken 7\nAnge en ny gatuadress: Gredelingatan 17 B 231\nGatuadressen är nu Gredelingatan 17 B 231

Det här betyder att om samma variabel används för att lagra indata två gånger i rad så kommer det första värdet inte längre att vara tillgängligt efter att det har ersatts:

gata = input(\"Vilken är din gatuadress? \")\ngata = input(\"Ange en ny gatuadress: \")\n\nprint(\"Gatuadressen är nu \" + gata)

Så här kan det se ut när programmet körs:

Vilken är din gatuadress? Slitagestigen 2\nAnge en ny gatuadress: Värjan 1\nGatuadressen är nu Värjan 1

Skapa ett program som frågar efter användarens namn och adress. Skriv sedan ut dem.

Så här ska programmet fungera:

Förnamn: Cecilia\nEfternamn: Citronlik\nGatuadress: Bulevarden 12\nPostnummer och adressort: 00120 HELSINGFORS\nCecilia Citronlik\nBulevarden 12\n00120 HELSINGFORS

I den här uppgiften finns ett program som borde fråga användaren efter tre ord och skriva dem ut på följande sätt.

Ge ord 1: ärtan\nGe ord 2: pärtan\nGe ord 3: puff\närtan-pärtan-puff!

Det finns några problem i koden som du nu borde lösa, så att programmet fungerar korrekt.

Skapa ett program som skriver ut den följande berättelsen. Namnet och årtalet användaren anger ska användas i texten.

Ange namn: Amelie\nAnge år: 1993

Amelie är kommunens bästa läkare. När Amelie, född 1993, vaknar till ett telefonsamtal klockan 4 på morgonen, blir det brådis. Amelie behövs till akuten för att rädda livet hos en cyklist som krockat med en bil.

Berättelsen ska ändras beroende på de värden användaren anger.

Du hittar ett repetitionsquiz för denna del här:

","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/2-info-fran-anvandaren.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man skriver ett program som kan läsa in information av användaren"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man använder variabler för att lagra indata och skriva ut motsvarande värden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du kombinera strängar."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Indata (input) syftar till information som programmet läser in från omvärlden. Det kan handla om allt från indata från tangentbordet eller musklick till sensorer. I Python används "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" för att läsa in en rad text som användaren matar in. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" kan också användas för att skriva ut ett meddelande till användaren för att be om någon specifik information."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet läser in användarens namn med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":". Därefter skriver programmet ut namnet med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej på dig, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, kan det se ut så här (indata från användaren markerat med rött):"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Pauline Python"}]},{"type":"text","value":"\nHej på dig, Pauline Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad programmet skriver ut beror här på den information som användaren ger. Utskriften kommer alltså att variera beroende på vad användaren matar in:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Kira Kodare"}]},{"type":"text","value":"\nHej på dig, Kira Kodare"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" i programmet ovan är en variabel. Inom programmering är en variabel ett ställe för att lagra ett värde – till exempel en sträng eller ett nummer. Värdet kan användas senare och det kan också ändras."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Att namnge variabler"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I princip kan variabler namnges relativt fritt, men Python har några begränsningar som måste tas i beaktande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är vanligt att namnge variabler på engelska, men du kan stöta på variabler som namngetts på andra språk – till exempel på programmerarens eget modersmål. Variabelns namn påverkar inte dess värde, så det har inte en direkt påverkan på programmets funktion. Däremot kan variabler namngivna på engelska förtydliga kodens funktion för läsaren – förutsatt att variablerna har logiska namn. Det viktigaste är att använda variabelnamn som gör koden lättläst och förståelig både för dig som programmerar och för andra som till exempel behöver granska koden."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Namn, två gånger","tmcname":"osa01-06_namn_tva_ganger"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör ett program som frågar efter användarens förnamn. Programmet ska sedan skriva ut namnet två gånger på varandra följande rader."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Pauline"}]},{"type":"text","value":"\nPauline\nPauline"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"hanvisa-till-en-variabel","style":"position:relative;"},"children":[{"type":"text","value":"Hänvisa till en variabel"},{"type":"element","tagName":"a","properties":{"href":"#hanvisa-till-en-variabel","ariaLabel":"hanvisa till en variabel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En viss variabel kan hänvisas till flera gånger i ett program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" är ett ganska fint namn.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren ger namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Paulus Python"}]},{"type":"text","value":", skriver programmet ut följande rader:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paulus Python"}]},{"type":"text","value":"\nHej, Paulus Python!\nPaulus Python är ett ganska fint namn."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss undersöka närmare hur "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" används ovan. Mellan parenteserna i instruktionen finns det både text inom citattecken och namn på variabler, som hänvisar till indata från användaren. De har kombinerats med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-operatorn, som kombinerar två strängar till en sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar och variabler kan kombineras ganska fritt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"! Ditt namn var alltså \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren ger namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Ellen Exempel"}]},{"type":"text","value":", skrivs följande ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ellen Exempel"}]},{"type":"text","value":"\nHej Ellen Exempel! Ditt namn var alltså Ellen Exempel?"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Namn med utropstecken","tmcname":"osa01-07_namn_utropstecken"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som frågar efter användarens förnamn. Programmet ska skriva ut det angivna namnet två gånger på samma rad, så att det finns ett utropstecken mellan namnen och i början och slutet av raden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Sandrine"}]},{"type":"text","value":"\n!Sandrine!Sandrine!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"samla-mera-data","style":"position:relative;"},"children":[{"type":"text","value":"Samla mera data"},{"type":"element","tagName":"a","properties":{"href":"#samla-mera-data","ariaLabel":"samla mera data permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett program kan be användaren att mata in data flera gånger. Observera att vi då behöver flera variabler för att lagra de olika värdena som användaren matar in via "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nepost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange din e-postadress: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsmeknamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt smeknamn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vi kollar ännu att alla uppgifter är korrekta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ditt namn: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Din e-postadress: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ditt smeknamn: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" smeknamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet kunde till exempel fungera så här när det körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Per-Pierre Påhittad"}]},{"type":"text","value":"\nAnge din e-postadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"mailto:perpierre01@example.com","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"perpierre01@example.com"}]}]},{"type":"text","value":"\nAnge ditt smeknamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"PP"}]},{"type":"text","value":"\nVi kollar ännu att alla uppgifter är korrekta\nDitt namn: Per-Pierre Påhittad\nDin e-postadress: "},{"type":"element","tagName":"a","properties":{"href":"mailto:perpierre01@example.com","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"perpierre01@example.com"}]},{"type":"text","value":"\nDitt smeknamn: PP"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en och samma variabel används för att lagra indata flera gånger kommer det nyaste värdet alltid att ersätta det föregående värdet. Till exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilken är din gatuadress? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du bor alltså på \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" gata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ngata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange en ny gatuadress: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gatuadressen är nu \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" gata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vilken är din gatuadress? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Pythonparken 7"}]},{"type":"text","value":"\nDu bor alltså på Pythonparken 7\nAnge en ny gatuadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Gredelingatan 17 B 231"}]},{"type":"text","value":"\nGatuadressen är nu Gredelingatan 17 B 231"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här betyder att om samma variabel används för att lagra indata två gånger i rad så kommer det första värdet inte längre att vara tillgängligt efter att det har ersatts:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilken är din gatuadress? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange en ny gatuadress: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gatuadressen är nu \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" gata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vilken är din gatuadress? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Slitagestigen 2"}]},{"type":"text","value":"\nAnge en ny gatuadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Värjan 1"}]},{"type":"text","value":"\nGatuadressen är nu Värjan 1"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Namn och adress","tmcname":"osa01-08_namn_adress"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter användarens namn och adress. Skriv sedan ut dem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Cecilia"}]},{"type":"text","value":"\nEfternamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Citronlik"}]},{"type":"text","value":"\nGatuadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Bulevarden 12"}]},{"type":"text","value":"\nPostnummer och adressort: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"00120 HELSINGFORS"}]},{"type":"text","value":"\nCecilia Citronlik\nBulevarden 12\n00120 HELSINGFORS"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Uttryck","tmcname":"osa01-09_uttryck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften finns ett program som borde fråga användaren efter tre ord och skriva dem ut på följande sätt."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ord 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ärtan"}]},{"type":"text","value":"\nGe ord 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pärtan"}]},{"type":"text","value":"\nGe ord 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"puff"}]},{"type":"text","value":"\närtan-pärtan-puff!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns några problem i koden som du nu borde lösa, så att programmet fungerar korrekt."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Berättelse","tmcname":"osa01-10_berattelse"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som skriver ut den följande berättelsen. Namnet och årtalet användaren anger ska användas i texten."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Amelie"}]},{"type":"text","value":"\nAnge år: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1993"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Amelie är kommunens bästa läkare. När Amelie, född 1993, vaknar till ett telefonsamtal klockan 4 på morgonen, blir det brådis. Amelie behövs till akuten för att rädda livet hos en cyklist som krockat med en bil."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berättelsen ska ändras beroende på de värden användaren anger."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du hittar ett repetitionsquiz för denna del här:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14"},"children":[]}]}]},"html":"

Efter den här delen

Indata (input) syftar till information som programmet läser in från omvärlden. Det kan handla om allt från indata från tangentbordet eller musklick till sensorer. I Python används input för att läsa in en rad text som användaren matar in. input kan också användas för att skriva ut ett meddelande till användaren för att be om någon specifik information.

Det här programmet läser in användarens namn med hjälp av input. Därefter skriver programmet ut namnet med print:

namn = input(\"Ange ditt namn: \")\nprint(\"Hej på dig, \" + namn)

När programmet körs, kan det se ut så här (indata från användaren markerat med rött):

Ange ditt namn: Pauline Python\nHej på dig, Pauline Python

Vad programmet skriver ut beror här på den information som användaren ger. Utskriften kommer alltså att variera beroende på vad användaren matar in:

Ange ditt namn: Kira Kodare\nHej på dig, Kira Kodare

Ordet namn i programmet ovan är en variabel. Inom programmering är en variabel ett ställe för att lagra ett värde – till exempel en sträng eller ett nummer. Värdet kan användas senare och det kan också ändras.

I princip kan variabler namnges relativt fritt, men Python har några begränsningar som måste tas i beaktande.

Det är vanligt att namnge variabler på engelska, men du kan stöta på variabler som namngetts på andra språk – till exempel på programmerarens eget modersmål. Variabelns namn påverkar inte dess värde, så det har inte en direkt påverkan på programmets funktion. Däremot kan variabler namngivna på engelska förtydliga kodens funktion för läsaren – förutsatt att variablerna har logiska namn. Det viktigaste är att använda variabelnamn som gör koden lättläst och förståelig både för dig som programmerar och för andra som till exempel behöver granska koden.

Gör ett program som frågar efter användarens förnamn. Programmet ska sedan skriva ut namnet två gånger på varandra följande rader.

Så här ska programmet fungera:

Ange ditt namn: Pauline\nPauline\nPauline

Hänvisa till en variabel

En viss variabel kan hänvisas till flera gånger i ett program:

namn = input(\"Ange ditt namn: \")\n\nprint(\"Hej, \" + namn + \"!\")\nprint(namn + \" är ett ganska fint namn.\")

Om användaren ger namnet Paulus Python, skriver programmet ut följande rader:

Ange ditt namn: Paulus Python\nHej, Paulus Python!\nPaulus Python är ett ganska fint namn.

Låt oss undersöka närmare hur print används ovan. Mellan parenteserna i instruktionen finns det både text inom citattecken och namn på variabler, som hänvisar till indata från användaren. De har kombinerats med +-operatorn, som kombinerar två strängar till en sträng.

Strängar och variabler kan kombineras ganska fritt:

namn = input(\"Ange ditt namn: \")\n\nprint(\"Hej \" + namn + \"! Ditt namn var alltså \" + namn + \"?\")

Om användaren ger namnet Ellen Exempel, skrivs följande ut:

Ange ditt namn: Ellen Exempel\nHej Ellen Exempel! Ditt namn var alltså Ellen Exempel?

Skriv ett program som frågar efter användarens förnamn. Programmet ska skriva ut det angivna namnet två gånger på samma rad, så att det finns ett utropstecken mellan namnen och i början och slutet av raden.

Så här ska programmet fungera:

Ange ditt namn: Sandrine\n!Sandrine!Sandrine!

Samla mera data

Ett program kan be användaren att mata in data flera gånger. Observera att vi då behöver flera variabler för att lagra de olika värdena som användaren matar in via input.

namn = input(\"Ange ditt namn: \")\nepost = input(\"Ange din e-postadress: \")\nsmeknamn = input(\"Ange ditt smeknamn: \")\n\nprint(\"Vi kollar ännu att alla uppgifter är korrekta\")\nprint(\"Ditt namn: \" + namn)\nprint(\"Din e-postadress: \" + epost)\nprint(\"Ditt smeknamn: \" + smeknamn)

Programmet kunde till exempel fungera så här när det körs:

Ange ditt namn: Per-Pierre Påhittad\nAnge din e-postadress: perpierre01@example.com\nAnge ditt smeknamn: PP\nVi kollar ännu att alla uppgifter är korrekta\nDitt namn: Per-Pierre Påhittad\nDin e-postadress: perpierre01@example.com\nDitt smeknamn: PP

Om en och samma variabel används för att lagra indata flera gånger kommer det nyaste värdet alltid att ersätta det föregående värdet. Till exempel:

gata = input(\"Vilken är din gatuadress? \")\nprint(\"Du bor alltså på \" + gata)\n\ngata = input(\"Ange en ny gatuadress: \")\nprint(\"Gatuadressen är nu \" + gata)

Så här kan det se ut när programmet körs:

Vilken är din gatuadress? Pythonparken 7\nDu bor alltså på Pythonparken 7\nAnge en ny gatuadress: Gredelingatan 17 B 231\nGatuadressen är nu Gredelingatan 17 B 231

Det här betyder att om samma variabel används för att lagra indata två gånger i rad så kommer det första värdet inte längre att vara tillgängligt efter att det har ersatts:

gata = input(\"Vilken är din gatuadress? \")\ngata = input(\"Ange en ny gatuadress: \")\n\nprint(\"Gatuadressen är nu \" + gata)

Så här kan det se ut när programmet körs:

Vilken är din gatuadress? Slitagestigen 2\nAnge en ny gatuadress: Värjan 1\nGatuadressen är nu Värjan 1

Skapa ett program som frågar efter användarens namn och adress. Skriv sedan ut dem.

Så här ska programmet fungera:

Förnamn: Cecilia\nEfternamn: Citronlik\nGatuadress: Bulevarden 12\nPostnummer och adressort: 00120 HELSINGFORS\nCecilia Citronlik\nBulevarden 12\n00120 HELSINGFORS

I den här uppgiften finns ett program som borde fråga användaren efter tre ord och skriva dem ut på följande sätt.

Ge ord 1: ärtan\nGe ord 2: pärtan\nGe ord 3: puff\närtan-pärtan-puff!

Det finns några problem i koden som du nu borde lösa, så att programmet fungerar korrekt.

Skapa ett program som skriver ut den följande berättelsen. Namnet och årtalet användaren anger ska användas i texten.

Ange namn: Amelie\nAnge år: 1993

Amelie är kommunens bästa läkare. När Amelie, född 1993, vaknar till ett telefonsamtal klockan 4 på morgonen, blir det brådis. Amelie behövs till akuten för att rädda livet hos en cyklist som krockat med en bil.

Berättelsen ska ändras beroende på de värden användaren anger.

Du hittar ett repetitionsquiz för denna del här:

","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/2-info-fran-anvandaren.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-1/3-mer-om-variabler/page-data.json b/page-data/osa-1/3-mer-om-variabler/page-data.json index d62a5aabf..29a3c61a1 100644 --- a/page-data/osa-1/3-mer-om-variabler/page-data.json +++ b/page-data/osa-1/3-mer-om-variabler/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-1/3-mer-om-variabler", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda variabler i olika situationer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vilken typ av data som kan lagras i variabler"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till skillnaderna mellan strängar, heltal och flyttal."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen fyll i den här enkäten före du börjar med den här delen. Du får ett poäng efter att du har fyllt i enkäten."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"64c34f03-2a59-5f9e-8850-e2112625b111"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabler har olika användningsområden inom programmering. Du kan använda variabler för att lagra vilken som helst typ av information som kan behövas senare medan ett program körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python skapas variabler på följande sätt:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"variabelns_namn = ..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"..."}]},{"type":"text","value":" syftar till värdet som sparas i variabeln."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel när du använde "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" för att läsa in en sträng från användaren, sparade du strängen i en variabel och använde variabeln senare i ditt program."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Konstantin"}]},{"type":"text","value":"\nHejsan Konstantin"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man ger en variabel ett värde säger man att man "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tilldelar"}]},{"type":"text","value":" den ett värde. I koden ovan tilldelar vi alltså variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" det värde som användaren matar in som svar på frågan \"Ange ditt namn:\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdet som lagras i variabeln kan också definieras med hjälp av andra variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fornamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gabrielle\""}]},{"type":"text","value":"\nefternamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gullholm\""}]},{"type":"text","value":"\n\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fornamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" efternamn\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gabrielle Gullholm"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdena som lagras i variablerna ovan kommer inte från användaren. De ändrar inte utan förblir de samma varje gång programmet körs. Det här kallas att hårdkoda data i programmet."}]},{"type":"element","tagName":"h2","properties":{"id":"att-byta-varde-pa-en-variabel","style":"position:relative;"},"children":[{"type":"text","value":"Att byta värde på en variabel"},{"type":"element","tagName":"a","properties":{"href":"#att-byta-varde-pa-en-variabel","ariaLabel":"att byta varde pa en variabel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som namnet möjligtvis avslöjar, kan värdet på en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"variabel"}]},{"type":"text","value":" variera, alltså ändra. I den förra delen observerade vi att ett nytt värde ersätter det tidigare värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Medan vi kör följande program, kommer variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" att ha tre olika värden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett annat ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"första"}]},{"type":"text","value":"\nförsta\nGe ett annat ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"andra"}]},{"type":"text","value":"\nandra\ntredje"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdet som är lagrat i variabeln ändrar varje gång vi tilldelar variabeln ett nytt värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det nya värdet på en variabel kan basera sig på det föregående värdet. I följande exempel tilldelas variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" först ett värde på basis av indata från användaren. Därefter tilldelas variabeln ett nytt värde – som består av det gamla värdet följt av tre utropstecken."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!!!\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test"}]},{"type":"text","value":"\ntest\ntest!!!"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Att välja ett bra namn för en variabel"},"children":[{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det är vanligtvis en bra idé att namnge variabler enligt deras funktion. Exempelvis om en variabel innehåller ett ord, så är namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" ett mycket bättre namn än "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det finns ingen begränsning på variabelnamnens längd i Python, men det finns en del andra begränsningar. Variabelns namn ska börja med en bokstav och det kan endast innehålla bokstäver, siffror och understreck (_)."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Stora och små bokstäver är olika tecken. Variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NAMN"}]},{"type":"text","value":" är därför enligt Python olika variabler. Den här regeln har några undantag, men vi ignorera dem tills vidare."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det är ett vanligt tillvägagångssätt att i Python endast använda små bokstäver i variabelnamn. Om namnet består av flera ord använder man understreck mellan orden (t.ex. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start_hastighet"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"slut_hastighet"}]},{"type":"text","value":"). Även den här regeln har några undantag, men vi ignorerar dem tills vidare."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"heltal","style":"position:relative;"},"children":[{"type":"text","value":"Heltal"},{"type":"element","tagName":"a","properties":{"href":"#heltal","ariaLabel":"heltal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi enbart lagrat strängar i variabler, men det finns också flera andra datatyper som vi kommer att vilja lagra och använda senare. Vi börjar med att titta på heltal. Heltal är tal utan decimal- eller bråkdel. Exempelvis -15, 0 och 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program skapar variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alder"}]},{"type":"text","value":". Eftersom vi tilldelar variabeln värdet 24, kommer variabeln att vara av typen heltal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften ser helt enkelt ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"24"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att citattecknen fattas. Om vi skulle lägga till citattecken runt siffran skulle det inte längre vara ett heltal, utan en sträng. En sträng kan innehålla siffror, men strängar behandlas på ett annat sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varför ska variabler då ha en typ när programmets utskrift ändå ser lika ut i vilket fall som helst?"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\nsiffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"100\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"100\n100"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabelns datatyp spelar en viktig roll, eftersom olika operationer påverkar olika typer av variabler på olika sätt. Ta en titt på följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\nsiffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"100\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden skriver ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"200\n100100"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För heltal betyder operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" addition, medan den för strängar innebär kombination av två strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla operatorer är inte tillgängliga för alla typer av variabler. Siffror kan divideras med operatorn /, medan vi inte kan dividera strängar. Att försöka göra det ger upphov till ett felmeddelande."}]},{"type":"element","tagName":"h2","properties":{"id":"att-kombinera-varden-i-samband-med-utskrift","style":"position:relative;"},"children":[{"type":"text","value":"Att kombinera värden i samband med utskrift"},{"type":"element","tagName":"a","properties":{"href":"#att-kombinera-varden-i-samband-med-utskrift","ariaLabel":"att kombinera varden i samband med utskrift permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande kommer inte att fungera eftersom "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Resultatet är \""}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":" är av olika typer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# nästa rad orsakar ett fel"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Resultatet är: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet skriver inte ut någonting – istället får vi ett fel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unsupported operand type(s) for +: 'str' and 'int'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I felet berättar Python att det inte går att kombinera två värden av olika typ så här enkelt. I det här exemplet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Resultatet är \""}]},{"type":"text","value":" av typen sträng medan värdet i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":" är ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill skriva ut en sträng och ett heltal i en och samma instruktion kan vi konvertera heltalet till en sträng med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":"-funktionen. Därefter kan de två strängarna kombineras normalt. Till exempel så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Resultatet är: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet är: 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" har också inbyggd funktionalitet för att skriva ut en kombination av olika typer av värden. Det enklaste sättet är att lägga in ett komma mellan värdena. Alla värden kommer då att skrivas ut – oavsett typ:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Resultatet är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet är: 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att det i detta fall automatiskt läggs till ett mellanslag mellan värdena."}]},{"type":"element","tagName":"h2","properties":{"id":"utskrift-med-f-strangar","style":"position:relative;"},"children":[{"type":"text","value":"Utskrift med f-strängar"},{"type":"element","tagName":"a","properties":{"href":"#utskrift-med-f-strangar","ariaLabel":"utskrift med f strangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur kan vi gå till väga om vi önskar mera flexibilitet och kontroll över det som skrivs ut? Ett sätt är att använda f-strängar. Syntaxen kan till att börja med verka en aning konstig, men f-strängar är ändå ofta det enklaste sättet att påverka formatet på en text."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med f-strängar skulle det föregående exemplet se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Resultatet är: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss se hur ovanstående exempel fungerar, del för del. Före strängen som vi vill skriva ut finns bokstaven f. Den här bokstaven berättar för Python att följande sträng ska tolkas som en f-sträng. Inom strängen finns variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":" innanför klamrar (måsvingar). Detta anger att variabelns värde kommer att bli en del av strängen då den skrivs ut. Utskriften ser helt likadan ut som i de föregående exemplen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet är: 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En och samma f-sträng kan innehålla flera variabler. Den här koden…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Joline\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"text","value":"\nstad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kyrkslätt\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Hej "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", du är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år. Du bor i "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"stad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":".\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skriver ut det följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Joline, du är 24 år. Du bor i Kyrkslätt."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är svårt att åstadkomma en likadan utskrift med hjälp av kommanotationen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":". Exempelvis skriver programmet…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Joline\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"text","value":"\nstad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kyrkslätt\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", du är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" år. Du bor i\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"… ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Joline , du är 24 år. Du bor i Kyrkslätt ."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera mellanslagen som automatiskt har lagts till mellan varje kommaseparerad del i instruktionen. Det är tekniskt sett möjligt att förhindra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen från att lägga till mellanslag, men det är inte värt det eftersom vi kan använda oss av f-strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kommanotationen kan vara till nytta ibland, men ofta orsakar den mer problem än vad den löser. f-strängar är ofta en pålitligare metod. I modul fyra kommer du att lära dig om andra egenskaper hos f-strängar som för att vi kan påverka den utskrivna textens format på flera sätt."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Med mellanslag eller utan","tmcname":"osa01-10b_mellanslag_eller_inte","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du får följande kodsnutt av en bekant:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tindra Testare\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\nkunskap1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"text","value":"\nniva1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nybörjare\""}]},{"type":"text","value":"\nkunskap2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"java\""}]},{"type":"text","value":"\nniva2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"expert\""}]},{"type":"text","value":"\nkunskap3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmering\""}]},{"type":"text","value":"\nniva3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nästan proffs\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3000"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mitt namn är \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" , jag är \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"till mina kunskaper hör\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kunskap1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" niva1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\")\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kunskap2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" niva2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\")\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kunskap3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" niva3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" )\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jag söker jobb vars lön är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"euro i månaden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden borde resultera i en exakt lika utskrift som följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"mitt namn är Tindra Testare, jag är 20 år\n\ntill mina kunskaper hör\n - python (nybörjare)\n - java (expert)\n - programmering (nästan proffs)\n\njag söker jobb vars lön är 2000-3000 euro i månaden\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden fungerar ungefär korrekt. Din uppgift är att korrigera koden. Testen i den här uppgiften är noggranna. Till och med ett litet mellanslag på fel ställe kommer att orsaka problem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden ska alltså korrigeras så att utskriften ser korrekt ut. Observera att framför allt kommanotationen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" ofta orsakar mellanslag på ställen där de är oönskade."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det enklaste sättet att korrigera koden är att använda f-strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Du kan lägga till ett radbyte med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print()"}]},{"type":"text","value":" eller genom att inkludera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" på det stället i en sträng där radbytet ska vara."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att lägga märke till utskriftsformatet i alla övningar även under resten av kursen. Vissa uppgifter kräver att utskriften från programmet är exakt densamma som i de givna exemplen."}]}]},{"type":"element","tagName":"h2","properties":{"id":"flyttal","style":"position:relative;"},"children":[{"type":"text","value":"Flyttal"},{"type":"element","tagName":"a","properties":{"href":"#flyttal","ariaLabel":"flyttal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flyttal är en term som du ofta kommer att stöta på i programmering. Det hänvisar till tal med decimaltecken. Flyttal kan användas på väldigt liknande sätt som heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program beräknar medeltalet av tre flyttal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.5"}]},{"type":"text","value":"\nsiffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.25"}]},{"type":"text","value":"\nsiffra3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.62"}]},{"type":"text","value":"\n\nmedeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Medelvärde: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Medelvärde: 1.6233333333333333"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Räkneoperationer","tmcname":"osa01-11_rakneoperationer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen finns ett färdigt program med heltal tilldelade till variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utveckla programmet vidare så att utskriften ser ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"27 + 15 = 42\n27 - 15 = 12\n27 * 15 = 405\n27 / 15 = 1.8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska också fungera då värdet på variablerna ändras. I följande fall..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...ser utskriften ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 + 9 = 13\n4 - 9 = -5\n4 * 9 = 36\n4 / 9 = 0.4444444444444444"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Utskrifterna på samma rad","tmcname":"osa01-12_utskrifter_pa_samma_rad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om man ger "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end = \"\""}]},{"type":"text","value":", kommer utskriften inte att avslutas med ett radbyte."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"allesammans!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej allesammans!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigera programmet så att räkneoperationen och resultatet skrivs ut på en rad. Antalet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktioner får dock inte ändras."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" + \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" - \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" = \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du hittar ett repetitionsquiz för denna del här:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2"},"children":[]}]}]},"html":"

Efter den här delen

Vänligen fyll i den här enkäten före du börjar med den här delen. Du får ett poäng efter att du har fyllt i enkäten.

Variabler har olika användningsområden inom programmering. Du kan använda variabler för att lagra vilken som helst typ av information som kan behövas senare medan ett program körs.

I Python skapas variabler på följande sätt:

variabelns_namn = ...

där ... syftar till värdet som sparas i variabeln.

Till exempel när du använde input för att läsa in en sträng från användaren, sparade du strängen i en variabel och använde variabeln senare i ditt program.

namn = input(\"Ange ditt namn: \")\nprint(\"Hejsan \" + namn)

Ange ditt namn: Konstantin\nHejsan Konstantin

När man ger en variabel ett värde säger man att man tilldelar den ett värde. I koden ovan tilldelar vi alltså variabeln namn det värde som användaren matar in som svar på frågan \"Ange ditt namn:\".

Värdet som lagras i variabeln kan också definieras med hjälp av andra variabler:

fornamn = \"Gabrielle\"\nefternamn = \"Gullholm\"\n\nnamn = fornamn + \" \" + efternamn\n\nprint(namn)

Gabrielle Gullholm

Värdena som lagras i variablerna ovan kommer inte från användaren. De ändrar inte utan förblir de samma varje gång programmet körs. Det här kallas att hårdkoda data i programmet.

Att byta värde på en variabel

Som namnet möjligtvis avslöjar, kan värdet på en variabel variera, alltså ändra. I den förra delen observerade vi att ett nytt värde ersätter det tidigare värdet.

Medan vi kör följande program, kommer variabeln ord att ha tre olika värden:

ord = input(\"Ge ett ord: \")\nprint(ord)\n\nord = input(\"Ge ett annat ord: \")\nprint(ord)\n\nord = \"tredje\"\nprint(ord)

Ge ett ord: första\nförsta\nGe ett annat ord: andra\nandra\ntredje

Värdet som är lagrat i variabeln ändrar varje gång vi tilldelar variabeln ett nytt värde.

Det nya värdet på en variabel kan basera sig på det föregående värdet. I följande exempel tilldelas variabeln ord först ett värde på basis av indata från användaren. Därefter tilldelas variabeln ett nytt värde – som består av det gamla värdet följt av tre utropstecken.

ord = input(\"Ge ett ord: \")\nprint(ord)\n\nord = ord + \"!!!\"\nprint(ord)

Ge ett ord: test\ntest\ntest!!!

Heltal

Hittills har vi enbart lagrat strängar i variabler, men det finns också flera andra datatyper som vi kommer att vilja lagra och använda senare. Vi börjar med att titta på heltal. Heltal är tal utan decimal- eller bråkdel. Exempelvis -15, 0 och 1.

Följande program skapar variabeln alder. Eftersom vi tilldelar variabeln värdet 24, kommer variabeln att vara av typen heltal:

alder = 24\nprint(alder)

Utskriften ser helt enkelt ut så här:

24

Märk att citattecknen fattas. Om vi skulle lägga till citattecken runt siffran skulle det inte längre vara ett heltal, utan en sträng. En sträng kan innehålla siffror, men strängar behandlas på ett annat sätt.

Varför ska variabler då ha en typ när programmets utskrift ändå ser lika ut i vilket fall som helst?

siffra1 = 100\nsiffra2 = \"100\"\n\nprint(siffra1)\nprint(siffra2)

100\n100

Variabelns datatyp spelar en viktig roll, eftersom olika operationer påverkar olika typer av variabler på olika sätt. Ta en titt på följande exempel:

siffra1 = 100\nsiffra2 = \"100\"\n\nprint(siffra1 + siffra1)\nprint(siffra2 + siffra2)

Koden skriver ut följande:

200\n100100

För heltal betyder operatorn + addition, medan den för strängar innebär kombination av två strängar.

Alla operatorer är inte tillgängliga för alla typer av variabler. Siffror kan divideras med operatorn /, medan vi inte kan dividera strängar. Att försöka göra det ger upphov till ett felmeddelande.

Att kombinera värden i samband med utskrift

Det följande kommer inte att fungera eftersom \"Resultatet är \" och resultat är av olika typer:

resultat = 10 * 25\n# nästa rad orsakar ett fel\nprint(\"Resultatet är: \" + resultat)

Programmet skriver inte ut någonting – istället får vi ett fel:

TypeError: unsupported operand type(s) for +: 'str' and 'int'

I felet berättar Python att det inte går att kombinera två värden av olika typ så här enkelt. I det här exemplet är \"Resultatet är \" av typen sträng medan värdet i variabeln resultat är ett heltal.

Om vi vill skriva ut en sträng och ett heltal i en och samma instruktion kan vi konvertera heltalet till en sträng med str-funktionen. Därefter kan de två strängarna kombineras normalt. Till exempel så här:

resultat = 10 * 25\nprint(\"Resultatet är: \" + str(resultat))

Resultatet är: 250

print har också inbyggd funktionalitet för att skriva ut en kombination av olika typer av värden. Det enklaste sättet är att lägga in ett komma mellan värdena. Alla värden kommer då att skrivas ut – oavsett typ:

resultat = 10 * 25\nprint(\"Resultatet är\", resultat)

Resultatet är: 250

Observera att det i detta fall automatiskt läggs till ett mellanslag mellan värdena.

Utskrift med f-strängar

Hur kan vi gå till väga om vi önskar mera flexibilitet och kontroll över det som skrivs ut? Ett sätt är att använda f-strängar. Syntaxen kan till att börja med verka en aning konstig, men f-strängar är ändå ofta det enklaste sättet att påverka formatet på en text.

Med f-strängar skulle det föregående exemplet se ut så här:

resultat = 10 * 25\nprint(f\"Resultatet är: {resultat}\")

Låt oss se hur ovanstående exempel fungerar, del för del. Före strängen som vi vill skriva ut finns bokstaven f. Den här bokstaven berättar för Python att följande sträng ska tolkas som en f-sträng. Inom strängen finns variabelnamnet resultat innanför klamrar (måsvingar). Detta anger att variabelns värde kommer att bli en del av strängen då den skrivs ut. Utskriften ser helt likadan ut som i de föregående exemplen:

Resultatet är: 250

En och samma f-sträng kan innehålla flera variabler. Den här koden…

namn = \"Joline\"\nalder = 24\nstad = \"Kyrkslätt\"\nprint(f\"Hej {namn}, du är {alder} år. Du bor i {stad}.\")

…skriver ut det följande:

Hej Joline, du är 24 år. Du bor i Kyrkslätt.

Det är svårt att åstadkomma en likadan utskrift med hjälp av kommanotationen i print. Exempelvis skriver programmet…

namn = \"Joline\"\nalder = 24\nstad = \"Kyrkslätt\"\nprint(\"Hej\", namn, \", du är\", alder, \" år. Du bor i\", stad, \".\")

… ut följande:

Hej Joline , du är 24 år. Du bor i Kyrkslätt .

Observera mellanslagen som automatiskt har lagts till mellan varje kommaseparerad del i instruktionen. Det är tekniskt sett möjligt att förhindra print-instruktionen från att lägga till mellanslag, men det är inte värt det eftersom vi kan använda oss av f-strängar.

Kommanotationen kan vara till nytta ibland, men ofta orsakar den mer problem än vad den löser. f-strängar är ofta en pålitligare metod. I modul fyra kommer du att lära dig om andra egenskaper hos f-strängar som för att vi kan påverka den utskrivna textens format på flera sätt.

Du får följande kodsnutt av en bekant:

namn = \"Tindra Testare\"\nalder = 20\nkunskap1 = \"python\"\nniva1 = \"nybörjare\"\nkunskap2 = \"java\"\nniva2 = \"expert\"\nkunskap3 = \"programmering\"\nniva3 = \"nästan proffs\"\nmin = 2000\nmax = 3000\n\nprint(\"mitt namn är \", namn, \" , jag är \", alder, \" år\")\nprint(\"till mina kunskaper hör\")\nprint(\"- \", kunskap1, \" (\", niva1, \")\")\nprint(\"- \", kunskap2, \" (\", niva2, \")\")\nprint(\"- \", kunskap3, \" (\", niva3, \" )\")\nprint(\"jag söker jobb vars lön är\", min, \"-\", max, \"euro i månaden\")

Koden borde resultera i en exakt lika utskrift som följande:

\nmitt namn är Tindra Testare, jag är 20 år\n\ntill mina kunskaper hör\n - python (nybörjare)\n - java (expert)\n - programmering (nästan proffs)\n\njag söker jobb vars lön är 2000-3000 euro i månaden\n

Koden fungerar ungefär korrekt. Din uppgift är att korrigera koden. Testen i den här uppgiften är noggranna. Till och med ett litet mellanslag på fel ställe kommer att orsaka problem.

Koden ska alltså korrigeras så att utskriften ser korrekt ut. Observera att framför allt kommanotationen i print ofta orsakar mellanslag på ställen där de är oönskade.

Det enklaste sättet att korrigera koden är att använda f-strängar.

Tips: Du kan lägga till ett radbyte med hjälp av print() eller genom att inkludera \\n på det stället i en sträng där radbytet ska vara.

Kom ihåg att lägga märke till utskriftsformatet i alla övningar även under resten av kursen. Vissa uppgifter kräver att utskriften från programmet är exakt densamma som i de givna exemplen.

Flyttal

Flyttal är en term som du ofta kommer att stöta på i programmering. Det hänvisar till tal med decimaltecken. Flyttal kan användas på väldigt liknande sätt som heltal.

Följande program beräknar medeltalet av tre flyttal:

siffra1 = 2.5\nsiffra2 = -1.25\nsiffra3 = 3.62\n\nmedeltal = (siffra1 + siffra2 + siffra3) / 3\nprint(f\"Medelvärde: {medeltal}\")

Medelvärde: 1.6233333333333333

I den här övningen finns ett färdigt program med heltal tilldelade till variablerna x och y:

x = 27\ny = 15

Utveckla programmet vidare så att utskriften ser ut så här:

27 + 15 = 42\n27 - 15 = 12\n27 * 15 = 405\n27 / 15 = 1.8

Programmet ska också fungera då värdet på variablerna ändras. I följande fall...

x = 4\ny = 9

...ser utskriften ut så här:

4 + 9 = 13\n4 - 9 = -5\n4 * 9 = 36\n4 / 9 = 0.4444444444444444

Om man ger print parametern end = \"\", kommer utskriften inte att avslutas med ett radbyte.

Exempel:

print(\"Hej \", end=\"\")\nprint(\"allesammans!\")

Hej allesammans!

Korrigera programmet så att räkneoperationen och resultatet skrivs ut på en rad. Antalet print-instruktioner får dock inte ändras.

print(5)\nprint(\" + \")\nprint(8)\nprint(\" - \")\nprint(4)\nprint(\" = \")\nprint(5 + 8 - 4)

Du hittar ett repetitionsquiz för denna del här:

","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/3-mer-om-variabler.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda variabler i olika situationer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vilken typ av data som kan lagras i variabler"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till skillnaderna mellan strängar, heltal och flyttal."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen fyll i den här enkäten före du börjar med den här delen. Du får ett poäng efter att du har fyllt i enkäten."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"64c34f03-2a59-5f9e-8850-e2112625b111"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabler har olika användningsområden inom programmering. Du kan använda variabler för att lagra vilken som helst typ av information som kan behövas senare medan ett program körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python skapas variabler på följande sätt:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"variabelns_namn = ..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"..."}]},{"type":"text","value":" syftar till värdet som sparas i variabeln."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel när du använde "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" för att läsa in en sträng från användaren, sparade du strängen i en variabel och använde variabeln senare i ditt program."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Konstantin"}]},{"type":"text","value":"\nHejsan Konstantin"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man ger en variabel ett värde säger man att man "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tilldelar"}]},{"type":"text","value":" den ett värde. I koden ovan tilldelar vi alltså variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" det värde som användaren matar in som svar på frågan \"Ange ditt namn:\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdet som lagras i variabeln kan också definieras med hjälp av andra variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fornamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gabrielle\""}]},{"type":"text","value":"\nefternamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gullholm\""}]},{"type":"text","value":"\n\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fornamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" efternamn\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gabrielle Gullholm"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdena som lagras i variablerna ovan kommer inte från användaren. De ändrar inte utan förblir de samma varje gång programmet körs. Det här kallas att hårdkoda data i programmet."}]},{"type":"element","tagName":"h2","properties":{"id":"att-byta-varde-pa-en-variabel","style":"position:relative;"},"children":[{"type":"text","value":"Att byta värde på en variabel"},{"type":"element","tagName":"a","properties":{"href":"#att-byta-varde-pa-en-variabel","ariaLabel":"att byta varde pa en variabel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som namnet möjligtvis avslöjar, kan värdet på en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"variabel"}]},{"type":"text","value":" variera, alltså ändra. I den förra delen observerade vi att ett nytt värde ersätter det tidigare värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Medan vi kör följande program, kommer variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" att ha tre olika värden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett annat ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"första"}]},{"type":"text","value":"\nförsta\nGe ett annat ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"andra"}]},{"type":"text","value":"\nandra\ntredje"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdet som är lagrat i variabeln ändrar varje gång vi tilldelar variabeln ett nytt värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det nya värdet på en variabel kan basera sig på det föregående värdet. I följande exempel tilldelas variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" först ett värde på basis av indata från användaren. Därefter tilldelas variabeln ett nytt värde – som består av det gamla värdet följt av tre utropstecken."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!!!\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test"}]},{"type":"text","value":"\ntest\ntest!!!"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Att välja ett bra namn för en variabel"},"children":[{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det är vanligtvis en bra idé att namnge variabler enligt deras funktion. Exempelvis om en variabel innehåller ett ord, så är namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" ett mycket bättre namn än "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det finns ingen begränsning på variabelnamnens längd i Python, men det finns en del andra begränsningar. Variabelns namn ska börja med en bokstav och det kan endast innehålla bokstäver, siffror och understreck (_)."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Stora och små bokstäver är olika tecken. Variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NAMN"}]},{"type":"text","value":" är därför enligt Python olika variabler. Den här regeln har några undantag, men vi ignorera dem tills vidare."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det är ett vanligt tillvägagångssätt att i Python endast använda små bokstäver i variabelnamn. Om namnet består av flera ord använder man understreck mellan orden (t.ex. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start_hastighet"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"slut_hastighet"}]},{"type":"text","value":"). Även den här regeln har några undantag, men vi ignorerar dem tills vidare."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"heltal","style":"position:relative;"},"children":[{"type":"text","value":"Heltal"},{"type":"element","tagName":"a","properties":{"href":"#heltal","ariaLabel":"heltal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi enbart lagrat strängar i variabler, men det finns också flera andra datatyper som vi kommer att vilja lagra och använda senare. Vi börjar med att titta på heltal. Heltal är tal utan decimal- eller bråkdel. Exempelvis -15, 0 och 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program skapar variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alder"}]},{"type":"text","value":". Eftersom vi tilldelar variabeln värdet 24, kommer variabeln att vara av typen heltal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften ser helt enkelt ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"24"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att citattecknen fattas. Om vi skulle lägga till citattecken runt siffran skulle det inte längre vara ett heltal, utan en sträng. En sträng kan innehålla siffror, men strängar behandlas på ett annat sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varför ska variabler då ha en typ när programmets utskrift ändå ser lika ut i vilket fall som helst?"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\nsiffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"100\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"100\n100"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabelns datatyp spelar en viktig roll, eftersom olika operationer påverkar olika typer av variabler på olika sätt. Ta en titt på följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\nsiffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"100\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden skriver ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"200\n100100"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För heltal betyder operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" addition, medan den för strängar innebär kombination av två strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla operatorer är inte tillgängliga för alla typer av variabler. Siffror kan divideras med operatorn /, medan vi inte kan dividera strängar. Att försöka göra det ger upphov till ett felmeddelande."}]},{"type":"element","tagName":"h2","properties":{"id":"att-kombinera-varden-i-samband-med-utskrift","style":"position:relative;"},"children":[{"type":"text","value":"Att kombinera värden i samband med utskrift"},{"type":"element","tagName":"a","properties":{"href":"#att-kombinera-varden-i-samband-med-utskrift","ariaLabel":"att kombinera varden i samband med utskrift permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande kommer inte att fungera eftersom "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Resultatet är \""}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":" är av olika typer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# nästa rad orsakar ett fel"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Resultatet är: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet skriver inte ut någonting – istället får vi ett fel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unsupported operand type(s) for +: 'str' and 'int'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I felet berättar Python att det inte går att kombinera två värden av olika typ så här enkelt. I det här exemplet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Resultatet är \""}]},{"type":"text","value":" av typen sträng medan värdet i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":" är ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill skriva ut en sträng och ett heltal i en och samma instruktion kan vi konvertera heltalet till en sträng med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":"-funktionen. Därefter kan de två strängarna kombineras normalt. Till exempel så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Resultatet är: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet är: 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" har också inbyggd funktionalitet för att skriva ut en kombination av olika typer av värden. Det enklaste sättet är att lägga in ett komma mellan värdena. Alla värden kommer då att skrivas ut – oavsett typ:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Resultatet är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet är: 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att det i detta fall automatiskt läggs till ett mellanslag mellan värdena."}]},{"type":"element","tagName":"h2","properties":{"id":"utskrift-med-f-strangar","style":"position:relative;"},"children":[{"type":"text","value":"Utskrift med f-strängar"},{"type":"element","tagName":"a","properties":{"href":"#utskrift-med-f-strangar","ariaLabel":"utskrift med f strangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur kan vi gå till väga om vi önskar mera flexibilitet och kontroll över det som skrivs ut? Ett sätt är att använda f-strängar. Syntaxen kan till att börja med verka en aning konstig, men f-strängar är ändå ofta det enklaste sättet att påverka formatet på en text."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med f-strängar skulle det föregående exemplet se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Resultatet är: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss se hur ovanstående exempel fungerar, del för del. Före strängen som vi vill skriva ut finns bokstaven f. Den här bokstaven berättar för Python att följande sträng ska tolkas som en f-sträng. Inom strängen finns variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":" innanför klamrar (måsvingar). Detta anger att variabelns värde kommer att bli en del av strängen då den skrivs ut. Utskriften ser helt likadan ut som i de föregående exemplen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet är: 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En och samma f-sträng kan innehålla flera variabler. Den här koden…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Joline\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"text","value":"\nstad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kyrkslätt\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Hej "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", du är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år. Du bor i "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"stad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":".\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skriver ut det följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Joline, du är 24 år. Du bor i Kyrkslätt."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är svårt att åstadkomma en likadan utskrift med hjälp av kommanotationen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":". Exempelvis skriver programmet…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Joline\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"text","value":"\nstad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kyrkslätt\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", du är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" år. Du bor i\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"… ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Joline , du är 24 år. Du bor i Kyrkslätt ."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera mellanslagen som automatiskt har lagts till mellan varje kommaseparerad del i instruktionen. Det är tekniskt sett möjligt att förhindra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen från att lägga till mellanslag, men det är inte värt det eftersom vi kan använda oss av f-strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kommanotationen kan vara till nytta ibland, men ofta orsakar den mer problem än vad den löser. f-strängar är ofta en pålitligare metod. I modul fyra kommer du att lära dig om andra egenskaper hos f-strängar som för att vi kan påverka den utskrivna textens format på flera sätt."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Med mellanslag eller utan","tmcname":"osa01-10b_mellanslag_eller_inte","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du får följande kodsnutt av en bekant:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tindra Testare\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\nkunskap1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"text","value":"\nniva1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nybörjare\""}]},{"type":"text","value":"\nkunskap2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"java\""}]},{"type":"text","value":"\nniva2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"expert\""}]},{"type":"text","value":"\nkunskap3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmering\""}]},{"type":"text","value":"\nniva3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nästan proffs\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3000"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mitt namn är \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" , jag är \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"till mina kunskaper hör\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kunskap1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" niva1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\")\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kunskap2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" niva2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\")\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kunskap3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" niva3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" )\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jag söker jobb vars lön är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"euro i månaden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden borde resultera i en exakt lika utskrift som följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"mitt namn är Tindra Testare, jag är 20 år\n\ntill mina kunskaper hör\n - python (nybörjare)\n - java (expert)\n - programmering (nästan proffs)\n\njag söker jobb vars lön är 2000-3000 euro i månaden\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden fungerar ungefär korrekt. Din uppgift är att korrigera koden. Testen i den här uppgiften är noggranna. Till och med ett litet mellanslag på fel ställe kommer att orsaka problem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden ska alltså korrigeras så att utskriften ser korrekt ut. Observera att framför allt kommanotationen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" ofta orsakar mellanslag på ställen där de är oönskade."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det enklaste sättet att korrigera koden är att använda f-strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Du kan lägga till ett radbyte med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print()"}]},{"type":"text","value":" eller genom att inkludera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" på det stället i en sträng där radbytet ska vara."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att lägga märke till utskriftsformatet i alla övningar även under resten av kursen. Vissa uppgifter kräver att utskriften från programmet är exakt densamma som i de givna exemplen."}]}]},{"type":"element","tagName":"h2","properties":{"id":"flyttal","style":"position:relative;"},"children":[{"type":"text","value":"Flyttal"},{"type":"element","tagName":"a","properties":{"href":"#flyttal","ariaLabel":"flyttal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flyttal är en term som du ofta kommer att stöta på i programmering. Det hänvisar till tal med decimaltecken. Flyttal kan användas på väldigt liknande sätt som heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program beräknar medeltalet av tre flyttal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.5"}]},{"type":"text","value":"\nsiffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.25"}]},{"type":"text","value":"\nsiffra3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.62"}]},{"type":"text","value":"\n\nmedeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Medelvärde: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Medelvärde: 1.6233333333333333"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Räkneoperationer","tmcname":"osa01-11_rakneoperationer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen finns ett färdigt program med heltal tilldelade till variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utveckla programmet vidare så att utskriften ser ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"27 + 15 = 42\n27 - 15 = 12\n27 * 15 = 405\n27 / 15 = 1.8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska också fungera då värdet på variablerna ändras. I följande fall..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...ser utskriften ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 + 9 = 13\n4 - 9 = -5\n4 * 9 = 36\n4 / 9 = 0.4444444444444444"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Utskrifterna på samma rad","tmcname":"osa01-12_utskrifter_pa_samma_rad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om man ger "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end = \"\""}]},{"type":"text","value":", kommer utskriften inte att avslutas med ett radbyte."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"allesammans!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej allesammans!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigera programmet så att räkneoperationen och resultatet skrivs ut på en rad. Antalet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktioner får dock inte ändras."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" + \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" - \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" = \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du hittar ett repetitionsquiz för denna del här:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2"},"children":[]}]}]},"html":"

Efter den här delen

Vänligen fyll i den här enkäten före du börjar med den här delen. Du får ett poäng efter att du har fyllt i enkäten.

Variabler har olika användningsområden inom programmering. Du kan använda variabler för att lagra vilken som helst typ av information som kan behövas senare medan ett program körs.

I Python skapas variabler på följande sätt:

variabelns_namn = ...

där ... syftar till värdet som sparas i variabeln.

Till exempel när du använde input för att läsa in en sträng från användaren, sparade du strängen i en variabel och använde variabeln senare i ditt program.

namn = input(\"Ange ditt namn: \")\nprint(\"Hejsan \" + namn)

Ange ditt namn: Konstantin\nHejsan Konstantin

När man ger en variabel ett värde säger man att man tilldelar den ett värde. I koden ovan tilldelar vi alltså variabeln namn det värde som användaren matar in som svar på frågan \"Ange ditt namn:\".

Värdet som lagras i variabeln kan också definieras med hjälp av andra variabler:

fornamn = \"Gabrielle\"\nefternamn = \"Gullholm\"\n\nnamn = fornamn + \" \" + efternamn\n\nprint(namn)

Gabrielle Gullholm

Värdena som lagras i variablerna ovan kommer inte från användaren. De ändrar inte utan förblir de samma varje gång programmet körs. Det här kallas att hårdkoda data i programmet.

Att byta värde på en variabel

Som namnet möjligtvis avslöjar, kan värdet på en variabel variera, alltså ändra. I den förra delen observerade vi att ett nytt värde ersätter det tidigare värdet.

Medan vi kör följande program, kommer variabeln ord att ha tre olika värden:

ord = input(\"Ge ett ord: \")\nprint(ord)\n\nord = input(\"Ge ett annat ord: \")\nprint(ord)\n\nord = \"tredje\"\nprint(ord)

Ge ett ord: första\nförsta\nGe ett annat ord: andra\nandra\ntredje

Värdet som är lagrat i variabeln ändrar varje gång vi tilldelar variabeln ett nytt värde.

Det nya värdet på en variabel kan basera sig på det föregående värdet. I följande exempel tilldelas variabeln ord först ett värde på basis av indata från användaren. Därefter tilldelas variabeln ett nytt värde – som består av det gamla värdet följt av tre utropstecken.

ord = input(\"Ge ett ord: \")\nprint(ord)\n\nord = ord + \"!!!\"\nprint(ord)

Ge ett ord: test\ntest\ntest!!!

Heltal

Hittills har vi enbart lagrat strängar i variabler, men det finns också flera andra datatyper som vi kommer att vilja lagra och använda senare. Vi börjar med att titta på heltal. Heltal är tal utan decimal- eller bråkdel. Exempelvis -15, 0 och 1.

Följande program skapar variabeln alder. Eftersom vi tilldelar variabeln värdet 24, kommer variabeln att vara av typen heltal:

alder = 24\nprint(alder)

Utskriften ser helt enkelt ut så här:

24

Märk att citattecknen fattas. Om vi skulle lägga till citattecken runt siffran skulle det inte längre vara ett heltal, utan en sträng. En sträng kan innehålla siffror, men strängar behandlas på ett annat sätt.

Varför ska variabler då ha en typ när programmets utskrift ändå ser lika ut i vilket fall som helst?

siffra1 = 100\nsiffra2 = \"100\"\n\nprint(siffra1)\nprint(siffra2)

100\n100

Variabelns datatyp spelar en viktig roll, eftersom olika operationer påverkar olika typer av variabler på olika sätt. Ta en titt på följande exempel:

siffra1 = 100\nsiffra2 = \"100\"\n\nprint(siffra1 + siffra1)\nprint(siffra2 + siffra2)

Koden skriver ut följande:

200\n100100

För heltal betyder operatorn + addition, medan den för strängar innebär kombination av två strängar.

Alla operatorer är inte tillgängliga för alla typer av variabler. Siffror kan divideras med operatorn /, medan vi inte kan dividera strängar. Att försöka göra det ger upphov till ett felmeddelande.

Att kombinera värden i samband med utskrift

Det följande kommer inte att fungera eftersom \"Resultatet är \" och resultat är av olika typer:

resultat = 10 * 25\n# nästa rad orsakar ett fel\nprint(\"Resultatet är: \" + resultat)

Programmet skriver inte ut någonting – istället får vi ett fel:

TypeError: unsupported operand type(s) for +: 'str' and 'int'

I felet berättar Python att det inte går att kombinera två värden av olika typ så här enkelt. I det här exemplet är \"Resultatet är \" av typen sträng medan värdet i variabeln resultat är ett heltal.

Om vi vill skriva ut en sträng och ett heltal i en och samma instruktion kan vi konvertera heltalet till en sträng med str-funktionen. Därefter kan de två strängarna kombineras normalt. Till exempel så här:

resultat = 10 * 25\nprint(\"Resultatet är: \" + str(resultat))

Resultatet är: 250

print har också inbyggd funktionalitet för att skriva ut en kombination av olika typer av värden. Det enklaste sättet är att lägga in ett komma mellan värdena. Alla värden kommer då att skrivas ut – oavsett typ:

resultat = 10 * 25\nprint(\"Resultatet är\", resultat)

Resultatet är: 250

Observera att det i detta fall automatiskt läggs till ett mellanslag mellan värdena.

Utskrift med f-strängar

Hur kan vi gå till väga om vi önskar mera flexibilitet och kontroll över det som skrivs ut? Ett sätt är att använda f-strängar. Syntaxen kan till att börja med verka en aning konstig, men f-strängar är ändå ofta det enklaste sättet att påverka formatet på en text.

Med f-strängar skulle det föregående exemplet se ut så här:

resultat = 10 * 25\nprint(f\"Resultatet är: {resultat}\")

Låt oss se hur ovanstående exempel fungerar, del för del. Före strängen som vi vill skriva ut finns bokstaven f. Den här bokstaven berättar för Python att följande sträng ska tolkas som en f-sträng. Inom strängen finns variabelnamnet resultat innanför klamrar (måsvingar). Detta anger att variabelns värde kommer att bli en del av strängen då den skrivs ut. Utskriften ser helt likadan ut som i de föregående exemplen:

Resultatet är: 250

En och samma f-sträng kan innehålla flera variabler. Den här koden…

namn = \"Joline\"\nalder = 24\nstad = \"Kyrkslätt\"\nprint(f\"Hej {namn}, du är {alder} år. Du bor i {stad}.\")

…skriver ut det följande:

Hej Joline, du är 24 år. Du bor i Kyrkslätt.

Det är svårt att åstadkomma en likadan utskrift med hjälp av kommanotationen i print. Exempelvis skriver programmet…

namn = \"Joline\"\nalder = 24\nstad = \"Kyrkslätt\"\nprint(\"Hej\", namn, \", du är\", alder, \" år. Du bor i\", stad, \".\")

… ut följande:

Hej Joline , du är 24 år. Du bor i Kyrkslätt .

Observera mellanslagen som automatiskt har lagts till mellan varje kommaseparerad del i instruktionen. Det är tekniskt sett möjligt att förhindra print-instruktionen från att lägga till mellanslag, men det är inte värt det eftersom vi kan använda oss av f-strängar.

Kommanotationen kan vara till nytta ibland, men ofta orsakar den mer problem än vad den löser. f-strängar är ofta en pålitligare metod. I modul fyra kommer du att lära dig om andra egenskaper hos f-strängar som för att vi kan påverka den utskrivna textens format på flera sätt.

Du får följande kodsnutt av en bekant:

namn = \"Tindra Testare\"\nalder = 20\nkunskap1 = \"python\"\nniva1 = \"nybörjare\"\nkunskap2 = \"java\"\nniva2 = \"expert\"\nkunskap3 = \"programmering\"\nniva3 = \"nästan proffs\"\nmin = 2000\nmax = 3000\n\nprint(\"mitt namn är \", namn, \" , jag är \", alder, \" år\")\nprint(\"till mina kunskaper hör\")\nprint(\"- \", kunskap1, \" (\", niva1, \")\")\nprint(\"- \", kunskap2, \" (\", niva2, \")\")\nprint(\"- \", kunskap3, \" (\", niva3, \" )\")\nprint(\"jag söker jobb vars lön är\", min, \"-\", max, \"euro i månaden\")

Koden borde resultera i en exakt lika utskrift som följande:

\nmitt namn är Tindra Testare, jag är 20 år\n\ntill mina kunskaper hör\n - python (nybörjare)\n - java (expert)\n - programmering (nästan proffs)\n\njag söker jobb vars lön är 2000-3000 euro i månaden\n

Koden fungerar ungefär korrekt. Din uppgift är att korrigera koden. Testen i den här uppgiften är noggranna. Till och med ett litet mellanslag på fel ställe kommer att orsaka problem.

Koden ska alltså korrigeras så att utskriften ser korrekt ut. Observera att framför allt kommanotationen i print ofta orsakar mellanslag på ställen där de är oönskade.

Det enklaste sättet att korrigera koden är att använda f-strängar.

Tips: Du kan lägga till ett radbyte med hjälp av print() eller genom att inkludera \\n på det stället i en sträng där radbytet ska vara.

Kom ihåg att lägga märke till utskriftsformatet i alla övningar även under resten av kursen. Vissa uppgifter kräver att utskriften från programmet är exakt densamma som i de givna exemplen.

Flyttal

Flyttal är en term som du ofta kommer att stöta på i programmering. Det hänvisar till tal med decimaltecken. Flyttal kan användas på väldigt liknande sätt som heltal.

Följande program beräknar medeltalet av tre flyttal:

siffra1 = 2.5\nsiffra2 = -1.25\nsiffra3 = 3.62\n\nmedeltal = (siffra1 + siffra2 + siffra3) / 3\nprint(f\"Medelvärde: {medeltal}\")

Medelvärde: 1.6233333333333333

I den här övningen finns ett färdigt program med heltal tilldelade till variablerna x och y:

x = 27\ny = 15

Utveckla programmet vidare så att utskriften ser ut så här:

27 + 15 = 42\n27 - 15 = 12\n27 * 15 = 405\n27 / 15 = 1.8

Programmet ska också fungera då värdet på variablerna ändras. I följande fall...

x = 4\ny = 9

...ser utskriften ut så här:

4 + 9 = 13\n4 - 9 = -5\n4 * 9 = 36\n4 / 9 = 0.4444444444444444

Om man ger print parametern end = \"\", kommer utskriften inte att avslutas med ett radbyte.

Exempel:

print(\"Hej \", end=\"\")\nprint(\"allesammans!\")

Hej allesammans!

Korrigera programmet så att räkneoperationen och resultatet skrivs ut på en rad. Antalet print-instruktioner får dock inte ändras.

print(5)\nprint(\" + \")\nprint(8)\nprint(\" - \")\nprint(4)\nprint(\" = \")\nprint(5 + 8 - 4)

Du hittar ett repetitionsquiz för denna del här:

","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/3-mer-om-variabler.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-1/4-rakneoperationer/page-data.json b/page-data/osa-1/4-rakneoperationer/page-data.json index 092ea8d17..be10543b1 100644 --- a/page-data/osa-1/4-rakneoperationer/page-data.json +++ b/page-data/osa-1/4-rakneoperationer/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-1/4-rakneoperationer", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av variabler i olika slags räkneoperationer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du hantera siffor i indata från användare"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du konvertera värden till olika grundläggande datatyper."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I de tidigare delarna har du sett exempel med enkla räkneoperationer. Följande tabell listar de mest allmänna operatorerna som kan användas för att räkna i Python:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Operator"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Betydelse"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Exempel"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Resultat"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Addition"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 4"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"6"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Subtraktion"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"10 - 2.5"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"7.5"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Multiplikation"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-2 * 123"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-246"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Division (resultat som flyttal)"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 / 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4.5"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Division (resultat som heltal)"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 // 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Rest"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 % 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"**"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Potens"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 ** 3"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"8"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordningen för operationerna är bekant från matematiken: potensuttryck räknas först, sedan multiplikation och division, till sist addition och subtraktion. Ordningen kan ändras genom att använda parenteser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel denna kodsnutt…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…resulterar i följande utskrift:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"11\n15"}]}]},{"type":"element","tagName":"h2","properties":{"id":"operand-operator-och-datatyper","style":"position:relative;"},"children":[{"type":"text","value":"Operand, operator och datatyper"},{"type":"element","tagName":"a","properties":{"href":"#operand-operator-och-datatyper","ariaLabel":"operand operator och datatyper permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En räkneoperation består oftast av operander och operatorer:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 324px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1d329086391ac8a20fcb843cd01f3459/67580/1_5.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 62.60869565217391%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsSAAALEgHS3X78AAAB30lEQVQ4y11T2Y7iMBDM///UCtBqJF4CD4BmAgJFBJyDnOROXKtqyyizllqx+6iuLjsOAGgNWU0DxDGQ5xplCdQ18H5D9vxWlYklidkva+1ylk4WDoM5+P4Vm80fuO4W2+1ffH1t8P19kNg8axSF/gDaen6dZYeugyTymyRv+H4MpUoEQQ6lciRJjbZlY42m+U3N4jiAxjQZICbGsUYUaSgFpClwvwPXq5GCozLGHJuf58A4CiS01mbkqiIIBHieTUIQaPS9aTaOWs51rYUJ4/RzzwbU9aNhFIUSYDIwIwwVpmkSduwahk9oPYkMXFn2wjxPYF1dl+IfBuPP89wAkhXpT9OI5/OJcZzQtmaE+/2Ovh9EMzZOkgRpmiIIAlRVJbfP+jiOUBQFnCsF+gircT6ff4lNwDerFos5A2ktVhRFQsbxvB85KBXi8XjgeDxKgAzo3+/3OBwOCMNQfIydTidpRJ9SSvJut5sZeRxHdF0n1vc9siyTBNrr9cLlcoHrujKm9XNva6xZxs6SNjUhA3ailWUpDQhMfbinn6xSc2v/vUXNh23E54rjGA3/P0CA1+u1sNvtdlitVvA8T2Jkw3EtyNIc6zRPIhOgtm0F3Pd9YUOjvrxhNrSjL2vtO/wHYUrnbWq2v1IAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1d329086391ac8a20fcb843cd01f3459/a0b58/1_5.webp 230w","/static/1d329086391ac8a20fcb843cd01f3459/4e123/1_5.webp 324w"],"sizes":"(max-width: 324px) 100vw, 324px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1d329086391ac8a20fcb843cd01f3459/81c8e/1_5.png 230w","/static/1d329086391ac8a20fcb843cd01f3459/67580/1_5.png 324w"],"sizes":"(max-width: 324px) 100vw, 324px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1d329086391ac8a20fcb843cd01f3459/67580/1_5.png","alt":"1 5","title":"1 5","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datatypen hos en operand bestämmer i regel datatypen hos resultatet: om två heltal adderas är resultatet också ett heltal. Om ett flyttal subtraheras från ett annat flyttal är resultatet ett flyttal. Vidare kan man notera att om en av operanderna är ett flyttal, kommer resultatet att vara ett flyttal oavsett de andra operanderna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Division med operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" är ett undantag. Dess resultat är ett flyttal även om operanderna är heltal. Till exempel kommer operationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 / 5"}]},{"type":"text","value":" att ge flyttalet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0.2"}]},{"type":"text","value":". Observera att Python använder punkt som decimaltecken, inte decimalkomma som vanligtvis används på svenska."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172.5"}]},{"type":"text","value":"\nvikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"68.55"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# viktindex beräknas genom att dividera vikten med kvadratroten av längden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# längden anges i meter"}]},{"type":"text","value":"\nbmi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Viktindexet är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bmi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets utskrift ser ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Viktindexet är 23.037177063642087"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att Python också har operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":", för division med heltal som resultat. Om operanderna är heltal, kommer resultatet också att vara ett heltal. Resultatet rundas ner till närmaste heltal. Exempelvis detta program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"/-operatorn "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"//-operatorn "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skriver ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"/-operatorn 1.5\n//-operatorn 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"nummer-som-indata","style":"position:relative;"},"children":[{"type":"text","value":"Nummer som indata"},{"type":"element","tagName":"a","properties":{"href":"#nummer-som-indata","ariaLabel":"nummer som indata permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan använt oss av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":"-funktionen för att läsa in strängar som användaren matat in. Samma funktion kan också användas för att läsa in nummer, men då måste strängen som funktionen returnerar först konverteras till någon av de datatyper som representerar ett nummer i programkoden. I den förra delen konverterade vi heltal till strängar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":"-funktionen. Nu gäller samma princip, men vi ska använda en annan funktion eftersom vi gör konverteringen åt motsatt håll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En sträng kan konverteras till ett heltal med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":". Det följande programmet frågar användarens födelseår och sparar det i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"indata_strang"}]},{"type":"text","value":". Programmet skapar därefter variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar"}]},{"type":"text","value":" som innehåller året konverterat till heltal. Efter det går det att räkna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2021 - ar"}]},{"type":"text","value":", med hjälp av det värde användaren angett."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilket är ditt födelseår? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Din ålder i slutet av 2020: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vilket är ditt födelseår? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1995"}]},{"type":"text","value":"\nDin ålder i slutet av 2020: 25"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Allt som oftast behöver man inte skapa två separata variabler (som ovan) för att läsa en siffra från användaren. Istället kan användarens text läsas in och konverteras till heltal samtidigt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilket är ditt födelseår? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Din ålder i slutet av 2020: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En sträng kan också konverteras till flyttal. Det sker med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":". Det här programmet frågar användaren om hennes eller hans längd och vikt, och använder svaren för att räkna ut BMI:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange längd: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange vikt: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\nbmi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Viktindexet är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bmi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel på en utskrift från programmet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange längd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"163"}]},{"type":"text","value":"\nAnge vikt: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"74.45"}]},{"type":"text","value":"\nViktindexet är 28.02137829801649"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Siffra gånger fem","tmcname":"osa01-13_x_ganger_fem"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter ett tal från användaren. Programmet ska skriva ut talet multiplicerat med fem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nNär 3 multipliceras med 5, får vi 15"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Namn och ålder","tmcname":"osa01-14_namn_alder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter användarens namn samt födelseår. Programmet ska därefter skriva ut ett meddelande enligt följande exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Sandro Syntetisk"}]},{"type":"text","value":"\nAnge födelseår: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1990"}]},{"type":"text","value":"\nHej Sandro Syntetisk, du är 30 år i slutet av 2020"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-anvanda-variabler","style":"position:relative;"},"children":[{"type":"text","value":"Att använda variabler"},{"type":"element","tagName":"a","properties":{"href":"#att-anvanda-variabler","ariaLabel":"att anvanda variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss kika på ett program som räknar summan av tre siffror som användaren anger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra ett: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra två: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffra3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra tre: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" siffra3\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan av siffrorna: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi ett exempel på när vi kör programmet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffra ett: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nSiffra två: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nSiffra tre: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nSumman av siffrorna: 33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet använder fyra variabler, men i det här fallet skulle det faktiskt räcka med två variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra ett: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nummer\n\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra två: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nummer\n\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffta tre: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nummer\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan av siffrorna: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu läses alla siffor som användaren ges in i en och samma variabel, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer"}]},{"type":"text","value":". Värdet på variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" ökas med värdet på variabeln nummer varje gång användaren skriver in ett nytt nummer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi tar en lite närmare titt på instruktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nummer"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här adderas värdena i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer"}]},{"type":"text","value":" ihop – för att lagras i variabeln summa. Till exempel om värdet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" och värdet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer"}]},{"type":"text","value":" är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":", kommer värdet på summa att vara "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" efter att instruktionen körts."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att öka på värdet hos en variabel är en vanlig operation. Därför finns det en liten genväg till förfogande. Den här notationen fungerar i praktiken som instruktionen ovan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" nummer"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här tillåter oss skriva programmet lite mer koncist:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra ett: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" nummer\n\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra två: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" nummer\n\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffta tre: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" nummer\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan av siffrorna: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Egentligen behöver vi inte alls variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer"}]},{"type":"text","value":". Vi kan också behandla siffrorna som användaren anger på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra ett: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra två: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra tre: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan av siffrorna: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I praktiken beror antalet variabler som behövs på situationen. Om man behöver minnas enskilda värden som användaren anger, är det inte möjligt att \"återanvända\" samma variabel för att läsa in olika värden. Här är ett exempel på en sådan situation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra ett: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffra2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffra två: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" + "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" = "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"siffra2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffra ett: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSiffra två: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n2 + 3 = 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Å andra sidan så har programmet inte en namngiven variabel där summan av de två angivna värdena skulle lagras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att \"återanvända\" en variabel lönar sig bara då när det finns ett tillfälligt behov att lagra värden av samma typ och orsak – till exempel då man summar ihop siffror."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel används variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" för att lagra användarens namn och därefter dess ålder. Det finns absolut ingen logik i det!"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"data "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är ditt namn? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" data "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är din ålder? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# programmet fortsätter..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En bättre idé vore att använda skilda variabler med namn som tydligt beskriver deras funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nimi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är ditt namn? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nimi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är din ålder? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# programmet fortsätter..."}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Dygn i sekunder","tmcname":"osa01-15_dygn_i_sekunder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar användaren om ett antal dagar. Programmet ska därefter räkna ut hur långa dessa dagar tillsammans är mätt i sekunder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många dygns längd ska räknas i sekunder? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n86400"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många dygns längd ska räknas i sekunder? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n604800"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Produkten av talen","tmcname":"osa01-20_produkten_av_talen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet borde fråga efter tre tal från användaren och räkna talens produkt. Det finns dessvärre fel i programmet. Korrigera dem, så att programmet fungerar på önskat sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel på hur programmet borde fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange tal 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge tal 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nAnge tal 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nProdukten av talen är 30"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Talens summa och produkt","tmcname":"osa01-16_summa_produkt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren att ge två siffror. Programmet ska räkna summan och produkten av dessa tal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffra 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nSiffra 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nSiffrornas summa 10\nProdukten av siffrorna 21"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Summa och medeltal","tmcname":"osa01-17_summa_medeltal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren att ange fyra siffror. Programmet ska räkna summan och medeltalet av dessa siffror."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffra 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSiffra 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSiffra 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nSiffra 4: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nSumman av siffrorna är 16 ja medeltalet är 4.0"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Matkostnader","tmcname":"osa01-19_matkostnader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som uppskattar användarens genomsnittliga matkostnader."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet frågar hur många gånger i veckan användaren besöker Unicafe och vad hon betalar för sin lunch där. Dessutom frågas också övriga matkostnader på veckonivå."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Baserat på den här informationen räknar programmet de genomsnittliga matkostnaderna under en vecka samt en dag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många gånger i veckan äter du på Unicafe? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nVad kostar Unicafe-lunchen? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2.5"}]},{"type":"text","value":"\nHur mycket spenderar då på övriga matinköp under veckan? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Genomsnittliga matkostnader:\nUnder en dag 5.5 euro\nUnder en vecka 38.5 euro"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Gruppindelning","tmcname":"osa01-18_gruppindelning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör ett program som frågar efter antalet studerande under en kurs samt storleken på en grupp. Programmet ska räkna hur många grupper bildas. En av grupperna kan vid behov ha färre medlemmar än den önskade gruppstorleken."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många studerande? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nVad är gruppstorleken? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nAntalet grupper: 2"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många studerande? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\nVad är gruppstorleken? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nAntalet grupper: 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Använd dig av operatorn för heltalsdivision "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips 2: Om du inte hittar på en lösning kan du bekanta dig med if-satser, som presenteras i nästa del. De förenklar uppgiften avsevärt."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kertauskysely tämän osan asioihin liittyen:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2"},"children":[]}]}]},"html":"

Efter den här delen

I de tidigare delarna har du sett exempel med enkla räkneoperationer. Följande tabell listar de mest allmänna operatorerna som kan användas för att räkna i Python:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperatorBetydelseExempelResultat
+Addition2 + 46
-Subtraktion10 - 2.57.5
*Multiplikation-2 * 123-246
/Division (resultat som flyttal)9 / 24.5
//Division (resultat som heltal)9 // 24
%Rest9 % 21
**Potens2 ** 38

Ordningen för operationerna är bekant från matematiken: potensuttryck räknas först, sedan multiplikation och division, till sist addition och subtraktion. Ordningen kan ändras genom att använda parenteser.

Till exempel denna kodsnutt…

print(2 + 3 * 3)\nprint((2 + 3) * 3)

…resulterar i följande utskrift:

11\n15

Operand, operator och datatyper

En räkneoperation består oftast av operander och operatorer:

\n \n

Efter den här delen

    \n
  • kan du använda dig av variabler i olika slags räkneoperationer
  • \n
  • kan du hantera siffor i indata från användare
  • \n
  • kan du konvertera värden till olika grundläggande datatyper.
  • \n

I de tidigare delarna har du sett exempel med enkla räkneoperationer. Följande tabell listar de mest allmänna operatorerna som kan användas för att räkna i Python:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperatorBetydelseExempelResultat
+Addition2 + 46
-Subtraktion10 - 2.57.5
*Multiplikation-2 * 123-246
/Division (resultat som flyttal)9 / 24.5
//Division (resultat som heltal)9 // 24
%Rest9 % 21
**Potens2 ** 38

Ordningen för operationerna är bekant från matematiken: potensuttryck räknas först, sedan multiplikation och division, till sist addition och subtraktion. Ordningen kan ändras genom att använda parenteser.

Till exempel denna kodsnutt…

print(2 + 3 * 3)\nprint((2 + 3) * 3)

…resulterar i följande utskrift:

11\n15

Operand, operator och datatyper

En räkneoperation består oftast av operander och operatorer:

\n \n "}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du är myndig.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…orsakas ett fel när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"File \"ohjelma.py\", line 3\n if alder > 17\n ^\nSyntaxError: invalid syntax\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"jamforelseoperatorer","style":"position:relative;"},"children":[{"type":"text","value":"Jämförelseoperatorer"},{"type":"element","tagName":"a","properties":{"href":"#jamforelseoperatorer","ariaLabel":"jamforelseoperatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är vanligt att man vill jämföra två värden sinsemellan. Här följer en tabell över de vanligaste jämförelseoperatorerna i Python:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Operator"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Betydelse"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Exempel"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Är lika med"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a == b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Är inte lika"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a != b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Större än"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a > b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Större än eller lika med"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a >= b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Mindre än"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a < b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Mindre än eller lika med"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a <= b"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi tar nu en titt på ett program som skriver ut olika saker baserat på det värde som användaren anger. Här har vi if-satser som kan uppfyllas då värdet är negativt, positivt eller lika med noll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är negativt.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är positivt.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är noll.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi tre exempel med olika indata:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nTalet är positivt."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-18"}]},{"type":"text","value":"\nTalet är negativt."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nTalet är noll."}]}]},{"type":"element","tagName":"h2","properties":{"id":"indentering","style":"position:relative;"},"children":[{"type":"text","value":"Indentering"},{"type":"element","tagName":"a","properties":{"href":"#indentering","ariaLabel":"indentering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python känner igen att en kodsnutt hör till en if-sats då varje rad är indenterad på samma sätt. Det här betyder att det finns mellanrum i början av varje kodrad som hör till if-satsen. Mellanrummet ska vara det samma för varje rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempelvis:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"losenord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange lösenord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" losenord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du visste lösenordet!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du måste alltså vara den riktiga användaren...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"...eller så är du en hacker.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programmet avslutades. Tack och hej!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda Tab-tangenten för att lägga till mellanrum där det behövs."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 537px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 67.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAEQklEQVQ4y4WT/TPbBxzHv39D7zZH57FDqdZQWykroYmQB/KAREhEHj0kCEHiKb6SSIR4SDwuw5W5YaImqnO7royutM1Zx7pq1TZ3G03cVrfK3Oqzy37aL7v99H7d+z6/vD+fzxsBgCMA2AeAg3/pf/H+/80iuy92v9VrW2eMBqN1espqNrQYFtr07WPWz2YGdGrdoqnTNDIzPWNBG9DFwb6PhsbHxofUKvXisGXYMj46PoI2oF+MjowODluGxzSodh65ab0JQX7BEBMVC0XC4mM3X4mMgWJhMfifDQBsIu5UWCACzzNeQCVRT3MZefDOW97AoDNPs6jZ4O3hA9xc7ht8cioE+gYBYn9of8rKYt2vlStXOg0dFj5HYFdW1y626dtv8Ni8DbSheUGLaj/hsPI39NrWebQB/ZTN5Ng7DB02RZVyKj+X+9DUaZorl8hmBFzhN8jdO3d/T8WTnQQi1alv61yiZ2Q5CViCs03fvsygMZw4TIqjRa1bFhaIHEkfJjubVepVaZH0l8Q4zG9oA/qoXCLbxsQnHTXWqTZKxJInyOSUFQLPeALeLwia8/mvos6Hg593IMgkssOIC5Hg7xUARYIiJyYOA95v+wCPzT8kppD+iZ3P4v6Rdj3txNfTD3IyWa9JeLIL2ZqadjZisNvl2azn1matzYBL2+bgST+bewdmuSzuJj0987nRYJwT8cTf0cj0HYOubb5YVGKnEKk/6jX6LyWF0hUKkbqnRbV3Kkorl5AHHw8dMNm8nRwmZzeDnvPEbDCuGcnUPTol64eB3gGbQq7Yyc3O3TYajLYauWKHnk5/ptPobU31TdsMGvOFBtUuqupUG1nU7J8aa1VLyESXGd6PvgqRoRHg5+EDUmkF5IRHQ5iXP5SVVjhxiTi4FBwOIp7Y4b6kOx43t8CRQaDAu96BwMnhHOEwKW98PHyBQWOeIF/1DRxdT6M4qemZh0QSzanXtj6TRccckvFkR19P/yo+OfUgJQn/sruje5VGph9gE3AOg67tXk4max8Tn3So0+gf5THZO0nxya9UdapNZGdick9LyVyr5InstTzRujGVZB9X1vcr65qsbEbeA3WTZs69NzaTvdZU3zTX3to+z80rWFdW1dq6O0zzfI5gTV5etWDqNH8u4onvIVbrLBAuXQbBB3GA9fSFi2cDQFxUusLKYv0aFnQRCDiiS1ggcrk5DUtwFQqKXO7HT7iaeFwilvzp9mMux/5VLpEdX4u5BsiCbeEoNjbhJQ5PdrwXccUZHhb1Wt3YfINKoq2HnAs9EXCFmzwO//sg32AXj8PfFBWIt84HhLgYNOaWpFC6GXIu9DiDQNmWSWWPIy5EHiFbm1tPy4rL7lfLqh4b9e23uo1dt+Tl8ts6tW7S3NWzUFOpWG1R62Z7Tb1zNZU1y9rmltv9Pf3TCrli2d1vy4Blqra67ut6ZcPiYN/gxN+K6mUiaF+/igAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/43a34a2e4b0bc023009954d6f7809f7c/a0b58/1_6_keyboard.webp 230w","/static/43a34a2e4b0bc023009954d6f7809f7c/bc10c/1_6_keyboard.webp 460w","/static/43a34a2e4b0bc023009954d6f7809f7c/27c24/1_6_keyboard.webp 537w"],"sizes":"(max-width: 537px) 100vw, 537px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/43a34a2e4b0bc023009954d6f7809f7c/81c8e/1_6_keyboard.png 230w","/static/43a34a2e4b0bc023009954d6f7809f7c/08a84/1_6_keyboard.png 460w","/static/43a34a2e4b0bc023009954d6f7809f7c/b1cde/1_6_keyboard.png 537w"],"sizes":"(max-width: 537px) 100vw, 537px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/43a34a2e4b0bc023009954d6f7809f7c/b1cde/1_6_keyboard.png","alt":"1 6 keyboard","title":"1 6 keyboard","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom kan flera texteditorer automatiskt indentera den följande raden när Enter-tangenten trycks efter ett kolon. Du får bort indenteringen genom att använda Backspace-tangenten i början av en rad."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 293px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 96.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAACXBIWXMAAAsTAAALEwEAmpwYAAAEtElEQVQ4y3WU6U+UVxSHSdMvbbWNSgXZh2FmGAYYdnBBERA6SsdWZYdgK4ggOyrKElBR6oYoaFllHdZhUQGRalkTo8QUqMFGwCVubfzWf+Bp3kuaNDF9k5O89+Te555zfvcck7DQMAb6Boj4PoLw78LxcvWmsb6JI5m5hG4PZkdAEGnJqRia29gVFELUnnB2BocyYOwnISoGfYiOwC3bKC4oRvpMtvj401B3Aw9nT6R/maWciovlREUkopBvRuMYQHTkQa5cq0ahCcLHT4dK7U9DUxtbg+Nx89mJQrGJtEO5K8DYyDgmJ6bQ63YTFvqtgN7sG6Awv4ygwDj8A6PIzjrJUGUVP3jq+EkfT5kumqe19ZRFprHPJZhtXjrKymtWgE8X/mDm0QzTU9NMjE/y6uUrJN+D6Wnuj4/x8pe7fGhqZLamlme3Bng6Mc785ARPJqdYvHuH2YZaxvOO8WHw9gqw6EQRjvZqtE5uOFgrMHYZ2Ru2BzuZI3q5mnMBwXQbOtF6+KFx8Uat0qJWuqJSOKNUadG4+iCXqenJy4eZGUwK8gpE/by1PqjlTrQ2txETEYuvmy/Zji7sCgmjtbWdiN37iI+ME+IFbNqOm9oNT40HXs6eKK3kGIZGYGoSk8zDWdhZyFDaqbBYZ0l9TT36b/Qigngza6I2bqOhowcbCxlySzlyCxkOlnIcrFZMYavCzMyWxoYmGPsVk672LqLDY8hOz+ZQYgpzs3OcO3OO/QlJnEjNYProcRa6ukmMjONAYgoH07JJTMkgMTWT5NRM0pNSSdHpeVRQBO/eYXJnaIT3799zs/8mfcY+UcOHDx6yvLiEob2L/p4+jCcKeVFTx9+Dg7xu7+BDXz9/Go0sNzYxejSP3sxs/lpcWhHFTeNFd2c3fh4bUdqq+PrL9STE7ud65c8orBVCsNVfmQl444XLaNdZoHP1Zq/fVrwsZKjtHDG3lIs6C2DA5u30dveiC96Jj7uvAKQmpVJXXYevu5+4SLbBDmNXD9U19ezYsYvomARCQsLQOLnj4eKF0kZB+79AF5Urbc1tQmnp2Ziu/prYiDguX6zAxtwWlcyRLz5dRfONZiorKvnsk89FJhoHZ7RqN9w1Hjj8F1h9rZo3r99QefkqZ0+d5VTxaYZuDbHwZIHTJaVCoNKSUpYWl1heWqbiYgUpiSloFP8DvFJ+lUvny7l04SrLS8/FoJCiO3/2PG0tBl48f0FlRZXYU3ryDI9nHjM6Morc2kHAPgIqbJTYWzmwfo09NddrOfhjMl5ab8zXbGCT12aRqnRYSn3dKlNyMnJFBpLPTeP+MVAq+kqXqDG0GIiLihebXB21oksMre3odXoCtwSJgyWFJQzfHhaNII06yaTLOto6VoDSQorSfK0FTTeahdqWplbYmtvh77uVlsYWnJUuODlohGDHcvLoN/azdpUpMgt7ZJb2wi86RQJ2GjrpMHSK6CRx7o3eE6pL6+HBYd6+eSvS6WzvFPD5ud9ZfLaINFSkmp4uLqXweBHzc/MrwKorVWJcZaVlk3k4k+QDhxkducfI8Ahph9KF/0jWUWF5uXkk7k9ioHeAsftjpKekk38sn5yMHGZ/mxXAfwDrwXYWIBU21wAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7f8b7a68350e9455732bc0462f21ccff/a0b58/1_6_keyboard2.webp 230w","/static/7f8b7a68350e9455732bc0462f21ccff/2b735/1_6_keyboard2.webp 293w"],"sizes":"(max-width: 293px) 100vw, 293px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7f8b7a68350e9455732bc0462f21ccff/81c8e/1_6_keyboard2.png 230w","/static/7f8b7a68350e9455732bc0462f21ccff/3cf3e/1_6_keyboard2.png 293w"],"sizes":"(max-width: 293px) 100vw, 293px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7f8b7a68350e9455732bc0462f21ccff/3cf3e/1_6_keyboard2.png","alt":"1 6 keyboard2","title":"1 6 keyboard2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"small","properties":{},"children":[{"type":"element","tagName":"center","properties":{},"children":[{"type":"text","value":"\nNäppäimistökuvien alkuperä:\n "},{"type":"element","tagName":"a","properties":{"href":"https://pixabay.com/users/Clker-Free-Vector-Images-3736/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=311803"},"children":[{"type":"text","value":"Clker-Free-Vector-Images"}]},{"type":"text","value":" from "},{"type":"element","tagName":"a","properties":{"href":"https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=311803"},"children":[{"type":"text","value":"Pixabay"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Orwell","tmcname":"osa01-21_orwell"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter ett heltal från användaren och skriver ut texten \"Orwell\" om siffran är 1984. Annars skrivs inget ut."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1984"}]},{"type":"text","value":"\nOrwell"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Absolutbelopp","tmcname":"osa01-22_absolutbelopp"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber efter ett heltal av användaren. Om siffran är mindre än noll, skriver programmet ut siffran multiplicerat med talet -1. I övriga fall skrivs den siffran användaren angett ut. Nedan finns några exempel på hur programmet ska fungera."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-7"}]},{"type":"text","value":"\nSiffrans absolutbelopp är 7"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSiffrans absolutbelopp är 1"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-99"}]},{"type":"text","value":"\nSiffrans absolutbelopp är 99"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Soppa. Eller inte.","tmcname":"osa01-23_soppa"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som först frågar efter användarens förnamn. Om namnet inte är \"Jerry\", fortsätter programmet med att fråga antalet sopportioner och berättar sedan priset för \"hela soppan\". En portion kostar 5,90."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Två exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad heter du: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Kramer"}]},{"type":"text","value":"\nHur många sopportioner: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSlutsumma 11.8\nNästa!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad heter du: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Jerry"}]},{"type":"text","value":"\nNästa!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Storleksklass","tmcname":"osa01-24_storleksklass"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter ett heltal från användaren. Programmet ska sedan berätta i vilken storleksklass siffran är, enligt följande exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"950"}]},{"type":"text","value":"\nSiffran är mindre än 1000\nTackar!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"59"}]},{"type":"text","value":"\nSiffran är mindre än 1000\nSiffran är mindre än 100\nTackar!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSiffran är mindre än 1000\nSiffran är mindre än 100\nSiffran är mindre än 10\nTackar!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1123"}]},{"type":"text","value":"\nTackar!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"boolean-varden-och--uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Boolean-värden och -uttryck"},{"type":"element","tagName":"a","properties":{"href":"#boolean-varden-och--uttryck","ariaLabel":"boolean varden och uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla villkor i en if-sats resulterar i ett sanningsvärde, det vill säga sant eller falskt. Till exempel villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a < 5"}]},{"type":"text","value":" är sant då "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" är mindre än fem och falskt då "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" är fem eller större."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna typ av värden kallas alltså Boolean-värden (efter matematikern George Boole). I Python representerar datatypen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" ett sanningsvärde. Variabler av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" kan endast ha ett av följande värden: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" (sant) och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" (falskt)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En kodsnutt som resulterar i något av de ovan nämnda värdena kallas Boolean-uttryck. Ett villkor i en if-sats är alltid ett Boolean-uttryck och kan i flera situationer användas som synonym för ordet villkor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet av ett Boolean-uttryck kan lagras i en variabel på samma sätt som vilken som helst annan numerisk räkneoperation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nvillkor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"villkor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" villkor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a är mindre än 5\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\na är mindre än 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons nyckelord "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" kan också användas direkt som sådana. I det följande exemplet körs "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen alltid, eftersom värdet på villkoret är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"villkor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" villkor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vi når hit alltid\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi når hit alltid"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan tycka att det inte i ovanstående exempel verkar vara en så nyttig funktion. Senare under kursens lopp ska vi se på situationer där vi kan ha mera nytta av Boolean-variabler."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Räknare","tmcname":"osa01-25_raknare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som först ber användaren ange två siffror och därefter ett kommando. Om kommandot är summa, produkt eller differens, ska programmet utföra den nämnda räkneoperationen. I övriga fall skriver inte programmet ut något."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nTal 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"17"}]},{"type":"text","value":"\nKommando: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"summa"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"10 + 17 = 27"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nTal 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nKommando: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"produkt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 * 6 = 24"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nTal 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nKommando: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"differens"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 - 6 = -2"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Temperaturer","tmcname":"osa01-26_temperaturer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en temperatur i Farenheit. Programmet ska skriva ut den här temperaturen i Celcius. Om temperaturen mätt i Celcius är under noll ska programmet också skriva ut texten \"Kallt!\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan söka på nätet efter den korrekta formeln för att konvertera temperaturer i Farenheit till Celcius."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange temperatur (i Farenheit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"101"}]},{"type":"text","value":"\n101 grader Farenheit on 38.333333333333336 grader Celcius"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange temperatur (i Farenheit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\n21 grader Farenheit on -6.111111111111111 grader Celcius\nKallt!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Lön","tmcname":"osa01-27_lon"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter timlön, antal arbetstimmar samt veckodag. Programmet skriver ut lönen som räknas med formeln timlön * arbetstimmar. På söndag är timlönen fördubblad."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Timlön: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8.5"}]},{"type":"text","value":"\nArbetstimmar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nVeckodag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"maanantai"}]},{"type":"text","value":"\nLön 25.5 euro"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Timlön: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12.5"}]},{"type":"text","value":"\nArbetstimmar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nVeckodag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sunnuntai"}]},{"type":"text","value":"\nLön 250.0 euro"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Ränta","tmcname":"osa01-28_ranta"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet räknar antalet bonuspoäng som ska adderas till ett bonuskort i slutet av året, enligt följande formel:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"bonuspoäng < 100: ränta 10 % extra poäng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"övriga fall: ränta 15 % extra poäng"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här fungerar programmet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många poäng? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"55"}]},{"type":"text","value":"\nDu fick 10 % i bonus\nDu har nu 60.5 poäng"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ohjelma toimii kuitenkin jollain syötteillä oudosti:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många poäng? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"95"}]},{"type":"text","value":"\nDu fick 10 % i bonus\nDu fick 15 % i bonus\nDu har nu 120.175 poäng"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigera programmet så att man endast får 10 % eller 15 % bonus – inte både och."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Morgondagens klädsel","tmcname":"osa01-29_kladsel"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör ett program som frågar användaren om morgondagens väderprognos. Programmet rekommenderar därefter klädsel enligt vädret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rekommendationen beror på om temperaturen är över fem, tio eller 20 grader samt om det regnar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berätta väderprognosen för imorgon:\nTemperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nRegnar det (ja/nej): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej"}]},{"type":"text","value":"\nTa på dig byxor och t-skjorta"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berätta väderprognosen för imorgon:\nTemperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\nRegnar det (ja/nej): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej"}]},{"type":"text","value":"\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berätta väderprognosen för imorgon:\nTemperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nRegnar det (ja/nej): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej"}]},{"type":"text","value":"\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja\nKlä på dig en jacka"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berätta väderprognosen för imorgon:\nTemperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nRegnar det (ja/nej): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ja"}]},{"type":"text","value":"\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja\nKlä på dig en jacka\nEn varm jacka rekommenderas\nVantar rekommenderas också\nKom ihåg paraplyet!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Andra gradens ekvation","tmcname":"osa01-30_andra_gradens_ekvation"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" i Python har funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" som kan användas för att räkna kvadratroten för ett tal. Så här fungerar funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör ett program som räknar resultatet för andra gradens ekvation ax² + bx + c. Till programmet ges värdena a, b och c, och därefter beräknas lösningen med följande formel:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"x = (-b ± sqrt(b² - 4ac)) / (2a)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att ekvationen har två rötter, varvid formeln ovan fungerar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge a: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nGe b: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nGe c: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rötterna är 2.0 och -4.0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kertauskysely tämän osan asioihin liittyen:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät om materialet i den här veckans modul. Du får ett poäng när du fyllt i enkäten."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du använda dig av en enkel if-sats när du programmerar
  • \n
  • vet du vad ett Boolean-värde är
  • \n
  • kan du uttrycka villkor med hjälp av jämförelseoperatorer.
  • \n

Hittills har vi skapat program där koden körts rad för rad från början till slut. Istället för att köra varje kodrad när programmet är igång är det ofta nyttigt att ha delar i programmet som endast körs i vissa situationer.

Till exempel följande program verifierar att användaren är tillräckligt gammal:

alder = int(input(\"Vad är din ålder? \"))\n\nif alder > 17:\n    print(\"Du är myndig!\")\n    print(\"Här får du alltså GTA 6.\")\n\nprint(\"Nästa kund!\")

När användaren är över 17 år, borde det se ut så här när programmet körs:

Vad är din ålder? 18\nDu är myndig!\nHär får du alltså GTA 6.\nNästa kund!

Däremot, om användaren är 17 eller yngre, ser utskriften ut så här:

Vad är din ålder? 16\nNästa kund!

Dessa exempel visar hur ett värde som getts till programmet påverkar vilka delar av koden som körs. Programmet innehåller en if-sats med kod som körs enbart då ett definierat villkor uppfylls.

\n \n \n \n \n \"1\n \n

I en if-sats följs nyckelordet if med ett villkor som till exempel kan vara en jämförelse av två värden. Koden som följer körs endast då villkoret uppfylls.

Notera kolontecknet. Om det saknas…

alder = 10\n\n# kaksoispiste unohtui seuraavan rivin lopusta\nif alder > 17\n    print(\"Du är myndig.\")

…orsakas ett fel när programmet körs:

\n
\nFile \"ohjelma.py\", line 3\n  if alder > 17\n              ^\nSyntaxError: invalid syntax\n
\n

Jämförelseoperatorer

Det är vanligt att man vill jämföra två värden sinsemellan. Här följer en tabell över de vanligaste jämförelseoperatorerna i Python:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperatorBetydelseExempel
==Är lika meda == b
!=Är inte likaa != b
>Större äna > b
>=Större än eller lika meda >= b
<Mindre äna < b
<=Mindre än eller lika meda <= b

Vi tar nu en titt på ett program som skriver ut olika saker baserat på det värde som användaren anger. Här har vi if-satser som kan uppfyllas då värdet är negativt, positivt eller lika med noll:

siffra = int(input(\"Ge ett tal: \"))\n\nif siffra < 0:\n    print(\"Talet är negativt.\")\n\nif siffra > 0:\n    print(\"Talet är positivt.\")\n\nif siffra == 0:\n    print(\"Talet är noll.\")

Här har vi tre exempel med olika indata:

Ge ett tal: 15\nTalet är positivt.

Ge ett tal: -18\nTalet är negativt.

Ge ett tal: 0\nTalet är noll.

Indentering

Python känner igen att en kodsnutt hör till en if-sats då varje rad är indenterad på samma sätt. Det här betyder att det finns mellanrum i början av varje kodrad som hör till if-satsen. Mellanrummet ska vara det samma för varje rad.

Exempelvis:

losenord = input(\"Ange lösenord: \")\n\nif losenord == \"katt\":\n    print(\"Du visste lösenordet!\")\n    print(\"Du måste alltså vara den riktiga användaren...\")\n    print(\"...eller så är du en hacker.\")\n\nprint(\"Programmet avslutades. Tack och hej!\")

Du kan använda Tab-tangenten för att lägga till mellanrum där det behövs.

\n \n \n \n \n \"1\n \n

Dessutom kan flera texteditorer automatiskt indentera den följande raden när Enter-tangenten trycks efter ett kolon. Du får bort indenteringen genom att använda Backspace-tangenten i början av en rad.

\n \n \n \n \n \"1\n \n \n
\nNäppäimistökuvien alkuperä:\n Clker-Free-Vector-Images from Pixabay\n

Skapa ett program som frågar efter ett heltal från användaren och skriver ut texten \"Orwell\" om siffran är 1984. Annars skrivs inget ut.

Ge ett tal: 2020

Ge ett tal: 1984\nOrwell

Skapa ett program som ber efter ett heltal av användaren. Om siffran är mindre än noll, skriver programmet ut siffran multiplicerat med talet -1. I övriga fall skrivs den siffran användaren angett ut. Nedan finns några exempel på hur programmet ska fungera.

Ge ett tal: -7\nSiffrans absolutbelopp är 7

Ge ett tal: 1\nSiffrans absolutbelopp är 1

Ge ett tal: -99\nSiffrans absolutbelopp är 99

Skapa ett program som först frågar efter användarens förnamn. Om namnet inte är \"Jerry\", fortsätter programmet med att fråga antalet sopportioner och berättar sedan priset för \"hela soppan\". En portion kostar 5,90.

Två exempel:

Vad heter du: Kramer\nHur många sopportioner: 2\nSlutsumma 11.8\nNästa!

Vad heter du: Jerry\nNästa!

Skapa ett program som frågar efter ett heltal från användaren. Programmet ska sedan berätta i vilken storleksklass siffran är, enligt följande exempel:

Ge ett tal: 950\nSiffran är mindre än 1000\nTackar!

Ge ett tal: 59\nSiffran är mindre än 1000\nSiffran är mindre än 100\nTackar!

Ge ett tal: 2\nSiffran är mindre än 1000\nSiffran är mindre än 100\nSiffran är mindre än 10\nTackar!

Ge ett tal: 1123\nTackar!

Boolean-värden och -uttryck

Alla villkor i en if-sats resulterar i ett sanningsvärde, det vill säga sant eller falskt. Till exempel villkoret a < 5 är sant då a är mindre än fem och falskt då a är fem eller större.

Denna typ av värden kallas alltså Boolean-värden (efter matematikern George Boole). I Python representerar datatypen bool ett sanningsvärde. Variabler av typen bool kan endast ha ett av följande värden: True (sant) och False (falskt).

En kodsnutt som resulterar i något av de ovan nämnda värdena kallas Boolean-uttryck. Ett villkor i en if-sats är alltid ett Boolean-uttryck och kan i flera situationer användas som synonym för ordet villkor.

Resultatet av ett Boolean-uttryck kan lagras i en variabel på samma sätt som vilken som helst annan numerisk räkneoperation:

a = 3\nvillkor = a < 5\nprint(villkor)\nif villkor:\n    print(\"a är mindre än 5\")

True\na är mindre än 5

Pythons nyckelord True och False kan också användas direkt som sådana. I det följande exemplet körs print-instruktionen alltid, eftersom värdet på villkoret är True:

villkor = True\nif villkor:\n    print(\"Vi når hit alltid\")

Vi når hit alltid

Man kan tycka att det inte i ovanstående exempel verkar vara en så nyttig funktion. Senare under kursens lopp ska vi se på situationer där vi kan ha mera nytta av Boolean-variabler.

Skapa ett program som först ber användaren ange två siffror och därefter ett kommando. Om kommandot är summa, produkt eller differens, ska programmet utföra den nämnda räkneoperationen. I övriga fall skriver inte programmet ut något.

Exempel:

Tal 1: 10\nTal 2: 17\nKommando: summa

10 + 17 = 27

Tal 1: 4\nTal 2: 6\nKommando: produkt

4 * 6 = 24

Tal 1: 4\nTal 2: 6\nKommando: differens

4 - 6 = -2

Skapa ett program som ber användaren ange en temperatur i Farenheit. Programmet ska skriva ut den här temperaturen i Celcius. Om temperaturen mätt i Celcius är under noll ska programmet också skriva ut texten \"Kallt!\".

Du kan söka på nätet efter den korrekta formeln för att konvertera temperaturer i Farenheit till Celcius.

Exempel:

Ange temperatur (i Farenheit): 101\n101 grader Farenheit on 38.333333333333336 grader Celcius

Ange temperatur (i Farenheit): 21\n21 grader Farenheit on -6.111111111111111 grader Celcius\nKallt!

Skapa ett program som frågar efter timlön, antal arbetstimmar samt veckodag. Programmet skriver ut lönen som räknas med formeln timlön * arbetstimmar. På söndag är timlönen fördubblad.

Timlön: 8.5\nArbetstimmar: 3\nVeckodag: maanantai\nLön 25.5 euro

Timlön: 12.5\nArbetstimmar: 10\nVeckodag: sunnuntai\nLön 250.0 euro

Det här programmet räknar antalet bonuspoäng som ska adderas till ett bonuskort i slutet av året, enligt följande formel:

    \n
  • bonuspoäng < 100: ränta 10 % extra poäng
  • \n
  • övriga fall: ränta 15 % extra poäng
  • \n

Så här fungerar programmet:

Hur många poäng? 55\nDu fick 10 % i bonus\nDu har nu 60.5 poäng

Ohjelma toimii kuitenkin jollain syötteillä oudosti:

Hur många poäng? 95\nDu fick 10 % i bonus\nDu fick 15 % i bonus\nDu har nu 120.175 poäng

Korrigera programmet så att man endast får 10 % eller 15 % bonus – inte både och.

Gör ett program som frågar användaren om morgondagens väderprognos. Programmet rekommenderar därefter klädsel enligt vädret.

Rekommendationen beror på om temperaturen är över fem, tio eller 20 grader samt om det regnar.

Så här ska programmet fungera:

Berätta väderprognosen för imorgon:\nTemperatur: 21\nRegnar det (ja/nej): nej\nTa på dig byxor och t-skjorta

Berätta väderprognosen för imorgon:\nTemperatur: 11\nRegnar det (ja/nej): nej\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja

Berätta väderprognosen för imorgon:\nTemperatur: 7\nRegnar det (ja/nej): nej\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja\nKlä på dig en jacka

Berätta väderprognosen för imorgon:\nTemperatur: 3\nRegnar det (ja/nej): ja\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja\nKlä på dig en jacka\nEn varm jacka rekommenderas\nVantar rekommenderas också\nKom ihåg paraplyet!

Modulen math i Python har funktionen sqrt som kan användas för att räkna kvadratroten för ett tal. Så här fungerar funktionen:

from math import sqrt\n\nprint(sqrt(9))

Programmet skriver ut:

3.0

Gör ett program som räknar resultatet för andra gradens ekvation ax² + bx + c. Till programmet ges värdena a, b och c, och därefter beräknas lösningen med följande formel:

x = (-b ± sqrt(b² - 4ac)) / (2a)

Du kan anta att ekvationen har två rötter, varvid formeln ovan fungerar.

Exempel:

Ge a: 1\nGe b: 2\nGe c: -8

Rötterna är 2.0 och -4.0

Kertauskysely tämän osan asioihin liittyen:

Vänligen svara på en kort enkät om materialet i den här veckans modul. Du får ett poäng när du fyllt i enkäten.

","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/5-villkorssatser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av en enkel if-sats när du programmerar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad ett Boolean-värde är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du uttrycka villkor med hjälp av jämförelseoperatorer."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi skapat program där koden körts rad för rad från början till slut. Istället för att köra varje kodrad när programmet är igång är det ofta nyttigt att ha delar i programmet som endast körs i vissa situationer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel följande program verifierar att användaren är tillräckligt gammal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är din ålder? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du är myndig!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Här får du alltså GTA 6.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nästa kund!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När användaren är över 17 år, borde det se ut så här när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad är din ålder? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nDu är myndig!\nHär får du alltså GTA 6.\nNästa kund!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Däremot, om användaren är 17 eller yngre, ser utskriften ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad är din ålder? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"16"}]},{"type":"text","value":"\nNästa kund!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa exempel visar hur ett värde som getts till programmet påverkar vilka delar av koden som körs. Programmet innehåller en if-sats med kod som körs enbart då ett definierat villkor uppfylls."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 363px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 126.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAYAAAAxFw7TAAAACXBIWXMAAAsSAAALEgHS3X78AAAB5klEQVQ4y5WVx47DMAxE/f8flGuQY3IJgrRD4nSk9965eFxoIctyWQKGZUkcDTmkHEgO+36/+r5cLvJ+vyNzrgU+R96+8Xq9lufzGZtPBHRPNk6fz0e/J5OJnM9n76GpDAHA0WVQLpel0+nECNiPF/DxeMhsNtMxOTsejzKfz2W1Wslms5HFYuE9MJEhecIZG4/H+pxOJ6lWq8rwfr9Lr9eTwWCg+5bLpUynUzkcDhK4lA1DA8gcYDDEYbvdKsPb7RbJLxWAaIFP+tfrFQE01u12FdhnHEA6AgYmlHa7Lc1mUxcIw1WzVqtJGIYxMSKAsNntdrLf73WCkKBvGGIIwFqlUtFDWbPrMQKYFTLGGCGMkUuUtwGv1+svoE8UW2WMCPhuNBqqLtEglFtqzCfWocsQBqQCprCDpe2Dwv1+P7kObTUNQ0KCBTXHAbYPc0QQ+NrItJ9dMuSVAocZY3e/N+S0CwLDwb0QYqKkXV/mG0aURbFYlHq9rmPTIbnKxj2AjahKPRpHN4f/AkQQko5QdNBwOPyrgly97AICQtuVSiVtTcQpFAp6SbDGw3VHFLkB2UwNmlY1N3fmLyCp0AGhSwibfrd/Vok3dlYJ2WXjK53cDE3iW62W5i0JLBPQBR6NRhpu2n/5BwkboGJCClQGAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/fe276f00f3db1991c7697c7925fc68c7/a0b58/1_6.webp 230w","/static/fe276f00f3db1991c7697c7925fc68c7/2ed29/1_6.webp 363w"],"sizes":"(max-width: 363px) 100vw, 363px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/fe276f00f3db1991c7697c7925fc68c7/81c8e/1_6.png 230w","/static/fe276f00f3db1991c7697c7925fc68c7/4e786/1_6.png 363w"],"sizes":"(max-width: 363px) 100vw, 363px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/fe276f00f3db1991c7697c7925fc68c7/4e786/1_6.png","alt":"1 6","title":"1 6","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I en if-sats följs nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" med ett villkor som till exempel kan vara en jämförelse av två värden. Koden som följer körs endast då villkoret uppfylls."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notera kolontecknet. Om det saknas…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kaksoispiste unohtui seuraavan rivin lopusta"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du är myndig.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…orsakas ett fel när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"File \"ohjelma.py\", line 3\n if alder > 17\n ^\nSyntaxError: invalid syntax\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"jamforelseoperatorer","style":"position:relative;"},"children":[{"type":"text","value":"Jämförelseoperatorer"},{"type":"element","tagName":"a","properties":{"href":"#jamforelseoperatorer","ariaLabel":"jamforelseoperatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är vanligt att man vill jämföra två värden sinsemellan. Här följer en tabell över de vanligaste jämförelseoperatorerna i Python:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Operator"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Betydelse"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Exempel"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Är lika med"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a == b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Är inte lika"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a != b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Större än"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a > b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Större än eller lika med"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a >= b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Mindre än"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a < b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Mindre än eller lika med"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a <= b"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi tar nu en titt på ett program som skriver ut olika saker baserat på det värde som användaren anger. Här har vi if-satser som kan uppfyllas då värdet är negativt, positivt eller lika med noll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är negativt.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är positivt.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är noll.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi tre exempel med olika indata:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nTalet är positivt."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-18"}]},{"type":"text","value":"\nTalet är negativt."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nTalet är noll."}]}]},{"type":"element","tagName":"h2","properties":{"id":"indentering","style":"position:relative;"},"children":[{"type":"text","value":"Indentering"},{"type":"element","tagName":"a","properties":{"href":"#indentering","ariaLabel":"indentering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python känner igen att en kodsnutt hör till en if-sats då varje rad är indenterad på samma sätt. Det här betyder att det finns mellanrum i början av varje kodrad som hör till if-satsen. Mellanrummet ska vara det samma för varje rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempelvis:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"losenord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange lösenord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" losenord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du visste lösenordet!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du måste alltså vara den riktiga användaren...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"...eller så är du en hacker.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programmet avslutades. Tack och hej!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda Tab-tangenten för att lägga till mellanrum där det behövs."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 537px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 67.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAEQklEQVQ4y4WT/TPbBxzHv39D7zZH57FDqdZQWykroYmQB/KAREhEHj0kCEHiKb6SSIR4SDwuw5W5YaImqnO7royutM1Zx7pq1TZ3G03cVrfK3Oqzy37aL7v99H7d+z6/vD+fzxsBgCMA2AeAg3/pf/H+/80iuy92v9VrW2eMBqN1espqNrQYFtr07WPWz2YGdGrdoqnTNDIzPWNBG9DFwb6PhsbHxofUKvXisGXYMj46PoI2oF+MjowODluGxzSodh65ab0JQX7BEBMVC0XC4mM3X4mMgWJhMfifDQBsIu5UWCACzzNeQCVRT3MZefDOW97AoDNPs6jZ4O3hA9xc7ht8cioE+gYBYn9of8rKYt2vlStXOg0dFj5HYFdW1y626dtv8Ni8DbSheUGLaj/hsPI39NrWebQB/ZTN5Ng7DB02RZVyKj+X+9DUaZorl8hmBFzhN8jdO3d/T8WTnQQi1alv61yiZ2Q5CViCs03fvsygMZw4TIqjRa1bFhaIHEkfJjubVepVaZH0l8Q4zG9oA/qoXCLbxsQnHTXWqTZKxJInyOSUFQLPeALeLwia8/mvos6Hg593IMgkssOIC5Hg7xUARYIiJyYOA95v+wCPzT8kppD+iZ3P4v6Rdj3txNfTD3IyWa9JeLIL2ZqadjZisNvl2azn1matzYBL2+bgST+bewdmuSzuJj0987nRYJwT8cTf0cj0HYOubb5YVGKnEKk/6jX6LyWF0hUKkbqnRbV3Kkorl5AHHw8dMNm8nRwmZzeDnvPEbDCuGcnUPTol64eB3gGbQq7Yyc3O3TYajLYauWKHnk5/ptPobU31TdsMGvOFBtUuqupUG1nU7J8aa1VLyESXGd6PvgqRoRHg5+EDUmkF5IRHQ5iXP5SVVjhxiTi4FBwOIp7Y4b6kOx43t8CRQaDAu96BwMnhHOEwKW98PHyBQWOeIF/1DRxdT6M4qemZh0QSzanXtj6TRccckvFkR19P/yo+OfUgJQn/sruje5VGph9gE3AOg67tXk4max8Tn3So0+gf5THZO0nxya9UdapNZGdick9LyVyr5InstTzRujGVZB9X1vcr65qsbEbeA3WTZs69NzaTvdZU3zTX3to+z80rWFdW1dq6O0zzfI5gTV5etWDqNH8u4onvIVbrLBAuXQbBB3GA9fSFi2cDQFxUusLKYv0aFnQRCDiiS1ggcrk5DUtwFQqKXO7HT7iaeFwilvzp9mMux/5VLpEdX4u5BsiCbeEoNjbhJQ5PdrwXccUZHhb1Wt3YfINKoq2HnAs9EXCFmzwO//sg32AXj8PfFBWIt84HhLgYNOaWpFC6GXIu9DiDQNmWSWWPIy5EHiFbm1tPy4rL7lfLqh4b9e23uo1dt+Tl8ts6tW7S3NWzUFOpWG1R62Z7Tb1zNZU1y9rmltv9Pf3TCrli2d1vy4Blqra67ut6ZcPiYN/gxN+K6mUiaF+/igAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/43a34a2e4b0bc023009954d6f7809f7c/a0b58/1_6_keyboard.webp 230w","/static/43a34a2e4b0bc023009954d6f7809f7c/bc10c/1_6_keyboard.webp 460w","/static/43a34a2e4b0bc023009954d6f7809f7c/27c24/1_6_keyboard.webp 537w"],"sizes":"(max-width: 537px) 100vw, 537px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/43a34a2e4b0bc023009954d6f7809f7c/81c8e/1_6_keyboard.png 230w","/static/43a34a2e4b0bc023009954d6f7809f7c/08a84/1_6_keyboard.png 460w","/static/43a34a2e4b0bc023009954d6f7809f7c/b1cde/1_6_keyboard.png 537w"],"sizes":"(max-width: 537px) 100vw, 537px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/43a34a2e4b0bc023009954d6f7809f7c/b1cde/1_6_keyboard.png","alt":"1 6 keyboard","title":"1 6 keyboard","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom kan flera texteditorer automatiskt indentera den följande raden när Enter-tangenten trycks efter ett kolon. Du får bort indenteringen genom att använda Backspace-tangenten i början av en rad."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 293px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 96.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAACXBIWXMAAAsTAAALEwEAmpwYAAAEtElEQVQ4y3WU6U+UVxSHSdMvbbWNSgXZh2FmGAYYdnBBERA6SsdWZYdgK4ggOyrKElBR6oYoaFllHdZhUQGRalkTo8QUqMFGwCVubfzWf+Bp3kuaNDF9k5O89+Te555zfvcck7DQMAb6Boj4PoLw78LxcvWmsb6JI5m5hG4PZkdAEGnJqRia29gVFELUnnB2BocyYOwnISoGfYiOwC3bKC4oRvpMtvj401B3Aw9nT6R/maWciovlREUkopBvRuMYQHTkQa5cq0ahCcLHT4dK7U9DUxtbg+Nx89mJQrGJtEO5K8DYyDgmJ6bQ63YTFvqtgN7sG6Awv4ygwDj8A6PIzjrJUGUVP3jq+EkfT5kumqe19ZRFprHPJZhtXjrKymtWgE8X/mDm0QzTU9NMjE/y6uUrJN+D6Wnuj4/x8pe7fGhqZLamlme3Bng6Mc785ARPJqdYvHuH2YZaxvOO8WHw9gqw6EQRjvZqtE5uOFgrMHYZ2Ru2BzuZI3q5mnMBwXQbOtF6+KFx8Uat0qJWuqJSOKNUadG4+iCXqenJy4eZGUwK8gpE/by1PqjlTrQ2txETEYuvmy/Zji7sCgmjtbWdiN37iI+ME+IFbNqOm9oNT40HXs6eKK3kGIZGYGoSk8zDWdhZyFDaqbBYZ0l9TT36b/Qigngza6I2bqOhowcbCxlySzlyCxkOlnIcrFZMYavCzMyWxoYmGPsVk672LqLDY8hOz+ZQYgpzs3OcO3OO/QlJnEjNYProcRa6ukmMjONAYgoH07JJTMkgMTWT5NRM0pNSSdHpeVRQBO/eYXJnaIT3799zs/8mfcY+UcOHDx6yvLiEob2L/p4+jCcKeVFTx9+Dg7xu7+BDXz9/Go0sNzYxejSP3sxs/lpcWhHFTeNFd2c3fh4bUdqq+PrL9STE7ud65c8orBVCsNVfmQl444XLaNdZoHP1Zq/fVrwsZKjtHDG3lIs6C2DA5u30dveiC96Jj7uvAKQmpVJXXYevu5+4SLbBDmNXD9U19ezYsYvomARCQsLQOLnj4eKF0kZB+79AF5Urbc1tQmnp2Ziu/prYiDguX6zAxtwWlcyRLz5dRfONZiorKvnsk89FJhoHZ7RqN9w1Hjj8F1h9rZo3r99QefkqZ0+d5VTxaYZuDbHwZIHTJaVCoNKSUpYWl1heWqbiYgUpiSloFP8DvFJ+lUvny7l04SrLS8/FoJCiO3/2PG0tBl48f0FlRZXYU3ryDI9nHjM6Morc2kHAPgIqbJTYWzmwfo09NddrOfhjMl5ab8zXbGCT12aRqnRYSn3dKlNyMnJFBpLPTeP+MVAq+kqXqDG0GIiLihebXB21oksMre3odXoCtwSJgyWFJQzfHhaNII06yaTLOto6VoDSQorSfK0FTTeahdqWplbYmtvh77uVlsYWnJUuODlohGDHcvLoN/azdpUpMgt7ZJb2wi86RQJ2GjrpMHSK6CRx7o3eE6pL6+HBYd6+eSvS6WzvFPD5ud9ZfLaINFSkmp4uLqXweBHzc/MrwKorVWJcZaVlk3k4k+QDhxkducfI8Ahph9KF/0jWUWF5uXkk7k9ioHeAsftjpKekk38sn5yMHGZ/mxXAfwDrwXYWIBU21wAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7f8b7a68350e9455732bc0462f21ccff/a0b58/1_6_keyboard2.webp 230w","/static/7f8b7a68350e9455732bc0462f21ccff/2b735/1_6_keyboard2.webp 293w"],"sizes":"(max-width: 293px) 100vw, 293px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7f8b7a68350e9455732bc0462f21ccff/81c8e/1_6_keyboard2.png 230w","/static/7f8b7a68350e9455732bc0462f21ccff/3cf3e/1_6_keyboard2.png 293w"],"sizes":"(max-width: 293px) 100vw, 293px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7f8b7a68350e9455732bc0462f21ccff/3cf3e/1_6_keyboard2.png","alt":"1 6 keyboard2","title":"1 6 keyboard2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"small","properties":{},"children":[{"type":"element","tagName":"center","properties":{},"children":[{"type":"text","value":"\nNäppäimistökuvien alkuperä:\n "},{"type":"element","tagName":"a","properties":{"href":"https://pixabay.com/users/Clker-Free-Vector-Images-3736/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=311803"},"children":[{"type":"text","value":"Clker-Free-Vector-Images"}]},{"type":"text","value":" from "},{"type":"element","tagName":"a","properties":{"href":"https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=311803"},"children":[{"type":"text","value":"Pixabay"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Orwell","tmcname":"osa01-21_orwell"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter ett heltal från användaren och skriver ut texten \"Orwell\" om siffran är 1984. Annars skrivs inget ut."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1984"}]},{"type":"text","value":"\nOrwell"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Absolutbelopp","tmcname":"osa01-22_absolutbelopp"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber efter ett heltal av användaren. Om siffran är mindre än noll, skriver programmet ut siffran multiplicerat med talet -1. I övriga fall skrivs den siffran användaren angett ut. Nedan finns några exempel på hur programmet ska fungera."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-7"}]},{"type":"text","value":"\nSiffrans absolutbelopp är 7"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSiffrans absolutbelopp är 1"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-99"}]},{"type":"text","value":"\nSiffrans absolutbelopp är 99"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Soppa. Eller inte.","tmcname":"osa01-23_soppa"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som först frågar efter användarens förnamn. Om namnet inte är \"Jerry\", fortsätter programmet med att fråga antalet sopportioner och berättar sedan priset för \"hela soppan\". En portion kostar 5,90."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Två exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad heter du: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Kramer"}]},{"type":"text","value":"\nHur många sopportioner: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSlutsumma 11.8\nNästa!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad heter du: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Jerry"}]},{"type":"text","value":"\nNästa!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Storleksklass","tmcname":"osa01-24_storleksklass"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter ett heltal från användaren. Programmet ska sedan berätta i vilken storleksklass siffran är, enligt följande exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"950"}]},{"type":"text","value":"\nSiffran är mindre än 1000\nTackar!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"59"}]},{"type":"text","value":"\nSiffran är mindre än 1000\nSiffran är mindre än 100\nTackar!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSiffran är mindre än 1000\nSiffran är mindre än 100\nSiffran är mindre än 10\nTackar!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1123"}]},{"type":"text","value":"\nTackar!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"boolean-varden-och--uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Boolean-värden och -uttryck"},{"type":"element","tagName":"a","properties":{"href":"#boolean-varden-och--uttryck","ariaLabel":"boolean varden och uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla villkor i en if-sats resulterar i ett sanningsvärde, det vill säga sant eller falskt. Till exempel villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a < 5"}]},{"type":"text","value":" är sant då "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" är mindre än fem och falskt då "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" är fem eller större."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna typ av värden kallas alltså Boolean-värden (efter matematikern George Boole). I Python representerar datatypen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" ett sanningsvärde. Variabler av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" kan endast ha ett av följande värden: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" (sant) och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" (falskt)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En kodsnutt som resulterar i något av de ovan nämnda värdena kallas Boolean-uttryck. Ett villkor i en if-sats är alltid ett Boolean-uttryck och kan i flera situationer användas som synonym för ordet villkor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet av ett Boolean-uttryck kan lagras i en variabel på samma sätt som vilken som helst annan numerisk räkneoperation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nvillkor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"villkor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" villkor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a är mindre än 5\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\na är mindre än 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons nyckelord "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" kan också användas direkt som sådana. I det följande exemplet körs "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen alltid, eftersom värdet på villkoret är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"villkor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" villkor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vi når hit alltid\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi når hit alltid"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan tycka att det inte i ovanstående exempel verkar vara en så nyttig funktion. Senare under kursens lopp ska vi se på situationer där vi kan ha mera nytta av Boolean-variabler."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Räknare","tmcname":"osa01-25_raknare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som först ber användaren ange två siffror och därefter ett kommando. Om kommandot är summa, produkt eller differens, ska programmet utföra den nämnda räkneoperationen. I övriga fall skriver inte programmet ut något."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nTal 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"17"}]},{"type":"text","value":"\nKommando: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"summa"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"10 + 17 = 27"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nTal 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nKommando: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"produkt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 * 6 = 24"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nTal 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nKommando: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"differens"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 - 6 = -2"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Temperaturer","tmcname":"osa01-26_temperaturer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en temperatur i Farenheit. Programmet ska skriva ut den här temperaturen i Celcius. Om temperaturen mätt i Celcius är under noll ska programmet också skriva ut texten \"Kallt!\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan söka på nätet efter den korrekta formeln för att konvertera temperaturer i Farenheit till Celcius."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange temperatur (i Farenheit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"101"}]},{"type":"text","value":"\n101 grader Farenheit on 38.333333333333336 grader Celcius"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange temperatur (i Farenheit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\n21 grader Farenheit on -6.111111111111111 grader Celcius\nKallt!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Lön","tmcname":"osa01-27_lon"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter timlön, antal arbetstimmar samt veckodag. Programmet skriver ut lönen som räknas med formeln timlön * arbetstimmar. På söndag är timlönen fördubblad."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Timlön: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8.5"}]},{"type":"text","value":"\nArbetstimmar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nVeckodag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"maanantai"}]},{"type":"text","value":"\nLön 25.5 euro"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Timlön: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12.5"}]},{"type":"text","value":"\nArbetstimmar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nVeckodag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sunnuntai"}]},{"type":"text","value":"\nLön 250.0 euro"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa programmet: Ränta","tmcname":"osa01-28_ranta"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet räknar antalet bonuspoäng som ska adderas till ett bonuskort i slutet av året, enligt följande formel:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"bonuspoäng < 100: ränta 10 % extra poäng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"övriga fall: ränta 15 % extra poäng"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här fungerar programmet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många poäng? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"55"}]},{"type":"text","value":"\nDu fick 10 % i bonus\nDu har nu 60.5 poäng"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ohjelma toimii kuitenkin jollain syötteillä oudosti:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många poäng? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"95"}]},{"type":"text","value":"\nDu fick 10 % i bonus\nDu fick 15 % i bonus\nDu har nu 120.175 poäng"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigera programmet så att man endast får 10 % eller 15 % bonus – inte både och."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Morgondagens klädsel","tmcname":"osa01-29_kladsel"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör ett program som frågar användaren om morgondagens väderprognos. Programmet rekommenderar därefter klädsel enligt vädret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rekommendationen beror på om temperaturen är över fem, tio eller 20 grader samt om det regnar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berätta väderprognosen för imorgon:\nTemperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nRegnar det (ja/nej): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej"}]},{"type":"text","value":"\nTa på dig byxor och t-skjorta"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berätta väderprognosen för imorgon:\nTemperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\nRegnar det (ja/nej): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej"}]},{"type":"text","value":"\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berätta väderprognosen för imorgon:\nTemperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nRegnar det (ja/nej): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej"}]},{"type":"text","value":"\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja\nKlä på dig en jacka"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Berätta väderprognosen för imorgon:\nTemperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nRegnar det (ja/nej): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ja"}]},{"type":"text","value":"\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja\nKlä på dig en jacka\nEn varm jacka rekommenderas\nVantar rekommenderas också\nKom ihåg paraplyet!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Andra gradens ekvation","tmcname":"osa01-30_andra_gradens_ekvation"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" i Python har funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" som kan användas för att räkna kvadratroten för ett tal. Så här fungerar funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör ett program som räknar resultatet för andra gradens ekvation ax² + bx + c. Till programmet ges värdena a, b och c, och därefter beräknas lösningen med följande formel:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"x = (-b ± sqrt(b² - 4ac)) / (2a)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att ekvationen har två rötter, varvid formeln ovan fungerar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge a: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nGe b: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nGe c: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rötterna är 2.0 och -4.0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kertauskysely tämän osan asioihin liittyen:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät om materialet i den här veckans modul. Du får ett poäng när du fyllt i enkäten."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du använda dig av en enkel if-sats när du programmerar
  • \n
  • vet du vad ett Boolean-värde är
  • \n
  • kan du uttrycka villkor med hjälp av jämförelseoperatorer.
  • \n

Hittills har vi skapat program där koden körts rad för rad från början till slut. Istället för att köra varje kodrad när programmet är igång är det ofta nyttigt att ha delar i programmet som endast körs i vissa situationer.

Till exempel följande program verifierar att användaren är tillräckligt gammal:

alder = int(input(\"Vad är din ålder? \"))\n\nif alder > 17:\n    print(\"Du är myndig!\")\n    print(\"Här får du alltså GTA 6.\")\n\nprint(\"Nästa kund!\")

När användaren är över 17 år, borde det se ut så här när programmet körs:

Vad är din ålder? 18\nDu är myndig!\nHär får du alltså GTA 6.\nNästa kund!

Däremot, om användaren är 17 eller yngre, ser utskriften ut så här:

Vad är din ålder? 16\nNästa kund!

Dessa exempel visar hur ett värde som getts till programmet påverkar vilka delar av koden som körs. Programmet innehåller en if-sats med kod som körs enbart då ett definierat villkor uppfylls.

\n \n \n \n \n \"1\n \n

I en if-sats följs nyckelordet if med ett villkor som till exempel kan vara en jämförelse av två värden. Koden som följer körs endast då villkoret uppfylls.

Notera kolontecknet. Om det saknas…

alder = 10\n\n# kaksoispiste unohtui seuraavan rivin lopusta\nif alder > 17\n    print(\"Du är myndig.\")

…orsakas ett fel när programmet körs:

\n
\nFile \"ohjelma.py\", line 3\n  if alder > 17\n              ^\nSyntaxError: invalid syntax\n
\n

Jämförelseoperatorer

Det är vanligt att man vill jämföra två värden sinsemellan. Här följer en tabell över de vanligaste jämförelseoperatorerna i Python:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperatorBetydelseExempel
==Är lika meda == b
!=Är inte likaa != b
>Större äna > b
>=Större än eller lika meda >= b
<Mindre äna < b
<=Mindre än eller lika meda <= b

Vi tar nu en titt på ett program som skriver ut olika saker baserat på det värde som användaren anger. Här har vi if-satser som kan uppfyllas då värdet är negativt, positivt eller lika med noll:

siffra = int(input(\"Ge ett tal: \"))\n\nif siffra < 0:\n    print(\"Talet är negativt.\")\n\nif siffra > 0:\n    print(\"Talet är positivt.\")\n\nif siffra == 0:\n    print(\"Talet är noll.\")

Här har vi tre exempel med olika indata:

Ge ett tal: 15\nTalet är positivt.

Ge ett tal: -18\nTalet är negativt.

Ge ett tal: 0\nTalet är noll.

Indentering

Python känner igen att en kodsnutt hör till en if-sats då varje rad är indenterad på samma sätt. Det här betyder att det finns mellanrum i början av varje kodrad som hör till if-satsen. Mellanrummet ska vara det samma för varje rad.

Exempelvis:

losenord = input(\"Ange lösenord: \")\n\nif losenord == \"katt\":\n    print(\"Du visste lösenordet!\")\n    print(\"Du måste alltså vara den riktiga användaren...\")\n    print(\"...eller så är du en hacker.\")\n\nprint(\"Programmet avslutades. Tack och hej!\")

Du kan använda Tab-tangenten för att lägga till mellanrum där det behövs.

\n \n \n \n \n \"1\n \n

Dessutom kan flera texteditorer automatiskt indentera den följande raden när Enter-tangenten trycks efter ett kolon. Du får bort indenteringen genom att använda Backspace-tangenten i början av en rad.

\n \n \n \n \n \"1\n \n \n
\nNäppäimistökuvien alkuperä:\n Clker-Free-Vector-Images from Pixabay\n

Skapa ett program som frågar efter ett heltal från användaren och skriver ut texten \"Orwell\" om siffran är 1984. Annars skrivs inget ut.

Ge ett tal: 2020

Ge ett tal: 1984\nOrwell

Skapa ett program som ber efter ett heltal av användaren. Om siffran är mindre än noll, skriver programmet ut siffran multiplicerat med talet -1. I övriga fall skrivs den siffran användaren angett ut. Nedan finns några exempel på hur programmet ska fungera.

Ge ett tal: -7\nSiffrans absolutbelopp är 7

Ge ett tal: 1\nSiffrans absolutbelopp är 1

Ge ett tal: -99\nSiffrans absolutbelopp är 99

Skapa ett program som först frågar efter användarens förnamn. Om namnet inte är \"Jerry\", fortsätter programmet med att fråga antalet sopportioner och berättar sedan priset för \"hela soppan\". En portion kostar 5,90.

Två exempel:

Vad heter du: Kramer\nHur många sopportioner: 2\nSlutsumma 11.8\nNästa!

Vad heter du: Jerry\nNästa!

Skapa ett program som frågar efter ett heltal från användaren. Programmet ska sedan berätta i vilken storleksklass siffran är, enligt följande exempel:

Ge ett tal: 950\nSiffran är mindre än 1000\nTackar!

Ge ett tal: 59\nSiffran är mindre än 1000\nSiffran är mindre än 100\nTackar!

Ge ett tal: 2\nSiffran är mindre än 1000\nSiffran är mindre än 100\nSiffran är mindre än 10\nTackar!

Ge ett tal: 1123\nTackar!

Boolean-värden och -uttryck

Alla villkor i en if-sats resulterar i ett sanningsvärde, det vill säga sant eller falskt. Till exempel villkoret a < 5 är sant då a är mindre än fem och falskt då a är fem eller större.

Denna typ av värden kallas alltså Boolean-värden (efter matematikern George Boole). I Python representerar datatypen bool ett sanningsvärde. Variabler av typen bool kan endast ha ett av följande värden: True (sant) och False (falskt).

En kodsnutt som resulterar i något av de ovan nämnda värdena kallas Boolean-uttryck. Ett villkor i en if-sats är alltid ett Boolean-uttryck och kan i flera situationer användas som synonym för ordet villkor.

Resultatet av ett Boolean-uttryck kan lagras i en variabel på samma sätt som vilken som helst annan numerisk räkneoperation:

a = 3\nvillkor = a < 5\nprint(villkor)\nif villkor:\n    print(\"a är mindre än 5\")

True\na är mindre än 5

Pythons nyckelord True och False kan också användas direkt som sådana. I det följande exemplet körs print-instruktionen alltid, eftersom värdet på villkoret är True:

villkor = True\nif villkor:\n    print(\"Vi når hit alltid\")

Vi når hit alltid

Man kan tycka att det inte i ovanstående exempel verkar vara en så nyttig funktion. Senare under kursens lopp ska vi se på situationer där vi kan ha mera nytta av Boolean-variabler.

Skapa ett program som först ber användaren ange två siffror och därefter ett kommando. Om kommandot är summa, produkt eller differens, ska programmet utföra den nämnda räkneoperationen. I övriga fall skriver inte programmet ut något.

Exempel:

Tal 1: 10\nTal 2: 17\nKommando: summa

10 + 17 = 27

Tal 1: 4\nTal 2: 6\nKommando: produkt

4 * 6 = 24

Tal 1: 4\nTal 2: 6\nKommando: differens

4 - 6 = -2

Skapa ett program som ber användaren ange en temperatur i Farenheit. Programmet ska skriva ut den här temperaturen i Celcius. Om temperaturen mätt i Celcius är under noll ska programmet också skriva ut texten \"Kallt!\".

Du kan söka på nätet efter den korrekta formeln för att konvertera temperaturer i Farenheit till Celcius.

Exempel:

Ange temperatur (i Farenheit): 101\n101 grader Farenheit on 38.333333333333336 grader Celcius

Ange temperatur (i Farenheit): 21\n21 grader Farenheit on -6.111111111111111 grader Celcius\nKallt!

Skapa ett program som frågar efter timlön, antal arbetstimmar samt veckodag. Programmet skriver ut lönen som räknas med formeln timlön * arbetstimmar. På söndag är timlönen fördubblad.

Timlön: 8.5\nArbetstimmar: 3\nVeckodag: maanantai\nLön 25.5 euro

Timlön: 12.5\nArbetstimmar: 10\nVeckodag: sunnuntai\nLön 250.0 euro

Det här programmet räknar antalet bonuspoäng som ska adderas till ett bonuskort i slutet av året, enligt följande formel:

    \n
  • bonuspoäng < 100: ränta 10 % extra poäng
  • \n
  • övriga fall: ränta 15 % extra poäng
  • \n

Så här fungerar programmet:

Hur många poäng? 55\nDu fick 10 % i bonus\nDu har nu 60.5 poäng

Ohjelma toimii kuitenkin jollain syötteillä oudosti:

Hur många poäng? 95\nDu fick 10 % i bonus\nDu fick 15 % i bonus\nDu har nu 120.175 poäng

Korrigera programmet så att man endast får 10 % eller 15 % bonus – inte både och.

Gör ett program som frågar användaren om morgondagens väderprognos. Programmet rekommenderar därefter klädsel enligt vädret.

Rekommendationen beror på om temperaturen är över fem, tio eller 20 grader samt om det regnar.

Så här ska programmet fungera:

Berätta väderprognosen för imorgon:\nTemperatur: 21\nRegnar det (ja/nej): nej\nTa på dig byxor och t-skjorta

Berätta väderprognosen för imorgon:\nTemperatur: 11\nRegnar det (ja/nej): nej\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja

Berätta väderprognosen för imorgon:\nTemperatur: 7\nRegnar det (ja/nej): nej\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja\nKlä på dig en jacka

Berätta väderprognosen för imorgon:\nTemperatur: 3\nRegnar det (ja/nej): ja\nTa på dig byxor och t-skjorta\nTa på dig också en långärmad tröja\nKlä på dig en jacka\nEn varm jacka rekommenderas\nVantar rekommenderas också\nKom ihåg paraplyet!

Modulen math i Python har funktionen sqrt som kan användas för att räkna kvadratroten för ett tal. Så här fungerar funktionen:

from math import sqrt\n\nprint(sqrt(9))

Programmet skriver ut:

3.0

Gör ett program som räknar resultatet för andra gradens ekvation ax² + bx + c. Till programmet ges värdena a, b och c, och därefter beräknas lösningen med följande formel:

x = (-b ± sqrt(b² - 4ac)) / (2a)

Du kan anta att ekvationen har två rötter, varvid formeln ovan fungerar.

Exempel:

Ge a: 1\nGe b: 2\nGe c: -8

Rötterna är 2.0 och -4.0

Kertauskysely tämän osan asioihin liittyen:

Vänligen svara på en kort enkät om materialet i den här veckans modul. Du får ett poäng när du fyllt i enkäten.

","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/5-villkorssatser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-1/page-data.json b/page-data/osa-1/page-data.json index 1cd446779..48a515cb8 100644 --- a/page-data/osa-1/page-data.json +++ b/page-data/osa-1/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-1", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-1","title":"Modul 1"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-1","title":"Modul 1"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-1/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-10/1-klasshierarkier/page-data.json b/page-data/osa-10/1-klasshierarkier/page-data.json index 70db400cb..304737eff 100644 --- a/page-data/osa-10/1-klasshierarkier/page-data.json +++ b/page-data/osa-10/1-klasshierarkier/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-10/1-klasshierarkier", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad arv betyder i programmeringssammanhang"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva klasser som ärver andra klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur arv påverkar egenskaperna i klasser"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"specialklasser-for-speciella-andamal","style":"position:relative;"},"children":[{"type":"text","value":"Specialklasser för speciella ändamål"},{"type":"element","tagName":"a","properties":{"href":"#specialklasser-for-speciella-andamal","ariaLabel":"specialklasser for speciella andamal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland stöter man på en situation där man redan har definierat en klass, men sedan inser att man behöver speciella egenskaper i vissa, men inte alla, instanser av klassen. Och ibland inser man att man har definierat två stycken mycket liknande klasser med bara små skillnader. Som programmerare strävar vi efter att alltid upprepa oss så lite som möjligt, medan vi behåller tydlighet och läsbarhet. Så hur kan vi ta hänsyn till olika implementeringar av liknande objekt?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på två klassdefinitioner: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Larare"}]},{"type":"text","value":". Getter- och sättar-metoder har utelämnats tills vidare för att hålla exemplet kort."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Larare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" larande_ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rum\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"larande_ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" larande_ar\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även i ett avskalat exempel som ovan har vi redan en hel del upprepningar: båda klasserna innehåller attributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"epost"}]},{"type":"text","value":". Det vore en bra idé att ha en enda attributdefinition, så att det räcker med en enda funktion för att redigera båda attributen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tänk dig till exempel att skolans e-postadress ändras. Alla adresser skulle behöva uppdateras. Vi skulle kunna skriva två separata versioner av i stort sett samma funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"uppdatera_epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".edu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"uppdatera_epost2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Larare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".edu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skriva i stort sett samma sak två gånger är onödig upprepning, för att inte tala om att det fördubblar möjligheterna till fel. Det skulle vara en klar förbättring om vi kunde använda en enda funktion för att arbeta med instanser av båda klasserna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Båda klasserna har också attribut som är unika för dem. Att bara kombinera alla attribut i en enda klass skulle innebära att alla instanser av klassen då skulle ha onödiga attribut, bara olika för olika instanser. Det verkar inte heller vara en idealisk situation."}]},{"type":"element","tagName":"h2","properties":{"id":"arv","style":"position:relative;"},"children":[{"type":"text","value":"Arv"},{"type":"element","tagName":"a","properties":{"href":"#arv","ariaLabel":"arv permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objektorienterade programmeringsspråk innehåller vanligtvis en teknik som kallas arv (eng. inheritance). En klass kan ärva egenskaper från en annan klass. Förutom dessa ärvda egenskaper kan en klass också innehålla egenskaper som är unika för den."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med detta i åtanke är det rimligt att klasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Larare"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" har en gemensam bas eller föräldraklass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den nya klassen innehåller de egenskaper som delas av de andra två klasserna. Nu kan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Larare"}]},{"type":"text","value":" ärva dessa egenskaper och dessutom lägga till sina egna. :"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Syntaxen för arv innebär helt enkelt att basklassens namn läggs till inom parentes på rubrikraden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"uppdatera_epost_doman"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ny_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n gammal_doman "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"gammal_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ny_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Larare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" larande_ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rum\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"larande_ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" larande_ar\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Test"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n sam "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sam@example.com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sam"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppdatera_epost_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.edu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sam"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lars "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Larare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lars Lärare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lars@example.fi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppdatera_epost_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.ex\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Både "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Larare"}]},{"type":"text","value":" ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":", så båda har de egenskaper som definieras i klassen Person, inklusive metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uppdatera_epost_doman"}]},{"type":"text","value":". Samma metod fungerar för instanser av båda de härledda klasserna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett annat exempel. Vi har en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"text","value":" som ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BokLada"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en enkel bok \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" forfattare\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BokLada"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en låda för böcker \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bocker "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"lista_bocker"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BokLada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en hylla för böcker \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" paikka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"paikka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"text","value":" innehåller metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"text","value":". En metod med samma namn finns definierad i basklassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BokLada"}]},{"type":"text","value":". Detta kallas överstyrning (eng. overriding): om en härledd klass har en metod med samma namn som basklassen, överstyr den härledda versionen originalet i instanser av den härledda klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tanken i exemplet ovan är att en ny bok som läggs till i en Bok låda alltid hamnar högst upp, men med en Bokhylla kan du själv ange platsen. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_bocker"}]},{"type":"text","value":" fungerar likadant för båda klasserna, eftersom det inte finns någon överordnad metod i den härledda klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss prova dessa klasser:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar några böcker för testning"}]},{"type":"text","value":"\n b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7 bröder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Aleksis Kivi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sinuhe\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mika Waltari\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Okänd soldat\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Väinö Linna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar en BokLada och tillsätter böckerna"}]},{"type":"text","value":"\n lada "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BokLada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar en Bokhylla och tillsätter böckerna (alltid till början av hyllan)"}]},{"type":"text","value":"\n hylla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bokhylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriver ut"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"I lådan:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lista_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"I hyllan:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lista_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"text","value":" "},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I lådan:\n7 bröder (Aleksis Kivi)\nSinuhe (Mika Waltari)\nOkänd soldat (Väinö Linna)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I hyllan:\nOkänd soldat (Väinö Linna)\nSinuhe (Mika Waltari)\n7 bröder (Aleksis Kivi)"}]},{"type":"text","value":" "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen Bokhylla har alltså också tillgång till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_bocker"}]},{"type":"text","value":". Genom ärvning är metoden medlem i alla klasser som kommer från klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BokLada"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"arv-och-rackvidd-av-egenskaper","style":"position:relative;"},"children":[{"type":"text","value":"Arv och räckvidd av egenskaper"},{"type":"element","tagName":"a","properties":{"href":"#arv-och-rackvidd-av-egenskaper","ariaLabel":"arv och rackvidd av egenskaper permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En härledd klass ärver alla egenskaper från sin basklass. Dessa egenskaper är direkt åtkomliga i den härledda klassen, såvida de inte har definierats som privata i basklassen (med två understreck före egenskapens namn)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom attributen för en Bokhylla är identiska med en BokLada, fanns det ingen anledning att skriva om konstruktorn för Bokhylla. Vi anropade helt enkelt basklassens konstruktor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BokLada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla egenskaper i basklassen kan nås från den härledda klassen med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"super()"}]},{"type":"text","value":". Argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" utelämnas från metodanropet, eftersom Python lägger till det automatiskt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men vad händer om attributen inte är identiska; kan vi fortfarande använda basklassens konstruktor på något sätt? Låt oss titta på en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Avhandling"}]},{"type":"text","value":" och som ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":". Den härledda klassen kan fortfarande anropa konstruktören från basklassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en enkel bok \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" forfattare\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Avhandling"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en magisteravhandling \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vitsord\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstruktorn i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Avhandling"}]},{"type":"text","value":"-klassen anropar konstruktorn i basklassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":" med argumenten för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forfattare"}]},{"type":"text","value":". Dessutom anger konstruktören i den härledda klassen värdet för attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vitsord"}]},{"type":"text","value":". Detta kan naturligtvis inte vara en del av basklassens konstruktor, eftersom basklassen inte har något sådant attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovanstående klass kan användas på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n avhandling "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Avhandling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python och Universum\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut attributens värden"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"avhandling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"avhandling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"avhandling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python och Universum\nPeter Python\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om en härledd klass överstyr en metod i sin basklass kan den härledda klassen fortfarande anropa den åsidosatta metoden i basklassen. I följande exempel har vi ett grundläggande "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bonuskort"}]},{"type":"text","value":" och ett särskilt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Platinumkort"}]},{"type":"text","value":" för särskilt lojala kunder. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rakna_bonus"}]},{"type":"text","value":" är åsidosatt i den härledda klassen, men den åsidosatta metoden anropar basmetoden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bonuskort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kopta_produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kopta_produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kopta_produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.05"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bonus\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Platinumkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Bonuskort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Anropar metoden i basklassen..."}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rakna_bonus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ...och tillsätter ännu fem procent till totalet"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.05"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bonus\n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bonusen för ett Platinumkort beräknas alltså genom att anropa den överstyrda metoden i basklassen och sedan lägga till 5 procent extra till basresultatet. Ett exempel på hur dessa klasser används:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bonuskort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bananer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mandariner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rakna_bonus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n kort2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Platinumkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kort2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bananer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kort2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mandariner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bonus2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kort2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rakna_bonus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bonus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bonus2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0.7225\n0.7586250000000001"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bärbar dator","tmcname":"osa10-01_barbar_dator"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns en klassdefinition för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Dator"}]},{"type":"text","value":", som har attributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"modell"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"snabbhet"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BarbarDator"}]},{"type":"text","value":", som "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ärver"}]},{"type":"text","value":" klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Dator"}]},{"type":"text","value":". Den nya klassens konstruktor ska ta ett tredgje argument, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vikt"}]},{"type":"text","value":", av typen heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa också en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod i klassdefinitionen. Se exemplet nedan för det förväntade formatet av strängen som skrivs ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"barbar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BarbarDator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"NoteBook Pro15\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"barbar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NoteBook Pro15, 1500 MHz, 2 kg"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spelmuseum","tmcname":"osa10-02_spelmuseum"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns klassdefinitioner för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Datorspel"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Spelforrad"}]},{"type":"text","value":". Spelforrads-objekt används för att förvara Datorspel-objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bekanta dig med dessa klasser. Definiera sedan klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Spelmuseum"}]},{"type":"text","value":", som ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Spelforrad"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelmuseum-klassen ska "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"överstyra"}]},{"type":"text","value":" metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_spel()"}]},{"type":"text","value":", så att den returnerar en lista av endast de spel som är gjorda innan år 1990."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom ska den nya klassen ha en konstruktor som "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"anropar konstruktorn från överklassen Spelforrad"}]},{"type":"text","value":". Konstruktorn tar inga argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"museum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Spelmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Datorspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pacman\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namco\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1980"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Datorspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"GTA 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rockstar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1999"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Datorspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bubble Bobble\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Taito\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1986"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" spel "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" museum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lista_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pacman\nBubble Bobble"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Arean","tmcname":"osa10-03_arean"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns en klassdefinition för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":" som representerar en "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Rektangel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"rektangelform"}]},{"type":"text","value":". Klassen används på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"area:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"rektangel 2x3\narea: 6"}]}]},{"type":"element","tagName":"h2","properties":{"id":"kvadrat","style":"position:relative;"},"children":[{"type":"text","value":"Kvadrat"},{"type":"element","tagName":"a","properties":{"href":"#kvadrat","ariaLabel":"kvadrat permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kvadrat"}]},{"type":"text","value":" som ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":". Sidorna på en "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Kvadrat","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"kvadrat"}]},{"type":"text","value":" har alla samma längd, alltså är den en speciell sort av rektangel. Den nya klassen ska inte innehålla nya attribut!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kvadrat-objekt används på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kvadrat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"area:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"kvadrat 4x4\narea: 16"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ordspel","tmcname":"osa10-04_ordspel"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbotten innehåller klassdefinitionen för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Ordspel"}]},{"type":"text","value":", som erbjuder enkel funktionalitet för att spela olika ordbaserade spel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" random\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordspel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rundor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rundor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rundor\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rundans_vinnare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare1_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare2_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi lottar ut en vinnare"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" random"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spela"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ordspel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rundor"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"runda "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n svar1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelare1: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n svar2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelare2: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rundans_vinnare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svar1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelare 1 vann\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rundans_vinnare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svar1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelare 2 vann\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# oavgjort"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelet är över, vinster:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"spelare 1: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"spelare 2: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelet spelas enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Ordspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordspel:\nrunda 1\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"långtord"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"??"}]},{"type":"text","value":"\nspelare 2 vann\nrunda 2\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jag är bäst"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"va?"}]},{"type":"text","value":"\nspelare 1 vann\nrunda 3\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"vem vinner"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jag"}]},{"type":"text","value":"\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 2\nspelare 2: 1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna ”grundläggande” version av spelet avgörs vinnaren slumpmässigt. Spelarnas insatser har ingen inverkan på resultatet."}]},{"type":"element","tagName":"h2","properties":{"id":"langsta-ordet-vinner","style":"position:relative;"},"children":[{"type":"text","value":"Längsta ordet vinner"},{"type":"element","tagName":"a","properties":{"href":"#langsta-ordet-vinner","ariaLabel":"langsta ordet vinner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LangstaOrdet"}]},{"type":"text","value":". Det är en version av spelet där den som skriver in det längsta ordet i varje omgång vinner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den nya versionen av spelet implementeras genom att ärva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Ordspel"}]},{"type":"text","value":"-klassen. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rundans_vinnare"}]},{"type":"text","value":" bör också överstyras på lämpligt sätt. Den nya klassen är uppbyggd på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LangstaOrdet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Ordspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rundor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rundor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rundans_vinnare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare1_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare2_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# koden som avgör vinnaren här"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur spelet fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LangstaOrdet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordspel:\nrunda 1\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kort"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"långtord"}]},{"type":"text","value":"\nspelare 2 vann\nrunda 2\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"va?"}]},{"type":"text","value":"\nrunda 3\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jag är bäst"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej jag!"}]},{"type":"text","value":"\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 1\nspelare 2: 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"flest-vokaler-vinner","style":"position:relative;"},"children":[{"type":"text","value":"Flest vokaler vinner"},{"type":"element","tagName":"a","properties":{"href":"#flest-vokaler-vinner","ariaLabel":"flest vokaler vinner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en annan Ordspel-klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FlestVokaler"}]},{"type":"text","value":". I den här versionen av spelet vinner den som har klämt in flest vokaler i sitt ord."}]},{"type":"element","tagName":"h2","properties":{"id":"sten-sax-pase","style":"position:relative;"},"children":[{"type":"text","value":"Sten sax påse"},{"type":"element","tagName":"a","properties":{"href":"#sten-sax-pase","ariaLabel":"sten sax pase permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera slutligen klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StenSaxPase"}]},{"type":"text","value":" som låter dig spela ett spel av "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Sten,_sax,_p%C3%A5se","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Sten, sax, påse"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelets regler är följande:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"sten vinner påse (stenen kan förstöra saxen men saxen kan inte klippa stenen)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"påse vinner sten (påsen kan omsluta stenen)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"sax vinner påse (saxen kan klippa påsen)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om inmatningen från en spelare är ogiltig förlorar den omgången. Om båda spelarna skriver in något annat än sten, sax eller påse blir resultatet oavgjort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur spelet spelas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" StenSaxPase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordspel:\nrunda 1\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sten"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sten"}]},{"type":"text","value":"\nrunda 2\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sten"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"påse"}]},{"type":"text","value":"\nspelare 2 vann\nrunda 3\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sax"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"påse"}]},{"type":"text","value":"\nspelare 1 vann\nrunda 4\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"påse"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dynamit"}]},{"type":"text","value":"\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 2\nspelare 2: 1"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad arv betyder i programmeringssammanhang
  • \n
  • Kommer du att kunna skriva klasser som ärver andra klasser
  • \n
  • Vet du hur arv påverkar egenskaperna i klasser
  • \n

Specialklasser för speciella ändamål

Ibland stöter man på en situation där man redan har definierat en klass, men sedan inser att man behöver speciella egenskaper i vissa, men inte alla, instanser av klassen. Och ibland inser man att man har definierat två stycken mycket liknande klasser med bara små skillnader. Som programmerare strävar vi efter att alltid upprepa oss så lite som möjligt, medan vi behåller tydlighet och läsbarhet. Så hur kan vi ta hänsyn till olika implementeringar av liknande objekt?

Låt oss ta en titt på två klassdefinitioner: Studerande och Larare. Getter- och sättar-metoder har utelämnats tills vidare för att hålla exemplet kort.

\nclass Studerande:\n\n    def __init__(self, namn: str, id: str, epost: str, studiepoang: str):\n        self.namn = namn\n        self.id = id\n        self.epost = epost\n        self.studiepoang = studiepoang\n\nclass Larare:\n\n    def __init__(self, namn: str, epost: str, rum: str, larande_ar: int):\n        self.namn = namn\n        self.epost = epost\n        self.rum = rum\n        self.larande_ar = larande_ar\n

Även i ett avskalat exempel som ovan har vi redan en hel del upprepningar: båda klasserna innehåller attributen namn och epost. Det vore en bra idé att ha en enda attributdefinition, så att det räcker med en enda funktion för att redigera båda attributen.

Tänk dig till exempel att skolans e-postadress ändras. Alla adresser skulle behöva uppdateras. Vi skulle kunna skriva två separata versioner av i stort sett samma funktion:

\ndef uppdatera_epost(s: Studerande):\n    s.epost = s.epost.replace(\".com\", \".edu\")\n\ndef uppdatera_epost2(s: Larare):\n    s.epost = s.epost.replace(\".com\", \".edu\")\n

Att skriva i stort sett samma sak två gånger är onödig upprepning, för att inte tala om att det fördubblar möjligheterna till fel. Det skulle vara en klar förbättring om vi kunde använda en enda funktion för att arbeta med instanser av båda klasserna.

Båda klasserna har också attribut som är unika för dem. Att bara kombinera alla attribut i en enda klass skulle innebära att alla instanser av klassen då skulle ha onödiga attribut, bara olika för olika instanser. Det verkar inte heller vara en idealisk situation.

Arv

Objektorienterade programmeringsspråk innehåller vanligtvis en teknik som kallas arv (eng. inheritance). En klass kan ärva egenskaper från en annan klass. Förutom dessa ärvda egenskaper kan en klass också innehålla egenskaper som är unika för den.

Med detta i åtanke är det rimligt att klasserna Larare och Studerande har en gemensam bas eller föräldraklass Person:

\nclass Person:\n\n   def __init__(self, namn: str, epost: str):\n       self.namn = namn\n       self.epost = epost\n

Den nya klassen innehåller de egenskaper som delas av de andra två klasserna. Nu kan Studerande och Larare ärva dessa egenskaper och dessutom lägga till sina egna. :

Syntaxen för arv innebär helt enkelt att basklassens namn läggs till inom parentes på rubrikraden:

\nclass Person:\n\n   def __init__(self, namn: str, epost: str):\n       self.namn = namn\n       self.epost = epost\n\n   def uppdatera_epost_doman(self, ny_doman: str):\n       gammal_doman = self.epost.split(\"@\")[1]\n       self.epost = self.epost.replace(gammal_doman, ny_doman)\n\nclass Studerande(Person):\n\n   def __init__(self, namn: str, id: str, epost: str, studiepoang: str):\n       self.namn = namn\n       self.id = id\n       self.epost = epost\n       self.studiepoang = studiepoang\n\nclass Larare(Person):\n\n   def __init__(self, namn: str, epost: str, rum: str, larande_ar: int):\n       self.namn = namn\n       self.epost = epost\n       self.rum = rum\n       self.larande_ar = larande_ar\n\n# Test\nif __name__ == \"__main__\":\n   sam = Studerande(\"Sam Studerande\", \"1234\", \"sam@example.com\", 0)\n   sam.uppdatera_epost_doman(\"example.edu\")\n   print(sam.epost)\n\n   lars = Larare(\"Lars Lärare\", \"lars@example.fi\", \"A123\", 2)\n   lars.uppdatera_epost_doman(\"example.ex\")\n   print(lars.epost)\n

Både Studerande och Larare ärver klassen Person, så båda har de egenskaper som definieras i klassen Person, inklusive metoden uppdatera_epost_doman. Samma metod fungerar för instanser av båda de härledda klasserna.

Låt oss titta på ett annat exempel. Vi har en Bokhylla som ärver klassen BokLada:

class Bok:\n   \"\"\" Klassen modellerar en enkel bok \"\"\"\n   def __init__(self, namn: str, forfattare: str):\n       self.namn = namn\n       self.forfattare = forfattare\n\n\nclass BokLada:\n   \"\"\" Klassen modellerar en låda för böcker \"\"\"\n\n   def __init__(self):\n       self.bocker = []\n\n   def tillsatt_bok(self, bok: Bok):\n       self.bocker.append(bok)\n\n   def lista_bocker(self):\n       for bok in self.bocker:\n           print(f\"{bok.namn} ({bok.forfattare})\")\n\nclass Bokhylla(BokLada):\n   \"\"\" Klassen modellerar en hylla för böcker \"\"\"\n\n   def __init__(self):\n       super().__init__()\n\n   def tillsatt_bok(self, bok: Bok, paikka: int):\n       self.bocker.insert(paikka, bok)\n\n

Klassen Bokhylla innehåller metoden tillsatt_bok. En metod med samma namn finns definierad i basklassen BokLada. Detta kallas överstyrning (eng. overriding): om en härledd klass har en metod med samma namn som basklassen, överstyr den härledda versionen originalet i instanser av den härledda klassen.

Tanken i exemplet ovan är att en ny bok som läggs till i en Bok låda alltid hamnar högst upp, men med en Bokhylla kan du själv ange platsen. Metoden lista_bocker fungerar likadant för båda klasserna, eftersom det inte finns någon överordnad metod i den härledda klassen.

Låt oss prova dessa klasser:

\nif __name__ == \"__main__\":\n   # Vi skapar några böcker för testning\n   b1 = Bok(\"7 bröder\", \"Aleksis Kivi\")\n   b2 = Bok(\"Sinuhe\", \"Mika Waltari\")\n   b3 = Bok(\"Okänd soldat\", \"Väinö Linna\")\n\n   # Vi skapar en BokLada och tillsätter böckerna\n   lada = BokLada()\n   lada.tillsatt_bok(b1)\n   lada.tillsatt_bok(b2)\n   lada.tillsatt_bok(b3)\n\n   # Vi skapar en Bokhylla och tillsätter böckerna (alltid till början av hyllan)\n   hylla = Bokhylla()\n   hylla.tillsatt_bok(b1, 0)\n   hylla.tillsatt_bok(b2, 0)\n   hylla.tillsatt_bok(b3, 0)\n\n\n   # Skriver ut\n   print(\"I lådan:\")\n   lada.lista_bocker()\n\n   print()\n\n   print(\"I hyllan:\")\n   hylla.lista_bocker()\n

I lådan:\n7 bröder (Aleksis Kivi)\nSinuhe (Mika Waltari)\nOkänd soldat (Väinö Linna)

I hyllan:\nOkänd soldat (Väinö Linna)\nSinuhe (Mika Waltari)\n7 bröder (Aleksis Kivi)

Klassen Bokhylla har alltså också tillgång till metoden lista_bocker. Genom ärvning är metoden medlem i alla klasser som kommer från klassen BokLada.

Arv och räckvidd av egenskaper

En härledd klass ärver alla egenskaper från sin basklass. Dessa egenskaper är direkt åtkomliga i den härledda klassen, såvida de inte har definierats som privata i basklassen (med två understreck före egenskapens namn).

Eftersom attributen för en Bokhylla är identiska med en BokLada, fanns det ingen anledning att skriva om konstruktorn för Bokhylla. Vi anropade helt enkelt basklassens konstruktor:

\nclass Bokhylla(BokLada):\n\n   def __init__(self):\n       super().__init__()\n

Alla egenskaper i basklassen kan nås från den härledda klassen med funktionen super(). Argumentet self utelämnas från metodanropet, eftersom Python lägger till det automatiskt.

Men vad händer om attributen inte är identiska; kan vi fortfarande använda basklassens konstruktor på något sätt? Låt oss titta på en klass som heter Avhandling och som ärver klassen Bok. Den härledda klassen kan fortfarande anropa konstruktören från basklassen:

\nclass Bok:\n    \"\"\" Klassen modellerar en enkel bok \"\"\"\n\n    def __init__(self, namn: str, forfattare: str):\n        self.namn = namn\n        self.forfattare = forfattare\n\n\nclass Avhandling(Bok):\n    \"\"\" Klassen modellerar en magisteravhandling \"\"\"\n\n    def __init__(self, namn: str, forfattare: str, vitsord: int):\n        super().__init__(namn, forfattare)\n        self.vitsord = vitsord\n

Konstruktorn i Avhandling-klassen anropar konstruktorn i basklassen Bok med argumenten för namn och forfattare. Dessutom anger konstruktören i den härledda klassen värdet för attributet vitsord. Detta kan naturligtvis inte vara en del av basklassens konstruktor, eftersom basklassen inte har något sådant attribut.

Ovanstående klass kan användas på följande sätt:

\n# Testar\nif __name__ == \"__main__\":\n    avhandling = Avhandling(\"Python och Universum\", \"Peter Python\", 3)\n\n    # Skriv ut attributens värden\n    print(avhandling.namn)\n    print(avhandling.forfattare)\n    print(avhandling.vitsord)\n

Python och Universum\nPeter Python\n3

Även om en härledd klass överstyr en metod i sin basklass kan den härledda klassen fortfarande anropa den åsidosatta metoden i basklassen. I följande exempel har vi ett grundläggande Bonuskort och ett särskilt Platinumkort för särskilt lojala kunder. Metoden rakna_bonus är åsidosatt i den härledda klassen, men den åsidosatta metoden anropar basmetoden:

\nclass Produkt:\n\n    def __init__(self, namn: str, pris: float):\n        self.namn = namn\n        self.pris = pris\n\nclass Bonuskort:\n\n    def __init__(self):\n        self.kopta_produkter = []\n\n    def tillsatt_produkt(self, produkt: Produkt):\n        self.kopta_produkter.append(produkt)\n\n    def rakna_bonus(self):\n        bonus = 0\n        for produkt in self.kopta_produkter:\n            bonus += produkt.pris * 0.05\n\n        return bonus\n\nclass Platinumkort(Bonuskort):\n\n    def __init__(self):\n        super().__init__()\n\n    def rakna_bonus(self):\n        # Anropar metoden i basklassen...\n        bonus = super().rakna_bonus()\n\n        # ...och tillsätter ännu fem procent till totalet\n        bonus = bonus * 1.05\n        return bonus\n\n

Bonusen för ett Platinumkort beräknas alltså genom att anropa den överstyrda metoden i basklassen och sedan lägga till 5 procent extra till basresultatet. Ett exempel på hur dessa klasser används:

if __name__ == \"__main__\":\n    kort = Bonuskort()\n    kort.tillsatt_produkt(Produkt(\"Bananer\", 6.50))\n    kort.tillsatt_produkt(Produkt(\"Mandariner\", 7.95))\n    bonus = kort.rakna_bonus()\n\n    kort2 = Platinumkort()\n    kort2.tillsatt_produkt(Produkt(\"Bananer\", 6.50))\n    kort2.tillsatt_produkt(Produkt(\"Mandariner\", 7.95))\n    bonus2 = kort2.rakna_bonus()\n\n    print(bonus)\n    print(bonus2)

0.7225\n0.7586250000000001

I uppgiftsbotten finns en klassdefinition för en Dator, som har attributen modell och snabbhet.

Skapa klassen BarbarDator, som ärver klassen Dator. Den nya klassens konstruktor ska ta ett tredgje argument, vikt, av typen heltal.

Skapa också en __str__-metod i klassdefinitionen. Se exemplet nedan för det förväntade formatet av strängen som skrivs ut.

Exempel:

barbar = BarbarDator(\"NoteBook Pro15\", 1500, 2)\nprint(barbar)

NoteBook Pro15, 1500 MHz, 2 kg

I uppgiftsbotten finns klassdefinitioner för Datorspel och Spelforrad. Spelforrads-objekt används för att förvara Datorspel-objekt.

Bekanta dig med dessa klasser. Definiera sedan klassen Spelmuseum, som ärver klassen Spelforrad.

Spelmuseum-klassen ska överstyra metoden lista_spel(), så att den returnerar en lista av endast de spel som är gjorda innan år 1990.

Dessutom ska den nya klassen ha en konstruktor som anropar konstruktorn från överklassen Spelforrad. Konstruktorn tar inga argument.

Exempel:

museum = Spelmuseum()\nmuseum.tillsatt_spel(Datorspel(\"Pacman\", \"Namco\", 1980))\nmuseum.tillsatt_spel(Datorspel(\"GTA 2\", \"Rockstar\", 1999))\nmuseum.tillsatt_spel(Datorspel(\"Bubble Bobble\", \"Taito\", 1986))\nfor spel in museum.lista_spel():\n    print(spel.namn)

Pacman\nBubble Bobble

I uppgiftsbotten finns en klassdefinition för Rektangel som representerar en rektangelform. Klassen används på följande sätt:

rektangel = Rektangel(2, 3)\nprint(rektangel)\nprint(\"area:\", rektangel.area())

rektangel 2x3\narea: 6

Kvadrat

Skapa en klass Kvadrat som ärver klassen Rektangel. Sidorna på en kvadrat har alla samma längd, alltså är den en speciell sort av rektangel. Den nya klassen ska inte innehålla nya attribut!

Kvadrat-objekt används på följande sätt:

kvadrat = Kvadrat(4)\nprint(kvadrat)\nprint(\"area:\", kvadrat.area())

kvadrat 4x4\narea: 16

Uppgiftsbotten innehåller klassdefinitionen för en Ordspel, som erbjuder enkel funktionalitet för att spela olika ordbaserade spel:

import random\n\nclass Ordspel():\n    def __init__(self, rundor: int):\n        self.vinster1 = 0\n        self.vinster2 = 0\n        self.rundor = rundor\n\n    def rundans_vinnare(self, spelare1_ord: str, spelare2_ord: str):\n        # vi lottar ut en vinnare\n        return random.randint(1, 2)\n\n    def spela(self):\n        print(\"Ordspel:\")\n        for i in range(1, self.rundor+1):\n            print(f\"runda {i}\")\n            svar1 = input(\"spelare1: \")\n            svar2 = input(\"spelare2: \")\n\n            if self.rundans_vinnare(svar1, svar2) == 1:\n                self.vinster1 += 1\n                print(\"spelare 1 vann\")\n            elif self.rundans_vinnare(svar1, svar2) == 2:\n                self.vinster2 += 1\n                print(\"spelare 2 vann\")\n            else:\n                pass # oavgjort\n\n        print(\"spelet är över, vinster:\")\n        print(f\"spelare 1: {self.vinster1}\")\n        print(f\"spelare 2: {self.vinster2}\")

Spelet spelas enligt följande:

s = Ordspel(3)\ns.spela()

Exempel:

Ordspel:\nrunda 1\nspelare1: långtord\nspelare2: ??\nspelare 2 vann\nrunda 2\nspelare1: jag är bäst\nspelare2: va?\nspelare 1 vann\nrunda 3\nspelare1: vem vinner\nspelare2: jag\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 2\nspelare 2: 1

I denna ”grundläggande” version av spelet avgörs vinnaren slumpmässigt. Spelarnas insatser har ingen inverkan på resultatet.

Längsta ordet vinner

Definiera en klass som heter LangstaOrdet. Det är en version av spelet där den som skriver in det längsta ordet i varje omgång vinner.

Den nya versionen av spelet implementeras genom att ärva Ordspel-klassen. Metoden rundans_vinnare bör också överstyras på lämpligt sätt. Den nya klassen är uppbyggd på följande sätt:

class LangstaOrdet(Ordspel):\n    def __init__(self, rundor: int):\n        super().__init__(rundor)\n\n    def rundans_vinnare(self, spelare1_ord: str, spelare2_ord: str):\n        # koden som avgör vinnaren här

Exempel på hur spelet fungerar:

s = LangstaOrdet(3)\ns.spela()

Ordspel:\nrunda 1\nspelare1: kort\nspelare2: långtord\nspelare 2 vann\nrunda 2\nspelare1: ord\nspelare2: va?\nrunda 3\nspelare1: jag är bäst\nspelare2: nej jag!\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 1\nspelare 2: 1

Flest vokaler vinner

Definiera en annan Ordspel-klass med namnet FlestVokaler. I den här versionen av spelet vinner den som har klämt in flest vokaler i sitt ord.

Sten sax påse

Definiera slutligen klassen StenSaxPase som låter dig spela ett spel av Sten, sax, påse.

Spelets regler är följande:

    \n
  • sten vinner påse (stenen kan förstöra saxen men saxen kan inte klippa stenen)
  • \n
  • påse vinner sten (påsen kan omsluta stenen)
  • \n
  • sax vinner påse (saxen kan klippa påsen)
  • \n

Om inmatningen från en spelare är ogiltig förlorar den omgången. Om båda spelarna skriver in något annat än sten, sax eller påse blir resultatet oavgjort.

Exempel på hur spelet spelas:

s = StenSaxPase(4)\ns.spela()

Ordspel:\nrunda 1\nspelare1: sten\nspelare2: sten\nrunda 2\nspelare1: sten\nspelare2: påse\nspelare 2 vann\nrunda 3\nspelare1: sax\nspelare2: påse\nspelare 1 vann\nrunda 4\nspelare1: påse\nspelare2: dynamit\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 2\nspelare 2: 1

","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/1-klasshierarkier.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad arv betyder i programmeringssammanhang"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva klasser som ärver andra klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur arv påverkar egenskaperna i klasser"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"specialklasser-for-speciella-andamal","style":"position:relative;"},"children":[{"type":"text","value":"Specialklasser för speciella ändamål"},{"type":"element","tagName":"a","properties":{"href":"#specialklasser-for-speciella-andamal","ariaLabel":"specialklasser for speciella andamal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland stöter man på en situation där man redan har definierat en klass, men sedan inser att man behöver speciella egenskaper i vissa, men inte alla, instanser av klassen. Och ibland inser man att man har definierat två stycken mycket liknande klasser med bara små skillnader. Som programmerare strävar vi efter att alltid upprepa oss så lite som möjligt, medan vi behåller tydlighet och läsbarhet. Så hur kan vi ta hänsyn till olika implementeringar av liknande objekt?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på två klassdefinitioner: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Larare"}]},{"type":"text","value":". Getter- och sättar-metoder har utelämnats tills vidare för att hålla exemplet kort."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Larare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" larande_ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rum\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"larande_ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" larande_ar\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även i ett avskalat exempel som ovan har vi redan en hel del upprepningar: båda klasserna innehåller attributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"epost"}]},{"type":"text","value":". Det vore en bra idé att ha en enda attributdefinition, så att det räcker med en enda funktion för att redigera båda attributen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tänk dig till exempel att skolans e-postadress ändras. Alla adresser skulle behöva uppdateras. Vi skulle kunna skriva två separata versioner av i stort sett samma funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"uppdatera_epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".edu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"uppdatera_epost2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Larare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".edu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skriva i stort sett samma sak två gånger är onödig upprepning, för att inte tala om att det fördubblar möjligheterna till fel. Det skulle vara en klar förbättring om vi kunde använda en enda funktion för att arbeta med instanser av båda klasserna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Båda klasserna har också attribut som är unika för dem. Att bara kombinera alla attribut i en enda klass skulle innebära att alla instanser av klassen då skulle ha onödiga attribut, bara olika för olika instanser. Det verkar inte heller vara en idealisk situation."}]},{"type":"element","tagName":"h2","properties":{"id":"arv","style":"position:relative;"},"children":[{"type":"text","value":"Arv"},{"type":"element","tagName":"a","properties":{"href":"#arv","ariaLabel":"arv permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objektorienterade programmeringsspråk innehåller vanligtvis en teknik som kallas arv (eng. inheritance). En klass kan ärva egenskaper från en annan klass. Förutom dessa ärvda egenskaper kan en klass också innehålla egenskaper som är unika för den."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med detta i åtanke är det rimligt att klasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Larare"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" har en gemensam bas eller föräldraklass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den nya klassen innehåller de egenskaper som delas av de andra två klasserna. Nu kan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Larare"}]},{"type":"text","value":" ärva dessa egenskaper och dessutom lägga till sina egna. :"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Syntaxen för arv innebär helt enkelt att basklassens namn läggs till inom parentes på rubrikraden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"uppdatera_epost_doman"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ny_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n gammal_doman "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"gammal_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ny_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Larare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" larande_ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rum\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"larande_ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" larande_ar\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Test"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n sam "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sam@example.com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sam"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppdatera_epost_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.edu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sam"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lars "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Larare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lars Lärare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lars@example.fi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppdatera_epost_doman"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.ex\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Både "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Larare"}]},{"type":"text","value":" ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":", så båda har de egenskaper som definieras i klassen Person, inklusive metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uppdatera_epost_doman"}]},{"type":"text","value":". Samma metod fungerar för instanser av båda de härledda klasserna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett annat exempel. Vi har en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"text","value":" som ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BokLada"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en enkel bok \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" forfattare\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BokLada"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en låda för böcker \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bocker "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"lista_bocker"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BokLada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en hylla för böcker \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" paikka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"paikka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"text","value":" innehåller metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"text","value":". En metod med samma namn finns definierad i basklassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BokLada"}]},{"type":"text","value":". Detta kallas överstyrning (eng. overriding): om en härledd klass har en metod med samma namn som basklassen, överstyr den härledda versionen originalet i instanser av den härledda klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tanken i exemplet ovan är att en ny bok som läggs till i en Bok låda alltid hamnar högst upp, men med en Bokhylla kan du själv ange platsen. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_bocker"}]},{"type":"text","value":" fungerar likadant för båda klasserna, eftersom det inte finns någon överordnad metod i den härledda klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss prova dessa klasser:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar några böcker för testning"}]},{"type":"text","value":"\n b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7 bröder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Aleksis Kivi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sinuhe\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mika Waltari\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Okänd soldat\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Väinö Linna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar en BokLada och tillsätter böckerna"}]},{"type":"text","value":"\n lada "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BokLada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar en Bokhylla och tillsätter böckerna (alltid till början av hyllan)"}]},{"type":"text","value":"\n hylla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bokhylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriver ut"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"I lådan:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lista_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"I hyllan:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lista_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"text","value":" "},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I lådan:\n7 bröder (Aleksis Kivi)\nSinuhe (Mika Waltari)\nOkänd soldat (Väinö Linna)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I hyllan:\nOkänd soldat (Väinö Linna)\nSinuhe (Mika Waltari)\n7 bröder (Aleksis Kivi)"}]},{"type":"text","value":" "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen Bokhylla har alltså också tillgång till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_bocker"}]},{"type":"text","value":". Genom ärvning är metoden medlem i alla klasser som kommer från klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BokLada"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"arv-och-rackvidd-av-egenskaper","style":"position:relative;"},"children":[{"type":"text","value":"Arv och räckvidd av egenskaper"},{"type":"element","tagName":"a","properties":{"href":"#arv-och-rackvidd-av-egenskaper","ariaLabel":"arv och rackvidd av egenskaper permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En härledd klass ärver alla egenskaper från sin basklass. Dessa egenskaper är direkt åtkomliga i den härledda klassen, såvida de inte har definierats som privata i basklassen (med två understreck före egenskapens namn)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom attributen för en Bokhylla är identiska med en BokLada, fanns det ingen anledning att skriva om konstruktorn för Bokhylla. Vi anropade helt enkelt basklassens konstruktor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BokLada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla egenskaper i basklassen kan nås från den härledda klassen med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"super()"}]},{"type":"text","value":". Argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" utelämnas från metodanropet, eftersom Python lägger till det automatiskt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men vad händer om attributen inte är identiska; kan vi fortfarande använda basklassens konstruktor på något sätt? Låt oss titta på en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Avhandling"}]},{"type":"text","value":" och som ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":". Den härledda klassen kan fortfarande anropa konstruktören från basklassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en enkel bok \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" forfattare\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Avhandling"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en magisteravhandling \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vitsord\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstruktorn i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Avhandling"}]},{"type":"text","value":"-klassen anropar konstruktorn i basklassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":" med argumenten för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forfattare"}]},{"type":"text","value":". Dessutom anger konstruktören i den härledda klassen värdet för attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vitsord"}]},{"type":"text","value":". Detta kan naturligtvis inte vara en del av basklassens konstruktor, eftersom basklassen inte har något sådant attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovanstående klass kan användas på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n avhandling "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Avhandling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python och Universum\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut attributens värden"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"avhandling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"avhandling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"avhandling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python och Universum\nPeter Python\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om en härledd klass överstyr en metod i sin basklass kan den härledda klassen fortfarande anropa den åsidosatta metoden i basklassen. I följande exempel har vi ett grundläggande "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bonuskort"}]},{"type":"text","value":" och ett särskilt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Platinumkort"}]},{"type":"text","value":" för särskilt lojala kunder. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rakna_bonus"}]},{"type":"text","value":" är åsidosatt i den härledda klassen, men den åsidosatta metoden anropar basmetoden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bonuskort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kopta_produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kopta_produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kopta_produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.05"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bonus\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Platinumkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Bonuskort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Anropar metoden i basklassen..."}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rakna_bonus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ...och tillsätter ännu fem procent till totalet"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.05"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bonus\n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bonusen för ett Platinumkort beräknas alltså genom att anropa den överstyrda metoden i basklassen och sedan lägga till 5 procent extra till basresultatet. Ett exempel på hur dessa klasser används:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bonuskort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bananer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mandariner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rakna_bonus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n kort2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Platinumkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kort2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bananer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kort2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mandariner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bonus2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kort2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rakna_bonus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bonus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bonus2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0.7225\n0.7586250000000001"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bärbar dator","tmcname":"osa10-01_barbar_dator"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns en klassdefinition för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Dator"}]},{"type":"text","value":", som har attributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"modell"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"snabbhet"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BarbarDator"}]},{"type":"text","value":", som "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ärver"}]},{"type":"text","value":" klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Dator"}]},{"type":"text","value":". Den nya klassens konstruktor ska ta ett tredgje argument, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vikt"}]},{"type":"text","value":", av typen heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa också en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod i klassdefinitionen. Se exemplet nedan för det förväntade formatet av strängen som skrivs ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"barbar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BarbarDator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"NoteBook Pro15\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"barbar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NoteBook Pro15, 1500 MHz, 2 kg"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spelmuseum","tmcname":"osa10-02_spelmuseum"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns klassdefinitioner för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Datorspel"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Spelforrad"}]},{"type":"text","value":". Spelforrads-objekt används för att förvara Datorspel-objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bekanta dig med dessa klasser. Definiera sedan klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Spelmuseum"}]},{"type":"text","value":", som ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Spelforrad"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelmuseum-klassen ska "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"överstyra"}]},{"type":"text","value":" metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_spel()"}]},{"type":"text","value":", så att den returnerar en lista av endast de spel som är gjorda innan år 1990."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom ska den nya klassen ha en konstruktor som "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"anropar konstruktorn från överklassen Spelforrad"}]},{"type":"text","value":". Konstruktorn tar inga argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"museum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Spelmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Datorspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pacman\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namco\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1980"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Datorspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"GTA 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rockstar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1999"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Datorspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bubble Bobble\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Taito\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1986"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" spel "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" museum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lista_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pacman\nBubble Bobble"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Arean","tmcname":"osa10-03_arean"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns en klassdefinition för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":" som representerar en "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Rektangel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"rektangelform"}]},{"type":"text","value":". Klassen används på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"area:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"rektangel 2x3\narea: 6"}]}]},{"type":"element","tagName":"h2","properties":{"id":"kvadrat","style":"position:relative;"},"children":[{"type":"text","value":"Kvadrat"},{"type":"element","tagName":"a","properties":{"href":"#kvadrat","ariaLabel":"kvadrat permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kvadrat"}]},{"type":"text","value":" som ärver klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":". Sidorna på en "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Kvadrat","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"kvadrat"}]},{"type":"text","value":" har alla samma längd, alltså är den en speciell sort av rektangel. Den nya klassen ska inte innehålla nya attribut!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kvadrat-objekt används på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kvadrat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"area:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"kvadrat 4x4\narea: 16"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ordspel","tmcname":"osa10-04_ordspel"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbotten innehåller klassdefinitionen för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Ordspel"}]},{"type":"text","value":", som erbjuder enkel funktionalitet för att spela olika ordbaserade spel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" random\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordspel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rundor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rundor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rundor\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rundans_vinnare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare1_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare2_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi lottar ut en vinnare"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" random"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spela"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ordspel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rundor"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"runda "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n svar1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelare1: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n svar2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelare2: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rundans_vinnare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svar1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelare 1 vann\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rundans_vinnare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svar1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelare 2 vann\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# oavgjort"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spelet är över, vinster:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"spelare 1: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"spelare 2: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vinster2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelet spelas enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Ordspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordspel:\nrunda 1\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"långtord"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"??"}]},{"type":"text","value":"\nspelare 2 vann\nrunda 2\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jag är bäst"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"va?"}]},{"type":"text","value":"\nspelare 1 vann\nrunda 3\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"vem vinner"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jag"}]},{"type":"text","value":"\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 2\nspelare 2: 1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna ”grundläggande” version av spelet avgörs vinnaren slumpmässigt. Spelarnas insatser har ingen inverkan på resultatet."}]},{"type":"element","tagName":"h2","properties":{"id":"langsta-ordet-vinner","style":"position:relative;"},"children":[{"type":"text","value":"Längsta ordet vinner"},{"type":"element","tagName":"a","properties":{"href":"#langsta-ordet-vinner","ariaLabel":"langsta ordet vinner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LangstaOrdet"}]},{"type":"text","value":". Det är en version av spelet där den som skriver in det längsta ordet i varje omgång vinner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den nya versionen av spelet implementeras genom att ärva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Ordspel"}]},{"type":"text","value":"-klassen. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rundans_vinnare"}]},{"type":"text","value":" bör också överstyras på lämpligt sätt. Den nya klassen är uppbyggd på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LangstaOrdet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Ordspel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rundor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rundor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rundans_vinnare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare1_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare2_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# koden som avgör vinnaren här"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur spelet fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LangstaOrdet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordspel:\nrunda 1\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kort"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"långtord"}]},{"type":"text","value":"\nspelare 2 vann\nrunda 2\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"va?"}]},{"type":"text","value":"\nrunda 3\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jag är bäst"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej jag!"}]},{"type":"text","value":"\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 1\nspelare 2: 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"flest-vokaler-vinner","style":"position:relative;"},"children":[{"type":"text","value":"Flest vokaler vinner"},{"type":"element","tagName":"a","properties":{"href":"#flest-vokaler-vinner","ariaLabel":"flest vokaler vinner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en annan Ordspel-klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FlestVokaler"}]},{"type":"text","value":". I den här versionen av spelet vinner den som har klämt in flest vokaler i sitt ord."}]},{"type":"element","tagName":"h2","properties":{"id":"sten-sax-pase","style":"position:relative;"},"children":[{"type":"text","value":"Sten sax påse"},{"type":"element","tagName":"a","properties":{"href":"#sten-sax-pase","ariaLabel":"sten sax pase permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera slutligen klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StenSaxPase"}]},{"type":"text","value":" som låter dig spela ett spel av "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Sten,_sax,_p%C3%A5se","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Sten, sax, påse"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelets regler är följande:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"sten vinner påse (stenen kan förstöra saxen men saxen kan inte klippa stenen)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"påse vinner sten (påsen kan omsluta stenen)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"sax vinner påse (saxen kan klippa påsen)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om inmatningen från en spelare är ogiltig förlorar den omgången. Om båda spelarna skriver in något annat än sten, sax eller påse blir resultatet oavgjort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur spelet spelas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" StenSaxPase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordspel:\nrunda 1\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sten"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sten"}]},{"type":"text","value":"\nrunda 2\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sten"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"påse"}]},{"type":"text","value":"\nspelare 2 vann\nrunda 3\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sax"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"påse"}]},{"type":"text","value":"\nspelare 1 vann\nrunda 4\nspelare1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"påse"}]},{"type":"text","value":"\nspelare2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dynamit"}]},{"type":"text","value":"\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 2\nspelare 2: 1"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad arv betyder i programmeringssammanhang
  • \n
  • Kommer du att kunna skriva klasser som ärver andra klasser
  • \n
  • Vet du hur arv påverkar egenskaperna i klasser
  • \n

Specialklasser för speciella ändamål

Ibland stöter man på en situation där man redan har definierat en klass, men sedan inser att man behöver speciella egenskaper i vissa, men inte alla, instanser av klassen. Och ibland inser man att man har definierat två stycken mycket liknande klasser med bara små skillnader. Som programmerare strävar vi efter att alltid upprepa oss så lite som möjligt, medan vi behåller tydlighet och läsbarhet. Så hur kan vi ta hänsyn till olika implementeringar av liknande objekt?

Låt oss ta en titt på två klassdefinitioner: Studerande och Larare. Getter- och sättar-metoder har utelämnats tills vidare för att hålla exemplet kort.

\nclass Studerande:\n\n    def __init__(self, namn: str, id: str, epost: str, studiepoang: str):\n        self.namn = namn\n        self.id = id\n        self.epost = epost\n        self.studiepoang = studiepoang\n\nclass Larare:\n\n    def __init__(self, namn: str, epost: str, rum: str, larande_ar: int):\n        self.namn = namn\n        self.epost = epost\n        self.rum = rum\n        self.larande_ar = larande_ar\n

Även i ett avskalat exempel som ovan har vi redan en hel del upprepningar: båda klasserna innehåller attributen namn och epost. Det vore en bra idé att ha en enda attributdefinition, så att det räcker med en enda funktion för att redigera båda attributen.

Tänk dig till exempel att skolans e-postadress ändras. Alla adresser skulle behöva uppdateras. Vi skulle kunna skriva två separata versioner av i stort sett samma funktion:

\ndef uppdatera_epost(s: Studerande):\n    s.epost = s.epost.replace(\".com\", \".edu\")\n\ndef uppdatera_epost2(s: Larare):\n    s.epost = s.epost.replace(\".com\", \".edu\")\n

Att skriva i stort sett samma sak två gånger är onödig upprepning, för att inte tala om att det fördubblar möjligheterna till fel. Det skulle vara en klar förbättring om vi kunde använda en enda funktion för att arbeta med instanser av båda klasserna.

Båda klasserna har också attribut som är unika för dem. Att bara kombinera alla attribut i en enda klass skulle innebära att alla instanser av klassen då skulle ha onödiga attribut, bara olika för olika instanser. Det verkar inte heller vara en idealisk situation.

Arv

Objektorienterade programmeringsspråk innehåller vanligtvis en teknik som kallas arv (eng. inheritance). En klass kan ärva egenskaper från en annan klass. Förutom dessa ärvda egenskaper kan en klass också innehålla egenskaper som är unika för den.

Med detta i åtanke är det rimligt att klasserna Larare och Studerande har en gemensam bas eller föräldraklass Person:

\nclass Person:\n\n   def __init__(self, namn: str, epost: str):\n       self.namn = namn\n       self.epost = epost\n

Den nya klassen innehåller de egenskaper som delas av de andra två klasserna. Nu kan Studerande och Larare ärva dessa egenskaper och dessutom lägga till sina egna. :

Syntaxen för arv innebär helt enkelt att basklassens namn läggs till inom parentes på rubrikraden:

\nclass Person:\n\n   def __init__(self, namn: str, epost: str):\n       self.namn = namn\n       self.epost = epost\n\n   def uppdatera_epost_doman(self, ny_doman: str):\n       gammal_doman = self.epost.split(\"@\")[1]\n       self.epost = self.epost.replace(gammal_doman, ny_doman)\n\nclass Studerande(Person):\n\n   def __init__(self, namn: str, id: str, epost: str, studiepoang: str):\n       self.namn = namn\n       self.id = id\n       self.epost = epost\n       self.studiepoang = studiepoang\n\nclass Larare(Person):\n\n   def __init__(self, namn: str, epost: str, rum: str, larande_ar: int):\n       self.namn = namn\n       self.epost = epost\n       self.rum = rum\n       self.larande_ar = larande_ar\n\n# Test\nif __name__ == \"__main__\":\n   sam = Studerande(\"Sam Studerande\", \"1234\", \"sam@example.com\", 0)\n   sam.uppdatera_epost_doman(\"example.edu\")\n   print(sam.epost)\n\n   lars = Larare(\"Lars Lärare\", \"lars@example.fi\", \"A123\", 2)\n   lars.uppdatera_epost_doman(\"example.ex\")\n   print(lars.epost)\n

Både Studerande och Larare ärver klassen Person, så båda har de egenskaper som definieras i klassen Person, inklusive metoden uppdatera_epost_doman. Samma metod fungerar för instanser av båda de härledda klasserna.

Låt oss titta på ett annat exempel. Vi har en Bokhylla som ärver klassen BokLada:

class Bok:\n   \"\"\" Klassen modellerar en enkel bok \"\"\"\n   def __init__(self, namn: str, forfattare: str):\n       self.namn = namn\n       self.forfattare = forfattare\n\n\nclass BokLada:\n   \"\"\" Klassen modellerar en låda för böcker \"\"\"\n\n   def __init__(self):\n       self.bocker = []\n\n   def tillsatt_bok(self, bok: Bok):\n       self.bocker.append(bok)\n\n   def lista_bocker(self):\n       for bok in self.bocker:\n           print(f\"{bok.namn} ({bok.forfattare})\")\n\nclass Bokhylla(BokLada):\n   \"\"\" Klassen modellerar en hylla för böcker \"\"\"\n\n   def __init__(self):\n       super().__init__()\n\n   def tillsatt_bok(self, bok: Bok, paikka: int):\n       self.bocker.insert(paikka, bok)\n\n

Klassen Bokhylla innehåller metoden tillsatt_bok. En metod med samma namn finns definierad i basklassen BokLada. Detta kallas överstyrning (eng. overriding): om en härledd klass har en metod med samma namn som basklassen, överstyr den härledda versionen originalet i instanser av den härledda klassen.

Tanken i exemplet ovan är att en ny bok som läggs till i en Bok låda alltid hamnar högst upp, men med en Bokhylla kan du själv ange platsen. Metoden lista_bocker fungerar likadant för båda klasserna, eftersom det inte finns någon överordnad metod i den härledda klassen.

Låt oss prova dessa klasser:

\nif __name__ == \"__main__\":\n   # Vi skapar några böcker för testning\n   b1 = Bok(\"7 bröder\", \"Aleksis Kivi\")\n   b2 = Bok(\"Sinuhe\", \"Mika Waltari\")\n   b3 = Bok(\"Okänd soldat\", \"Väinö Linna\")\n\n   # Vi skapar en BokLada och tillsätter böckerna\n   lada = BokLada()\n   lada.tillsatt_bok(b1)\n   lada.tillsatt_bok(b2)\n   lada.tillsatt_bok(b3)\n\n   # Vi skapar en Bokhylla och tillsätter böckerna (alltid till början av hyllan)\n   hylla = Bokhylla()\n   hylla.tillsatt_bok(b1, 0)\n   hylla.tillsatt_bok(b2, 0)\n   hylla.tillsatt_bok(b3, 0)\n\n\n   # Skriver ut\n   print(\"I lådan:\")\n   lada.lista_bocker()\n\n   print()\n\n   print(\"I hyllan:\")\n   hylla.lista_bocker()\n

I lådan:\n7 bröder (Aleksis Kivi)\nSinuhe (Mika Waltari)\nOkänd soldat (Väinö Linna)

I hyllan:\nOkänd soldat (Väinö Linna)\nSinuhe (Mika Waltari)\n7 bröder (Aleksis Kivi)

Klassen Bokhylla har alltså också tillgång till metoden lista_bocker. Genom ärvning är metoden medlem i alla klasser som kommer från klassen BokLada.

Arv och räckvidd av egenskaper

En härledd klass ärver alla egenskaper från sin basklass. Dessa egenskaper är direkt åtkomliga i den härledda klassen, såvida de inte har definierats som privata i basklassen (med två understreck före egenskapens namn).

Eftersom attributen för en Bokhylla är identiska med en BokLada, fanns det ingen anledning att skriva om konstruktorn för Bokhylla. Vi anropade helt enkelt basklassens konstruktor:

\nclass Bokhylla(BokLada):\n\n   def __init__(self):\n       super().__init__()\n

Alla egenskaper i basklassen kan nås från den härledda klassen med funktionen super(). Argumentet self utelämnas från metodanropet, eftersom Python lägger till det automatiskt.

Men vad händer om attributen inte är identiska; kan vi fortfarande använda basklassens konstruktor på något sätt? Låt oss titta på en klass som heter Avhandling och som ärver klassen Bok. Den härledda klassen kan fortfarande anropa konstruktören från basklassen:

\nclass Bok:\n    \"\"\" Klassen modellerar en enkel bok \"\"\"\n\n    def __init__(self, namn: str, forfattare: str):\n        self.namn = namn\n        self.forfattare = forfattare\n\n\nclass Avhandling(Bok):\n    \"\"\" Klassen modellerar en magisteravhandling \"\"\"\n\n    def __init__(self, namn: str, forfattare: str, vitsord: int):\n        super().__init__(namn, forfattare)\n        self.vitsord = vitsord\n

Konstruktorn i Avhandling-klassen anropar konstruktorn i basklassen Bok med argumenten för namn och forfattare. Dessutom anger konstruktören i den härledda klassen värdet för attributet vitsord. Detta kan naturligtvis inte vara en del av basklassens konstruktor, eftersom basklassen inte har något sådant attribut.

Ovanstående klass kan användas på följande sätt:

\n# Testar\nif __name__ == \"__main__\":\n    avhandling = Avhandling(\"Python och Universum\", \"Peter Python\", 3)\n\n    # Skriv ut attributens värden\n    print(avhandling.namn)\n    print(avhandling.forfattare)\n    print(avhandling.vitsord)\n

Python och Universum\nPeter Python\n3

Även om en härledd klass överstyr en metod i sin basklass kan den härledda klassen fortfarande anropa den åsidosatta metoden i basklassen. I följande exempel har vi ett grundläggande Bonuskort och ett särskilt Platinumkort för särskilt lojala kunder. Metoden rakna_bonus är åsidosatt i den härledda klassen, men den åsidosatta metoden anropar basmetoden:

\nclass Produkt:\n\n    def __init__(self, namn: str, pris: float):\n        self.namn = namn\n        self.pris = pris\n\nclass Bonuskort:\n\n    def __init__(self):\n        self.kopta_produkter = []\n\n    def tillsatt_produkt(self, produkt: Produkt):\n        self.kopta_produkter.append(produkt)\n\n    def rakna_bonus(self):\n        bonus = 0\n        for produkt in self.kopta_produkter:\n            bonus += produkt.pris * 0.05\n\n        return bonus\n\nclass Platinumkort(Bonuskort):\n\n    def __init__(self):\n        super().__init__()\n\n    def rakna_bonus(self):\n        # Anropar metoden i basklassen...\n        bonus = super().rakna_bonus()\n\n        # ...och tillsätter ännu fem procent till totalet\n        bonus = bonus * 1.05\n        return bonus\n\n

Bonusen för ett Platinumkort beräknas alltså genom att anropa den överstyrda metoden i basklassen och sedan lägga till 5 procent extra till basresultatet. Ett exempel på hur dessa klasser används:

if __name__ == \"__main__\":\n    kort = Bonuskort()\n    kort.tillsatt_produkt(Produkt(\"Bananer\", 6.50))\n    kort.tillsatt_produkt(Produkt(\"Mandariner\", 7.95))\n    bonus = kort.rakna_bonus()\n\n    kort2 = Platinumkort()\n    kort2.tillsatt_produkt(Produkt(\"Bananer\", 6.50))\n    kort2.tillsatt_produkt(Produkt(\"Mandariner\", 7.95))\n    bonus2 = kort2.rakna_bonus()\n\n    print(bonus)\n    print(bonus2)

0.7225\n0.7586250000000001

I uppgiftsbotten finns en klassdefinition för en Dator, som har attributen modell och snabbhet.

Skapa klassen BarbarDator, som ärver klassen Dator. Den nya klassens konstruktor ska ta ett tredgje argument, vikt, av typen heltal.

Skapa också en __str__-metod i klassdefinitionen. Se exemplet nedan för det förväntade formatet av strängen som skrivs ut.

Exempel:

barbar = BarbarDator(\"NoteBook Pro15\", 1500, 2)\nprint(barbar)

NoteBook Pro15, 1500 MHz, 2 kg

I uppgiftsbotten finns klassdefinitioner för Datorspel och Spelforrad. Spelforrads-objekt används för att förvara Datorspel-objekt.

Bekanta dig med dessa klasser. Definiera sedan klassen Spelmuseum, som ärver klassen Spelforrad.

Spelmuseum-klassen ska överstyra metoden lista_spel(), så att den returnerar en lista av endast de spel som är gjorda innan år 1990.

Dessutom ska den nya klassen ha en konstruktor som anropar konstruktorn från överklassen Spelforrad. Konstruktorn tar inga argument.

Exempel:

museum = Spelmuseum()\nmuseum.tillsatt_spel(Datorspel(\"Pacman\", \"Namco\", 1980))\nmuseum.tillsatt_spel(Datorspel(\"GTA 2\", \"Rockstar\", 1999))\nmuseum.tillsatt_spel(Datorspel(\"Bubble Bobble\", \"Taito\", 1986))\nfor spel in museum.lista_spel():\n    print(spel.namn)

Pacman\nBubble Bobble

I uppgiftsbotten finns en klassdefinition för Rektangel som representerar en rektangelform. Klassen används på följande sätt:

rektangel = Rektangel(2, 3)\nprint(rektangel)\nprint(\"area:\", rektangel.area())

rektangel 2x3\narea: 6

Kvadrat

Skapa en klass Kvadrat som ärver klassen Rektangel. Sidorna på en kvadrat har alla samma längd, alltså är den en speciell sort av rektangel. Den nya klassen ska inte innehålla nya attribut!

Kvadrat-objekt används på följande sätt:

kvadrat = Kvadrat(4)\nprint(kvadrat)\nprint(\"area:\", kvadrat.area())

kvadrat 4x4\narea: 16

Uppgiftsbotten innehåller klassdefinitionen för en Ordspel, som erbjuder enkel funktionalitet för att spela olika ordbaserade spel:

import random\n\nclass Ordspel():\n    def __init__(self, rundor: int):\n        self.vinster1 = 0\n        self.vinster2 = 0\n        self.rundor = rundor\n\n    def rundans_vinnare(self, spelare1_ord: str, spelare2_ord: str):\n        # vi lottar ut en vinnare\n        return random.randint(1, 2)\n\n    def spela(self):\n        print(\"Ordspel:\")\n        for i in range(1, self.rundor+1):\n            print(f\"runda {i}\")\n            svar1 = input(\"spelare1: \")\n            svar2 = input(\"spelare2: \")\n\n            if self.rundans_vinnare(svar1, svar2) == 1:\n                self.vinster1 += 1\n                print(\"spelare 1 vann\")\n            elif self.rundans_vinnare(svar1, svar2) == 2:\n                self.vinster2 += 1\n                print(\"spelare 2 vann\")\n            else:\n                pass # oavgjort\n\n        print(\"spelet är över, vinster:\")\n        print(f\"spelare 1: {self.vinster1}\")\n        print(f\"spelare 2: {self.vinster2}\")

Spelet spelas enligt följande:

s = Ordspel(3)\ns.spela()

Exempel:

Ordspel:\nrunda 1\nspelare1: långtord\nspelare2: ??\nspelare 2 vann\nrunda 2\nspelare1: jag är bäst\nspelare2: va?\nspelare 1 vann\nrunda 3\nspelare1: vem vinner\nspelare2: jag\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 2\nspelare 2: 1

I denna ”grundläggande” version av spelet avgörs vinnaren slumpmässigt. Spelarnas insatser har ingen inverkan på resultatet.

Längsta ordet vinner

Definiera en klass som heter LangstaOrdet. Det är en version av spelet där den som skriver in det längsta ordet i varje omgång vinner.

Den nya versionen av spelet implementeras genom att ärva Ordspel-klassen. Metoden rundans_vinnare bör också överstyras på lämpligt sätt. Den nya klassen är uppbyggd på följande sätt:

class LangstaOrdet(Ordspel):\n    def __init__(self, rundor: int):\n        super().__init__(rundor)\n\n    def rundans_vinnare(self, spelare1_ord: str, spelare2_ord: str):\n        # koden som avgör vinnaren här

Exempel på hur spelet fungerar:

s = LangstaOrdet(3)\ns.spela()

Ordspel:\nrunda 1\nspelare1: kort\nspelare2: långtord\nspelare 2 vann\nrunda 2\nspelare1: ord\nspelare2: va?\nrunda 3\nspelare1: jag är bäst\nspelare2: nej jag!\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 1\nspelare 2: 1

Flest vokaler vinner

Definiera en annan Ordspel-klass med namnet FlestVokaler. I den här versionen av spelet vinner den som har klämt in flest vokaler i sitt ord.

Sten sax påse

Definiera slutligen klassen StenSaxPase som låter dig spela ett spel av Sten, sax, påse.

Spelets regler är följande:

    \n
  • sten vinner påse (stenen kan förstöra saxen men saxen kan inte klippa stenen)
  • \n
  • påse vinner sten (påsen kan omsluta stenen)
  • \n
  • sax vinner påse (saxen kan klippa påsen)
  • \n

Om inmatningen från en spelare är ogiltig förlorar den omgången. Om båda spelarna skriver in något annat än sten, sax eller påse blir resultatet oavgjort.

Exempel på hur spelet spelas:

s = StenSaxPase(4)\ns.spela()

Ordspel:\nrunda 1\nspelare1: sten\nspelare2: sten\nrunda 2\nspelare1: sten\nspelare2: påse\nspelare 2 vann\nrunda 3\nspelare1: sax\nspelare2: påse\nspelare 1 vann\nrunda 4\nspelare1: påse\nspelare2: dynamit\nspelare 1 vann\nspelet är över, vinster:\nspelare 1: 2\nspelare 2: 1

","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/1-klasshierarkier.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-10/2-atkamstmodifierare/page-data.json b/page-data/osa-10/2-atkamstmodifierare/page-data.json index 2b17db060..d084846a5 100644 --- a/page-data/osa-10/2-atkamstmodifierare/page-data.json +++ b/page-data/osa-10/2-atkamstmodifierare/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-10/2-atkamstmodifierare", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att förstå åtkomstmodifierarna privat och skyddad"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur synligheten för egenskaper bestäms i Python"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en egenskap definieras som privat i basklassen är den inte direkt åtkomlig i några härledda klasser, liksom kort nämndes i föregående avsnitt. Låt oss ta en titt på ett exempel. I klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Anteckningsbok"}]},{"type":"text","value":" nedan lagras anteckningarna i en lista, och listattributet är privat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Anteckningsbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" En Anteckningsbok förvarar anteckningar i strängformat \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# privat attribut"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alla_anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om klassens integritet är viktig är det vettigt att göra listattributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"anteckningar"}]},{"type":"text","value":" privat. Klassen förser trots allt klienten med lämpliga metoder för att lägga till och bläddra i anteckningar. Detta tillvägagångssätt blir problematiskt om vi definierar en ny klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"AnteckningsbokPro"}]},{"type":"text","value":", som ärver "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Anteckningsbok"}]},{"type":"text","value":"-klassen. Det privata listattributet är inte tillgängligt för klienten, men det är inte heller tillgängligt för de härledda klasserna. Om vi försöker komma åt det, som i metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_anteckningar"}]},{"type":"text","value":" nedan, får vi ett felmeddelande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"AnteckningsbokPro"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Anteckningsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" En bättre Anteckningsbok med sökfunktionalitet \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta är ok, eftersom konstruktorn är offentlig trots understrykning"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta orsakar ett fel"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hitta_anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sokord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Attributet __anteckningar är privat, den härledda"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# klassen kan inte komma åt den direkt"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" anteckning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" sokord "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" hittade\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'AnteckningsbokPro' object has no attribute '_AnteckningsbokPro__anteckningar'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"skyddade-egenskaper","style":"position:relative;"},"children":[{"type":"text","value":"Skyddade egenskaper"},{"type":"element","tagName":"a","properties":{"href":"#skyddade-egenskaper","ariaLabel":"skyddade egenskaper permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Många objektorienterade programmeringsspråk har en funktion, oftast ett speciellt nyckelord, för att skydda egenskaper. Detta innebär att en egenskap ska vara dold för klassens klienter, men hållas tillgänglig för dess underklasser. Python avskyr i allmänhet nyckelord, så ingen sådan funktion är direkt tillgänglig i Python. Istället finns det en konvention för att markera skyddade egenskaper på ett visst sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att en egenskap kan döljas genom att prefixera dess namn med två understreck:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den överenskomna konventionen för att skydda en egenskap är att prefixera namnet med endast ett understreck. Nu är detta bara en konvention. Ingenting hindrar en programmerare från att bryta mot konventionen, men det anses vara en dålig programmeringspraxis."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan har vi hela Anteckningsbok-exemplet, med skyddade "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"_anteckningar"}]},{"type":"text","value":" istället för privata "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__anteckningar"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Anteckningsbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" En Anteckningsbok förvarar anteckningar i strängformat \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skyddade attribut"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alla_anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"AnteckningsbokPro"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Anteckningsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" En bättre Anteckningsbok med sökfunktionalitet \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta är ok, eftersom konstruktorn är offentlig trots understrykning"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Nu fungerar metoden, eftersom den skyddadde attributen är"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ankomstbar till den härledda klassen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hitta_anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sokord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" anteckning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" sokord "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" hittade\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan har vi en praktisk tabell för synligheten av attribut med olika åtkomstmodifierare:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Åtkomstmodifierare"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Exempel"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Synlig till klienten"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Synlig till härledd klass"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Offentlig"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.namn"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"ja"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"ja"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Skyddad"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self._namn"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"nej"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"ja"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Privat"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.__namn"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"nej"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"nej"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Åtkomstmodifierare fungerar på samma sätt med alla egenskaper. I klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" nedan har vi till exempel den skyddade metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"versalisera_initialer"}]},{"type":"text","value":" Den kan användas från den härledda klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fotbollsspelare"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_versalisera_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"_versalisera_initialer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn_versaliserat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn_versaliserat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn_versaliserat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Fotbollsspelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" smeknamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" position"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metoden är ankomstbar eftersom den är skyddad i basklassen"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__smeknamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_versalisera_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"smeknamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__position "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" position\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n r "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Fotbollsspelare - namn:"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", smeknamn: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__smeknamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n r "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\", position: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__position"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" r\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar klasserna"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fotbollsspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"putte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"anfallare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fotbollsspelare - namn:Peter Pythonson, smeknamn: Putte, position: anfallare"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Supergrupp","tmcname":"osa10-05_supergrupp"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns färdigt klassdefinitionen för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Superhjalte"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Supergrupp"}]},{"type":"text","value":", som representerar en grupp av superhjältar. Klassen ska innehålla följande:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Skyddade"}]},{"type":"text","value":" attributen namn (sträng), lokation (sträng) och medlemmar (lista)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"En konstruktor, som får namn och lokation av gruppen som argument, i den ordningen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Getter-metoden för namn- och lokation-attributen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_medlem(hjalte: Superhjalte)"}]},{"type":"text","value":", som lägger till en ny medlem till gruppen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_grupp"}]},{"type":"text","value":", som skriver ut information om gruppen och dess medlemmer, enligt formatet nedan"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel av klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"superperson "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Superhjalte"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SuperPerson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Supersnabbhet, superstyrka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nosynlig "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Superhjalte"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Osynliga Olle\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Osynlighet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Supergrupp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Revengers\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Åland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_medlem"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"superperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_medlem"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"osynlig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_grupp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Revengers, Åland\nMedlemmar:\nSuperPerson, specialförmågor: Supersnabbhet, superstyrka\nOsynliga Olle, specialförmågor: Osynlighet"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/3-inkapsling","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Detta"}]},{"type":"text","value":" kapitel kan vara användbart."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hemlig trolldryck","tmcname":"osa10-06_hemlig_trolldryck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller klassdefinitionen för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Trolldryck"}]},{"type":"text","value":" som gör att du kan spara ett recept på en trolldryck. Klassdefinitionen innehåller en konstruktor tillsammans med metoderna"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_ingrediens(ingrediens: str, mangd: float)"}]},{"type":"text","value":" och"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_recept()"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HemligTrolldryck"}]},{"type":"text","value":" som ärver "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Trolldryck"}]},{"type":"text","value":"-klassen och som gör att du också kan skydda receptet med ett lösenord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den nya klassen ska ha en konstruktor som dessutom tar en lösenordssträng som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska dessutom innehålla följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_ingrediens(ingrediens: str, mangd: float, losenord: str)"}]},{"type":"text","value":" och"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_recept(losenord: str)"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om lösenordsargumentet som ges till någon av dessa metoder är felaktigt ska metoderna ge upphov till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om lösenordet är korrekt ska varje metod anropa den relevanta metoden i den överordnade klassen. Kopiera och klistra inte in något från "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Trolldryck"}]},{"type":"text","value":"-klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur detta ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"krympning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" HemligTrolldryck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Krympning maximus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ingrediens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Granrot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ingrediens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Magisk sand\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ingrediens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grodyngel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pocushocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# FEL LÖSENORD"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Krympning maximus:\nGranrot 1.5 gram\nMagisk sand 3.0 gram\nGrodyngel 4.0 gram\nTraceback (most recent call last):\nFile \"hemlig_trolldryck.py\", line 98, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nraise ValueError(\"Fel lösenord!\")\nValueError: Fel lösenord!"}]}]}]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att förstå åtkomstmodifierarna privat och skyddad
  • \n
  • Vet du hur synligheten för egenskaper bestäms i Python
  • \n

Om en egenskap definieras som privat i basklassen är den inte direkt åtkomlig i några härledda klasser, liksom kort nämndes i föregående avsnitt. Låt oss ta en titt på ett exempel. I klassen Anteckningsbok nedan lagras anteckningarna i en lista, och listattributet är privat:

\nclass Anteckningsbok:\n    \"\"\" En Anteckningsbok förvarar anteckningar i strängformat \"\"\"\n\n    def __init__(self):\n        # privat attribut\n        self.__anteckningar = []\n\n    def tillsatt_anteckning(self, anteckning):\n        self.__anteckningar.append(anteckning)\n\n    def hamta_anteckning(self, index):\n        return self.__anteckningar[index]\n\n    def alla_anteckningar(self):\n        return \",\".join(self.__anteckningar)\n

Om klassens integritet är viktig är det vettigt att göra listattributen anteckningar privat. Klassen förser trots allt klienten med lämpliga metoder för att lägga till och bläddra i anteckningar. Detta tillvägagångssätt blir problematiskt om vi definierar en ny klass AnteckningsbokPro, som ärver Anteckningsbok-klassen. Det privata listattributet är inte tillgängligt för klienten, men det är inte heller tillgängligt för de härledda klasserna. Om vi försöker komma åt det, som i metoden hamta_anteckningar nedan, får vi ett felmeddelande:

class AnteckningsbokPro(Anteckningsbok):\n    \"\"\" En bättre Anteckningsbok med sökfunktionalitet \"\"\"\n    def __init__(self):\n        # Detta är ok, eftersom konstruktorn är offentlig trots understrykning\n        super().__init__()\n\n    # Detta orsakar ett fel\n    def hitta_anteckningar(self, sokord):\n        hittade = []\n        # Attributet __anteckningar är privat, den härledda\n        # klassen kan inte komma åt den direkt\n        for anteckning in self.__anteckningar:\n            if sokord in anteckning:\n                hittade.append(anteckning)\n\n        return hittade\n

AttributeError: 'AnteckningsbokPro' object has no attribute '_AnteckningsbokPro__anteckningar'

Skyddade egenskaper

Många objektorienterade programmeringsspråk har en funktion, oftast ett speciellt nyckelord, för att skydda egenskaper. Detta innebär att en egenskap ska vara dold för klassens klienter, men hållas tillgänglig för dess underklasser. Python avskyr i allmänhet nyckelord, så ingen sådan funktion är direkt tillgänglig i Python. Istället finns det en konvention för att markera skyddade egenskaper på ett visst sätt.

Kom ihåg att en egenskap kan döljas genom att prefixera dess namn med två understreck:

\ndef __init__(self):\n    self.__anteckningar = []\n

Den överenskomna konventionen för att skydda en egenskap är att prefixera namnet med endast ett understreck. Nu är detta bara en konvention. Ingenting hindrar en programmerare från att bryta mot konventionen, men det anses vara en dålig programmeringspraxis.

\ndef __init__(self):\n    self._anteckningar = []\n

Nedan har vi hela Anteckningsbok-exemplet, med skyddade _anteckningar istället för privata __anteckningar:

\nclass Anteckningsbok:\n    \"\"\" En Anteckningsbok förvarar anteckningar i strängformat \"\"\"\n\n    def __init__(self):\n        # Skyddade attribut\n        self._anteckningar = []\n\n    def tillsatt_anteckning(self, anteckning):\n        self._anteckningar.append(anteckning)\n\n    def hamta_anteckning(self, index):\n        return self._anteckningar[index]\n\n    def alla_anteckningar(self):\n        return \",\".join(self._anteckningar)\n\nclass AnteckningsbokPro(Anteckningsbok):\n    \"\"\" En bättre Anteckningsbok med sökfunktionalitet \"\"\"\n    def __init__(self):\n        # Detta är ok, eftersom konstruktorn är offentlig trots understrykning\n        super().__init__()\n\n    # Nu fungerar metoden, eftersom den skyddadde attributen är\n    # ankomstbar till den härledda klassen\n    def hitta_anteckningar(self, sokord):\n        hittade = []\n        for anteckning in self._anteckningar:\n            if sokord in anteckning:\n                hittade.append(anteckning)\n\n        return hittade\n

Nedan har vi en praktisk tabell för synligheten av attribut med olika åtkomstmodifierare:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ÅtkomstmodifierareExempelSynlig till klientenSynlig till härledd klass
Offentligself.namnjaja
Skyddadself._namnnejja
Privatself.__namnnejnej

Åtkomstmodifierare fungerar på samma sätt med alla egenskaper. I klassen Person nedan har vi till exempel den skyddade metoden versalisera_initialer Den kan användas från den härledda klassen Fotbollsspelare:

\nclass Person:\n    def __init__(self, namn: str):\n        self._namn = self._versalisera_initialer(namn)\n\n    def _versalisera_initialer(self, namn):\n        namn_versaliserat = []\n        for n in namn.split(\" \"):\n            namn_versaliserat.append(n.capitalize())\n\n        return \" \".join(namn_versaliserat)\n\n    def __repr__(self):\n        return self.__namn\n\nclass Fotbollsspelare(Person):\n\n    def __init__(self, namn: str, smeknamn: str, position: str):\n        super().__init__(namn)\n        # metoden är ankomstbar eftersom den är skyddad i basklassen\n        self.__smeknamn = self._versalisera_initialer(smeknamn)\n        self.__position = position\n\n    def __repr__(self):\n        r =  f\"Fotbollsspelare - namn:{self._namn}, smeknamn: {self.__smeknamn}\"\n        r += f\", position: {self.__position}\"\n        return r\n\n# Testar klasserna\nif __name__ == \"__main__\":\n    fs = Fotbollsspelare(\"peter pythonson\", \"putte\", \"anfallare\")\n    print(fs)\n

Fotbollsspelare - namn:Peter Pythonson, smeknamn: Putte, position: anfallare

I uppgiftsbotten finns färdigt klassdefinitionen för en Superhjalte.

Skapa klassen Supergrupp, som representerar en grupp av superhjältar. Klassen ska innehålla följande:

    \n
  • Skyddade attributen namn (sträng), lokation (sträng) och medlemmar (lista)
  • \n
  • En konstruktor, som får namn och lokation av gruppen som argument, i den ordningen
  • \n
  • Getter-metoden för namn- och lokation-attributen
  • \n
  • Metoden tillsatt_medlem(hjalte: Superhjalte), som lägger till en ny medlem till gruppen
  • \n
  • Metoden skriv_ut_grupp, som skriver ut information om gruppen och dess medlemmer, enligt formatet nedan
  • \n

Exempel av klassen:

superperson = Superhjalte(\"SuperPerson\", \"Supersnabbhet, superstyrka\")\nosynlig = Superhjalte(\"Osynliga Olle\", \"Osynlighet\")\nrevengers = Supergrupp(\"Revengers\", \"Åland\")\n\nrevengers.tillsatt_medlem(superperson)\nrevengers.tillsatt_medlem(osynlig)\nrevengers.skriv_ut_grupp()

Revengers, Åland\nMedlemmar:\nSuperPerson, specialförmågor: Supersnabbhet, superstyrka\nOsynliga Olle, specialförmågor: Osynlighet

Detta kapitel kan vara användbart.

Övningsmallen innehåller klassdefinitionen för en Trolldryck som gör att du kan spara ett recept på en trolldryck. Klassdefinitionen innehåller en konstruktor tillsammans med metoderna

    \n
  • tillsatt_ingrediens(ingrediens: str, mangd: float) och
  • \n
  • skriv_ut_recept()
  • \n

Definiera en klass med namnet HemligTrolldryck som ärver Trolldryck-klassen och som gör att du också kan skydda receptet med ett lösenord.

Den nya klassen ska ha en konstruktor som dessutom tar en lösenordssträng som argument.

Klassen ska dessutom innehålla följande metoder:

    \n
  • tillsatt_ingrediens(ingrediens: str, mangd: float, losenord: str) och
  • \n
  • skriv_ut_recept(losenord: str)
  • \n

Om lösenordsargumentet som ges till någon av dessa metoder är felaktigt ska metoderna ge upphov till ett ValueError-undantag.

Om lösenordet är korrekt ska varje metod anropa den relevanta metoden i den överordnade klassen. Kopiera och klistra inte in något från Trolldryck-klassen.

Exempel på hur detta ska fungera:

krympning = HemligTrolldryck(\"Krympning maximus\", \"hocuspocus\")\nkrympning.tillsatt_ingrediens(\"Granrot\", 1.5, \"hocuspocus\")\nkrympning.tillsatt_ingrediens(\"Magisk sand\", 3.0, \"hocuspocus\")\nkrympning.tillsatt_ingrediens(\"Grodyngel\", 4.0, \"hocuspocus\")\nkrympning.skriv_ut_recept(\"hocuspocus\")\n\nkrympning.skriv_ut_recept(\"pocushocus\") # FEL LÖSENORD
Krympning maximus:\nGranrot 1.5 gram\nMagisk sand 3.0 gram\nGrodyngel 4.0 gram\nTraceback (most recent call last):\nFile \"hemlig_trolldryck.py\", line 98, in \nraise ValueError(\"Fel lösenord!\")\nValueError: Fel lösenord!
","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/2-atkamstmodifierare.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att förstå åtkomstmodifierarna privat och skyddad"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur synligheten för egenskaper bestäms i Python"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en egenskap definieras som privat i basklassen är den inte direkt åtkomlig i några härledda klasser, liksom kort nämndes i föregående avsnitt. Låt oss ta en titt på ett exempel. I klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Anteckningsbok"}]},{"type":"text","value":" nedan lagras anteckningarna i en lista, och listattributet är privat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Anteckningsbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" En Anteckningsbok förvarar anteckningar i strängformat \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# privat attribut"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alla_anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om klassens integritet är viktig är det vettigt att göra listattributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"anteckningar"}]},{"type":"text","value":" privat. Klassen förser trots allt klienten med lämpliga metoder för att lägga till och bläddra i anteckningar. Detta tillvägagångssätt blir problematiskt om vi definierar en ny klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"AnteckningsbokPro"}]},{"type":"text","value":", som ärver "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Anteckningsbok"}]},{"type":"text","value":"-klassen. Det privata listattributet är inte tillgängligt för klienten, men det är inte heller tillgängligt för de härledda klasserna. Om vi försöker komma åt det, som i metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_anteckningar"}]},{"type":"text","value":" nedan, får vi ett felmeddelande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"AnteckningsbokPro"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Anteckningsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" En bättre Anteckningsbok med sökfunktionalitet \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta är ok, eftersom konstruktorn är offentlig trots understrykning"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta orsakar ett fel"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hitta_anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sokord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Attributet __anteckningar är privat, den härledda"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# klassen kan inte komma åt den direkt"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" anteckning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" sokord "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" hittade\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'AnteckningsbokPro' object has no attribute '_AnteckningsbokPro__anteckningar'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"skyddade-egenskaper","style":"position:relative;"},"children":[{"type":"text","value":"Skyddade egenskaper"},{"type":"element","tagName":"a","properties":{"href":"#skyddade-egenskaper","ariaLabel":"skyddade egenskaper permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Många objektorienterade programmeringsspråk har en funktion, oftast ett speciellt nyckelord, för att skydda egenskaper. Detta innebär att en egenskap ska vara dold för klassens klienter, men hållas tillgänglig för dess underklasser. Python avskyr i allmänhet nyckelord, så ingen sådan funktion är direkt tillgänglig i Python. Istället finns det en konvention för att markera skyddade egenskaper på ett visst sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att en egenskap kan döljas genom att prefixera dess namn med två understreck:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den överenskomna konventionen för att skydda en egenskap är att prefixera namnet med endast ett understreck. Nu är detta bara en konvention. Ingenting hindrar en programmerare från att bryta mot konventionen, men det anses vara en dålig programmeringspraxis."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan har vi hela Anteckningsbok-exemplet, med skyddade "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"_anteckningar"}]},{"type":"text","value":" istället för privata "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__anteckningar"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Anteckningsbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" En Anteckningsbok förvarar anteckningar i strängformat \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skyddade attribut"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alla_anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"AnteckningsbokPro"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Anteckningsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" En bättre Anteckningsbok med sökfunktionalitet \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta är ok, eftersom konstruktorn är offentlig trots understrykning"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Nu fungerar metoden, eftersom den skyddadde attributen är"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ankomstbar till den härledda klassen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hitta_anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sokord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" anteckning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" sokord "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" hittade\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan har vi en praktisk tabell för synligheten av attribut med olika åtkomstmodifierare:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Åtkomstmodifierare"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Exempel"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Synlig till klienten"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Synlig till härledd klass"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Offentlig"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.namn"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"ja"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"ja"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Skyddad"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self._namn"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"nej"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"ja"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Privat"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.__namn"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"nej"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"nej"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Åtkomstmodifierare fungerar på samma sätt med alla egenskaper. I klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" nedan har vi till exempel den skyddade metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"versalisera_initialer"}]},{"type":"text","value":" Den kan användas från den härledda klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fotbollsspelare"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_versalisera_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"_versalisera_initialer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn_versaliserat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn_versaliserat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn_versaliserat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Fotbollsspelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" smeknamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" position"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metoden är ankomstbar eftersom den är skyddad i basklassen"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__smeknamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_versalisera_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"smeknamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__position "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" position\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n r "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Fotbollsspelare - namn:"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", smeknamn: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__smeknamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n r "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\", position: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__position"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" r\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar klasserna"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fotbollsspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"putte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"anfallare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fotbollsspelare - namn:Peter Pythonson, smeknamn: Putte, position: anfallare"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Supergrupp","tmcname":"osa10-05_supergrupp"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns färdigt klassdefinitionen för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Superhjalte"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Supergrupp"}]},{"type":"text","value":", som representerar en grupp av superhjältar. Klassen ska innehålla följande:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Skyddade"}]},{"type":"text","value":" attributen namn (sträng), lokation (sträng) och medlemmar (lista)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"En konstruktor, som får namn och lokation av gruppen som argument, i den ordningen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Getter-metoden för namn- och lokation-attributen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_medlem(hjalte: Superhjalte)"}]},{"type":"text","value":", som lägger till en ny medlem till gruppen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_grupp"}]},{"type":"text","value":", som skriver ut information om gruppen och dess medlemmer, enligt formatet nedan"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel av klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"superperson "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Superhjalte"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SuperPerson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Supersnabbhet, superstyrka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nosynlig "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Superhjalte"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Osynliga Olle\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Osynlighet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Supergrupp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Revengers\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Åland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_medlem"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"superperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_medlem"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"osynlig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_grupp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Revengers, Åland\nMedlemmar:\nSuperPerson, specialförmågor: Supersnabbhet, superstyrka\nOsynliga Olle, specialförmågor: Osynlighet"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/3-inkapsling","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Detta"}]},{"type":"text","value":" kapitel kan vara användbart."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hemlig trolldryck","tmcname":"osa10-06_hemlig_trolldryck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller klassdefinitionen för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Trolldryck"}]},{"type":"text","value":" som gör att du kan spara ett recept på en trolldryck. Klassdefinitionen innehåller en konstruktor tillsammans med metoderna"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_ingrediens(ingrediens: str, mangd: float)"}]},{"type":"text","value":" och"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_recept()"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"HemligTrolldryck"}]},{"type":"text","value":" som ärver "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Trolldryck"}]},{"type":"text","value":"-klassen och som gör att du också kan skydda receptet med ett lösenord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den nya klassen ska ha en konstruktor som dessutom tar en lösenordssträng som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska dessutom innehålla följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_ingrediens(ingrediens: str, mangd: float, losenord: str)"}]},{"type":"text","value":" och"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_recept(losenord: str)"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om lösenordsargumentet som ges till någon av dessa metoder är felaktigt ska metoderna ge upphov till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om lösenordet är korrekt ska varje metod anropa den relevanta metoden i den överordnade klassen. Kopiera och klistra inte in något från "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Trolldryck"}]},{"type":"text","value":"-klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur detta ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"krympning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" HemligTrolldryck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Krympning maximus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ingrediens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Granrot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ingrediens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Magisk sand\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ingrediens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grodyngel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkrympning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pocushocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# FEL LÖSENORD"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Krympning maximus:\nGranrot 1.5 gram\nMagisk sand 3.0 gram\nGrodyngel 4.0 gram\nTraceback (most recent call last):\nFile \"hemlig_trolldryck.py\", line 98, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nraise ValueError(\"Fel lösenord!\")\nValueError: Fel lösenord!"}]}]}]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att förstå åtkomstmodifierarna privat och skyddad
  • \n
  • Vet du hur synligheten för egenskaper bestäms i Python
  • \n

Om en egenskap definieras som privat i basklassen är den inte direkt åtkomlig i några härledda klasser, liksom kort nämndes i föregående avsnitt. Låt oss ta en titt på ett exempel. I klassen Anteckningsbok nedan lagras anteckningarna i en lista, och listattributet är privat:

\nclass Anteckningsbok:\n    \"\"\" En Anteckningsbok förvarar anteckningar i strängformat \"\"\"\n\n    def __init__(self):\n        # privat attribut\n        self.__anteckningar = []\n\n    def tillsatt_anteckning(self, anteckning):\n        self.__anteckningar.append(anteckning)\n\n    def hamta_anteckning(self, index):\n        return self.__anteckningar[index]\n\n    def alla_anteckningar(self):\n        return \",\".join(self.__anteckningar)\n

Om klassens integritet är viktig är det vettigt att göra listattributen anteckningar privat. Klassen förser trots allt klienten med lämpliga metoder för att lägga till och bläddra i anteckningar. Detta tillvägagångssätt blir problematiskt om vi definierar en ny klass AnteckningsbokPro, som ärver Anteckningsbok-klassen. Det privata listattributet är inte tillgängligt för klienten, men det är inte heller tillgängligt för de härledda klasserna. Om vi försöker komma åt det, som i metoden hamta_anteckningar nedan, får vi ett felmeddelande:

class AnteckningsbokPro(Anteckningsbok):\n    \"\"\" En bättre Anteckningsbok med sökfunktionalitet \"\"\"\n    def __init__(self):\n        # Detta är ok, eftersom konstruktorn är offentlig trots understrykning\n        super().__init__()\n\n    # Detta orsakar ett fel\n    def hitta_anteckningar(self, sokord):\n        hittade = []\n        # Attributet __anteckningar är privat, den härledda\n        # klassen kan inte komma åt den direkt\n        for anteckning in self.__anteckningar:\n            if sokord in anteckning:\n                hittade.append(anteckning)\n\n        return hittade\n

AttributeError: 'AnteckningsbokPro' object has no attribute '_AnteckningsbokPro__anteckningar'

Skyddade egenskaper

Många objektorienterade programmeringsspråk har en funktion, oftast ett speciellt nyckelord, för att skydda egenskaper. Detta innebär att en egenskap ska vara dold för klassens klienter, men hållas tillgänglig för dess underklasser. Python avskyr i allmänhet nyckelord, så ingen sådan funktion är direkt tillgänglig i Python. Istället finns det en konvention för att markera skyddade egenskaper på ett visst sätt.

Kom ihåg att en egenskap kan döljas genom att prefixera dess namn med två understreck:

\ndef __init__(self):\n    self.__anteckningar = []\n

Den överenskomna konventionen för att skydda en egenskap är att prefixera namnet med endast ett understreck. Nu är detta bara en konvention. Ingenting hindrar en programmerare från att bryta mot konventionen, men det anses vara en dålig programmeringspraxis.

\ndef __init__(self):\n    self._anteckningar = []\n

Nedan har vi hela Anteckningsbok-exemplet, med skyddade _anteckningar istället för privata __anteckningar:

\nclass Anteckningsbok:\n    \"\"\" En Anteckningsbok förvarar anteckningar i strängformat \"\"\"\n\n    def __init__(self):\n        # Skyddade attribut\n        self._anteckningar = []\n\n    def tillsatt_anteckning(self, anteckning):\n        self._anteckningar.append(anteckning)\n\n    def hamta_anteckning(self, index):\n        return self._anteckningar[index]\n\n    def alla_anteckningar(self):\n        return \",\".join(self._anteckningar)\n\nclass AnteckningsbokPro(Anteckningsbok):\n    \"\"\" En bättre Anteckningsbok med sökfunktionalitet \"\"\"\n    def __init__(self):\n        # Detta är ok, eftersom konstruktorn är offentlig trots understrykning\n        super().__init__()\n\n    # Nu fungerar metoden, eftersom den skyddadde attributen är\n    # ankomstbar till den härledda klassen\n    def hitta_anteckningar(self, sokord):\n        hittade = []\n        for anteckning in self._anteckningar:\n            if sokord in anteckning:\n                hittade.append(anteckning)\n\n        return hittade\n

Nedan har vi en praktisk tabell för synligheten av attribut med olika åtkomstmodifierare:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ÅtkomstmodifierareExempelSynlig till klientenSynlig till härledd klass
Offentligself.namnjaja
Skyddadself._namnnejja
Privatself.__namnnejnej

Åtkomstmodifierare fungerar på samma sätt med alla egenskaper. I klassen Person nedan har vi till exempel den skyddade metoden versalisera_initialer Den kan användas från den härledda klassen Fotbollsspelare:

\nclass Person:\n    def __init__(self, namn: str):\n        self._namn = self._versalisera_initialer(namn)\n\n    def _versalisera_initialer(self, namn):\n        namn_versaliserat = []\n        for n in namn.split(\" \"):\n            namn_versaliserat.append(n.capitalize())\n\n        return \" \".join(namn_versaliserat)\n\n    def __repr__(self):\n        return self.__namn\n\nclass Fotbollsspelare(Person):\n\n    def __init__(self, namn: str, smeknamn: str, position: str):\n        super().__init__(namn)\n        # metoden är ankomstbar eftersom den är skyddad i basklassen\n        self.__smeknamn = self._versalisera_initialer(smeknamn)\n        self.__position = position\n\n    def __repr__(self):\n        r =  f\"Fotbollsspelare - namn:{self._namn}, smeknamn: {self.__smeknamn}\"\n        r += f\", position: {self.__position}\"\n        return r\n\n# Testar klasserna\nif __name__ == \"__main__\":\n    fs = Fotbollsspelare(\"peter pythonson\", \"putte\", \"anfallare\")\n    print(fs)\n

Fotbollsspelare - namn:Peter Pythonson, smeknamn: Putte, position: anfallare

I uppgiftsbotten finns färdigt klassdefinitionen för en Superhjalte.

Skapa klassen Supergrupp, som representerar en grupp av superhjältar. Klassen ska innehålla följande:

    \n
  • Skyddade attributen namn (sträng), lokation (sträng) och medlemmar (lista)
  • \n
  • En konstruktor, som får namn och lokation av gruppen som argument, i den ordningen
  • \n
  • Getter-metoden för namn- och lokation-attributen
  • \n
  • Metoden tillsatt_medlem(hjalte: Superhjalte), som lägger till en ny medlem till gruppen
  • \n
  • Metoden skriv_ut_grupp, som skriver ut information om gruppen och dess medlemmer, enligt formatet nedan
  • \n

Exempel av klassen:

superperson = Superhjalte(\"SuperPerson\", \"Supersnabbhet, superstyrka\")\nosynlig = Superhjalte(\"Osynliga Olle\", \"Osynlighet\")\nrevengers = Supergrupp(\"Revengers\", \"Åland\")\n\nrevengers.tillsatt_medlem(superperson)\nrevengers.tillsatt_medlem(osynlig)\nrevengers.skriv_ut_grupp()

Revengers, Åland\nMedlemmar:\nSuperPerson, specialförmågor: Supersnabbhet, superstyrka\nOsynliga Olle, specialförmågor: Osynlighet

Detta kapitel kan vara användbart.

Övningsmallen innehåller klassdefinitionen för en Trolldryck som gör att du kan spara ett recept på en trolldryck. Klassdefinitionen innehåller en konstruktor tillsammans med metoderna

    \n
  • tillsatt_ingrediens(ingrediens: str, mangd: float) och
  • \n
  • skriv_ut_recept()
  • \n

Definiera en klass med namnet HemligTrolldryck som ärver Trolldryck-klassen och som gör att du också kan skydda receptet med ett lösenord.

Den nya klassen ska ha en konstruktor som dessutom tar en lösenordssträng som argument.

Klassen ska dessutom innehålla följande metoder:

    \n
  • tillsatt_ingrediens(ingrediens: str, mangd: float, losenord: str) och
  • \n
  • skriv_ut_recept(losenord: str)
  • \n

Om lösenordsargumentet som ges till någon av dessa metoder är felaktigt ska metoderna ge upphov till ett ValueError-undantag.

Om lösenordet är korrekt ska varje metod anropa den relevanta metoden i den överordnade klassen. Kopiera och klistra inte in något från Trolldryck-klassen.

Exempel på hur detta ska fungera:

krympning = HemligTrolldryck(\"Krympning maximus\", \"hocuspocus\")\nkrympning.tillsatt_ingrediens(\"Granrot\", 1.5, \"hocuspocus\")\nkrympning.tillsatt_ingrediens(\"Magisk sand\", 3.0, \"hocuspocus\")\nkrympning.tillsatt_ingrediens(\"Grodyngel\", 4.0, \"hocuspocus\")\nkrympning.skriv_ut_recept(\"hocuspocus\")\n\nkrympning.skriv_ut_recept(\"pocushocus\") # FEL LÖSENORD
Krympning maximus:\nGranrot 1.5 gram\nMagisk sand 3.0 gram\nGrodyngel 4.0 gram\nTraceback (most recent call last):\nFile \"hemlig_trolldryck.py\", line 98, in \nraise ValueError(\"Fel lösenord!\")\nValueError: Fel lösenord!
","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/2-atkamstmodifierare.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-10/3-objektorienterade-programmeringstekniker/page-data.json b/page-data/osa-10/3-objektorienterade-programmeringstekniker/page-data.json index c3daca324..69dd137f7 100644 --- a/page-data/osa-10/3-objektorienterade-programmeringstekniker/page-data.json +++ b/page-data/osa-10/3-objektorienterade-programmeringstekniker/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-10/3-objektorienterade-programmeringstekniker", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Känner du till några av de olika användningsområdena för variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du överlagrar operatorer i dina egna klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa en iterabel klass"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klass kan innehålla en metod som returnerar ett objekt av samma klass. Nedan har vi t.ex. klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"text","value":", vars metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reaprodukt"}]},{"type":"text","value":" returnerar ett nytt Produkt-objekt med samma namn som det ursprungliga men med ett pris som är 25 % lägre:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (pris "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"reaprodukt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n nedsatt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.75"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" nedsatt"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"banan1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbanan2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" banan1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reaprodukt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"banan1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"banan2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Banan (pris 2.99)\nBanan (pris 2.2425)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss gå igenom syftet med variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":": inom en klassdefinition hänvisar den till själva objektet. Vanligtvis används den för att hänvisa till objektets egna egenskaper, dess attribut och metoder. Variabeln kan också användas för att hänvisa till hela objektet, till exempel om själva objektet måste returneras till klientkoden. I exemplet nedan har vi lagt till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"billigare"}]},{"type":"text","value":" i klassdefinitionen. Den tar en annan Produkt som sitt argument och returnerar det billigare av de två:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (pris "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"billigare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" produkt"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"banan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nananas "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ananas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"apelsin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"billigare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"banan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"apelsin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"billigare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ananas"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Banan (2.99)\nApelsin (3.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om det här fungerar bra är det ett mycket specialiserat fall av att jämföra två objekt. Det skulle vara bättre om vi kunde använda Pythons jämförelseoperatorer direkt på dessa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"text","value":"-objekt."}]},{"type":"element","tagName":"h2","properties":{"id":"overlagring-av-operatorer","style":"position:relative;"},"children":[{"type":"text","value":"Överlagring av operatorer"},{"type":"element","tagName":"a","properties":{"href":"#overlagring-av-operatorer","ariaLabel":"overlagring av operatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python innehåller några speciellt namngivna inbyggda metoder för att arbeta med standardoperatorerna för aritmetik och jämförelse. Tekniken kallas operatörsöverlagring (eng. operator overloading). Om du vill kunna använda en viss operator på instanser av självdefinierade klasser kan du skriva en speciell metod som returnerar det korrekta resultatet av operatorn. Vi har redan använt den här tekniken med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" metoden: Python vet att leta efter en metod som heter så här när en strängrepresentation av ett objekt efterfrågas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja med operatorn > som talar om för oss om den första operanden är större än den andra. Klassdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"text","value":" nedan innehåller metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"text","value":", som är en förkortning av greater than. Denna speciellt namngivna metod ska returnera det korrekta resultatet av jämförelsen. Specifikt ska den returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om och endast om det aktuella objektet är större än det objekt som skickas som ett argument. De kriterier som används kan bestämmas av programmeraren. Med aktuellt objekt menar vi det objekt som metoden anropas på med punkt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" notationen"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (pris "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annan_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" annan_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I implementationen ovan returnerar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om priset på den aktuella produkten är högre än priset på den produkt som skickas som argument. I annat fall returnerar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu finns jämförelseoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" tillgänglig för användning med objekt av typen Produkt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"apelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.90"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbanan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" apelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" banan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin är större\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan är större\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Apelsin är större"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som nämnts ovan är det upp till programmeraren att bestämma vilka kriterier som ska gälla för att avgöra vad som är störst och vad som är minst. Vi kan t.ex. bestämma att ordningen inte ska baseras på pris, utan i stället ska vara alfabetisk enligt namn. Detta skulle innebära att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"banan"}]},{"type":"text","value":" nu skulle vara \"större än\" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"apelsin"}]},{"type":"text","value":", eftersom \"banan\" kommer senare alfabetiskt."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (pris "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annan_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" annan_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"apelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.90"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbanan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" apelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" banan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin är större\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan är större\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Banan är större"}]}]},{"type":"element","tagName":"h2","properties":{"id":"fler-operatorer","style":"position:relative;"},"children":[{"type":"text","value":"Fler operatorer"},{"type":"element","tagName":"a","properties":{"href":"#fler-operatorer","ariaLabel":"fler operatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en tabell som innehåller de vanliga jämförelseoperatorerna, tillsammans med de metoder som måste implementeras om vi vill göra dem tillgängliga för användning på våra objekt:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Operator"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Traditionell mening"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Metodens namn"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Mindre än"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__lt__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Större än"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Lika med"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__eq__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Inte lika med"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__ne__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Mindre eller lika med"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__le__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Större eller lika med"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__ge__(self, annan)"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också implementera några andra operatorer, inklusive följande aritmetiska operatorer:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Operator"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Traditionell mening"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Metodens namn"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Addition"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__add__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Subtraktion"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__sub__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Multiplikation"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__mul__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Division (bråktal)"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__truediv__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Division (heltal)"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__floordiv__(self, annan)"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fler operatorer och metodnamn finns lätt att hitta på nätet. Kom också ihåg "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":"-instruktionen för att lista de metoder som är tillgängliga för användning på ett visst objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är mycket sällan nödvändigt att implementera alla aritmetiska operatorer och jämförelseoperatorer i dina egna klasser. Division är t.ex. en operation som sällan är meningsfull utanför numeriska objekt. Vad skulle resultatet av att dividera ett Student-objekt med tre, eller med ett annat Student-objekt bli? Trots detta är vissa av dessa operatorer ofta mycket användbara även i egna klasser. Valet av metoder att implementera beror på vad som är vettigt, med tanke på egenskaperna hos dina objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en klass som modellerar en enda anteckning. Om vi implementerar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__add__"}]},{"type":"text","value":" i vår klassdefinition blir additionsoperatorn + tillgänglig för våra Anteckning-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dtm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dtm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dtm\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" inlagg\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dtm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__add__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Datumet för den nya anteckningen är aktuell tid"}]},{"type":"text","value":"\n nytt_inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nytt_inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"och \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" annan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" nytt_inlagg"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"inlagg1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2016"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kom ihåg att köpa presenter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ninlagg2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2016"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kom ihåg att hämta en gran\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Nu kan dessa anteckningar bli sammanlagda med + operatorn - detta kallar metoden __add__ i Anteckning-klassen"}]},{"type":"text","value":"\nbada "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" inlagg1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" inlagg2\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2020-09-09 14:13:02.163170: Kom ihåg att köpa presenter och Kom ihåg att hämta en gran"}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-strangrepresentation-av-ett-objekt","style":"position:relative;"},"children":[{"type":"text","value":"En strängrepresentation av ett objekt"},{"type":"element","tagName":"a","properties":{"href":"#en-strangrepresentation-av-ett-objekt","ariaLabel":"en strangrepresentation av ett objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du har redan implementerat en hel del "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metoder i dina klasser. Som du vet returnerar metoden en strängrepresentation av objektet. En annan ganska liknande metod är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" som returnerar en teknisk representation av objektet. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" är ofta implementerad så att den returnerar den programkod som kan exekveras för att returnera ett objekt med identiskt innehåll som det aktuella objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"repr"}]},{"type":"text","value":" returnerar denna tekniska strängrepresentation av objektet. Den tekniska representationen används även när metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" inte har definierats för objektet. Exemplet nedan kommer att göra detta tydligare:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Person("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"repr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"person1 = Person(\"Anna\", 25)\nperson2 = Person(\"Peter\", 99)\nprint(person1)\nprint(person2)"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person('Anna', 25)\nPerson('Peter', 99)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg märke till hur metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" själv använder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"repr"}]},{"type":"text","value":"-funktionen för att hämta den tekniska representationen av strängen. Detta är nödvändigt för att inkludera tecknen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"'"}]},{"type":"text","value":" i resultatet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande klass har definitioner för både "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Person("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"repr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år)\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"person = Person(\"Anna\", 25)\nprint(person)\nprint(repr(person))"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anna (25 år)\nPerson('Anna', 25)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är värt att nämna att med datastrukturer, såsom listor, använder Python alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":"-metoden för strängrepresentationen av innehållet. Detta kan ibland se lite förvirrande ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"personer = []\npersoner.append(Person(\"Anna\", 25))\npersoner.append(Person(\"Peter\", 99))\npersoner.append(Person(\"Maja\", 55))\nprint(personer)"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[Person('Anna', 25), Person('Peter', 99), Person('Maja', 55)]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Pengar","tmcname":"osa10-07_pengar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller en mall för en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pengar"}]},{"type":"text","value":". Den här övningen ber dig att implementera några ytterligare metoder och att åtgärda några små problem i mallen"}]},{"type":"element","tagName":"h2","properties":{"id":"fixa-strangrepresentationen","style":"position:relative;"},"children":[{"type":"text","value":"Fixa strängrepresentationen"},{"type":"element","tagName":"a","properties":{"href":"#fixa-strangrepresentationen","ariaLabel":"fixa strangrepresentationen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" i klassdefinitionen fungerar inte riktigt som den ska. Med följande två "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pengar"}]},{"type":"text","value":"-objekt skrivs det senare ut på fel sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# två euro och 5 cent"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10\n2.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fixa metoden så att den skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10 eur\n2.05 eur"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lika-stora-mangder","style":"position:relative;"},"children":[{"type":"text","value":"Lika stora mängder"},{"type":"element","tagName":"a","properties":{"href":"#lika-stora-mangder","ariaLabel":"lika stora mangder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en ny metod med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__eq__(self, annan)"}]},{"type":"text","value":" som gör att du kan använda jämförelseoperatorn == på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pengar"}]},{"type":"text","value":"-objekt. Du kan testa din implementation med följande kod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" e3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10 eur\n2.05 eur\n4.10 eur\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"andra-jamforelseoperatorer","style":"position:relative;"},"children":[{"type":"text","value":"Andra jämförelseoperatörer"},{"type":"element","tagName":"a","properties":{"href":"#andra-jamforelseoperatorer","ariaLabel":"andra jamforelseoperatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera även metoder för jämförelseoperatörerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"addition-och-subtraktion","style":"position:relative;"},"children":[{"type":"text","value":"Addition och subtraktion"},{"type":"element","tagName":"a","properties":{"href":"#addition-och-subtraktion","ariaLabel":"addition och subtraktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen implementera additions- och subtraktionsoperatorerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" för Pengar-objekt. Båda ska returnera ett nytt objekt av typen Pengar. Varken objektet i sig eller det objekt som skickas som argument ska ändras som ett resultat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs: Värdet på ett Pengar-objekt kan inte vara negativt. Om ett försök att subtrahera skulle resultera i ett negativt resultat ska metoden ge upphov till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ne3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" e2\ne4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" e2\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ne5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"e1"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"7.00 eur\n1.10 eur\nTraceback (most recent call last):\nFile \"fil.py\", line 416, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\ne5 = e2-e1\nFile \"fil.py\", line 404, in "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sub"}]},{"type":"text","value":"\nraise ValueError(f\"negativt resultat inte tillåtet\")\nValueError: negativt resultat inte tillåtet"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"vardet-kan-inte-bli-direkt-atkommet","style":"position:relative;"},"children":[{"type":"text","value":"Värdet kan inte bli direkt åtkommet"},{"type":"element","tagName":"a","properties":{"href":"#vardet-kan-inte-bli-direkt-atkommet","ariaLabel":"vardet kan inte bli direkt atkommet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen har fortfarande ett litet integritetsproblem. Användaren kan ”fuska” genom att komma åt attributen direkt och ändra det värde som lagras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pengar"}]},{"type":"text","value":"-objektet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"euron "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.05 eur\n1000.05 eur"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/3-inkapsling","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Inkapsla"}]},{"type":"text","value":" implementeringen av de attribut som definieras i klassen så att fusket ovan inte är möjligt. Klassen ska inte ha några offentliga attribut och inga getter- eller sätter-metoder för euro eller cent."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"SimpelDatum","tmcname":"osa10-08_simpeldatum"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen ska du implementera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SimpelDatum"}]},{"type":"text","value":" som gör att du kan hantera datum. För enkelhetens skull antar vi här att "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"varje månad har 30 dagar"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På grund av denna förenkling bör du inte använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-modulen från Pythons standardbibliotek. Du kommer att implementera liknande funktionalitet själv istället."}]},{"type":"element","tagName":"h2","properties":{"id":"jamforelse","style":"position:relative;"},"children":[{"type":"text","value":"Jämförelse"},{"type":"element","tagName":"a","properties":{"href":"#jamforelse","ariaLabel":"jamforelse permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera mallen för klassen, såväl som metoder för jämförelseoperatörerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":". Exempel på användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" d3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10.2020\n28.12.1985\nFalse\nTrue\nFalse\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"okning","style":"position:relative;"},"children":[{"type":"text","value":"Ökning"},{"type":"element","tagName":"a","properties":{"href":"#okning","ariaLabel":"okning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen implementera additionsoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" som gör att du kan lägga till ett givet antal dagar till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SimpelDatum"}]},{"type":"text","value":"-objekt. Operatorn ska returnera ett nytt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SimpelDatum"}]},{"type":"text","value":"-objekt. Det ursprungliga objektet ska inte ändras."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nd4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" d2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"400"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10.2020\n28.12.1985\n7.10.2020\n8.2.1987"}]}]},{"type":"element","tagName":"h2","properties":{"id":"skillnad","style":"position:relative;"},"children":[{"type":"text","value":"Skillnad"},{"type":"element","tagName":"a","properties":{"href":"#skillnad","ariaLabel":"skillnad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen implementera subtraktionsoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" som gör att du kan ta reda på skillnaden i dagar mellan två SimpelDatum-objekt. Eftersom vi antar att varje månad har 30 dagar, är ett år inom ramen för denna övning 12*30 = 360 dagar långt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Operatorn fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"28\n28\n12516"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"iteratorer","style":"position:relative;"},"children":[{"type":"text","value":"Iteratorer"},{"type":"element","tagName":"a","properties":{"href":"#iteratorer","ariaLabel":"iteratorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi vet att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-satsen kan användas för att iterera genom många olika datastrukturer, filer och samlingar av objekt. Ett typiskt användningsfall skulle kunna vara följande funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_positiva"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen går igenom objekten i listan ett efter ett och håller reda på hur många av objekten som var positiva."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att göra sina egna klasser itererbara. Detta är användbart när klassens huvudsyfte handlar om att lagra en samling objekt. Klassen Bokhylla från ett tidigare exempel skulle vara en bra kandidat, eftersom det skulle vara vettigt att använda en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop för att gå igenom böckerna på hyllan. Detsamma gäller för, exempelvis, ett studentregister. Att kunna iterera genom samlingen av studenter kan vara användbart."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att göra en klass itererbar måste du implementera iteratormetoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":". Vi återkommer till detaljerna i dessa metoder efter följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sidor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" forfattare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sidor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sidor\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Initialiseringsmetoden för iteratorn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Här bör iterationsvariabeln(eller variablerna) initialiseras"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar en referens till själva objektet eftersom"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# iteratorn är implementerad inom samma klassdefinition"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar nästa föremål inom objektet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall inga föremål är kvar, åstadkomms StopIteration-händelsen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__next__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Väljer det aktuella föremålet från listan inom objektet"}]},{"type":"text","value":"\n bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Öka räknaren med ett"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ... och returnera det aktuella föremålet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bok\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Inga fler böcker"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" StopIteration\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"text","value":" initialiserar iterationsvariabeln eller variablerna. I det här fallet räcker det med att ha en enkel räknare som innehåller index för det aktuella objektet i listan. Vi behöver också metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":", som returnerar nästa objekt i iteratorn. I exemplet ovan returnerar metoden objektet med index n från listan i Bokhylla-objektet, och iteratorvariabeln inkrementeras också."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När alla objekt har genomgåtts utlöser metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":" undantaget "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StopIteration"}]},{"type":"text","value":". Processen skiljer sig inte från andra undantag, men det här undantaget hanteras automatiskt av Python och dess syfte är att signalera till koden som anropar iteratorn (t.ex. en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop) att iterationen nu är över."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vår bokhylla är nu redo för iteration, till exempel med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Livet av en Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"123"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den gamle och Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ernest Hemingjava\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"204"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C-värdheter på nätet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Karl Kodare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n hylla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bokhylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriver ut namnet på alla böcker"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Livet av en Python\nDen gamle och Java\nC-värdheter på nätet"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Itererbar affärslista","tmcname":"osa10-09_itererbar_affarslista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" från "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-8/2-klasser-och-objekt","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"övningen i modul 8"}]},{"type":"text","value":". Ändra klassen så att den är itererbar och därmed kan användas på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsiner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ananas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" st\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"apelsiner: 10 st\nbananer: 5 st\nananas: 1 st"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Iteratormetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":" ska returnera tuplar, där det första föremålet är namnet på produkten och det andra är antalet."}]}]}]},"html":"

Efter den här delen

    \n
  • Känner du till några av de olika användningsområdena för variabelnamnet self
  • \n
  • Vet du hur du överlagrar operatorer i dina egna klasser
  • \n
  • Kommer du att kunna skapa en iterabel klass
  • \n

En klass kan innehålla en metod som returnerar ett objekt av samma klass. Nedan har vi t.ex. klassen Produkt, vars metod reaprodukt returnerar ett nytt Produkt-objekt med samma namn som det ursprungliga men med ett pris som är 25 % lägre:

class Produkt:\n    def __init__(self, namn: str, pris: float):\n        self.__namn = namn\n        self.__pris = pris\n\n    def __str__(self):\n        return f\"{self.__namn} (pris {self.__pris})\"\n\n    def reaprodukt(self):\n        nedsatt = Produkt(self.__namn, self.__pris * 0.75)\n        return nedsatt
banan1 = Produkt(\"Banan\", 2.99)\nbanan2 = banan1.reaprodukt()\nprint(banan1)\nprint(banan2)

Banan (pris 2.99)\nBanan (pris 2.2425)

Låt oss gå igenom syftet med variabeln self: inom en klassdefinition hänvisar den till själva objektet. Vanligtvis används den för att hänvisa till objektets egna egenskaper, dess attribut och metoder. Variabeln kan också användas för att hänvisa till hela objektet, till exempel om själva objektet måste returneras till klientkoden. I exemplet nedan har vi lagt till metoden billigare i klassdefinitionen. Den tar en annan Produkt som sitt argument och returnerar det billigare av de två:

class Produkt:\n    def __init__(self, namn: str, pris: float):\n        self.__namn = namn\n        self.__pris = pris\n\n    def __str__(self):\n        return f\"{self.__namn} (pris {self.__pris})\"\n\n    @property\n    def pris(self):\n        return self.__pris\n\n    def billigare(self, produkt):\n        if self.__pris < produkt.pris:\n            return self\n        else:\n            return produkt
banan = Produkt(\"Banan\", 2.99)\napelsin = Produkt(\"Apelsin\", 3.95)\nananas = Produkt(\"Ananas\", 5.25)\n\nprint(apelsin.billigare(banan))\nprint(apelsin.billigare(ananas))

Banan (2.99)\nApelsin (3.95)

Även om det här fungerar bra är det ett mycket specialiserat fall av att jämföra två objekt. Det skulle vara bättre om vi kunde använda Pythons jämförelseoperatorer direkt på dessa Produkt-objekt.

Överlagring av operatorer

Python innehåller några speciellt namngivna inbyggda metoder för att arbeta med standardoperatorerna för aritmetik och jämförelse. Tekniken kallas operatörsöverlagring (eng. operator overloading). Om du vill kunna använda en viss operator på instanser av självdefinierade klasser kan du skriva en speciell metod som returnerar det korrekta resultatet av operatorn. Vi har redan använt den här tekniken med __str__ metoden: Python vet att leta efter en metod som heter så här när en strängrepresentation av ett objekt efterfrågas.

Låt oss börja med operatorn > som talar om för oss om den första operanden är större än den andra. Klassdefinitionen Produkt nedan innehåller metoden __gt__, som är en förkortning av greater than. Denna speciellt namngivna metod ska returnera det korrekta resultatet av jämförelsen. Specifikt ska den returnera True om och endast om det aktuella objektet är större än det objekt som skickas som ett argument. De kriterier som används kan bestämmas av programmeraren. Med aktuellt objekt menar vi det objekt som metoden anropas på med punkt . notationen

class Produkt:\n    def __init__(self, namn: str, pris: float):\n        self.__namn = namn\n        self.__pris = pris\n\n    def __str__(self):\n        return f\"{self.__namn} (pris {self.__pris})\"\n\n    @property\n    def pris(self):\n        return self.__pris\n\n    def __gt__(self, annan_produkt):\n        return self.pris > annan_produkt.pris

I implementationen ovan returnerar metoden __gt__ True om priset på den aktuella produkten är högre än priset på den produkt som skickas som argument. I annat fall returnerar metoden False.

Nu finns jämförelseoperatorn > tillgänglig för användning med objekt av typen Produkt:

apelsin = Produkt(\"Apelsin\", 4.90)\nbanan = Produkt(\"Banan\", 3.95)\n\nif apelsin > banan:\n    print(\"Apelsin är större\")\nelse:\n    print(\"Banan är större\")

Apelsin är större

Som nämnts ovan är det upp till programmeraren att bestämma vilka kriterier som ska gälla för att avgöra vad som är störst och vad som är minst. Vi kan t.ex. bestämma att ordningen inte ska baseras på pris, utan i stället ska vara alfabetisk enligt namn. Detta skulle innebära att banan nu skulle vara \"större än\" apelsin, eftersom \"banan\" kommer senare alfabetiskt.

class Produkt:\n    def __init__(self, namn: str, pris: float):\n        self.__namn = namn\n        self.__pris = pris\n\n    def __str__(self):\n        return f\"{self.__namn} (pris {self.__pris})\"\n\n    @property\n    def pris(self):\n        return self.__pris\n\n    @property\n    def namn(self):\n        return self.__namn\n\n    def __gt__(self, annan_produkt):\n        return self.namn > annan_produkt.namn
apelsin = Produkt(\"Apelsin\", 4.90)\nbanan = Produkt(\"Banan\", 3.95)\n\nif apelsin > banan:\n    print(\"Apelsin är större\")\nelse:\n    print(\"Banan är större\")

Banan är större

Fler operatorer

Här har vi en tabell som innehåller de vanliga jämförelseoperatorerna, tillsammans med de metoder som måste implementeras om vi vill göra dem tillgängliga för användning på våra objekt:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperatorTraditionell meningMetodens namn
<Mindre än__lt__(self, annan)
>Större än__gt__(self, annan)
==Lika med__eq__(self, annan)
!=Inte lika med__ne__(self, annan)
<=Mindre eller lika med__le__(self, annan)
>=Större eller lika med__ge__(self, annan)

Du kan också implementera några andra operatorer, inklusive följande aritmetiska operatorer:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperatorTraditionell meningMetodens namn
+Addition__add__(self, annan)
-Subtraktion__sub__(self, annan)
*Multiplikation__mul__(self, annan)
/Division (bråktal)__truediv__(self, annan)
//Division (heltal)__floordiv__(self, annan)

Fler operatorer och metodnamn finns lätt att hitta på nätet. Kom också ihåg dir-instruktionen för att lista de metoder som är tillgängliga för användning på ett visst objekt.

Det är mycket sällan nödvändigt att implementera alla aritmetiska operatorer och jämförelseoperatorer i dina egna klasser. Division är t.ex. en operation som sällan är meningsfull utanför numeriska objekt. Vad skulle resultatet av att dividera ett Student-objekt med tre, eller med ett annat Student-objekt bli? Trots detta är vissa av dessa operatorer ofta mycket användbara även i egna klasser. Valet av metoder att implementera beror på vad som är vettigt, med tanke på egenskaperna hos dina objekt.

Låt oss ta en titt på en klass som modellerar en enda anteckning. Om vi implementerar metoden __add__ i vår klassdefinition blir additionsoperatorn + tillgänglig för våra Anteckning-objekt:

from datetime import datetime\n\nclass Anteckning:\n    def __init__(self, dtm: datetime, inlagg: str):\n        self.dtm = dtm\n        self.inlagg = inlagg\n\n    def __str__(self):\n        return f\"{self.dtm}: {self.inlagg}\"\n\n    def __add__(self, annan):\n        # Datumet för den nya anteckningen är aktuell tid\n        nytt_inlagg = Anteckning(datetime.now(), \"\")\n        nytt_inlagg.inlagg = self.inlagg + \"och \" + annan.inlagg\n        return nytt_inlagg
inlagg1 = Anteckning(datetime(2016, 12, 17), \"Kom ihåg att köpa presenter\")\ninlagg2 = Anteckning(datetime(2016, 12, 23), \"Kom ihåg att hämta en gran\")\n\n# Nu kan dessa anteckningar bli sammanlagda med + operatorn - detta kallar metoden __add__ i Anteckning-klassen\nbada = inlagg1 + inlagg2\nprint(bada)

2020-09-09 14:13:02.163170: Kom ihåg att köpa presenter och Kom ihåg att hämta en gran

En strängrepresentation av ett objekt

Du har redan implementerat en hel del __str__-metoder i dina klasser. Som du vet returnerar metoden en strängrepresentation av objektet. En annan ganska liknande metod är __repr__ som returnerar en teknisk representation av objektet. Metoden __repr__ är ofta implementerad så att den returnerar den programkod som kan exekveras för att returnera ett objekt med identiskt innehåll som det aktuella objektet.

Funktionen repr returnerar denna tekniska strängrepresentation av objektet. Den tekniska representationen används även när metoden __str__ inte har definierats för objektet. Exemplet nedan kommer att göra detta tydligare:

class Person:\n    def __init__(self, namn: str, alder: int):\n        self.namn = namn\n        self.alder = alder\n\n    def __repr__(self):\n        return f\"Person({repr(self.namn)}, {self.alder})\"
person1 = Person("Anna", 25)\nperson2 = Person("Peter", 99)\nprint(person1)\nprint(person2)

Person('Anna', 25)\nPerson('Peter', 99)

Lägg märke till hur metoden __repr__ själv använder repr-funktionen för att hämta den tekniska representationen av strängen. Detta är nödvändigt för att inkludera tecknen ' i resultatet.

Följande klass har definitioner för både __repr__ och __str__:

class Person:\n    def __init__(self, namn: str, alder: int):\n        self.namn = namn\n        self.alder = alder\n\n    def __repr__(self):\n        return f\"Person({repr(self.namn)}, {self.alder})\"\n\n    def __str__(self):\n        return f\"{self.namn} ({self.alder} år)\"
person = Person("Anna", 25)\nprint(person)\nprint(repr(person))

Anna (25 år)\nPerson('Anna', 25)

Det är värt att nämna att med datastrukturer, såsom listor, använder Python alltid __repr__-metoden för strängrepresentationen av innehållet. Detta kan ibland se lite förvirrande ut:

personer = []\npersoner.append(Person("Anna", 25))\npersoner.append(Person("Peter", 99))\npersoner.append(Person("Maja", 55))\nprint(personer)

[Person('Anna', 25), Person('Peter', 99), Person('Maja', 55)]

Övningsmallen innehåller en mall för en klass som heter Pengar. Den här övningen ber dig att implementera några ytterligare metoder och att åtgärda några små problem i mallen

Fixa strängrepresentationen

Metoden __str__ i klassdefinitionen fungerar inte riktigt som den ska. Med följande två Pengar-objekt skrivs det senare ut på fel sätt:

e1 = Pengar(4, 10)\ne2 = Pengar(2, 5)  # två euro och 5 cent\n\nprint(e1)\nprint(e2)

4.10\n2.5

Fixa metoden så att den skriver ut:

4.10 eur\n2.05 eur

Lika stora mängder

Definiera en ny metod med namnet __eq__(self, annan) som gör att du kan använda jämförelseoperatorn == på Pengar-objekt. Du kan testa din implementation med följande kod:

e1 = Pengar(4, 10)\ne2 = Pengar(2, 5)\ne3 = Pengar(4, 10)\n\nprint(e1)\nprint(e2)\nprint(e3)\nprint(e1 == e2)\nprint(e1 == e3)

4.10 eur\n2.05 eur\n4.10 eur\nFalse\nTrue

Andra jämförelseoperatörer

Implementera även metoder för jämförelseoperatörerna <, > och !=.

e1 = Pengar(4, 10)\ne2 = Pengar(2, 5)\n\nprint(e1 != e2)\nprint(e1 < e2)\nprint(e1 > e2)

True\nFalse\nTrue

Addition och subtraktion

Vänligen implementera additions- och subtraktionsoperatorerna + och - för Pengar-objekt. Båda ska returnera ett nytt objekt av typen Pengar. Varken objektet i sig eller det objekt som skickas som argument ska ändras som ett resultat.

Obs: Värdet på ett Pengar-objekt kan inte vara negativt. Om ett försök att subtrahera skulle resultera i ett negativt resultat ska metoden ge upphov till ett ValueError-undantag.

e1 = Pengar(4, 5)\ne2 = Pengar(2, 95)\n\ne3 = e1 + e2\ne4 = e1 - e2\n\nprint(e3)\nprint(e4)\n\ne5 = e2-e1
7.00 eur\n1.10 eur\nTraceback (most recent call last):\nFile \"fil.py\", line 416, in \ne5 = e2-e1\nFile \"fil.py\", line 404, in sub\nraise ValueError(f\"negativt resultat inte tillåtet\")\nValueError: negativt resultat inte tillåtet

Värdet kan inte bli direkt åtkommet

Klassen har fortfarande ett litet integritetsproblem. Användaren kan ”fuska” genom att komma åt attributen direkt och ändra det värde som lagras i Pengar-objektet:

print(e1)\ne1.euron = 1000\nprint(e1)

4.05 eur\n1000.05 eur

Inkapsla implementeringen av de attribut som definieras i klassen så att fusket ovan inte är möjligt. Klassen ska inte ha några offentliga attribut och inga getter- eller sätter-metoder för euro eller cent.

I den här övningen ska du implementera klassen SimpelDatum som gör att du kan hantera datum. För enkelhetens skull antar vi här att varje månad har 30 dagar.

På grund av denna förenkling bör du inte använda datetime-modulen från Pythons standardbibliotek. Du kommer att implementera liknande funktionalitet själv istället.

Jämförelse

Implementera mallen för klassen, såväl som metoder för jämförelseoperatörerna <, >, == och !=. Exempel på användning:

d1 = SimpelDatum(4, 10, 2020)\nd2 = SimpelDatum(28, 12, 1985)\nd3 = SimpelDatum(28, 12, 1985)\n\nprint(d1)\nprint(d2)\nprint(d1 == d2)\nprint(d1 != d2)\nprint(d1 == d3)\nprint(d1 < d2)\nprint(d1 > d2)

4.10.2020\n28.12.1985\nFalse\nTrue\nFalse\nFalse\nTrue

Ökning

Vänligen implementera additionsoperatorn + som gör att du kan lägga till ett givet antal dagar till ett SimpelDatum-objekt. Operatorn ska returnera ett nytt SimpelDatum-objekt. Det ursprungliga objektet ska inte ändras.

d1 = SimpelDatum(4, 10, 2020)\nd2 = SimpelDatum(28, 12, 1985)\n\nd3 = d1 + 3\nd4 = d2 + 400\n\nprint(d1)\nprint(d2)\nprint(d3)\nprint(d4)

4.10.2020\n28.12.1985\n7.10.2020\n8.2.1987

Skillnad

Vänligen implementera subtraktionsoperatorn - som gör att du kan ta reda på skillnaden i dagar mellan två SimpelDatum-objekt. Eftersom vi antar att varje månad har 30 dagar, är ett år inom ramen för denna övning 12*30 = 360 dagar långt.

Operatorn fungerar enligt följande:

d1 = SimpelDatum(4, 10, 2020)\nd2 = SimpelDatum(2, 11, 2020)\nd3 = SimpelDatum(28, 12, 1985)\n\nprint(d2-d1)\nprint(d1-d2)\nprint(d1-d3)

28\n28\n12516

Iteratorer

Vi vet att for-satsen kan användas för att iterera genom många olika datastrukturer, filer och samlingar av objekt. Ett typiskt användningsfall skulle kunna vara följande funktion:

\ndef rakna_positiva(lista: list):\n    n = 0\n    for foremal in lista:\n        if foremal > 0:\n            n += 1\n    return n\n

Funktionen går igenom objekten i listan ett efter ett och håller reda på hur många av objekten som var positiva.

Det är också möjligt att göra sina egna klasser itererbara. Detta är användbart när klassens huvudsyfte handlar om att lagra en samling objekt. Klassen Bokhylla från ett tidigare exempel skulle vara en bra kandidat, eftersom det skulle vara vettigt att använda en for-loop för att gå igenom böckerna på hyllan. Detsamma gäller för, exempelvis, ett studentregister. Att kunna iterera genom samlingen av studenter kan vara användbart.

För att göra en klass itererbar måste du implementera iteratormetoderna __iter__ och __next__. Vi återkommer till detaljerna i dessa metoder efter följande exempel:

\nclass Bok:\n    def __init__(self, namn: str, forfattare: str, sidor: int):\n        self.namn = namn\n        self.forfattare = forfattare\n        self.sidor = sidor\n\nclass Bokhylla:\n    def __init__(self):\n        self._bocker = []\n\n    def tillsatt_bok(self, bok: Bok):\n        self._bocker.append(bok)\n\n    # Initialiseringsmetoden för iteratorn\n    # Här bör iterationsvariabeln(eller variablerna) initialiseras\n    def __iter__(self):\n        self.n = 0\n        # Metoden returnerar en referens till själva objektet eftersom\n        # iteratorn är implementerad inom samma klassdefinition\n        return self\n\n    # Metoden returnerar nästa föremål inom objektet\n    # Ifall inga föremål är kvar, åstadkomms StopIteration-händelsen\n    def __next__(self):\n        if self.n < len(self._bocker):\n            # Väljer det aktuella föremålet från listan inom objektet\n            bok = self._bocker[self.n]\n            # Öka räknaren med ett\n            self.n += 1\n            # ... och returnera det aktuella föremålet\n            return bok\n        else:\n            # Inga fler böcker\n            raise StopIteration\n

Metoden __iter__ initialiserar iterationsvariabeln eller variablerna. I det här fallet räcker det med att ha en enkel räknare som innehåller index för det aktuella objektet i listan. Vi behöver också metoden __next__, som returnerar nästa objekt i iteratorn. I exemplet ovan returnerar metoden objektet med index n från listan i Bokhylla-objektet, och iteratorvariabeln inkrementeras också.

När alla objekt har genomgåtts utlöser metoden __next__ undantaget StopIteration. Processen skiljer sig inte från andra undantag, men det här undantaget hanteras automatiskt av Python och dess syfte är att signalera till koden som anropar iteratorn (t.ex. en for-loop) att iterationen nu är över.

Vår bokhylla är nu redo för iteration, till exempel med en for-loop:

\nif __name__ == \"__main__\":\n    b1 = Bok(\"Livet av en Python\", \"Peter Python\", 123)\n    b2 = Bok(\"Den gamle och Java\", \"Ernest Hemingjava\", 204)\n    b3 = Bok(\"C-värdheter på nätet\", \"Karl Kodare\", 997)\n\n    hylla = Bokhylla()\n    hylla.tillsatt_bok(b1)\n    hylla.tillsatt_bok(b2)\n    hylla.tillsatt_bok(b3)\n\n    # Skriver ut namnet på alla böcker\n    for bok in hylla:\n        print(bok.namn)\n

Livet av en Python\nDen gamle och Java\nC-värdheter på nätet

I uppgiftsbotten finns klassen Affarslista från övningen i modul 8. Ändra klassen så att den är itererbar och därmed kan användas på följande sätt:

lista = Affarslista()\nlista.tillsatt(\"apelsiner\", 10)\nlista.tillsatt(\"bananer\", 5)\nlista.tillsatt(\"ananas\", 1)\n\nfor produkt in lista:\n    print(f\"{produkt[0]}: {produkt[1]} st\")

apelsiner: 10 st\nbananer: 5 st\nananas: 1 st

Iteratormetoden __next__ ska returnera tuplar, där det första föremålet är namnet på produkten och det andra är antalet.

","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/3-objektorienterade-programmeringstekniker.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Känner du till några av de olika användningsområdena för variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du överlagrar operatorer i dina egna klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa en iterabel klass"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klass kan innehålla en metod som returnerar ett objekt av samma klass. Nedan har vi t.ex. klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"text","value":", vars metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reaprodukt"}]},{"type":"text","value":" returnerar ett nytt Produkt-objekt med samma namn som det ursprungliga men med ett pris som är 25 % lägre:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (pris "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"reaprodukt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n nedsatt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.75"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" nedsatt"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"banan1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbanan2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" banan1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reaprodukt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"banan1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"banan2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Banan (pris 2.99)\nBanan (pris 2.2425)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss gå igenom syftet med variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":": inom en klassdefinition hänvisar den till själva objektet. Vanligtvis används den för att hänvisa till objektets egna egenskaper, dess attribut och metoder. Variabeln kan också användas för att hänvisa till hela objektet, till exempel om själva objektet måste returneras till klientkoden. I exemplet nedan har vi lagt till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"billigare"}]},{"type":"text","value":" i klassdefinitionen. Den tar en annan Produkt som sitt argument och returnerar det billigare av de två:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (pris "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"billigare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" produkt"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"banan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nananas "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ananas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"apelsin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"billigare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"banan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"apelsin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"billigare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ananas"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Banan (2.99)\nApelsin (3.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om det här fungerar bra är det ett mycket specialiserat fall av att jämföra två objekt. Det skulle vara bättre om vi kunde använda Pythons jämförelseoperatorer direkt på dessa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"text","value":"-objekt."}]},{"type":"element","tagName":"h2","properties":{"id":"overlagring-av-operatorer","style":"position:relative;"},"children":[{"type":"text","value":"Överlagring av operatorer"},{"type":"element","tagName":"a","properties":{"href":"#overlagring-av-operatorer","ariaLabel":"overlagring av operatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python innehåller några speciellt namngivna inbyggda metoder för att arbeta med standardoperatorerna för aritmetik och jämförelse. Tekniken kallas operatörsöverlagring (eng. operator overloading). Om du vill kunna använda en viss operator på instanser av självdefinierade klasser kan du skriva en speciell metod som returnerar det korrekta resultatet av operatorn. Vi har redan använt den här tekniken med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" metoden: Python vet att leta efter en metod som heter så här när en strängrepresentation av ett objekt efterfrågas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja med operatorn > som talar om för oss om den första operanden är större än den andra. Klassdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"text","value":" nedan innehåller metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"text","value":", som är en förkortning av greater than. Denna speciellt namngivna metod ska returnera det korrekta resultatet av jämförelsen. Specifikt ska den returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om och endast om det aktuella objektet är större än det objekt som skickas som ett argument. De kriterier som används kan bestämmas av programmeraren. Med aktuellt objekt menar vi det objekt som metoden anropas på med punkt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" notationen"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (pris "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annan_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" annan_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pris"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I implementationen ovan returnerar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om priset på den aktuella produkten är högre än priset på den produkt som skickas som argument. I annat fall returnerar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu finns jämförelseoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" tillgänglig för användning med objekt av typen Produkt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"apelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.90"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbanan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" apelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" banan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin är större\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan är större\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Apelsin är större"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som nämnts ovan är det upp till programmeraren att bestämma vilka kriterier som ska gälla för att avgöra vad som är störst och vad som är minst. Vi kan t.ex. bestämma att ordningen inte ska baseras på pris, utan i stället ska vara alfabetisk enligt namn. Detta skulle innebära att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"banan"}]},{"type":"text","value":" nu skulle vara \"större än\" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"apelsin"}]},{"type":"text","value":", eftersom \"banan\" kommer senare alfabetiskt."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (pris "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pris\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annan_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" annan_produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"apelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.90"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbanan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" apelsin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" banan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apelsin är större\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banan är större\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Banan är större"}]}]},{"type":"element","tagName":"h2","properties":{"id":"fler-operatorer","style":"position:relative;"},"children":[{"type":"text","value":"Fler operatorer"},{"type":"element","tagName":"a","properties":{"href":"#fler-operatorer","ariaLabel":"fler operatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en tabell som innehåller de vanliga jämförelseoperatorerna, tillsammans med de metoder som måste implementeras om vi vill göra dem tillgängliga för användning på våra objekt:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Operator"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Traditionell mening"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Metodens namn"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Mindre än"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__lt__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Större än"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Lika med"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__eq__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Inte lika med"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__ne__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Mindre eller lika med"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__le__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Större eller lika med"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__ge__(self, annan)"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också implementera några andra operatorer, inklusive följande aritmetiska operatorer:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Operator"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Traditionell mening"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Metodens namn"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Addition"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__add__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Subtraktion"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__sub__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Multiplikation"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__mul__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Division (bråktal)"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__truediv__(self, annan)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Division (heltal)"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__floordiv__(self, annan)"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fler operatorer och metodnamn finns lätt att hitta på nätet. Kom också ihåg "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":"-instruktionen för att lista de metoder som är tillgängliga för användning på ett visst objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är mycket sällan nödvändigt att implementera alla aritmetiska operatorer och jämförelseoperatorer i dina egna klasser. Division är t.ex. en operation som sällan är meningsfull utanför numeriska objekt. Vad skulle resultatet av att dividera ett Student-objekt med tre, eller med ett annat Student-objekt bli? Trots detta är vissa av dessa operatorer ofta mycket användbara även i egna klasser. Valet av metoder att implementera beror på vad som är vettigt, med tanke på egenskaperna hos dina objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en klass som modellerar en enda anteckning. Om vi implementerar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__add__"}]},{"type":"text","value":" i vår klassdefinition blir additionsoperatorn + tillgänglig för våra Anteckning-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Anteckning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dtm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dtm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dtm\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" inlagg\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dtm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__add__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Datumet för den nya anteckningen är aktuell tid"}]},{"type":"text","value":"\n nytt_inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nytt_inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"och \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" annan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" nytt_inlagg"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"inlagg1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2016"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kom ihåg att köpa presenter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ninlagg2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Anteckning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2016"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kom ihåg att hämta en gran\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Nu kan dessa anteckningar bli sammanlagda med + operatorn - detta kallar metoden __add__ i Anteckning-klassen"}]},{"type":"text","value":"\nbada "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" inlagg1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" inlagg2\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2020-09-09 14:13:02.163170: Kom ihåg att köpa presenter och Kom ihåg att hämta en gran"}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-strangrepresentation-av-ett-objekt","style":"position:relative;"},"children":[{"type":"text","value":"En strängrepresentation av ett objekt"},{"type":"element","tagName":"a","properties":{"href":"#en-strangrepresentation-av-ett-objekt","ariaLabel":"en strangrepresentation av ett objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du har redan implementerat en hel del "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metoder i dina klasser. Som du vet returnerar metoden en strängrepresentation av objektet. En annan ganska liknande metod är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" som returnerar en teknisk representation av objektet. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" är ofta implementerad så att den returnerar den programkod som kan exekveras för att returnera ett objekt med identiskt innehåll som det aktuella objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"repr"}]},{"type":"text","value":" returnerar denna tekniska strängrepresentation av objektet. Den tekniska representationen används även när metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" inte har definierats för objektet. Exemplet nedan kommer att göra detta tydligare:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Person("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"repr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"person1 = Person(\"Anna\", 25)\nperson2 = Person(\"Peter\", 99)\nprint(person1)\nprint(person2)"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person('Anna', 25)\nPerson('Peter', 99)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg märke till hur metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" själv använder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"repr"}]},{"type":"text","value":"-funktionen för att hämta den tekniska representationen av strängen. Detta är nödvändigt för att inkludera tecknen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"'"}]},{"type":"text","value":" i resultatet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande klass har definitioner för både "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Person("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"repr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år)\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"person = Person(\"Anna\", 25)\nprint(person)\nprint(repr(person))"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anna (25 år)\nPerson('Anna', 25)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är värt att nämna att med datastrukturer, såsom listor, använder Python alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":"-metoden för strängrepresentationen av innehållet. Detta kan ibland se lite förvirrande ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"personer = []\npersoner.append(Person(\"Anna\", 25))\npersoner.append(Person(\"Peter\", 99))\npersoner.append(Person(\"Maja\", 55))\nprint(personer)"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[Person('Anna', 25), Person('Peter', 99), Person('Maja', 55)]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Pengar","tmcname":"osa10-07_pengar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller en mall för en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pengar"}]},{"type":"text","value":". Den här övningen ber dig att implementera några ytterligare metoder och att åtgärda några små problem i mallen"}]},{"type":"element","tagName":"h2","properties":{"id":"fixa-strangrepresentationen","style":"position:relative;"},"children":[{"type":"text","value":"Fixa strängrepresentationen"},{"type":"element","tagName":"a","properties":{"href":"#fixa-strangrepresentationen","ariaLabel":"fixa strangrepresentationen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" i klassdefinitionen fungerar inte riktigt som den ska. Med följande två "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pengar"}]},{"type":"text","value":"-objekt skrivs det senare ut på fel sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# två euro och 5 cent"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10\n2.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fixa metoden så att den skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10 eur\n2.05 eur"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lika-stora-mangder","style":"position:relative;"},"children":[{"type":"text","value":"Lika stora mängder"},{"type":"element","tagName":"a","properties":{"href":"#lika-stora-mangder","ariaLabel":"lika stora mangder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en ny metod med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__eq__(self, annan)"}]},{"type":"text","value":" som gör att du kan använda jämförelseoperatorn == på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pengar"}]},{"type":"text","value":"-objekt. Du kan testa din implementation med följande kod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" e3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10 eur\n2.05 eur\n4.10 eur\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"andra-jamforelseoperatorer","style":"position:relative;"},"children":[{"type":"text","value":"Andra jämförelseoperatörer"},{"type":"element","tagName":"a","properties":{"href":"#andra-jamforelseoperatorer","ariaLabel":"andra jamforelseoperatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera även metoder för jämförelseoperatörerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"addition-och-subtraktion","style":"position:relative;"},"children":[{"type":"text","value":"Addition och subtraktion"},{"type":"element","tagName":"a","properties":{"href":"#addition-och-subtraktion","ariaLabel":"addition och subtraktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen implementera additions- och subtraktionsoperatorerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" för Pengar-objekt. Båda ska returnera ett nytt objekt av typen Pengar. Varken objektet i sig eller det objekt som skickas som argument ska ändras som ett resultat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs: Värdet på ett Pengar-objekt kan inte vara negativt. Om ett försök att subtrahera skulle resultera i ett negativt resultat ska metoden ge upphov till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ne3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" e2\ne4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" e2\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ne5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"e1"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"7.00 eur\n1.10 eur\nTraceback (most recent call last):\nFile \"fil.py\", line 416, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\ne5 = e2-e1\nFile \"fil.py\", line 404, in "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sub"}]},{"type":"text","value":"\nraise ValueError(f\"negativt resultat inte tillåtet\")\nValueError: negativt resultat inte tillåtet"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"vardet-kan-inte-bli-direkt-atkommet","style":"position:relative;"},"children":[{"type":"text","value":"Värdet kan inte bli direkt åtkommet"},{"type":"element","tagName":"a","properties":{"href":"#vardet-kan-inte-bli-direkt-atkommet","ariaLabel":"vardet kan inte bli direkt atkommet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen har fortfarande ett litet integritetsproblem. Användaren kan ”fuska” genom att komma åt attributen direkt och ändra det värde som lagras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pengar"}]},{"type":"text","value":"-objektet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"euron "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.05 eur\n1000.05 eur"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/3-inkapsling","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Inkapsla"}]},{"type":"text","value":" implementeringen av de attribut som definieras i klassen så att fusket ovan inte är möjligt. Klassen ska inte ha några offentliga attribut och inga getter- eller sätter-metoder för euro eller cent."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"SimpelDatum","tmcname":"osa10-08_simpeldatum"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen ska du implementera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SimpelDatum"}]},{"type":"text","value":" som gör att du kan hantera datum. För enkelhetens skull antar vi här att "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"varje månad har 30 dagar"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På grund av denna förenkling bör du inte använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-modulen från Pythons standardbibliotek. Du kommer att implementera liknande funktionalitet själv istället."}]},{"type":"element","tagName":"h2","properties":{"id":"jamforelse","style":"position:relative;"},"children":[{"type":"text","value":"Jämförelse"},{"type":"element","tagName":"a","properties":{"href":"#jamforelse","ariaLabel":"jamforelse permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera mallen för klassen, såväl som metoder för jämförelseoperatörerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":". Exempel på användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" d3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10.2020\n28.12.1985\nFalse\nTrue\nFalse\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"okning","style":"position:relative;"},"children":[{"type":"text","value":"Ökning"},{"type":"element","tagName":"a","properties":{"href":"#okning","ariaLabel":"okning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen implementera additionsoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" som gör att du kan lägga till ett givet antal dagar till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SimpelDatum"}]},{"type":"text","value":"-objekt. Operatorn ska returnera ett nytt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SimpelDatum"}]},{"type":"text","value":"-objekt. Det ursprungliga objektet ska inte ändras."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nd4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" d2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"400"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10.2020\n28.12.1985\n7.10.2020\n8.2.1987"}]}]},{"type":"element","tagName":"h2","properties":{"id":"skillnad","style":"position:relative;"},"children":[{"type":"text","value":"Skillnad"},{"type":"element","tagName":"a","properties":{"href":"#skillnad","ariaLabel":"skillnad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen implementera subtraktionsoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" som gör att du kan ta reda på skillnaden i dagar mellan två SimpelDatum-objekt. Eftersom vi antar att varje månad har 30 dagar, är ett år inom ramen för denna övning 12*30 = 360 dagar långt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Operatorn fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpelDatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"28\n28\n12516"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"iteratorer","style":"position:relative;"},"children":[{"type":"text","value":"Iteratorer"},{"type":"element","tagName":"a","properties":{"href":"#iteratorer","ariaLabel":"iteratorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi vet att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-satsen kan användas för att iterera genom många olika datastrukturer, filer och samlingar av objekt. Ett typiskt användningsfall skulle kunna vara följande funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_positiva"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen går igenom objekten i listan ett efter ett och håller reda på hur många av objekten som var positiva."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att göra sina egna klasser itererbara. Detta är användbart när klassens huvudsyfte handlar om att lagra en samling objekt. Klassen Bokhylla från ett tidigare exempel skulle vara en bra kandidat, eftersom det skulle vara vettigt att använda en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop för att gå igenom böckerna på hyllan. Detsamma gäller för, exempelvis, ett studentregister. Att kunna iterera genom samlingen av studenter kan vara användbart."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att göra en klass itererbar måste du implementera iteratormetoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":". Vi återkommer till detaljerna i dessa metoder efter följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sidor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" forfattare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sidor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sidor\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Initialiseringsmetoden för iteratorn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Här bör iterationsvariabeln(eller variablerna) initialiseras"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar en referens till själva objektet eftersom"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# iteratorn är implementerad inom samma klassdefinition"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar nästa föremål inom objektet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall inga föremål är kvar, åstadkomms StopIteration-händelsen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__next__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Väljer det aktuella föremålet från listan inom objektet"}]},{"type":"text","value":"\n bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Öka räknaren med ett"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ... och returnera det aktuella föremålet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bok\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Inga fler böcker"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" StopIteration\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"text","value":" initialiserar iterationsvariabeln eller variablerna. I det här fallet räcker det med att ha en enkel räknare som innehåller index för det aktuella objektet i listan. Vi behöver också metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":", som returnerar nästa objekt i iteratorn. I exemplet ovan returnerar metoden objektet med index n från listan i Bokhylla-objektet, och iteratorvariabeln inkrementeras också."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När alla objekt har genomgåtts utlöser metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":" undantaget "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StopIteration"}]},{"type":"text","value":". Processen skiljer sig inte från andra undantag, men det här undantaget hanteras automatiskt av Python och dess syfte är att signalera till koden som anropar iteratorn (t.ex. en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop) att iterationen nu är över."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vår bokhylla är nu redo för iteration, till exempel med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Livet av en Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"123"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den gamle och Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ernest Hemingjava\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"204"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C-värdheter på nätet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Karl Kodare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n hylla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bokhylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriver ut namnet på alla böcker"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Livet av en Python\nDen gamle och Java\nC-värdheter på nätet"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Itererbar affärslista","tmcname":"osa10-09_itererbar_affarslista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" från "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-8/2-klasser-och-objekt","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"övningen i modul 8"}]},{"type":"text","value":". Ändra klassen så att den är itererbar och därmed kan användas på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsiner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ananas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" st\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"apelsiner: 10 st\nbananer: 5 st\nananas: 1 st"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Iteratormetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":" ska returnera tuplar, där det första föremålet är namnet på produkten och det andra är antalet."}]}]}]},"html":"

Efter den här delen

    \n
  • Känner du till några av de olika användningsområdena för variabelnamnet self
  • \n
  • Vet du hur du överlagrar operatorer i dina egna klasser
  • \n
  • Kommer du att kunna skapa en iterabel klass
  • \n

En klass kan innehålla en metod som returnerar ett objekt av samma klass. Nedan har vi t.ex. klassen Produkt, vars metod reaprodukt returnerar ett nytt Produkt-objekt med samma namn som det ursprungliga men med ett pris som är 25 % lägre:

class Produkt:\n    def __init__(self, namn: str, pris: float):\n        self.__namn = namn\n        self.__pris = pris\n\n    def __str__(self):\n        return f\"{self.__namn} (pris {self.__pris})\"\n\n    def reaprodukt(self):\n        nedsatt = Produkt(self.__namn, self.__pris * 0.75)\n        return nedsatt
banan1 = Produkt(\"Banan\", 2.99)\nbanan2 = banan1.reaprodukt()\nprint(banan1)\nprint(banan2)

Banan (pris 2.99)\nBanan (pris 2.2425)

Låt oss gå igenom syftet med variabeln self: inom en klassdefinition hänvisar den till själva objektet. Vanligtvis används den för att hänvisa till objektets egna egenskaper, dess attribut och metoder. Variabeln kan också användas för att hänvisa till hela objektet, till exempel om själva objektet måste returneras till klientkoden. I exemplet nedan har vi lagt till metoden billigare i klassdefinitionen. Den tar en annan Produkt som sitt argument och returnerar det billigare av de två:

class Produkt:\n    def __init__(self, namn: str, pris: float):\n        self.__namn = namn\n        self.__pris = pris\n\n    def __str__(self):\n        return f\"{self.__namn} (pris {self.__pris})\"\n\n    @property\n    def pris(self):\n        return self.__pris\n\n    def billigare(self, produkt):\n        if self.__pris < produkt.pris:\n            return self\n        else:\n            return produkt
banan = Produkt(\"Banan\", 2.99)\napelsin = Produkt(\"Apelsin\", 3.95)\nananas = Produkt(\"Ananas\", 5.25)\n\nprint(apelsin.billigare(banan))\nprint(apelsin.billigare(ananas))

Banan (2.99)\nApelsin (3.95)

Även om det här fungerar bra är det ett mycket specialiserat fall av att jämföra två objekt. Det skulle vara bättre om vi kunde använda Pythons jämförelseoperatorer direkt på dessa Produkt-objekt.

Överlagring av operatorer

Python innehåller några speciellt namngivna inbyggda metoder för att arbeta med standardoperatorerna för aritmetik och jämförelse. Tekniken kallas operatörsöverlagring (eng. operator overloading). Om du vill kunna använda en viss operator på instanser av självdefinierade klasser kan du skriva en speciell metod som returnerar det korrekta resultatet av operatorn. Vi har redan använt den här tekniken med __str__ metoden: Python vet att leta efter en metod som heter så här när en strängrepresentation av ett objekt efterfrågas.

Låt oss börja med operatorn > som talar om för oss om den första operanden är större än den andra. Klassdefinitionen Produkt nedan innehåller metoden __gt__, som är en förkortning av greater than. Denna speciellt namngivna metod ska returnera det korrekta resultatet av jämförelsen. Specifikt ska den returnera True om och endast om det aktuella objektet är större än det objekt som skickas som ett argument. De kriterier som används kan bestämmas av programmeraren. Med aktuellt objekt menar vi det objekt som metoden anropas på med punkt . notationen

class Produkt:\n    def __init__(self, namn: str, pris: float):\n        self.__namn = namn\n        self.__pris = pris\n\n    def __str__(self):\n        return f\"{self.__namn} (pris {self.__pris})\"\n\n    @property\n    def pris(self):\n        return self.__pris\n\n    def __gt__(self, annan_produkt):\n        return self.pris > annan_produkt.pris

I implementationen ovan returnerar metoden __gt__ True om priset på den aktuella produkten är högre än priset på den produkt som skickas som argument. I annat fall returnerar metoden False.

Nu finns jämförelseoperatorn > tillgänglig för användning med objekt av typen Produkt:

apelsin = Produkt(\"Apelsin\", 4.90)\nbanan = Produkt(\"Banan\", 3.95)\n\nif apelsin > banan:\n    print(\"Apelsin är större\")\nelse:\n    print(\"Banan är större\")

Apelsin är större

Som nämnts ovan är det upp till programmeraren att bestämma vilka kriterier som ska gälla för att avgöra vad som är störst och vad som är minst. Vi kan t.ex. bestämma att ordningen inte ska baseras på pris, utan i stället ska vara alfabetisk enligt namn. Detta skulle innebära att banan nu skulle vara \"större än\" apelsin, eftersom \"banan\" kommer senare alfabetiskt.

class Produkt:\n    def __init__(self, namn: str, pris: float):\n        self.__namn = namn\n        self.__pris = pris\n\n    def __str__(self):\n        return f\"{self.__namn} (pris {self.__pris})\"\n\n    @property\n    def pris(self):\n        return self.__pris\n\n    @property\n    def namn(self):\n        return self.__namn\n\n    def __gt__(self, annan_produkt):\n        return self.namn > annan_produkt.namn
apelsin = Produkt(\"Apelsin\", 4.90)\nbanan = Produkt(\"Banan\", 3.95)\n\nif apelsin > banan:\n    print(\"Apelsin är större\")\nelse:\n    print(\"Banan är större\")

Banan är större

Fler operatorer

Här har vi en tabell som innehåller de vanliga jämförelseoperatorerna, tillsammans med de metoder som måste implementeras om vi vill göra dem tillgängliga för användning på våra objekt:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperatorTraditionell meningMetodens namn
<Mindre än__lt__(self, annan)
>Större än__gt__(self, annan)
==Lika med__eq__(self, annan)
!=Inte lika med__ne__(self, annan)
<=Mindre eller lika med__le__(self, annan)
>=Större eller lika med__ge__(self, annan)

Du kan också implementera några andra operatorer, inklusive följande aritmetiska operatorer:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperatorTraditionell meningMetodens namn
+Addition__add__(self, annan)
-Subtraktion__sub__(self, annan)
*Multiplikation__mul__(self, annan)
/Division (bråktal)__truediv__(self, annan)
//Division (heltal)__floordiv__(self, annan)

Fler operatorer och metodnamn finns lätt att hitta på nätet. Kom också ihåg dir-instruktionen för att lista de metoder som är tillgängliga för användning på ett visst objekt.

Det är mycket sällan nödvändigt att implementera alla aritmetiska operatorer och jämförelseoperatorer i dina egna klasser. Division är t.ex. en operation som sällan är meningsfull utanför numeriska objekt. Vad skulle resultatet av att dividera ett Student-objekt med tre, eller med ett annat Student-objekt bli? Trots detta är vissa av dessa operatorer ofta mycket användbara även i egna klasser. Valet av metoder att implementera beror på vad som är vettigt, med tanke på egenskaperna hos dina objekt.

Låt oss ta en titt på en klass som modellerar en enda anteckning. Om vi implementerar metoden __add__ i vår klassdefinition blir additionsoperatorn + tillgänglig för våra Anteckning-objekt:

from datetime import datetime\n\nclass Anteckning:\n    def __init__(self, dtm: datetime, inlagg: str):\n        self.dtm = dtm\n        self.inlagg = inlagg\n\n    def __str__(self):\n        return f\"{self.dtm}: {self.inlagg}\"\n\n    def __add__(self, annan):\n        # Datumet för den nya anteckningen är aktuell tid\n        nytt_inlagg = Anteckning(datetime.now(), \"\")\n        nytt_inlagg.inlagg = self.inlagg + \"och \" + annan.inlagg\n        return nytt_inlagg
inlagg1 = Anteckning(datetime(2016, 12, 17), \"Kom ihåg att köpa presenter\")\ninlagg2 = Anteckning(datetime(2016, 12, 23), \"Kom ihåg att hämta en gran\")\n\n# Nu kan dessa anteckningar bli sammanlagda med + operatorn - detta kallar metoden __add__ i Anteckning-klassen\nbada = inlagg1 + inlagg2\nprint(bada)

2020-09-09 14:13:02.163170: Kom ihåg att köpa presenter och Kom ihåg att hämta en gran

En strängrepresentation av ett objekt

Du har redan implementerat en hel del __str__-metoder i dina klasser. Som du vet returnerar metoden en strängrepresentation av objektet. En annan ganska liknande metod är __repr__ som returnerar en teknisk representation av objektet. Metoden __repr__ är ofta implementerad så att den returnerar den programkod som kan exekveras för att returnera ett objekt med identiskt innehåll som det aktuella objektet.

Funktionen repr returnerar denna tekniska strängrepresentation av objektet. Den tekniska representationen används även när metoden __str__ inte har definierats för objektet. Exemplet nedan kommer att göra detta tydligare:

class Person:\n    def __init__(self, namn: str, alder: int):\n        self.namn = namn\n        self.alder = alder\n\n    def __repr__(self):\n        return f\"Person({repr(self.namn)}, {self.alder})\"
person1 = Person("Anna", 25)\nperson2 = Person("Peter", 99)\nprint(person1)\nprint(person2)

Person('Anna', 25)\nPerson('Peter', 99)

Lägg märke till hur metoden __repr__ själv använder repr-funktionen för att hämta den tekniska representationen av strängen. Detta är nödvändigt för att inkludera tecknen ' i resultatet.

Följande klass har definitioner för både __repr__ och __str__:

class Person:\n    def __init__(self, namn: str, alder: int):\n        self.namn = namn\n        self.alder = alder\n\n    def __repr__(self):\n        return f\"Person({repr(self.namn)}, {self.alder})\"\n\n    def __str__(self):\n        return f\"{self.namn} ({self.alder} år)\"
person = Person("Anna", 25)\nprint(person)\nprint(repr(person))

Anna (25 år)\nPerson('Anna', 25)

Det är värt att nämna att med datastrukturer, såsom listor, använder Python alltid __repr__-metoden för strängrepresentationen av innehållet. Detta kan ibland se lite förvirrande ut:

personer = []\npersoner.append(Person("Anna", 25))\npersoner.append(Person("Peter", 99))\npersoner.append(Person("Maja", 55))\nprint(personer)

[Person('Anna', 25), Person('Peter', 99), Person('Maja', 55)]

Övningsmallen innehåller en mall för en klass som heter Pengar. Den här övningen ber dig att implementera några ytterligare metoder och att åtgärda några små problem i mallen

Fixa strängrepresentationen

Metoden __str__ i klassdefinitionen fungerar inte riktigt som den ska. Med följande två Pengar-objekt skrivs det senare ut på fel sätt:

e1 = Pengar(4, 10)\ne2 = Pengar(2, 5)  # två euro och 5 cent\n\nprint(e1)\nprint(e2)

4.10\n2.5

Fixa metoden så att den skriver ut:

4.10 eur\n2.05 eur

Lika stora mängder

Definiera en ny metod med namnet __eq__(self, annan) som gör att du kan använda jämförelseoperatorn == på Pengar-objekt. Du kan testa din implementation med följande kod:

e1 = Pengar(4, 10)\ne2 = Pengar(2, 5)\ne3 = Pengar(4, 10)\n\nprint(e1)\nprint(e2)\nprint(e3)\nprint(e1 == e2)\nprint(e1 == e3)

4.10 eur\n2.05 eur\n4.10 eur\nFalse\nTrue

Andra jämförelseoperatörer

Implementera även metoder för jämförelseoperatörerna <, > och !=.

e1 = Pengar(4, 10)\ne2 = Pengar(2, 5)\n\nprint(e1 != e2)\nprint(e1 < e2)\nprint(e1 > e2)

True\nFalse\nTrue

Addition och subtraktion

Vänligen implementera additions- och subtraktionsoperatorerna + och - för Pengar-objekt. Båda ska returnera ett nytt objekt av typen Pengar. Varken objektet i sig eller det objekt som skickas som argument ska ändras som ett resultat.

Obs: Värdet på ett Pengar-objekt kan inte vara negativt. Om ett försök att subtrahera skulle resultera i ett negativt resultat ska metoden ge upphov till ett ValueError-undantag.

e1 = Pengar(4, 5)\ne2 = Pengar(2, 95)\n\ne3 = e1 + e2\ne4 = e1 - e2\n\nprint(e3)\nprint(e4)\n\ne5 = e2-e1
7.00 eur\n1.10 eur\nTraceback (most recent call last):\nFile \"fil.py\", line 416, in \ne5 = e2-e1\nFile \"fil.py\", line 404, in sub\nraise ValueError(f\"negativt resultat inte tillåtet\")\nValueError: negativt resultat inte tillåtet

Värdet kan inte bli direkt åtkommet

Klassen har fortfarande ett litet integritetsproblem. Användaren kan ”fuska” genom att komma åt attributen direkt och ändra det värde som lagras i Pengar-objektet:

print(e1)\ne1.euron = 1000\nprint(e1)

4.05 eur\n1000.05 eur

Inkapsla implementeringen av de attribut som definieras i klassen så att fusket ovan inte är möjligt. Klassen ska inte ha några offentliga attribut och inga getter- eller sätter-metoder för euro eller cent.

I den här övningen ska du implementera klassen SimpelDatum som gör att du kan hantera datum. För enkelhetens skull antar vi här att varje månad har 30 dagar.

På grund av denna förenkling bör du inte använda datetime-modulen från Pythons standardbibliotek. Du kommer att implementera liknande funktionalitet själv istället.

Jämförelse

Implementera mallen för klassen, såväl som metoder för jämförelseoperatörerna <, >, == och !=. Exempel på användning:

d1 = SimpelDatum(4, 10, 2020)\nd2 = SimpelDatum(28, 12, 1985)\nd3 = SimpelDatum(28, 12, 1985)\n\nprint(d1)\nprint(d2)\nprint(d1 == d2)\nprint(d1 != d2)\nprint(d1 == d3)\nprint(d1 < d2)\nprint(d1 > d2)

4.10.2020\n28.12.1985\nFalse\nTrue\nFalse\nFalse\nTrue

Ökning

Vänligen implementera additionsoperatorn + som gör att du kan lägga till ett givet antal dagar till ett SimpelDatum-objekt. Operatorn ska returnera ett nytt SimpelDatum-objekt. Det ursprungliga objektet ska inte ändras.

d1 = SimpelDatum(4, 10, 2020)\nd2 = SimpelDatum(28, 12, 1985)\n\nd3 = d1 + 3\nd4 = d2 + 400\n\nprint(d1)\nprint(d2)\nprint(d3)\nprint(d4)

4.10.2020\n28.12.1985\n7.10.2020\n8.2.1987

Skillnad

Vänligen implementera subtraktionsoperatorn - som gör att du kan ta reda på skillnaden i dagar mellan två SimpelDatum-objekt. Eftersom vi antar att varje månad har 30 dagar, är ett år inom ramen för denna övning 12*30 = 360 dagar långt.

Operatorn fungerar enligt följande:

d1 = SimpelDatum(4, 10, 2020)\nd2 = SimpelDatum(2, 11, 2020)\nd3 = SimpelDatum(28, 12, 1985)\n\nprint(d2-d1)\nprint(d1-d2)\nprint(d1-d3)

28\n28\n12516

Iteratorer

Vi vet att for-satsen kan användas för att iterera genom många olika datastrukturer, filer och samlingar av objekt. Ett typiskt användningsfall skulle kunna vara följande funktion:

\ndef rakna_positiva(lista: list):\n    n = 0\n    for foremal in lista:\n        if foremal > 0:\n            n += 1\n    return n\n

Funktionen går igenom objekten i listan ett efter ett och håller reda på hur många av objekten som var positiva.

Det är också möjligt att göra sina egna klasser itererbara. Detta är användbart när klassens huvudsyfte handlar om att lagra en samling objekt. Klassen Bokhylla från ett tidigare exempel skulle vara en bra kandidat, eftersom det skulle vara vettigt att använda en for-loop för att gå igenom böckerna på hyllan. Detsamma gäller för, exempelvis, ett studentregister. Att kunna iterera genom samlingen av studenter kan vara användbart.

För att göra en klass itererbar måste du implementera iteratormetoderna __iter__ och __next__. Vi återkommer till detaljerna i dessa metoder efter följande exempel:

\nclass Bok:\n    def __init__(self, namn: str, forfattare: str, sidor: int):\n        self.namn = namn\n        self.forfattare = forfattare\n        self.sidor = sidor\n\nclass Bokhylla:\n    def __init__(self):\n        self._bocker = []\n\n    def tillsatt_bok(self, bok: Bok):\n        self._bocker.append(bok)\n\n    # Initialiseringsmetoden för iteratorn\n    # Här bör iterationsvariabeln(eller variablerna) initialiseras\n    def __iter__(self):\n        self.n = 0\n        # Metoden returnerar en referens till själva objektet eftersom\n        # iteratorn är implementerad inom samma klassdefinition\n        return self\n\n    # Metoden returnerar nästa föremål inom objektet\n    # Ifall inga föremål är kvar, åstadkomms StopIteration-händelsen\n    def __next__(self):\n        if self.n < len(self._bocker):\n            # Väljer det aktuella föremålet från listan inom objektet\n            bok = self._bocker[self.n]\n            # Öka räknaren med ett\n            self.n += 1\n            # ... och returnera det aktuella föremålet\n            return bok\n        else:\n            # Inga fler böcker\n            raise StopIteration\n

Metoden __iter__ initialiserar iterationsvariabeln eller variablerna. I det här fallet räcker det med att ha en enkel räknare som innehåller index för det aktuella objektet i listan. Vi behöver också metoden __next__, som returnerar nästa objekt i iteratorn. I exemplet ovan returnerar metoden objektet med index n från listan i Bokhylla-objektet, och iteratorvariabeln inkrementeras också.

När alla objekt har genomgåtts utlöser metoden __next__ undantaget StopIteration. Processen skiljer sig inte från andra undantag, men det här undantaget hanteras automatiskt av Python och dess syfte är att signalera till koden som anropar iteratorn (t.ex. en for-loop) att iterationen nu är över.

Vår bokhylla är nu redo för iteration, till exempel med en for-loop:

\nif __name__ == \"__main__\":\n    b1 = Bok(\"Livet av en Python\", \"Peter Python\", 123)\n    b2 = Bok(\"Den gamle och Java\", \"Ernest Hemingjava\", 204)\n    b3 = Bok(\"C-värdheter på nätet\", \"Karl Kodare\", 997)\n\n    hylla = Bokhylla()\n    hylla.tillsatt_bok(b1)\n    hylla.tillsatt_bok(b2)\n    hylla.tillsatt_bok(b3)\n\n    # Skriver ut namnet på alla böcker\n    for bok in hylla:\n        print(bok.namn)\n

Livet av en Python\nDen gamle och Java\nC-värdheter på nätet

I uppgiftsbotten finns klassen Affarslista från övningen i modul 8. Ändra klassen så att den är itererbar och därmed kan användas på följande sätt:

lista = Affarslista()\nlista.tillsatt(\"apelsiner\", 10)\nlista.tillsatt(\"bananer\", 5)\nlista.tillsatt(\"ananas\", 1)\n\nfor produkt in lista:\n    print(f\"{produkt[0]}: {produkt[1]} st\")

apelsiner: 10 st\nbananer: 5 st\nananas: 1 st

Iteratormetoden __next__ ska returnera tuplar, där det första föremålet är namnet på produkten och det andra är antalet.

","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/3-objektorienterade-programmeringstekniker.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-10/4-storre-applikation/page-data.json b/page-data/osa-10/4-storre-applikation/page-data.json index 9f68356c2..b31c04e28 100644 --- a/page-data/osa-10/4-storre-applikation/page-data.json +++ b/page-data/osa-10/4-storre-applikation/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-10/4-storre-applikation", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Känner du till några grundläggande principer för applikationsutveckling"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att känna dig bekväm med att skilja mellan de olika delarna av en applikation (användargränssnitt, programlogik och filhantering)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Har du övat på att skriva din egna lite större applikation"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills i detta kursmaterial har vi gått igenom ett stort antal Python-funktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Introduktion till programmering-kursen introducerades kontrollstrukturer som while och for, funktioner samt grundläggande datastrukturer som listor, tupler och ordlistor. I princip är dessa verktyg allt som behövs för att uttrycka vad som helst som en programmerare kan tänka sig vilja uttrycka med Python."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På denna avancerade kurs i programmering, med början i modul 8 av materialet, har du blivit bekant med klasser och objekt. Låt oss ta en stund att fundera på när och varför de är nödvändiga, ifall de grundläggande verktygen från modul 1 till 7 borde räcka."}]},{"type":"element","tagName":"h2","properties":{"id":"att-hantera-komplexitet","style":"position:relative;"},"children":[{"type":"text","value":"Att hantera komplexitet"},{"type":"element","tagName":"a","properties":{"href":"#att-hantera-komplexitet","ariaLabel":"att hantera komplexitet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objekt och klasser är långt ifrån nödvändiga i alla programmeringssammanhang. Om du t.ex. programmerar ett litet skript för engångsbruk är objekt oftast överflödiga. Men när du ska programmera något större och mer komplicerat blir objekten mycket användbara."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmen blir allt mer komplexa blir mängden detaljer snabbt ohanterlig, såvida inte programmet är organiserat på något systematiskt sätt. Även några av de mer komplicerade övningarna på den här kursen hittills skulle ha haft nytta av de exempel som ges i den här delen av materialet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I flera decennier har begreppet "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Inkapsling_(Separation_of_Concerns)","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Separation of concerns"}]},{"type":"text","value":" varit en av de centrala principerna inom programmering och inom datavetenskapen i stort. Citat från Wikipedia:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Separation of concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Genom att dela upp programmet i olika delar, så att varje del har sina egna problem att hantera, blir det lättare att hantera den oundvikliga komplexiteten i ett datorprogram."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner är ett sätt att organisera ett program i distinkta, hanterbara helheter. I stället för att skriva ett enda skript är tanken att formulera små, separat verifierbara funktioner som var och en löser en del av det större problemet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En annan vanlig metod för att hantera större program är objekt, genom objektorienterade programmeringsprinciper. Det finns fördelar och nackdelar med båda metoderna, och varje programmerare har sin egen favorit. Som vi har sett hittills kan vi med hjälp av objekt och klasser samla alla data och den kod som bearbetar dessa data i en enda enhet, i ett objekts attribut och metoder. Dessutom ger objekten ett sätt att kapsla in de data de kontrollerar, så att andra delar av programmet inte behöver oroa sig för de interna detaljerna i ett objekt."}]},{"type":"element","tagName":"h2","properties":{"id":"ett-fungerande-exempel-telefonkatalog","style":"position:relative;"},"children":[{"type":"text","value":"Ett fungerande exempel: telefonkatalog"},{"type":"element","tagName":"a","properties":{"href":"#ett-fungerande-exempel-telefonkatalog","ariaLabel":"ett fungerande exempel telefonkatalog permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur ska ett program delas in i klasser och objekt? Det här är inte alls en enkel fråga med ett enda godtagbart svar, så vi fortsätter med ett exempel. I del fem genomförde du en "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-5/3-lexikon","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"telefonkatalogsapplikation"}]},{"type":"text","value":", och nu ska vi genomföra något liknande med hjälp av objektorienterade programmeringsprinciper."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enligt principen om separation of concerns bör ett program delas upp i sektioner som var och en har sin egen sak att ta hand om. I objektorienterad programmering översätts detta till "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Single_responsibility_principle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"principen om ett ansvar"}]},{"type":"text","value":". Utan att gå in på detaljerna framgår det grundläggande syftet redan av namnet: en enda klass och de objekt som skapas utifrån den ska ha ett enda ansvar i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objektorienterad programmering används ofta som ett sätt att modellera objekt och fenomen i den verkliga världen. Ett enskilt objekt i den verkliga världen modelleras med en enda klass i programkoden. I fallet med en telefonkatalog kan sådana objekt vara"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en person"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett telefonnummer"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett namn och ett telefonnummer kan uppfattas som data som inte förtjänar egna klasser, men en person är en distinkt fysisk enhet i den verkliga världen, och i programmeringsvärlden skulle den kunna fungera som en klass. Ett Person-objekt skulle vara ansvarigt för att knyta ihop ett namn och de telefonnummer som är kopplade till det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En telefonkatalog i sig skulle kunna vara en bra kandidat för en klass. Dess ansvar skulle vara att hantera olika personobjekt och de data de innehåller."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu har vi skissat kärnan i vår applikation: telefonkatalog och person utgör programmeringslogiken i vår applikation, eller applikationslogiken i korthet. Vår applikation skulle behöva några andra klasser också."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är oftast en bra idé att hålla all interaktion med en användare skild från applikationslogiken. Det är ju trots allt ett helt eget ansvar. Förutom den centrala applikationslogiken bör vårt program därför innehålla en klass som hanterar användargränssnittet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom bör vår telefonkatalog ha någon form av beständig lagring mellan exekveringar. Filhanteringen är återigen ett tydligt separat ansvar, så den förtjänar en egen klass."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu när vi har en översikt över de grundläggande komponenterna i vårt program uppstår frågan: var ska vi börja programmera? Inte heller här finns det något rätt eller fel svar, men det är ofta en bra idé att börja med någon del av programlogiken."}]},{"type":"element","tagName":"h2","properties":{"id":"steg-1-en-skiss-for-applikationslogiken","style":"position:relative;"},"children":[{"type":"text","value":"Steg 1: en skiss för applikationslogiken"},{"type":"element","tagName":"a","properties":{"href":"#steg-1-en-skiss-for-applikationslogiken","ariaLabel":"steg 1 en skiss for applikationslogiken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja med klassen Telefonkatalog. En skelettimplementering skulle kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna klass består av en lista med personer samt metoder för att både lägga till och hämta data."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje person kan vara kopplad till flera nummer, så låt oss implementera den interna strukturen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"personer"}]},{"type":"text","value":" med en ordlista. En ordlista ger oss möjlighet att söka efter nycklar enligt namn, och värdet som är kopplat till en ordlistas nyckel kan vara en lista. Hittills ser det ut som att vi inte behöver en separat klass för att representera en person – ett inlägg i en ordlista räcker."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss implementera de metoder som listas ovan och testa vår telefonkatalog:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# till personen är en lista med nummer anknyten"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testkod"}]},{"type":"text","value":"\nkatalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"02-123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde utskriva följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['02-123456']\nNone"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_nummer"}]},{"type":"text","value":" returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" om ett namn inte finns med i telefonkatalogen. Om namnet finns returneras listan med de nummer som är kopplade till namnet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man gör ändringar i ett program är det alltid värt att testa att koden fungerar som förväntat innan man går vidare med andra ändringar. Den kod som används för testning är vanligtvis något som raderas strax efteråt, och därför kanske du tycker att det inte är värt besväret att skriva några tester i första hand. I de flesta fall är detta inte sant. Testning är en förutsättning för bra programmeringsresultat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En bugg i programmet bör fångas upp och åtgärdas så snart som möjligt. Om du tar för vana att kontrollera funktionaliteten i praktiskt taget varje ny kodrad kommer du att upptäcka att buggarna oftast är lätta att hitta och åtgärda, eftersom du kan vara helt säker på att buggen orsakades av den senaste ändringen. Om du bara testar programmet efter att ha lagt till dussintals rader kod ökar de potentiella källorna till buggar också med dussintals gånger."}]},{"type":"element","tagName":"h2","properties":{"id":"steg-2-en-skiss-for-anvandargranssnittet","style":"position:relative;"},"children":[{"type":"text","value":"Steg 2: en skiss för användargränssnittet"},{"type":"element","tagName":"a","properties":{"href":"#steg-2-en-skiss-for-anvandargranssnittet","ariaLabel":"steg 2 en skiss for anvandargranssnittet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med den grundläggande applikationslogiken ur vägen är det dags att implementera ett textbaserat användargränssnitt. Vi kommer att behöva en ny klass, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":", med följande inledande funktionalitet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hjalp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktioner: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0 avsluta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet gör inte så mycket ännu, men låt oss gå igenom innehållet. Konstruktormetoden skapar en ny Telefonkatalog, som lagras i ett privat attribut. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exekvera(self)"}]},{"type":"text","value":" startar programmets textbaserade användargränssnitt, vars kärna är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loopen, som fortsätter att be användaren om instruktioner tills de skriver in instruktionen för att avsluta. Det finns också en metod för instruktioner, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hjalp(self)"}]},{"type":"text","value":", som anropas innan man går in i loopen, så att instruktionerna skrivs ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss nu lägga till lite faktisk funktionalitet. Först implementerar vi att kunna lägga till nya data i telefonkatalogen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hjalp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktioner: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0 avsluta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1 tillsätt inlägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nummer: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren skriver in 1 för att lägga till ett nytt nummer, frågar användargränssnittet efter ett namn och ett nummer och lägger till dessa i telefonkatalogen med hjälp av den lämpliga metod som definieras i klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Användargränssnittets enda ansvar är att kommunicera med användaren. All annan funktionalitet, t.ex. att lagra ett nytt namn- och nummerpar, är Telefonkatalog-objektets ansvar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns utrymme för förbättringar i strukturen i vår användargränssnittsklass. Låt oss skapa en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inlagg(self)"}]},{"type":"text","value":" som hanterar instruktionen för att lägga till ett nytt inlägg:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hjalp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktioner: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0 avsluta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1 tillsätt inlägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# separation of concerns i praktiken: en ny metod för att lägga till ett inlägg"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"inlagg"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nummer: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Separation of concerns-principen sträcker sig även till metodnivå. Vi skulle kunna ha hela användargränssnittets funktionalitet i en enda komplicerad "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loop, men det är bättre att separera varje funktionalitet i en egen metod. Ansvaret för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exekvera()"}]},{"type":"text","value":"-metoden är bara att delegera de instruktioner som användaren skriver in till relevanta metoder. Detta hjälper till att hantera den växande komplexiteten i vårt program. Om vi till exempel senare vill ändra hur det fungerar att lägga till inlägg, är det omedelbart klart att vi då måste fokusera våra ansträngningar på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inlagg()"}]},{"type":"text","value":"-metoden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss inkludera funktionalitet för att söka efter inlägg i vårt användargränssnitt. Detta bör också ha sin egen metod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hjalp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktioner: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0 avsluta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1 tillsätt inlägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"2 sök\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"inlagg"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nummer: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n numren "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" numren "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nummer okänd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har nu en enkel fungerande telefonkatalogsapplikation som är redo för testning. Följande är ett exempel på en exekvering:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 tillsätt inlägg\n2 sök"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-4356713"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\n02-123456\n045-4356713"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: Emilia\nnummer okänd"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För en så enkel applikation har vi skrivit ganska mycket kod. Om vi hade skrivit allt i den enda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loopen hade vi förmodligen kunnat komma undan med mycket mindre kod. Det är dock ganska lätt att läsa koden, strukturen är tydlig och vi borde inte ha några problem med att lägga till nya funktioner."}]},{"type":"element","tagName":"h2","properties":{"id":"steg-3-importera-data-fran-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Steg 3: Importera data från en fil"},{"type":"element","tagName":"a","properties":{"href":"#steg-3-importera-data-fran-en-fil","ariaLabel":"steg 3 importera data fran en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi redan har några telefonnummer lagrade i en fil, och att vi vill läsa detta när programmet startar. Datafilen är i följande CSV-format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Erik;02-1234567;045-4356713"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Emilia;040-324344"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hantering av filer är helt klart ett eget ansvarsområde, så det förtjänar en klass för sig:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ladda"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namnen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":"numren "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar\n namnen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" numren\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" namnen"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstruktormetoden tar namnet på filen som sitt argument. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ladda(self)"}]},{"type":"text","value":" läser innehållet i filen. Varje rad delas upp i två delar: ett namn och en lista med siffror. Sedan läggs dessa till i en ordbok, med namnet som nyckel och listan som värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden använder en smidig Python-funktion: det är möjligt att först välja några objekt från en lista separat och sedan ta resten av objekten i en ny lista. Du kan se ett exempel på detta nedan. Du kanske minns från "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/1-lasa-filer","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":" att strängmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" returnerar en lista."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nforsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" andra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":"resten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"forsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"andra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2\n[3, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" framför variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resten"}]},{"type":"text","value":" i ovanstående exempel betyder att den sista variabeln ska innehålla alla återstående poster i listan, från den tredje och framåt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi bör absolut testa filhanteraren separat innan vi inkluderar den i vår applikation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"t "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ladda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Erik': ['02-1234567', '045-4356713'], 'Emilia': ['040-324344']}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom filhanteraren verkar fungera bra kan vi lägga till den i vår applikation. Låt oss anta att vi vill läsa filen som det första varje gång programmet körs. Den logiska platsen för att läsa filen skulle vara konstruktören för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till namn och nummer från filen till telefonkatalogen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numren "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ladda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# annan kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna funktionalitet bör också testas. När vi har försäkrat oss om att filens innehåll är tillgängligt via användargränssnittet i vår applikation kan vi gå vidare till nästa steg."}]},{"type":"element","tagName":"h2","properties":{"id":"steg-4-exportera-data-till-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Steg 4: exportera data till en fil"},{"type":"element","tagName":"a","properties":{"href":"#steg-4-exportera-data-till-en-fil","ariaLabel":"steg 4 exportera data till en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den sista funktionen i vår grundversion av applikationen är att spara innehållet i telefonkatalogen tillbaka i samma fil som data lästes från."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta innebär en förändring av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen. Vi måste kunna exportera innehållet i telefonkatalogen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# returnera alla inlägg (i ordlistsformat)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alla_uppgifter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Själva sparandet till filen bör hanteras av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"text","value":"-klassen. Låt oss lägga till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spara"}]},{"type":"text","value":" som tar en ordlistsrepresentation av telefonkatalogen som argument:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ladda"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spara"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dict"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numren "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" numren\n f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sparandet bör ske när programmet avslutas. Låt oss lägga till en metod för detta ändamål i användargränssnittet och anropa den innan vi bryter ut ur "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loopen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# resten av koden för användargränssnittet"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metod som exekveras till programmet avslutas"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"avsluta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spara"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alla_uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"avsluta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Utveckling av telefonkatalogen, del 1","tmcname":"osa10-10_telefonkatalog_del1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen kommer du att skapa en liten utvidgning av telefonkatalogapplikationen. Koden från exemplet ovan finns i övningsmallen. Lägg till ett kommando som låter användaren söka efter nummer i telefonkatalogen. Efter tillägget ska applikationen fungera på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 tillsätt inlägg\n2 sök\n3 sök enligt nummer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-4356713"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]},{"type":"text","value":"\nErik"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0100100"}]},{"type":"text","value":"\nokänt nummer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera detta tillägg med hänsyn till den nuvarande strukturen i programmet. Detta innebär att du i klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":" bör lägga till en lämplig hjälpmetod för att möjliggöra den nya funktionaliteten, och även lägga till en ny gren i while-loopen. I "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen bör du lägga till en metod som gör det möjligt att söka med ett nummer."}]}]},{"type":"element","tagName":"h2","properties":{"id":"objekt-i-en-ordlista","style":"position:relative;"},"children":[{"type":"text","value":"Objekt i en ordlista"},{"type":"element","tagName":"a","properties":{"href":"#objekt-i-en-ordlista","ariaLabel":"objekt i en ordlista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I nästa övning ombeds du att ändra din telefonkatalog så att värdena i ordlistan är objekt, inte listor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är inget konstigt med det i sig, men det är första gången på den här kursen som något sådant föreslås, så låt oss gå igenom ett enklare exempel innan vi dyker ner i övningen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en applikation som håller reda på hur många övningar som studenterna har gjort på en kurs. Varje elevs antal övningar lagras i ett enkelt objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Uppgiftsraknare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"klar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"gjorda"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__uppgifter"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande huvudfunktion använder klassen ovan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studeranden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"markerar uppgifter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studerande: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skapa ett nytt objekt ifall det inte finns ännu"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till en ny utförd uppgift i räknaren"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"klar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"gjorda uppgifter:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" uppgifter "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" st\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att exekvera koden ovan kunde se ut enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"markerar uppgifter\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sara"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"anton"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sara"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"anton"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sara"}]},{"type":"text","value":"\nstuderande:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"gjorda uppgifter:\npeter uppgifter 1 st\nanton uppgifter 2 st\nsara uppgifter 3 st\njonas uppgifter 2 st"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns ett par saker att tänka på i exemplet ovan. När användaren matar in ett namn kontrollerar programmet först om namnet redan är en nyckel i ordlistan. Om namnet inte finns skapas ett nytt objekt som läggs till som en post i ordlistan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter detta kan vi vara säkra på att objektet existerar, kopplat till namnet på den student som används som nyckel. Antingen skapades det precis, eller så fanns det redan från en tidigare iteration av loopen. I vilket fall som helst kan vi nu hämta objektet med nyckeln och kalla metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"klar"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"klar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Raden ovan innehåller egentligen två separata händelser. Vi kan lika gärna använda en hjälpvariabel och skriva den på två separata kodrader:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerandes_raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstuderandes_raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"klar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Även om objektet här tilldelas en hjälpvariabel, så finns objektet kvar i ordlistan precis som tidigare. Hjälparvariabeln innehåller en referens till objektet i ordlistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du inte är helt säker på vad som egentligen händer i koden ovan kan du prova med "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyget"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Utveckling av telefonkatalogen, del 2","tmcname":"osa10-11_telefonkatalog_del2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna övning kommer du att skapa en annan version av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":". Du kommer att lägga till adresser i datan som kan kopplas till ett namn. För enkelhetens skull har funktionaliteten för att spara till fil tagits bort, och vissa andra metoder har bytt namn för att bättre passa in i förändringen."}]},{"type":"element","tagName":"h2","properties":{"id":"en-separat-klass-for-en-persons-data","style":"position:relative;"},"children":[{"type":"text","value":"En separat klass för en persons data"},{"type":"element","tagName":"a","properties":{"href":"#en-separat-klass-for-en-persons-data","ariaLabel":"en separat klass for en persons data permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ändra hur data om en person hanteras. Implementera en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":", som tar hand om personers telefonnummer och adresser. Klassen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"adress"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"040-123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_adress"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mannerheimintie 10 Helsinki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"adress"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik\n[]\nNone\n['040-123456']\nMannerheimintie 10 Helsinki"}]}]},{"type":"element","tagName":"h2","properties":{"id":"telefonkatalogs-anvandning-av-klassen-person","style":"position:relative;"},"children":[{"type":"text","value":"Telefonkatalogs användning av klassen Person"},{"type":"element","tagName":"a","properties":{"href":"#telefonkatalogs-anvandning-av-klassen-person","ariaLabel":"telefonkatalogs anvandning av klassen person permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ändra den interna implementeringen av din applikation så att din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klass använder objekt av klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" för att lagra data i telefonkatalogen. Det vill säga, attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__personer"}]},{"type":"text","value":" ska fortfarande innehålla en ordlista, men värdena ska vara Person-objekt och inte listor. Användaren av din applikation ska inte märka någon skillnad; ändringarna får inte påverka användargränssnittet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"VARNING:"}]},{"type":"text","value":" när du gör strukturella ändringar i din kod, som beskrivs i den här övningen, ska du alltid ta små steg och testa i varje möjligt skede. Försök inte att göra alla ändringar på en gång. Det är ett säkert sätt att "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"stöta på allvarliga problem med din kod"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett lämpligt första steg kan vara att skriva lite kod för att kontrollera funktionaliteten i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen direkt. Till exempel bör följande åtminstone inte orsaka några fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"02-123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg märke till det nya namnet på metoden för att hämta ett inlägg från telefonkatalogen. De automatiska testerna kontrollerar inte vad utskriften från din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_uppgifter"}]},{"type":"text","value":"-metod är, utan ser till att inga fel uppstår av ovanstående kod och att resultatet är logiskt inom din implementation."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du har gjort de nödvändiga ändringarna i ditt program och absolut har verifierat funktionaliteten i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen kan du gå vidare till användargränssnittet och se om allt fortfarande fungerar som förväntat."}]},{"type":"element","tagName":"h2","properties":{"id":"tillagg-av-adress","style":"position:relative;"},"children":[{"type":"text","value":"Tillägg av adress"},{"type":"element","tagName":"a","properties":{"href":"#tillagg-av-adress","ariaLabel":"tillagg av adress permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen implementera funktionaliteten för att lägga till en adress till ett inlägg i din telefonkatalog. Programmet ska fungera på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 tillsätt nummer\n2 sök\n3 tillsätt adress"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emilia"}]},{"type":"text","value":"\nadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Viherlaaksontie 7, Espoo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\n02-123456\nadress okänd"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emilia"}]},{"type":"text","value":"\nnummer okänd\nViherlaaksontie 7, Espoo"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Linnankatu 75, Turku"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: 2\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\n02-123456\nLinnankatu 75, Turku"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Wilhelm"}]},{"type":"text","value":"\nadress okänd\nnummer okänd"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"VARNING och tips:"}]},{"type":"text","value":" som nämndes ovan i den föregående övningen, försök inte att göra alla ändringar på en gång. Det är ett säkert sätt att "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"stöta på allvarliga problem med din kod"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kontrollera först att du på ett tillförlitligt sätt kan lägga till adresser med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen direkt. När du har verifierat detta kan du gå vidare till de nödvändiga ändringarna i användargränssnittet."}]}]},{"type":"element","tagName":"h2","properties":{"id":"nagra-avslutande-anmarkningar","style":"position:relative;"},"children":[{"type":"text","value":"Några avslutande anmärkningar"},{"type":"element","tagName":"a","properties":{"href":"#nagra-avslutande-anmarkningar","ariaLabel":"nagra avslutande anmarkningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strukturen i Telefonkatalog-exemplet ovan följer de grundläggande principerna för objektorienterad programmering ganska väl. Den centrala principen är att identifiera de olika ansvarsområdena i programmet och fördela dessa logiskt mellan de olika klasserna och metoderna. Ett av de viktigaste motiven för denna uppdelning är att hantera komplexiteten. Ett annat viktigt motiv är att en logisk uppdelning av ansvarsområden - modularitet i facktermer - ofta gör koden lättare att underhålla och bygga vidare på."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I de programvarupaket som utvecklas och används ute i världen är underhåll och utbyggnad, dvs felsökning av befintlig programvara och implementering av nya funktioner, den absolut dyraste delen av utvecklingen. Korrekt implementerad modularitet är ekonomiskt sett en mycket viktig funktion i programvaruutvecklingen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns några fler objektorienterade programmeringsprinciper som är värda att lyfta fram här. Telefonkatalog är ett bra exempel på hur den centrala programlogiken kan (och bör) separeras från både användargränssnitt och datalagring. Detta är viktigt på grund av ett par olika skäl. För det första gör denna separation det möjligt att testa koden i mindre enheter, en klass och metod i taget. För det andra, eftersom kärnlogiken nu är oberoende av gränssnitten som kommer till omvärlden, är det möjligt att i viss utsträckning ändra implementeringen av antingen kärnlogiken eller gränssnitten utan att hela applikationen går sönder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filhanteringen i Telefonkatalog-programmet går till på följande sätt: Programmet läser filen en enda gång, när det startar. Efter detta lagras all data i variabler i programmet. När programmet avslutas lagras alla data igen, vilket i praktiken innebär att filen skrivs över. I de flesta fall är detta det rekommenderade sättet att hantera externa filer, eftersom det ofta är mycket mer komplicerat att redigera data på plats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns många bra guideböcker för att lära sig om god programmeringspraxis. En sådan är "},{"type":"element","tagName":"a","properties":{"href":"https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Clean Code"}]},{"type":"text","value":" av Robert Martin. Kodexemplen i boken är dock implementerade i Java, så att arbeta igenom exemplen kan vara ganska besvärligt vid denna tidpunkt i din programmeringskarriär, även om boken i sig rekommenderas mycket av kurspersonalen. Teman som lätt underhållen, utbyggbar kod av god kvalitet kommer att utforskas ytterligare på kurserna "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/opintotarjonta/cu/hy-CU-118024742-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Development Methods"}]},{"type":"text","value":" och "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/opintotarjonta/cu/hy-CU-118024909-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skriva kod enligt etablerade principer för objektorienterad programmering har ett pris. Du kommer sannolikt att få skriva mer kod än om du skulle skriva din implementation i en enda kontinuerlig stöt av spaghettikod. En av de viktigaste färdigheterna hos en programmerare är att bestämma det bästa tillvägagångssättet för varje situation. Ibland är det nödvändigt att bara hacka ihop något snabbt för omedelbar användning. Å andra sidan, om det inom överskådlig framtid kan förväntas att koden kommer att återanvändas, underhållas eller vidareutvecklas, antingen av dig eller, mer kritiskt, av någon helt annan, blir programkodens läsbarhet och logiska modularitet väsentlig. Oftast är det så att om det är värt att göra, så är det värt att göra det bra, även i de allra tidigaste utvecklingsstadierna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att avsluta denna del av materialet kommer du att implementera ytterligare en större applikation."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Studieregister","tmcname":"osa10-12_studieregister"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv en interaktiv applikation för att hålla koll på dina studier. Den interna strukturen är upp till dig, men det här skulle vara ett bra tillfälle att öva på att skapa en liknande struktur som i Telefonkatalog-exemplet ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ditt program bör fungera på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 tillsätt prestation\n2 hämta prestation\n3 statistik\n0 avsluta"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 sp) vitsord 3"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 sp) vitsord 5"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 sp) vitsord 5"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Introduktion till Java"}]},{"type":"text","value":"\ninga prestationer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tira"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tilpe"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Lapio"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Lama"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n5 avklarade kurser, totalt 29 studiepoäng\nmedeltal 3.4\nvitsordsdistribution\n5: xx\n4: x\n3:\n2: x\n1: x"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje kursnamn ska resultera i ett enda tillägg i registret. Ett betyg kan höjas genom att kursuppgifterna skrivs in på nytt, men betyget får aldrig sänkas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna övning är värd två övningspoäng. Den första ges om kommandona 1, 2 och 0 fungerar korrekt i ditt program. Det andra ges om även kommando 3 fungerar som förväntat."}]}]},{"type":"element","tagName":"h2","properties":{"id":"epilog","style":"position:relative;"},"children":[{"type":"text","value":"Epilog"},{"type":"element","tagName":"a","properties":{"href":"#epilog","ariaLabel":"epilog permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att avsluta den här delen av materialet återvänder vi till användargränssnittet i telefonkatalogsexemplet för en stund."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# annan kod"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"-objekt innehåller både ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-objekt och ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"text","value":"-objekt. Namnet på den fil som skickas till FilHanterare är för närvarande hårdkodat i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"-klassen. Detta är en helt irrelevant detalj när det gäller applikationens användargränssnitt. Faktum är att den bryter mot principen om separation of concerns: var ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-objekt sparar sitt innehåll ska inte vara en angelägenhet för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":", men om vi vill ändra platsen måste vi ändra koden för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det skulle vara en bättre idé att skapa ett FilHanterare-objekt någonstans utanför "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"-klassen och skicka det som ett argument till applikationen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# annan kod"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skapar ett objekt för att hantera sparning av filen"}]},{"type":"text","value":"\nforvaringstjanst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# och passerar det som argument till TelefonkatalogApplikation-objektets konstruktor"}]},{"type":"text","value":"\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"forvaringstjanst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta tar bort ett onödigt beroende från "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"-klassen. Om namnet på filen ändras behöver användargränssnittet inte längre ändras. Vi behöver bara skicka ett annat argument till konstruktören:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# annan kod"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använd ett annat filnamn"}]},{"type":"text","value":"\nforvaringstjanst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"forvaringstjanst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna förändring gör det också möjligt för oss att överväga mer exotiska lagringsplatser, till exempel en molntjänst på internet. Vi behöver bara implementera en klass som använder molntjänsten och erbjuder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":" exakt samma metoder som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En instans av denna nya \"moln hanterar\"-klass kan skickas som ett argument till konstruktören och inte en enda rad kod behöver ändras i användargränssnittet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"MolnHanterare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod som sparar innehållet av telefonkatalogen i en molntjänst på internet"}]},{"type":"text","value":"\n\nforvaringstjanst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MolnHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"amazon-cloud\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"anvandarnamn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"losenord\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"forvaringstjanst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du har sett tidigare har den här typen av tekniker ett pris, eftersom det blir mer kod att skriva, så en programmerare måste överväga om det är en acceptabel avvägning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den teknik som beskrivs ovan kallas beroendeinjektion. Som namnet antyder är tanken att alla beroenden som krävs av ett objekt ska tillhandahållas utanför objektet. Det är ett mycket användbart verktyg i en programmerares verktygslåda, eftersom det gör det lättare att implementera nya funktioner i program och underlättar automatisk testning. Detta tema kommer att utforskas ytterligare på de tidigare nämnda kurserna "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/opintotarjonta/cu/hy-CU-118024742-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Development"}]},{"type":"text","value":" och "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/opintotarjonta/cu/hy-CU-118024909-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara vänligen på en snabb enkät om denna del av kursen."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Känner du till några grundläggande principer för applikationsutveckling
  • \n
  • Kommer du att känna dig bekväm med att skilja mellan de olika delarna av en applikation (användargränssnitt, programlogik och filhantering)
  • \n
  • Har du övat på att skriva din egna lite större applikation
  • \n

Hittills i detta kursmaterial har vi gått igenom ett stort antal Python-funktioner.

I Introduktion till programmering-kursen introducerades kontrollstrukturer som while och for, funktioner samt grundläggande datastrukturer som listor, tupler och ordlistor. I princip är dessa verktyg allt som behövs för att uttrycka vad som helst som en programmerare kan tänka sig vilja uttrycka med Python.

På denna avancerade kurs i programmering, med början i modul 8 av materialet, har du blivit bekant med klasser och objekt. Låt oss ta en stund att fundera på när och varför de är nödvändiga, ifall de grundläggande verktygen från modul 1 till 7 borde räcka.

Att hantera komplexitet

Objekt och klasser är långt ifrån nödvändiga i alla programmeringssammanhang. Om du t.ex. programmerar ett litet skript för engångsbruk är objekt oftast överflödiga. Men när du ska programmera något större och mer komplicerat blir objekten mycket användbara.

När programmen blir allt mer komplexa blir mängden detaljer snabbt ohanterlig, såvida inte programmet är organiserat på något systematiskt sätt. Även några av de mer komplicerade övningarna på den här kursen hittills skulle ha haft nytta av de exempel som ges i den här delen av materialet.

I flera decennier har begreppet Separation of concerns varit en av de centrala principerna inom programmering och inom datavetenskapen i stort. Citat från Wikipedia:

Separation of concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program

Genom att dela upp programmet i olika delar, så att varje del har sina egna problem att hantera, blir det lättare att hantera den oundvikliga komplexiteten i ett datorprogram.

Funktioner är ett sätt att organisera ett program i distinkta, hanterbara helheter. I stället för att skriva ett enda skript är tanken att formulera små, separat verifierbara funktioner som var och en löser en del av det större problemet.

En annan vanlig metod för att hantera större program är objekt, genom objektorienterade programmeringsprinciper. Det finns fördelar och nackdelar med båda metoderna, och varje programmerare har sin egen favorit. Som vi har sett hittills kan vi med hjälp av objekt och klasser samla alla data och den kod som bearbetar dessa data i en enda enhet, i ett objekts attribut och metoder. Dessutom ger objekten ett sätt att kapsla in de data de kontrollerar, så att andra delar av programmet inte behöver oroa sig för de interna detaljerna i ett objekt.

Ett fungerande exempel: telefonkatalog

Hur ska ett program delas in i klasser och objekt? Det här är inte alls en enkel fråga med ett enda godtagbart svar, så vi fortsätter med ett exempel. I del fem genomförde du en telefonkatalogsapplikation, och nu ska vi genomföra något liknande med hjälp av objektorienterade programmeringsprinciper.

Enligt principen om separation of concerns bör ett program delas upp i sektioner som var och en har sin egen sak att ta hand om. I objektorienterad programmering översätts detta till principen om ett ansvar. Utan att gå in på detaljerna framgår det grundläggande syftet redan av namnet: en enda klass och de objekt som skapas utifrån den ska ha ett enda ansvar i programmet.

Objektorienterad programmering används ofta som ett sätt att modellera objekt och fenomen i den verkliga världen. Ett enskilt objekt i den verkliga världen modelleras med en enda klass i programkoden. I fallet med en telefonkatalog kan sådana objekt vara

    \n
  • en person
  • \n
  • ett namn
  • \n
  • ett telefonnummer
  • \n

Ett namn och ett telefonnummer kan uppfattas som data som inte förtjänar egna klasser, men en person är en distinkt fysisk enhet i den verkliga världen, och i programmeringsvärlden skulle den kunna fungera som en klass. Ett Person-objekt skulle vara ansvarigt för att knyta ihop ett namn och de telefonnummer som är kopplade till det.

En telefonkatalog i sig skulle kunna vara en bra kandidat för en klass. Dess ansvar skulle vara att hantera olika personobjekt och de data de innehåller.

Nu har vi skissat kärnan i vår applikation: telefonkatalog och person utgör programmeringslogiken i vår applikation, eller applikationslogiken i korthet. Vår applikation skulle behöva några andra klasser också.

Det är oftast en bra idé att hålla all interaktion med en användare skild från applikationslogiken. Det är ju trots allt ett helt eget ansvar. Förutom den centrala applikationslogiken bör vårt program därför innehålla en klass som hanterar användargränssnittet.

Dessutom bör vår telefonkatalog ha någon form av beständig lagring mellan exekveringar. Filhanteringen är återigen ett tydligt separat ansvar, så den förtjänar en egen klass.

Nu när vi har en översikt över de grundläggande komponenterna i vårt program uppstår frågan: var ska vi börja programmera? Inte heller här finns det något rätt eller fel svar, men det är ofta en bra idé att börja med någon del av programlogiken.

Steg 1: en skiss för applikationslogiken

Låt oss börja med klassen Telefonkatalog. En skelettimplementering skulle kunna se ut så här:

class Telefonkatalog:\n    def __init__(self):\n        self.__personer = []\n\n    def tillsatt_nummer(self, namn: str, nummer: str):\n        pass\n\n    def hamta_nummer(self, namn: str):\n        pass\n

Denna klass består av en lista med personer samt metoder för att både lägga till och hämta data.

Varje person kan vara kopplad till flera nummer, så låt oss implementera den interna strukturen för personer med en ordlista. En ordlista ger oss möjlighet att söka efter nycklar enligt namn, och värdet som är kopplat till en ordlistas nyckel kan vara en lista. Hittills ser det ut som att vi inte behöver en separat klass för att representera en person – ett inlägg i en ordlista räcker.

Låt oss implementera de metoder som listas ovan och testa vår telefonkatalog:

class Telefonkatalog:\n    def __init__(self):\n        self.__personer = {}\n\n    def tillsatt_nummer(self, namn: str, nummer: str):\n        if not namn in self.__personer:\n            # till personen är en lista med nummer anknyten\n            self.__personer[namn] = []\n\n        self.__personer[namn].append(nummer)\n\n    def hamta_nummer(self, namn: str):\n        if not namn in self.__personer:\n            return None\n\n        return self.__personer[namn]\n\n# testkod\nkatalog = Telefonkatalog()\nkatalog.tillsatt_nummer(\"Erik\", \"02-123456\")\nprint(katalog.hamta_nummer(\"Erik\"))\nprint(katalog.hamta_nummer(\"Emilia\"))

Detta borde utskriva följande:

['02-123456']\nNone

Metoden hamta_nummer returnerar None om ett namn inte finns med i telefonkatalogen. Om namnet finns returneras listan med de nummer som är kopplade till namnet.

När man gör ändringar i ett program är det alltid värt att testa att koden fungerar som förväntat innan man går vidare med andra ändringar. Den kod som används för testning är vanligtvis något som raderas strax efteråt, och därför kanske du tycker att det inte är värt besväret att skriva några tester i första hand. I de flesta fall är detta inte sant. Testning är en förutsättning för bra programmeringsresultat.

En bugg i programmet bör fångas upp och åtgärdas så snart som möjligt. Om du tar för vana att kontrollera funktionaliteten i praktiskt taget varje ny kodrad kommer du att upptäcka att buggarna oftast är lätta att hitta och åtgärda, eftersom du kan vara helt säker på att buggen orsakades av den senaste ändringen. Om du bara testar programmet efter att ha lagt till dussintals rader kod ökar de potentiella källorna till buggar också med dussintals gånger.

Steg 2: en skiss för användargränssnittet

Med den grundläggande applikationslogiken ur vägen är det dags att implementera ett textbaserat användargränssnitt. Vi kommer att behöva en ny klass, TelefonkatalogApplikation, med följande inledande funktionalitet:

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n\n    def hjalp(self):\n        print(\"instruktioner: \")\n        print(\"0 avsluta\")\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n                break\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Det här programmet gör inte så mycket ännu, men låt oss gå igenom innehållet. Konstruktormetoden skapar en ny Telefonkatalog, som lagras i ett privat attribut. Metoden exekvera(self) startar programmets textbaserade användargränssnitt, vars kärna är while-loopen, som fortsätter att be användaren om instruktioner tills de skriver in instruktionen för att avsluta. Det finns också en metod för instruktioner, hjalp(self), som anropas innan man går in i loopen, så att instruktionerna skrivs ut.

Låt oss nu lägga till lite faktisk funktionalitet. Först implementerar vi att kunna lägga till nya data i telefonkatalogen:

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n\n    def hjalp(self):\n        print(\"instruktioner: \")\n        print(\"0 avsluta\")\n        print(\"1 tillsätt inlägg\")\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n                break\n            elif instruktion == \"1\":\n                namn = input(\"namn: \")\n                nummer = input(\"nummer: \")\n                self.__katalog.tillsatt_nummer(namn, nummer)\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Om användaren skriver in 1 för att lägga till ett nytt nummer, frågar användargränssnittet efter ett namn och ett nummer och lägger till dessa i telefonkatalogen med hjälp av den lämpliga metod som definieras i klassen.

Användargränssnittets enda ansvar är att kommunicera med användaren. All annan funktionalitet, t.ex. att lagra ett nytt namn- och nummerpar, är Telefonkatalog-objektets ansvar.

Det finns utrymme för förbättringar i strukturen i vår användargränssnittsklass. Låt oss skapa en metod inlagg(self) som hanterar instruktionen för att lägga till ett nytt inlägg:

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n\n    def hjalp(self):\n        print(\"instruktioner: \")\n        print(\"0 avsluta\")\n        print(\"1 tillsätt inlägg\")\n\n    # separation of concerns i praktiken: en ny metod för att lägga till ett inlägg\n    def inlagg(self):\n        namn = input(\"namn: \")\n        nummer = input(\"nummer: \")\n        self.__katalog.tillsatt_nummer(namn, nummer)\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n                break\n            elif instruktion == \"1\":\n                self.inlagg()\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Separation of concerns-principen sträcker sig även till metodnivå. Vi skulle kunna ha hela användargränssnittets funktionalitet i en enda komplicerad while-loop, men det är bättre att separera varje funktionalitet i en egen metod. Ansvaret för exekvera()-metoden är bara att delegera de instruktioner som användaren skriver in till relevanta metoder. Detta hjälper till att hantera den växande komplexiteten i vårt program. Om vi till exempel senare vill ändra hur det fungerar att lägga till inlägg, är det omedelbart klart att vi då måste fokusera våra ansträngningar på inlagg()-metoden.

Låt oss inkludera funktionalitet för att söka efter inlägg i vårt användargränssnitt. Detta bör också ha sin egen metod:

\nclass TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n\n    def hjalp(self):\n        print(\"instruktioner: \")\n        print(\"0 avsluta\")\n        print(\"1 tillsätt inlägg\")\n        print(\"2 sök\")\n\n    def inlagg(self):\n        namn = input(\"namn: \")\n        nummer = input(\"nummer: \")\n        self.__katalog.tillsatt_nummer(namn, nummer)\n\n    def sok(self):\n        namn = input(\"namn: \")\n        numren = self.__katalog.hamta_nummer(namn)\n        if numren == None:\n            print(\"nummer okänd\")\n            return\n        for nummer in numren:\n            print(nummer)\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n                break\n            elif instruktion == \"1\":\n                self.inlagg()\n            elif instruktion == \"2\":\n                self.sok()\n            else:\n                self.hjalp()\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Vi har nu en enkel fungerande telefonkatalogsapplikation som är redo för testning. Följande är ett exempel på en exekvering:

instruktioner:\n0 avsluta\n1 tillsätt inlägg\n2 sök

instruktion: 1\nnamn: Erik\nnummer: 02-123456

instruktion: 1\nnamn: Erik\nnummer: 045-4356713

instruktion: 2\nnamn: Erik\n02-123456\n045-4356713

instruktion: 2\nnamn: Emilia\nnummer okänd

instruktion: 0

För en så enkel applikation har vi skrivit ganska mycket kod. Om vi hade skrivit allt i den enda while-loopen hade vi förmodligen kunnat komma undan med mycket mindre kod. Det är dock ganska lätt att läsa koden, strukturen är tydlig och vi borde inte ha några problem med att lägga till nya funktioner.

Steg 3: Importera data från en fil

Låt oss anta att vi redan har några telefonnummer lagrade i en fil, och att vi vill läsa detta när programmet startar. Datafilen är i följande CSV-format:

Erik;02-1234567;045-4356713\nEmilia;040-324344

Hantering av filer är helt klart ett eget ansvarsområde, så det förtjänar en klass för sig:

class FilHanterare():\n    def __init__(self, fil):\n        self.__fil = fil\n\n    def ladda(self):\n        namnen = {}\n        with open(self.__fil) as f:\n            for rad in f:\n                delar = rad.strip().split(';')\n                namn, *numren = delar\n                namnen[namn] = numren\n\n        return namnen

Konstruktormetoden tar namnet på filen som sitt argument. Metoden ladda(self) läser innehållet i filen. Varje rad delas upp i två delar: ett namn och en lista med siffror. Sedan läggs dessa till i en ordbok, med namnet som nyckel och listan som värde.

Metoden använder en smidig Python-funktion: det är möjligt att först välja några objekt från en lista separat och sedan ta resten av objekten i en ny lista. Du kan se ett exempel på detta nedan. Du kanske minns från modul 6 att strängmetoden split returnerar en lista.

lista = [1, 2, 3, 4, 5]\nforsta, andra, *resten = lista\nprint(forsta)\nprint(andra)\nprint(resten)

1\n2\n[3, 4, 5]

Tecknet * framför variabelnamnet resten i ovanstående exempel betyder att den sista variabeln ska innehålla alla återstående poster i listan, från den tredje och framåt.

Vi bör absolut testa filhanteraren separat innan vi inkluderar den i vår applikation:

t = FilHanterare(\"katalog.txt\")\nprint(t.ladda())

{'Erik': ['02-1234567', '045-4356713'], 'Emilia': ['040-324344']}

Eftersom filhanteraren verkar fungera bra kan vi lägga till den i vår applikation. Låt oss anta att vi vill läsa filen som det första varje gång programmet körs. Den logiska platsen för att läsa filen skulle vara konstruktören för klassen TelefonkatalogApplikation:

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n        self.__fil = FilHanterare(\"katalog.txt\")\n\n        # lägg till namn och nummer från filen till telefonkatalogen\n        for namn, numren in self.__fil.ladda().items():\n            for nummer in numren:\n                self.__katalog.tillsatt_nummer(namn, nummer)\n\n    # annan kod

Denna funktionalitet bör också testas. När vi har försäkrat oss om att filens innehåll är tillgängligt via användargränssnittet i vår applikation kan vi gå vidare till nästa steg.

Steg 4: exportera data till en fil

Den sista funktionen i vår grundversion av applikationen är att spara innehållet i telefonkatalogen tillbaka i samma fil som data lästes från.

Detta innebär en förändring av Telefonkatalog-klassen. Vi måste kunna exportera innehållet i telefonkatalogen:

class Telefonkatalog:\n    def __init__(self):\n        self.__personer = {}\n\n    # ...\n\n    # returnera alla inlägg (i ordlistsformat)\n    def alla_uppgifter(self):\n        return self.__personer

Själva sparandet till filen bör hanteras av FilHanterare-klassen. Låt oss lägga till metoden spara som tar en ordlistsrepresentation av telefonkatalogen som argument:

class FilHanterare():\n    def __init__(self, fil):\n        self.__fil = fil\n\n    def ladda(self):\n        # ...\n\n    def spara(self, katalog: dict):\n        with open(self.__fil, \"w\") as f:\n            for namn, numren in katalog.items():\n                rad = [namn] + numren\n                f.write(\";\".join(rad) + \"\\n\")

Sparandet bör ske när programmet avslutas. Låt oss lägga till en metod för detta ändamål i användargränssnittet och anropa den innan vi bryter ut ur while-loopen:

\nclass TelefonkatalogApplikation:\n    # resten av koden för användargränssnittet\n\n    # metod som exekveras till programmet avslutas\n    def avsluta(self):\n        self.__fil.spara(self.__katalog.alla_uppgifter())\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n\n                self.avsluta()\n                break\n            elif instruktion == \"1\":\n                self.inlagg()\n            elif instruktion == \"2\":\n                self.sok()\n            else:\n                self.hjalp()

I den här övningen kommer du att skapa en liten utvidgning av telefonkatalogapplikationen. Koden från exemplet ovan finns i övningsmallen. Lägg till ett kommando som låter användaren söka efter nummer i telefonkatalogen. Efter tillägget ska applikationen fungera på följande sätt:

instruktioner:\n0 avsluta\n1 tillsätt inlägg\n2 sök\n3 sök enligt nummer

instruktion: 1\nnamn: Erik\nnummer: 02-123456

instruktion: 1\nnamn: Erik\nnummer: 045-4356713

instruktion: 3\nnummer: 02-123456\nErik

instruktion: 3\nnummer: 0100100\nokänt nummer

instruktion: 0

Implementera detta tillägg med hänsyn till den nuvarande strukturen i programmet. Detta innebär att du i klassen TelefonkatalogApplikation bör lägga till en lämplig hjälpmetod för att möjliggöra den nya funktionaliteten, och även lägga till en ny gren i while-loopen. I Telefonkatalog-klassen bör du lägga till en metod som gör det möjligt att söka med ett nummer.

Objekt i en ordlista

I nästa övning ombeds du att ändra din telefonkatalog så att värdena i ordlistan är objekt, inte listor.

Det är inget konstigt med det i sig, men det är första gången på den här kursen som något sådant föreslås, så låt oss gå igenom ett enklare exempel innan vi dyker ner i övningen.

Här har vi en applikation som håller reda på hur många övningar som studenterna har gjort på en kurs. Varje elevs antal övningar lagras i ett enkelt objekt:

class Uppgiftsraknare:\n    def __init__(self):\n        self.__uppgifter = 0\n\n    def klar(self):\n        self.__uppgifter += 1\n\n    def gjorda(self):\n        return self.__uppgifter

Följande huvudfunktion använder klassen ovan:

studeranden = {}\n\nprint(\"markerar uppgifter\")\nwhile True:\n    namn = input(\"studerande: \")\n    if len(namn) == 0:\n        break\n\n    # skapa ett nytt objekt ifall det inte finns ännu\n    if not namn in studeranden:\n        studeranden[namn] = Uppgiftsraknare()\n\n    # lägg till en ny utförd uppgift i räknaren\n    studeranden[namn].klar()\n\nprint()\nprint(\"gjorda uppgifter:\")\n\nfor studerande, uppgifter in studeranden.items():\n    print(f\"{studerande} uppgifter {uppgifter.gjorda()} st\")

Att exekvera koden ovan kunde se ut enligt följande:

markerar uppgifter\nstuderande: peter\nstuderande: sara\nstuderande: anton\nstuderande: sara\nstuderande: jonas\nstuderande: jonas\nstuderande: anton\nstuderande: sara\nstuderande:

gjorda uppgifter:\npeter uppgifter 1 st\nanton uppgifter 2 st\nsara uppgifter 3 st\njonas uppgifter 2 st

Det finns ett par saker att tänka på i exemplet ovan. När användaren matar in ett namn kontrollerar programmet först om namnet redan är en nyckel i ordlistan. Om namnet inte finns skapas ett nytt objekt som läggs till som en post i ordlistan:

if not namn in studeranden:\n    studeranden[namn] = Uppgiftsraknare()

Efter detta kan vi vara säkra på att objektet existerar, kopplat till namnet på den student som används som nyckel. Antingen skapades det precis, eller så fanns det redan från en tidigare iteration av loopen. I vilket fall som helst kan vi nu hämta objektet med nyckeln och kalla metoden klar:

studeranden[namn].klar()

Raden ovan innehåller egentligen två separata händelser. Vi kan lika gärna använda en hjälpvariabel och skriva den på två separata kodrader:

studerandes_raknare = studeranden[namn]\nstuderandes_raknare.klar()

OBS: Även om objektet här tilldelas en hjälpvariabel, så finns objektet kvar i ordlistan precis som tidigare. Hjälparvariabeln innehåller en referens till objektet i ordlistan.

Om du inte är helt säker på vad som egentligen händer i koden ovan kan du prova med visualiseringsverktyget.

I denna övning kommer du att skapa en annan version av TelefonkatalogApplikation. Du kommer att lägga till adresser i datan som kan kopplas till ett namn. För enkelhetens skull har funktionaliteten för att spara till fil tagits bort, och vissa andra metoder har bytt namn för att bättre passa in i förändringen.

En separat klass för en persons data

Ändra hur data om en person hanteras. Implementera en klass med namnet Person, som tar hand om personers telefonnummer och adresser. Klassen ska fungera på följande sätt:

person = Person(\"Erik\")\nprint(person.namn())\nprint(person.numren())\nprint(person.adress())\nperson.tillsatt_nummer(\"040-123456\")\nperson.tillsatt_adress(\"Mannerheimintie 10 Helsinki\")\nprint(person.numren())\nprint(person.adress())

Erik\n[]\nNone\n['040-123456']\nMannerheimintie 10 Helsinki

Telefonkatalogs användning av klassen Person

Ändra den interna implementeringen av din applikation så att din Telefonkatalog-klass använder objekt av klassen Person för att lagra data i telefonkatalogen. Det vill säga, attributet __personer ska fortfarande innehålla en ordlista, men värdena ska vara Person-objekt och inte listor. Användaren av din applikation ska inte märka någon skillnad; ändringarna får inte påverka användargränssnittet.

VARNING: när du gör strukturella ändringar i din kod, som beskrivs i den här övningen, ska du alltid ta små steg och testa i varje möjligt skede. Försök inte att göra alla ändringar på en gång. Det är ett säkert sätt att stöta på allvarliga problem med din kod.

Ett lämpligt första steg kan vara att skriva lite kod för att kontrollera funktionaliteten i Telefonkatalog-klassen direkt. Till exempel bör följande åtminstone inte orsaka några fel:

katalog = Telefonkatalog()\nkatalog.tillsatt_nummer(\"Erik\", \"02-123456\")\nprint(katalog.hamta_uppgifter(\"Erik\"))\nprint(katalog.hamta_uppgifter(\"Emilia\"))

Lägg märke till det nya namnet på metoden för att hämta ett inlägg från telefonkatalogen. De automatiska testerna kontrollerar inte vad utskriften från din hamta_uppgifter-metod är, utan ser till att inga fel uppstår av ovanstående kod och att resultatet är logiskt inom din implementation.

När du har gjort de nödvändiga ändringarna i ditt program och absolut har verifierat funktionaliteten i Telefonkatalog-klassen kan du gå vidare till användargränssnittet och se om allt fortfarande fungerar som förväntat.

Tillägg av adress

Vänligen implementera funktionaliteten för att lägga till en adress till ett inlägg i din telefonkatalog. Programmet ska fungera på följande sätt:

instruktioner:\n0 avsluta\n1 tillsätt nummer\n2 sök\n3 tillsätt adress

instruktion: 1\nnamn: Erik\nnummer: 02-123456

instruktion: 3\nnamn: Emilia\nadress: Viherlaaksontie 7, Espoo

instruktion: 2\nnamn: Erik\n02-123456\nadress okänd

instruktion: 2\nnamn: Emilia\nnummer okänd\nViherlaaksontie 7, Espoo

instruktion: 3\nnamn: Erik\nadress: Linnankatu 75, Turku

instruktion: 2\nnamn: Erik\n02-123456\nLinnankatu 75, Turku

instruktion: 2\nnamn: Wilhelm\nadress okänd\nnummer okänd

instruktion: 0

VARNING och tips: som nämndes ovan i den föregående övningen, försök inte att göra alla ändringar på en gång. Det är ett säkert sätt att stöta på allvarliga problem med din kod.

Kontrollera först att du på ett tillförlitligt sätt kan lägga till adresser med hjälp av Telefonkatalog-klassen direkt. När du har verifierat detta kan du gå vidare till de nödvändiga ändringarna i användargränssnittet.

Några avslutande anmärkningar

Strukturen i Telefonkatalog-exemplet ovan följer de grundläggande principerna för objektorienterad programmering ganska väl. Den centrala principen är att identifiera de olika ansvarsområdena i programmet och fördela dessa logiskt mellan de olika klasserna och metoderna. Ett av de viktigaste motiven för denna uppdelning är att hantera komplexiteten. Ett annat viktigt motiv är att en logisk uppdelning av ansvarsområden - modularitet i facktermer - ofta gör koden lättare att underhålla och bygga vidare på.

I de programvarupaket som utvecklas och används ute i världen är underhåll och utbyggnad, dvs felsökning av befintlig programvara och implementering av nya funktioner, den absolut dyraste delen av utvecklingen. Korrekt implementerad modularitet är ekonomiskt sett en mycket viktig funktion i programvaruutvecklingen.

Det finns några fler objektorienterade programmeringsprinciper som är värda att lyfta fram här. Telefonkatalog är ett bra exempel på hur den centrala programlogiken kan (och bör) separeras från både användargränssnitt och datalagring. Detta är viktigt på grund av ett par olika skäl. För det första gör denna separation det möjligt att testa koden i mindre enheter, en klass och metod i taget. För det andra, eftersom kärnlogiken nu är oberoende av gränssnitten som kommer till omvärlden, är det möjligt att i viss utsträckning ändra implementeringen av antingen kärnlogiken eller gränssnitten utan att hela applikationen går sönder.

Filhanteringen i Telefonkatalog-programmet går till på följande sätt: Programmet läser filen en enda gång, när det startar. Efter detta lagras all data i variabler i programmet. När programmet avslutas lagras alla data igen, vilket i praktiken innebär att filen skrivs över. I de flesta fall är detta det rekommenderade sättet att hantera externa filer, eftersom det ofta är mycket mer komplicerat att redigera data på plats.

Det finns många bra guideböcker för att lära sig om god programmeringspraxis. En sådan är Clean Code av Robert Martin. Kodexemplen i boken är dock implementerade i Java, så att arbeta igenom exemplen kan vara ganska besvärligt vid denna tidpunkt i din programmeringskarriär, även om boken i sig rekommenderas mycket av kurspersonalen. Teman som lätt underhållen, utbyggbar kod av god kvalitet kommer att utforskas ytterligare på kurserna Software Development Methods och Software Engineering.

Att skriva kod enligt etablerade principer för objektorienterad programmering har ett pris. Du kommer sannolikt att få skriva mer kod än om du skulle skriva din implementation i en enda kontinuerlig stöt av spaghettikod. En av de viktigaste färdigheterna hos en programmerare är att bestämma det bästa tillvägagångssättet för varje situation. Ibland är det nödvändigt att bara hacka ihop något snabbt för omedelbar användning. Å andra sidan, om det inom överskådlig framtid kan förväntas att koden kommer att återanvändas, underhållas eller vidareutvecklas, antingen av dig eller, mer kritiskt, av någon helt annan, blir programkodens läsbarhet och logiska modularitet väsentlig. Oftast är det så att om det är värt att göra, så är det värt att göra det bra, även i de allra tidigaste utvecklingsstadierna.

För att avsluta denna del av materialet kommer du att implementera ytterligare en större applikation.

Skriv en interaktiv applikation för att hålla koll på dina studier. Den interna strukturen är upp till dig, men det här skulle vara ett bra tillfälle att öva på att skapa en liknande struktur som i Telefonkatalog-exemplet ovan.

Ditt program bör fungera på följande sätt:

1 tillsätt prestation\n2 hämta prestation\n3 statistik\n0 avsluta

instruktion: 1\nkurs: ItP\nvitsord: 3\nstudiepoäng: 5

instruktion: 2\nkurs: ItP\nItP (5 sp) vitsord 3

instruktion: 1\nkurs: ItP\nvitsord: 5\nstudiepoäng: 5

instruktion: 2\nkurs: ItP\nItP (5 sp) vitsord 5

instruktion: 1\nkurs: ItP\nvitsord: 1\nstudiepoäng: 5

instruktion: 2\nkurs: ItP\nItP (5 sp) vitsord 5

instruktion: 2\nkurs: Introduktion till Java\ninga prestationer

instruktion: 1\nkurs: Tira\nvitsord: 1\nstudiepoäng: 10

instruktion: 1\nkurs: Tilpe\nvitsord: 2\nstudiepoäng: 5

instruktion: 1\nkurs: Lapio\nvitsord: 4\nstudiepoäng: 1

instruktion: 1\nkurs: Lama\nvitsord: 5\nstudiepoäng: 8

instruktion: 3\n5 avklarade kurser, totalt 29 studiepoäng\nmedeltal 3.4\nvitsordsdistribution\n5: xx\n4: x\n3:\n2: x\n1: x

instruktion: 0

Varje kursnamn ska resultera i ett enda tillägg i registret. Ett betyg kan höjas genom att kursuppgifterna skrivs in på nytt, men betyget får aldrig sänkas.

Denna övning är värd två övningspoäng. Den första ges om kommandona 1, 2 och 0 fungerar korrekt i ditt program. Det andra ges om även kommando 3 fungerar som förväntat.

Epilog

För att avsluta den här delen av materialet återvänder vi till användargränssnittet i telefonkatalogsexemplet för en stund.

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n        self.__fil = FilHanterare(\"katalog.txt\")\n\n    # annan kod\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Ett TelefonkatalogApplikation-objekt innehåller både ett Telefonkatalog-objekt och ett FilHanterare-objekt. Namnet på den fil som skickas till FilHanterare är för närvarande hårdkodat i TelefonkatalogApplikation-klassen. Detta är en helt irrelevant detalj när det gäller applikationens användargränssnitt. Faktum är att den bryter mot principen om separation of concerns: var ett Telefonkatalog-objekt sparar sitt innehåll ska inte vara en angelägenhet för en TelefonkatalogApplikation, men om vi vill ändra platsen måste vi ändra koden för TelefonkatalogApplikation.

Det skulle vara en bättre idé att skapa ett FilHanterare-objekt någonstans utanför TelefonkatalogApplikation-klassen och skicka det som ett argument till applikationen:

class TelefonkatalogApplikation:\n    def __init__(self, fil):\n        self.__katalog = Telefonkatalog()\n        self.__fil = fil\n\n    # annan kod\n\n# skapar ett objekt för att hantera sparning av filen\nforvaringstjanst = FilHanterare(\"katalog.txt\")\n# och passerar det som argument till TelefonkatalogApplikation-objektets konstruktor\napplikation = TelefonkatalogApplikation(forvaringstjanst)\napplikation.exekvera()

Detta tar bort ett onödigt beroende från TelefonkatalogApplikation-klassen. Om namnet på filen ändras behöver användargränssnittet inte längre ändras. Vi behöver bara skicka ett annat argument till konstruktören:

class TelefonkatalogApplikation:\n    def __init__(self, fil):\n        self.__katalog = Telefonkatalog()\n        self.__fil = fil\n\n    # annan kod\n\n# använd ett annat filnamn\nforvaringstjanst = FilHanterare(\"ny_katalog.txt\")\napplikation = TelefonkatalogApplikation(forvaringstjanst)\napplikation.exekvera()

Denna förändring gör det också möjligt för oss att överväga mer exotiska lagringsplatser, till exempel en molntjänst på internet. Vi behöver bara implementera en klass som använder molntjänsten och erbjuder TelefonkatalogApplikation exakt samma metoder som FilHanterare.

En instans av denna nya \"moln hanterar\"-klass kan skickas som ett argument till konstruktören och inte en enda rad kod behöver ändras i användargränssnittet:

class MolnHanterare:\n    # kod som sparar innehållet av telefonkatalogen i en molntjänst på internet\n\nforvaringstjanst = MolnHanterare(\"amazon-cloud\", \"anvandarnamn\", \"losenord\")\napplikation = TelefonkatalogApplikation(forvaringstjanst)\napplikation.exekvera()

Som du har sett tidigare har den här typen av tekniker ett pris, eftersom det blir mer kod att skriva, så en programmerare måste överväga om det är en acceptabel avvägning.

Den teknik som beskrivs ovan kallas beroendeinjektion. Som namnet antyder är tanken att alla beroenden som krävs av ett objekt ska tillhandahållas utanför objektet. Det är ett mycket användbart verktyg i en programmerares verktygslåda, eftersom det gör det lättare att implementera nya funktioner i program och underlättar automatisk testning. Detta tema kommer att utforskas ytterligare på de tidigare nämnda kurserna Software Development och Software Engineering.

Svara vänligen på en snabb enkät om denna del av kursen.

","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/4-storre-applikation.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Känner du till några grundläggande principer för applikationsutveckling"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att känna dig bekväm med att skilja mellan de olika delarna av en applikation (användargränssnitt, programlogik och filhantering)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Har du övat på att skriva din egna lite större applikation"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills i detta kursmaterial har vi gått igenom ett stort antal Python-funktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Introduktion till programmering-kursen introducerades kontrollstrukturer som while och for, funktioner samt grundläggande datastrukturer som listor, tupler och ordlistor. I princip är dessa verktyg allt som behövs för att uttrycka vad som helst som en programmerare kan tänka sig vilja uttrycka med Python."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På denna avancerade kurs i programmering, med början i modul 8 av materialet, har du blivit bekant med klasser och objekt. Låt oss ta en stund att fundera på när och varför de är nödvändiga, ifall de grundläggande verktygen från modul 1 till 7 borde räcka."}]},{"type":"element","tagName":"h2","properties":{"id":"att-hantera-komplexitet","style":"position:relative;"},"children":[{"type":"text","value":"Att hantera komplexitet"},{"type":"element","tagName":"a","properties":{"href":"#att-hantera-komplexitet","ariaLabel":"att hantera komplexitet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objekt och klasser är långt ifrån nödvändiga i alla programmeringssammanhang. Om du t.ex. programmerar ett litet skript för engångsbruk är objekt oftast överflödiga. Men när du ska programmera något större och mer komplicerat blir objekten mycket användbara."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmen blir allt mer komplexa blir mängden detaljer snabbt ohanterlig, såvida inte programmet är organiserat på något systematiskt sätt. Även några av de mer komplicerade övningarna på den här kursen hittills skulle ha haft nytta av de exempel som ges i den här delen av materialet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I flera decennier har begreppet "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Inkapsling_(Separation_of_Concerns)","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Separation of concerns"}]},{"type":"text","value":" varit en av de centrala principerna inom programmering och inom datavetenskapen i stort. Citat från Wikipedia:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Separation of concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Genom att dela upp programmet i olika delar, så att varje del har sina egna problem att hantera, blir det lättare att hantera den oundvikliga komplexiteten i ett datorprogram."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner är ett sätt att organisera ett program i distinkta, hanterbara helheter. I stället för att skriva ett enda skript är tanken att formulera små, separat verifierbara funktioner som var och en löser en del av det större problemet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En annan vanlig metod för att hantera större program är objekt, genom objektorienterade programmeringsprinciper. Det finns fördelar och nackdelar med båda metoderna, och varje programmerare har sin egen favorit. Som vi har sett hittills kan vi med hjälp av objekt och klasser samla alla data och den kod som bearbetar dessa data i en enda enhet, i ett objekts attribut och metoder. Dessutom ger objekten ett sätt att kapsla in de data de kontrollerar, så att andra delar av programmet inte behöver oroa sig för de interna detaljerna i ett objekt."}]},{"type":"element","tagName":"h2","properties":{"id":"ett-fungerande-exempel-telefonkatalog","style":"position:relative;"},"children":[{"type":"text","value":"Ett fungerande exempel: telefonkatalog"},{"type":"element","tagName":"a","properties":{"href":"#ett-fungerande-exempel-telefonkatalog","ariaLabel":"ett fungerande exempel telefonkatalog permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur ska ett program delas in i klasser och objekt? Det här är inte alls en enkel fråga med ett enda godtagbart svar, så vi fortsätter med ett exempel. I del fem genomförde du en "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-5/3-lexikon","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"telefonkatalogsapplikation"}]},{"type":"text","value":", och nu ska vi genomföra något liknande med hjälp av objektorienterade programmeringsprinciper."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enligt principen om separation of concerns bör ett program delas upp i sektioner som var och en har sin egen sak att ta hand om. I objektorienterad programmering översätts detta till "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Single_responsibility_principle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"principen om ett ansvar"}]},{"type":"text","value":". Utan att gå in på detaljerna framgår det grundläggande syftet redan av namnet: en enda klass och de objekt som skapas utifrån den ska ha ett enda ansvar i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objektorienterad programmering används ofta som ett sätt att modellera objekt och fenomen i den verkliga världen. Ett enskilt objekt i den verkliga världen modelleras med en enda klass i programkoden. I fallet med en telefonkatalog kan sådana objekt vara"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en person"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett telefonnummer"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett namn och ett telefonnummer kan uppfattas som data som inte förtjänar egna klasser, men en person är en distinkt fysisk enhet i den verkliga världen, och i programmeringsvärlden skulle den kunna fungera som en klass. Ett Person-objekt skulle vara ansvarigt för att knyta ihop ett namn och de telefonnummer som är kopplade till det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En telefonkatalog i sig skulle kunna vara en bra kandidat för en klass. Dess ansvar skulle vara att hantera olika personobjekt och de data de innehåller."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu har vi skissat kärnan i vår applikation: telefonkatalog och person utgör programmeringslogiken i vår applikation, eller applikationslogiken i korthet. Vår applikation skulle behöva några andra klasser också."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är oftast en bra idé att hålla all interaktion med en användare skild från applikationslogiken. Det är ju trots allt ett helt eget ansvar. Förutom den centrala applikationslogiken bör vårt program därför innehålla en klass som hanterar användargränssnittet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom bör vår telefonkatalog ha någon form av beständig lagring mellan exekveringar. Filhanteringen är återigen ett tydligt separat ansvar, så den förtjänar en egen klass."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu när vi har en översikt över de grundläggande komponenterna i vårt program uppstår frågan: var ska vi börja programmera? Inte heller här finns det något rätt eller fel svar, men det är ofta en bra idé att börja med någon del av programlogiken."}]},{"type":"element","tagName":"h2","properties":{"id":"steg-1-en-skiss-for-applikationslogiken","style":"position:relative;"},"children":[{"type":"text","value":"Steg 1: en skiss för applikationslogiken"},{"type":"element","tagName":"a","properties":{"href":"#steg-1-en-skiss-for-applikationslogiken","ariaLabel":"steg 1 en skiss for applikationslogiken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja med klassen Telefonkatalog. En skelettimplementering skulle kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna klass består av en lista med personer samt metoder för att både lägga till och hämta data."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje person kan vara kopplad till flera nummer, så låt oss implementera den interna strukturen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"personer"}]},{"type":"text","value":" med en ordlista. En ordlista ger oss möjlighet att söka efter nycklar enligt namn, och värdet som är kopplat till en ordlistas nyckel kan vara en lista. Hittills ser det ut som att vi inte behöver en separat klass för att representera en person – ett inlägg i en ordlista räcker."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss implementera de metoder som listas ovan och testa vår telefonkatalog:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# till personen är en lista med nummer anknyten"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testkod"}]},{"type":"text","value":"\nkatalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"02-123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde utskriva följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['02-123456']\nNone"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_nummer"}]},{"type":"text","value":" returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" om ett namn inte finns med i telefonkatalogen. Om namnet finns returneras listan med de nummer som är kopplade till namnet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man gör ändringar i ett program är det alltid värt att testa att koden fungerar som förväntat innan man går vidare med andra ändringar. Den kod som används för testning är vanligtvis något som raderas strax efteråt, och därför kanske du tycker att det inte är värt besväret att skriva några tester i första hand. I de flesta fall är detta inte sant. Testning är en förutsättning för bra programmeringsresultat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En bugg i programmet bör fångas upp och åtgärdas så snart som möjligt. Om du tar för vana att kontrollera funktionaliteten i praktiskt taget varje ny kodrad kommer du att upptäcka att buggarna oftast är lätta att hitta och åtgärda, eftersom du kan vara helt säker på att buggen orsakades av den senaste ändringen. Om du bara testar programmet efter att ha lagt till dussintals rader kod ökar de potentiella källorna till buggar också med dussintals gånger."}]},{"type":"element","tagName":"h2","properties":{"id":"steg-2-en-skiss-for-anvandargranssnittet","style":"position:relative;"},"children":[{"type":"text","value":"Steg 2: en skiss för användargränssnittet"},{"type":"element","tagName":"a","properties":{"href":"#steg-2-en-skiss-for-anvandargranssnittet","ariaLabel":"steg 2 en skiss for anvandargranssnittet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med den grundläggande applikationslogiken ur vägen är det dags att implementera ett textbaserat användargränssnitt. Vi kommer att behöva en ny klass, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":", med följande inledande funktionalitet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hjalp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktioner: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0 avsluta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet gör inte så mycket ännu, men låt oss gå igenom innehållet. Konstruktormetoden skapar en ny Telefonkatalog, som lagras i ett privat attribut. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exekvera(self)"}]},{"type":"text","value":" startar programmets textbaserade användargränssnitt, vars kärna är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loopen, som fortsätter att be användaren om instruktioner tills de skriver in instruktionen för att avsluta. Det finns också en metod för instruktioner, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hjalp(self)"}]},{"type":"text","value":", som anropas innan man går in i loopen, så att instruktionerna skrivs ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss nu lägga till lite faktisk funktionalitet. Först implementerar vi att kunna lägga till nya data i telefonkatalogen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hjalp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktioner: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0 avsluta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1 tillsätt inlägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nummer: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren skriver in 1 för att lägga till ett nytt nummer, frågar användargränssnittet efter ett namn och ett nummer och lägger till dessa i telefonkatalogen med hjälp av den lämpliga metod som definieras i klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Användargränssnittets enda ansvar är att kommunicera med användaren. All annan funktionalitet, t.ex. att lagra ett nytt namn- och nummerpar, är Telefonkatalog-objektets ansvar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns utrymme för förbättringar i strukturen i vår användargränssnittsklass. Låt oss skapa en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inlagg(self)"}]},{"type":"text","value":" som hanterar instruktionen för att lägga till ett nytt inlägg:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hjalp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktioner: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0 avsluta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1 tillsätt inlägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# separation of concerns i praktiken: en ny metod för att lägga till ett inlägg"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"inlagg"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nummer: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Separation of concerns-principen sträcker sig även till metodnivå. Vi skulle kunna ha hela användargränssnittets funktionalitet i en enda komplicerad "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loop, men det är bättre att separera varje funktionalitet i en egen metod. Ansvaret för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exekvera()"}]},{"type":"text","value":"-metoden är bara att delegera de instruktioner som användaren skriver in till relevanta metoder. Detta hjälper till att hantera den växande komplexiteten i vårt program. Om vi till exempel senare vill ändra hur det fungerar att lägga till inlägg, är det omedelbart klart att vi då måste fokusera våra ansträngningar på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inlagg()"}]},{"type":"text","value":"-metoden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss inkludera funktionalitet för att söka efter inlägg i vårt användargränssnitt. Detta bör också ha sin egen metod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hjalp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktioner: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0 avsluta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1 tillsätt inlägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"2 sök\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"inlagg"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nummer: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n numren "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" numren "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nummer okänd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har nu en enkel fungerande telefonkatalogsapplikation som är redo för testning. Följande är ett exempel på en exekvering:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 tillsätt inlägg\n2 sök"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-4356713"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\n02-123456\n045-4356713"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: Emilia\nnummer okänd"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För en så enkel applikation har vi skrivit ganska mycket kod. Om vi hade skrivit allt i den enda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loopen hade vi förmodligen kunnat komma undan med mycket mindre kod. Det är dock ganska lätt att läsa koden, strukturen är tydlig och vi borde inte ha några problem med att lägga till nya funktioner."}]},{"type":"element","tagName":"h2","properties":{"id":"steg-3-importera-data-fran-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Steg 3: Importera data från en fil"},{"type":"element","tagName":"a","properties":{"href":"#steg-3-importera-data-fran-en-fil","ariaLabel":"steg 3 importera data fran en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi redan har några telefonnummer lagrade i en fil, och att vi vill läsa detta när programmet startar. Datafilen är i följande CSV-format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Erik;02-1234567;045-4356713"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Emilia;040-324344"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hantering av filer är helt klart ett eget ansvarsområde, så det förtjänar en klass för sig:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ladda"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namnen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":"numren "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar\n namnen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" numren\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" namnen"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstruktormetoden tar namnet på filen som sitt argument. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ladda(self)"}]},{"type":"text","value":" läser innehållet i filen. Varje rad delas upp i två delar: ett namn och en lista med siffror. Sedan läggs dessa till i en ordbok, med namnet som nyckel och listan som värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden använder en smidig Python-funktion: det är möjligt att först välja några objekt från en lista separat och sedan ta resten av objekten i en ny lista. Du kan se ett exempel på detta nedan. Du kanske minns från "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/1-lasa-filer","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":" att strängmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" returnerar en lista."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nforsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" andra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":"resten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"forsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"andra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2\n[3, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" framför variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resten"}]},{"type":"text","value":" i ovanstående exempel betyder att den sista variabeln ska innehålla alla återstående poster i listan, från den tredje och framåt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi bör absolut testa filhanteraren separat innan vi inkluderar den i vår applikation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"t "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ladda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Erik': ['02-1234567', '045-4356713'], 'Emilia': ['040-324344']}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom filhanteraren verkar fungera bra kan vi lägga till den i vår applikation. Låt oss anta att vi vill läsa filen som det första varje gång programmet körs. Den logiska platsen för att läsa filen skulle vara konstruktören för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till namn och nummer från filen till telefonkatalogen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numren "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ladda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# annan kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna funktionalitet bör också testas. När vi har försäkrat oss om att filens innehåll är tillgängligt via användargränssnittet i vår applikation kan vi gå vidare till nästa steg."}]},{"type":"element","tagName":"h2","properties":{"id":"steg-4-exportera-data-till-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Steg 4: exportera data till en fil"},{"type":"element","tagName":"a","properties":{"href":"#steg-4-exportera-data-till-en-fil","ariaLabel":"steg 4 exportera data till en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den sista funktionen i vår grundversion av applikationen är att spara innehållet i telefonkatalogen tillbaka i samma fil som data lästes från."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta innebär en förändring av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen. Vi måste kunna exportera innehållet i telefonkatalogen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# returnera alla inlägg (i ordlistsformat)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"alla_uppgifter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__personer"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Själva sparandet till filen bör hanteras av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"text","value":"-klassen. Låt oss lägga till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spara"}]},{"type":"text","value":" som tar en ordlistsrepresentation av telefonkatalogen som argument:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ladda"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spara"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dict"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numren "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" numren\n f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sparandet bör ske när programmet avslutas. Låt oss lägga till en metod för detta ändamål i användargränssnittet och anropa den innan vi bryter ut ur "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loopen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# resten av koden för användargränssnittet"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metod som exekveras till programmet avslutas"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"avsluta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spara"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alla_uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"exekvera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"instruktion: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"avsluta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" instruktion "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Utveckling av telefonkatalogen, del 1","tmcname":"osa10-10_telefonkatalog_del1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen kommer du att skapa en liten utvidgning av telefonkatalogapplikationen. Koden från exemplet ovan finns i övningsmallen. Lägg till ett kommando som låter användaren söka efter nummer i telefonkatalogen. Efter tillägget ska applikationen fungera på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 tillsätt inlägg\n2 sök\n3 sök enligt nummer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-4356713"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]},{"type":"text","value":"\nErik"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0100100"}]},{"type":"text","value":"\nokänt nummer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera detta tillägg med hänsyn till den nuvarande strukturen i programmet. Detta innebär att du i klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":" bör lägga till en lämplig hjälpmetod för att möjliggöra den nya funktionaliteten, och även lägga till en ny gren i while-loopen. I "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen bör du lägga till en metod som gör det möjligt att söka med ett nummer."}]}]},{"type":"element","tagName":"h2","properties":{"id":"objekt-i-en-ordlista","style":"position:relative;"},"children":[{"type":"text","value":"Objekt i en ordlista"},{"type":"element","tagName":"a","properties":{"href":"#objekt-i-en-ordlista","ariaLabel":"objekt i en ordlista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I nästa övning ombeds du att ändra din telefonkatalog så att värdena i ordlistan är objekt, inte listor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är inget konstigt med det i sig, men det är första gången på den här kursen som något sådant föreslås, så låt oss gå igenom ett enklare exempel innan vi dyker ner i övningen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en applikation som håller reda på hur många övningar som studenterna har gjort på en kurs. Varje elevs antal övningar lagras i ett enkelt objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Uppgiftsraknare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"klar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"gjorda"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__uppgifter"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande huvudfunktion använder klassen ovan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studeranden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"markerar uppgifter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studerande: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skapa ett nytt objekt ifall det inte finns ännu"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till en ny utförd uppgift i räknaren"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"klar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"gjorda uppgifter:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" uppgifter "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" st\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att exekvera koden ovan kunde se ut enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"markerar uppgifter\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sara"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"anton"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sara"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"anton"}]},{"type":"text","value":"\nstuderande: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sara"}]},{"type":"text","value":"\nstuderande:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"gjorda uppgifter:\npeter uppgifter 1 st\nanton uppgifter 2 st\nsara uppgifter 3 st\njonas uppgifter 2 st"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns ett par saker att tänka på i exemplet ovan. När användaren matar in ett namn kontrollerar programmet först om namnet redan är en nyckel i ordlistan. Om namnet inte finns skapas ett nytt objekt som läggs till som en post i ordlistan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter detta kan vi vara säkra på att objektet existerar, kopplat till namnet på den student som används som nyckel. Antingen skapades det precis, eller så fanns det redan från en tidigare iteration av loopen. I vilket fall som helst kan vi nu hämta objektet med nyckeln och kalla metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"klar"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"klar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Raden ovan innehåller egentligen två separata händelser. Vi kan lika gärna använda en hjälpvariabel och skriva den på två separata kodrader:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerandes_raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstuderandes_raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"klar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Även om objektet här tilldelas en hjälpvariabel, så finns objektet kvar i ordlistan precis som tidigare. Hjälparvariabeln innehåller en referens till objektet i ordlistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du inte är helt säker på vad som egentligen händer i koden ovan kan du prova med "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyget"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Utveckling av telefonkatalogen, del 2","tmcname":"osa10-11_telefonkatalog_del2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna övning kommer du att skapa en annan version av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":". Du kommer att lägga till adresser i datan som kan kopplas till ett namn. För enkelhetens skull har funktionaliteten för att spara till fil tagits bort, och vissa andra metoder har bytt namn för att bättre passa in i förändringen."}]},{"type":"element","tagName":"h2","properties":{"id":"en-separat-klass-for-en-persons-data","style":"position:relative;"},"children":[{"type":"text","value":"En separat klass för en persons data"},{"type":"element","tagName":"a","properties":{"href":"#en-separat-klass-for-en-persons-data","ariaLabel":"en separat klass for en persons data permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ändra hur data om en person hanteras. Implementera en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":", som tar hand om personers telefonnummer och adresser. Klassen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"adress"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"040-123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_adress"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mannerheimintie 10 Helsinki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"adress"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik\n[]\nNone\n['040-123456']\nMannerheimintie 10 Helsinki"}]}]},{"type":"element","tagName":"h2","properties":{"id":"telefonkatalogs-anvandning-av-klassen-person","style":"position:relative;"},"children":[{"type":"text","value":"Telefonkatalogs användning av klassen Person"},{"type":"element","tagName":"a","properties":{"href":"#telefonkatalogs-anvandning-av-klassen-person","ariaLabel":"telefonkatalogs anvandning av klassen person permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ändra den interna implementeringen av din applikation så att din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klass använder objekt av klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" för att lagra data i telefonkatalogen. Det vill säga, attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__personer"}]},{"type":"text","value":" ska fortfarande innehålla en ordlista, men värdena ska vara Person-objekt och inte listor. Användaren av din applikation ska inte märka någon skillnad; ändringarna får inte påverka användargränssnittet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"VARNING:"}]},{"type":"text","value":" när du gör strukturella ändringar i din kod, som beskrivs i den här övningen, ska du alltid ta små steg och testa i varje möjligt skede. Försök inte att göra alla ändringar på en gång. Det är ett säkert sätt att "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"stöta på allvarliga problem med din kod"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett lämpligt första steg kan vara att skriva lite kod för att kontrollera funktionaliteten i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen direkt. Till exempel bör följande åtminstone inte orsaka några fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"02-123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"katalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg märke till det nya namnet på metoden för att hämta ett inlägg från telefonkatalogen. De automatiska testerna kontrollerar inte vad utskriften från din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_uppgifter"}]},{"type":"text","value":"-metod är, utan ser till att inga fel uppstår av ovanstående kod och att resultatet är logiskt inom din implementation."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du har gjort de nödvändiga ändringarna i ditt program och absolut har verifierat funktionaliteten i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen kan du gå vidare till användargränssnittet och se om allt fortfarande fungerar som förväntat."}]},{"type":"element","tagName":"h2","properties":{"id":"tillagg-av-adress","style":"position:relative;"},"children":[{"type":"text","value":"Tillägg av adress"},{"type":"element","tagName":"a","properties":{"href":"#tillagg-av-adress","ariaLabel":"tillagg av adress permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen implementera funktionaliteten för att lägga till en adress till ett inlägg i din telefonkatalog. Programmet ska fungera på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 tillsätt nummer\n2 sök\n3 tillsätt adress"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emilia"}]},{"type":"text","value":"\nadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Viherlaaksontie 7, Espoo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\n02-123456\nadress okänd"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emilia"}]},{"type":"text","value":"\nnummer okänd\nViherlaaksontie 7, Espoo"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\nadress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Linnankatu 75, Turku"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: 2\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Erik"}]},{"type":"text","value":"\n02-123456\nLinnankatu 75, Turku"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Wilhelm"}]},{"type":"text","value":"\nadress okänd\nnummer okänd"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"VARNING och tips:"}]},{"type":"text","value":" som nämndes ovan i den föregående övningen, försök inte att göra alla ändringar på en gång. Det är ett säkert sätt att "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"stöta på allvarliga problem med din kod"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kontrollera först att du på ett tillförlitligt sätt kan lägga till adresser med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-klassen direkt. När du har verifierat detta kan du gå vidare till de nödvändiga ändringarna i användargränssnittet."}]}]},{"type":"element","tagName":"h2","properties":{"id":"nagra-avslutande-anmarkningar","style":"position:relative;"},"children":[{"type":"text","value":"Några avslutande anmärkningar"},{"type":"element","tagName":"a","properties":{"href":"#nagra-avslutande-anmarkningar","ariaLabel":"nagra avslutande anmarkningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strukturen i Telefonkatalog-exemplet ovan följer de grundläggande principerna för objektorienterad programmering ganska väl. Den centrala principen är att identifiera de olika ansvarsområdena i programmet och fördela dessa logiskt mellan de olika klasserna och metoderna. Ett av de viktigaste motiven för denna uppdelning är att hantera komplexiteten. Ett annat viktigt motiv är att en logisk uppdelning av ansvarsområden - modularitet i facktermer - ofta gör koden lättare att underhålla och bygga vidare på."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I de programvarupaket som utvecklas och används ute i världen är underhåll och utbyggnad, dvs felsökning av befintlig programvara och implementering av nya funktioner, den absolut dyraste delen av utvecklingen. Korrekt implementerad modularitet är ekonomiskt sett en mycket viktig funktion i programvaruutvecklingen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns några fler objektorienterade programmeringsprinciper som är värda att lyfta fram här. Telefonkatalog är ett bra exempel på hur den centrala programlogiken kan (och bör) separeras från både användargränssnitt och datalagring. Detta är viktigt på grund av ett par olika skäl. För det första gör denna separation det möjligt att testa koden i mindre enheter, en klass och metod i taget. För det andra, eftersom kärnlogiken nu är oberoende av gränssnitten som kommer till omvärlden, är det möjligt att i viss utsträckning ändra implementeringen av antingen kärnlogiken eller gränssnitten utan att hela applikationen går sönder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filhanteringen i Telefonkatalog-programmet går till på följande sätt: Programmet läser filen en enda gång, när det startar. Efter detta lagras all data i variabler i programmet. När programmet avslutas lagras alla data igen, vilket i praktiken innebär att filen skrivs över. I de flesta fall är detta det rekommenderade sättet att hantera externa filer, eftersom det ofta är mycket mer komplicerat att redigera data på plats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns många bra guideböcker för att lära sig om god programmeringspraxis. En sådan är "},{"type":"element","tagName":"a","properties":{"href":"https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Clean Code"}]},{"type":"text","value":" av Robert Martin. Kodexemplen i boken är dock implementerade i Java, så att arbeta igenom exemplen kan vara ganska besvärligt vid denna tidpunkt i din programmeringskarriär, även om boken i sig rekommenderas mycket av kurspersonalen. Teman som lätt underhållen, utbyggbar kod av god kvalitet kommer att utforskas ytterligare på kurserna "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/opintotarjonta/cu/hy-CU-118024742-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Development Methods"}]},{"type":"text","value":" och "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/opintotarjonta/cu/hy-CU-118024909-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skriva kod enligt etablerade principer för objektorienterad programmering har ett pris. Du kommer sannolikt att få skriva mer kod än om du skulle skriva din implementation i en enda kontinuerlig stöt av spaghettikod. En av de viktigaste färdigheterna hos en programmerare är att bestämma det bästa tillvägagångssättet för varje situation. Ibland är det nödvändigt att bara hacka ihop något snabbt för omedelbar användning. Å andra sidan, om det inom överskådlig framtid kan förväntas att koden kommer att återanvändas, underhållas eller vidareutvecklas, antingen av dig eller, mer kritiskt, av någon helt annan, blir programkodens läsbarhet och logiska modularitet väsentlig. Oftast är det så att om det är värt att göra, så är det värt att göra det bra, även i de allra tidigaste utvecklingsstadierna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att avsluta denna del av materialet kommer du att implementera ytterligare en större applikation."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Studieregister","tmcname":"osa10-12_studieregister"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv en interaktiv applikation för att hålla koll på dina studier. Den interna strukturen är upp till dig, men det här skulle vara ett bra tillfälle att öva på att skapa en liknande struktur som i Telefonkatalog-exemplet ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ditt program bör fungera på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 tillsätt prestation\n2 hämta prestation\n3 statistik\n0 avsluta"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 sp) vitsord 3"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 sp) vitsord 5"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 sp) vitsord 5"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Introduktion till Java"}]},{"type":"text","value":"\ninga prestationer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tira"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tilpe"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Lapio"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nkurs: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Lama"}]},{"type":"text","value":"\nvitsord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nstudiepoäng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n5 avklarade kurser, totalt 29 studiepoäng\nmedeltal 3.4\nvitsordsdistribution\n5: xx\n4: x\n3:\n2: x\n1: x"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje kursnamn ska resultera i ett enda tillägg i registret. Ett betyg kan höjas genom att kursuppgifterna skrivs in på nytt, men betyget får aldrig sänkas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna övning är värd två övningspoäng. Den första ges om kommandona 1, 2 och 0 fungerar korrekt i ditt program. Det andra ges om även kommando 3 fungerar som förväntat."}]}]},{"type":"element","tagName":"h2","properties":{"id":"epilog","style":"position:relative;"},"children":[{"type":"text","value":"Epilog"},{"type":"element","tagName":"a","properties":{"href":"#epilog","ariaLabel":"epilog permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att avsluta den här delen av materialet återvänder vi till användargränssnittet i telefonkatalogsexemplet för en stund."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# annan kod"}]},{"type":"text","value":"\n\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"-objekt innehåller både ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-objekt och ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"text","value":"-objekt. Namnet på den fil som skickas till FilHanterare är för närvarande hårdkodat i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"-klassen. Detta är en helt irrelevant detalj när det gäller applikationens användargränssnitt. Faktum är att den bryter mot principen om separation of concerns: var ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonkatalog"}]},{"type":"text","value":"-objekt sparar sitt innehåll ska inte vara en angelägenhet för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":", men om vi vill ändra platsen måste vi ändra koden för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det skulle vara en bättre idé att skapa ett FilHanterare-objekt någonstans utanför "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"-klassen och skicka det som ett argument till applikationen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# annan kod"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skapar ett objekt för att hantera sparning av filen"}]},{"type":"text","value":"\nforvaringstjanst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# och passerar det som argument till TelefonkatalogApplikation-objektets konstruktor"}]},{"type":"text","value":"\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"forvaringstjanst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta tar bort ett onödigt beroende från "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":"-klassen. Om namnet på filen ändras behöver användargränssnittet inte längre ändras. Vi behöver bara skicka ett annat argument till konstruktören:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__katalog "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonkatalog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__fil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# annan kod"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använd ett annat filnamn"}]},{"type":"text","value":"\nforvaringstjanst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FilHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_katalog.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"forvaringstjanst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna förändring gör det också möjligt för oss att överväga mer exotiska lagringsplatser, till exempel en molntjänst på internet. Vi behöver bara implementera en klass som använder molntjänsten och erbjuder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TelefonkatalogApplikation"}]},{"type":"text","value":" exakt samma metoder som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FilHanterare"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En instans av denna nya \"moln hanterar\"-klass kan skickas som ett argument till konstruktören och inte en enda rad kod behöver ändras i användargränssnittet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"MolnHanterare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod som sparar innehållet av telefonkatalogen i en molntjänst på internet"}]},{"type":"text","value":"\n\nforvaringstjanst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MolnHanterare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"amazon-cloud\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"anvandarnamn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"losenord\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TelefonkatalogApplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"forvaringstjanst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplikation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exekvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du har sett tidigare har den här typen av tekniker ett pris, eftersom det blir mer kod att skriva, så en programmerare måste överväga om det är en acceptabel avvägning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den teknik som beskrivs ovan kallas beroendeinjektion. Som namnet antyder är tanken att alla beroenden som krävs av ett objekt ska tillhandahållas utanför objektet. Det är ett mycket användbart verktyg i en programmerares verktygslåda, eftersom det gör det lättare att implementera nya funktioner i program och underlättar automatisk testning. Detta tema kommer att utforskas ytterligare på de tidigare nämnda kurserna "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/opintotarjonta/cu/hy-CU-118024742-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Development"}]},{"type":"text","value":" och "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/opintotarjonta/cu/hy-CU-118024909-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara vänligen på en snabb enkät om denna del av kursen."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Känner du till några grundläggande principer för applikationsutveckling
  • \n
  • Kommer du att känna dig bekväm med att skilja mellan de olika delarna av en applikation (användargränssnitt, programlogik och filhantering)
  • \n
  • Har du övat på att skriva din egna lite större applikation
  • \n

Hittills i detta kursmaterial har vi gått igenom ett stort antal Python-funktioner.

I Introduktion till programmering-kursen introducerades kontrollstrukturer som while och for, funktioner samt grundläggande datastrukturer som listor, tupler och ordlistor. I princip är dessa verktyg allt som behövs för att uttrycka vad som helst som en programmerare kan tänka sig vilja uttrycka med Python.

På denna avancerade kurs i programmering, med början i modul 8 av materialet, har du blivit bekant med klasser och objekt. Låt oss ta en stund att fundera på när och varför de är nödvändiga, ifall de grundläggande verktygen från modul 1 till 7 borde räcka.

Att hantera komplexitet

Objekt och klasser är långt ifrån nödvändiga i alla programmeringssammanhang. Om du t.ex. programmerar ett litet skript för engångsbruk är objekt oftast överflödiga. Men när du ska programmera något större och mer komplicerat blir objekten mycket användbara.

När programmen blir allt mer komplexa blir mängden detaljer snabbt ohanterlig, såvida inte programmet är organiserat på något systematiskt sätt. Även några av de mer komplicerade övningarna på den här kursen hittills skulle ha haft nytta av de exempel som ges i den här delen av materialet.

I flera decennier har begreppet Separation of concerns varit en av de centrala principerna inom programmering och inom datavetenskapen i stort. Citat från Wikipedia:

Separation of concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program

Genom att dela upp programmet i olika delar, så att varje del har sina egna problem att hantera, blir det lättare att hantera den oundvikliga komplexiteten i ett datorprogram.

Funktioner är ett sätt att organisera ett program i distinkta, hanterbara helheter. I stället för att skriva ett enda skript är tanken att formulera små, separat verifierbara funktioner som var och en löser en del av det större problemet.

En annan vanlig metod för att hantera större program är objekt, genom objektorienterade programmeringsprinciper. Det finns fördelar och nackdelar med båda metoderna, och varje programmerare har sin egen favorit. Som vi har sett hittills kan vi med hjälp av objekt och klasser samla alla data och den kod som bearbetar dessa data i en enda enhet, i ett objekts attribut och metoder. Dessutom ger objekten ett sätt att kapsla in de data de kontrollerar, så att andra delar av programmet inte behöver oroa sig för de interna detaljerna i ett objekt.

Ett fungerande exempel: telefonkatalog

Hur ska ett program delas in i klasser och objekt? Det här är inte alls en enkel fråga med ett enda godtagbart svar, så vi fortsätter med ett exempel. I del fem genomförde du en telefonkatalogsapplikation, och nu ska vi genomföra något liknande med hjälp av objektorienterade programmeringsprinciper.

Enligt principen om separation of concerns bör ett program delas upp i sektioner som var och en har sin egen sak att ta hand om. I objektorienterad programmering översätts detta till principen om ett ansvar. Utan att gå in på detaljerna framgår det grundläggande syftet redan av namnet: en enda klass och de objekt som skapas utifrån den ska ha ett enda ansvar i programmet.

Objektorienterad programmering används ofta som ett sätt att modellera objekt och fenomen i den verkliga världen. Ett enskilt objekt i den verkliga världen modelleras med en enda klass i programkoden. I fallet med en telefonkatalog kan sådana objekt vara

    \n
  • en person
  • \n
  • ett namn
  • \n
  • ett telefonnummer
  • \n

Ett namn och ett telefonnummer kan uppfattas som data som inte förtjänar egna klasser, men en person är en distinkt fysisk enhet i den verkliga världen, och i programmeringsvärlden skulle den kunna fungera som en klass. Ett Person-objekt skulle vara ansvarigt för att knyta ihop ett namn och de telefonnummer som är kopplade till det.

En telefonkatalog i sig skulle kunna vara en bra kandidat för en klass. Dess ansvar skulle vara att hantera olika personobjekt och de data de innehåller.

Nu har vi skissat kärnan i vår applikation: telefonkatalog och person utgör programmeringslogiken i vår applikation, eller applikationslogiken i korthet. Vår applikation skulle behöva några andra klasser också.

Det är oftast en bra idé att hålla all interaktion med en användare skild från applikationslogiken. Det är ju trots allt ett helt eget ansvar. Förutom den centrala applikationslogiken bör vårt program därför innehålla en klass som hanterar användargränssnittet.

Dessutom bör vår telefonkatalog ha någon form av beständig lagring mellan exekveringar. Filhanteringen är återigen ett tydligt separat ansvar, så den förtjänar en egen klass.

Nu när vi har en översikt över de grundläggande komponenterna i vårt program uppstår frågan: var ska vi börja programmera? Inte heller här finns det något rätt eller fel svar, men det är ofta en bra idé att börja med någon del av programlogiken.

Steg 1: en skiss för applikationslogiken

Låt oss börja med klassen Telefonkatalog. En skelettimplementering skulle kunna se ut så här:

class Telefonkatalog:\n    def __init__(self):\n        self.__personer = []\n\n    def tillsatt_nummer(self, namn: str, nummer: str):\n        pass\n\n    def hamta_nummer(self, namn: str):\n        pass\n

Denna klass består av en lista med personer samt metoder för att både lägga till och hämta data.

Varje person kan vara kopplad till flera nummer, så låt oss implementera den interna strukturen för personer med en ordlista. En ordlista ger oss möjlighet att söka efter nycklar enligt namn, och värdet som är kopplat till en ordlistas nyckel kan vara en lista. Hittills ser det ut som att vi inte behöver en separat klass för att representera en person – ett inlägg i en ordlista räcker.

Låt oss implementera de metoder som listas ovan och testa vår telefonkatalog:

class Telefonkatalog:\n    def __init__(self):\n        self.__personer = {}\n\n    def tillsatt_nummer(self, namn: str, nummer: str):\n        if not namn in self.__personer:\n            # till personen är en lista med nummer anknyten\n            self.__personer[namn] = []\n\n        self.__personer[namn].append(nummer)\n\n    def hamta_nummer(self, namn: str):\n        if not namn in self.__personer:\n            return None\n\n        return self.__personer[namn]\n\n# testkod\nkatalog = Telefonkatalog()\nkatalog.tillsatt_nummer(\"Erik\", \"02-123456\")\nprint(katalog.hamta_nummer(\"Erik\"))\nprint(katalog.hamta_nummer(\"Emilia\"))

Detta borde utskriva följande:

['02-123456']\nNone

Metoden hamta_nummer returnerar None om ett namn inte finns med i telefonkatalogen. Om namnet finns returneras listan med de nummer som är kopplade till namnet.

När man gör ändringar i ett program är det alltid värt att testa att koden fungerar som förväntat innan man går vidare med andra ändringar. Den kod som används för testning är vanligtvis något som raderas strax efteråt, och därför kanske du tycker att det inte är värt besväret att skriva några tester i första hand. I de flesta fall är detta inte sant. Testning är en förutsättning för bra programmeringsresultat.

En bugg i programmet bör fångas upp och åtgärdas så snart som möjligt. Om du tar för vana att kontrollera funktionaliteten i praktiskt taget varje ny kodrad kommer du att upptäcka att buggarna oftast är lätta att hitta och åtgärda, eftersom du kan vara helt säker på att buggen orsakades av den senaste ändringen. Om du bara testar programmet efter att ha lagt till dussintals rader kod ökar de potentiella källorna till buggar också med dussintals gånger.

Steg 2: en skiss för användargränssnittet

Med den grundläggande applikationslogiken ur vägen är det dags att implementera ett textbaserat användargränssnitt. Vi kommer att behöva en ny klass, TelefonkatalogApplikation, med följande inledande funktionalitet:

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n\n    def hjalp(self):\n        print(\"instruktioner: \")\n        print(\"0 avsluta\")\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n                break\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Det här programmet gör inte så mycket ännu, men låt oss gå igenom innehållet. Konstruktormetoden skapar en ny Telefonkatalog, som lagras i ett privat attribut. Metoden exekvera(self) startar programmets textbaserade användargränssnitt, vars kärna är while-loopen, som fortsätter att be användaren om instruktioner tills de skriver in instruktionen för att avsluta. Det finns också en metod för instruktioner, hjalp(self), som anropas innan man går in i loopen, så att instruktionerna skrivs ut.

Låt oss nu lägga till lite faktisk funktionalitet. Först implementerar vi att kunna lägga till nya data i telefonkatalogen:

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n\n    def hjalp(self):\n        print(\"instruktioner: \")\n        print(\"0 avsluta\")\n        print(\"1 tillsätt inlägg\")\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n                break\n            elif instruktion == \"1\":\n                namn = input(\"namn: \")\n                nummer = input(\"nummer: \")\n                self.__katalog.tillsatt_nummer(namn, nummer)\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Om användaren skriver in 1 för att lägga till ett nytt nummer, frågar användargränssnittet efter ett namn och ett nummer och lägger till dessa i telefonkatalogen med hjälp av den lämpliga metod som definieras i klassen.

Användargränssnittets enda ansvar är att kommunicera med användaren. All annan funktionalitet, t.ex. att lagra ett nytt namn- och nummerpar, är Telefonkatalog-objektets ansvar.

Det finns utrymme för förbättringar i strukturen i vår användargränssnittsklass. Låt oss skapa en metod inlagg(self) som hanterar instruktionen för att lägga till ett nytt inlägg:

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n\n    def hjalp(self):\n        print(\"instruktioner: \")\n        print(\"0 avsluta\")\n        print(\"1 tillsätt inlägg\")\n\n    # separation of concerns i praktiken: en ny metod för att lägga till ett inlägg\n    def inlagg(self):\n        namn = input(\"namn: \")\n        nummer = input(\"nummer: \")\n        self.__katalog.tillsatt_nummer(namn, nummer)\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n                break\n            elif instruktion == \"1\":\n                self.inlagg()\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Separation of concerns-principen sträcker sig även till metodnivå. Vi skulle kunna ha hela användargränssnittets funktionalitet i en enda komplicerad while-loop, men det är bättre att separera varje funktionalitet i en egen metod. Ansvaret för exekvera()-metoden är bara att delegera de instruktioner som användaren skriver in till relevanta metoder. Detta hjälper till att hantera den växande komplexiteten i vårt program. Om vi till exempel senare vill ändra hur det fungerar att lägga till inlägg, är det omedelbart klart att vi då måste fokusera våra ansträngningar på inlagg()-metoden.

Låt oss inkludera funktionalitet för att söka efter inlägg i vårt användargränssnitt. Detta bör också ha sin egen metod:

\nclass TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n\n    def hjalp(self):\n        print(\"instruktioner: \")\n        print(\"0 avsluta\")\n        print(\"1 tillsätt inlägg\")\n        print(\"2 sök\")\n\n    def inlagg(self):\n        namn = input(\"namn: \")\n        nummer = input(\"nummer: \")\n        self.__katalog.tillsatt_nummer(namn, nummer)\n\n    def sok(self):\n        namn = input(\"namn: \")\n        numren = self.__katalog.hamta_nummer(namn)\n        if numren == None:\n            print(\"nummer okänd\")\n            return\n        for nummer in numren:\n            print(nummer)\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n                break\n            elif instruktion == \"1\":\n                self.inlagg()\n            elif instruktion == \"2\":\n                self.sok()\n            else:\n                self.hjalp()\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Vi har nu en enkel fungerande telefonkatalogsapplikation som är redo för testning. Följande är ett exempel på en exekvering:

instruktioner:\n0 avsluta\n1 tillsätt inlägg\n2 sök

instruktion: 1\nnamn: Erik\nnummer: 02-123456

instruktion: 1\nnamn: Erik\nnummer: 045-4356713

instruktion: 2\nnamn: Erik\n02-123456\n045-4356713

instruktion: 2\nnamn: Emilia\nnummer okänd

instruktion: 0

För en så enkel applikation har vi skrivit ganska mycket kod. Om vi hade skrivit allt i den enda while-loopen hade vi förmodligen kunnat komma undan med mycket mindre kod. Det är dock ganska lätt att läsa koden, strukturen är tydlig och vi borde inte ha några problem med att lägga till nya funktioner.

Steg 3: Importera data från en fil

Låt oss anta att vi redan har några telefonnummer lagrade i en fil, och att vi vill läsa detta när programmet startar. Datafilen är i följande CSV-format:

Erik;02-1234567;045-4356713\nEmilia;040-324344

Hantering av filer är helt klart ett eget ansvarsområde, så det förtjänar en klass för sig:

class FilHanterare():\n    def __init__(self, fil):\n        self.__fil = fil\n\n    def ladda(self):\n        namnen = {}\n        with open(self.__fil) as f:\n            for rad in f:\n                delar = rad.strip().split(';')\n                namn, *numren = delar\n                namnen[namn] = numren\n\n        return namnen

Konstruktormetoden tar namnet på filen som sitt argument. Metoden ladda(self) läser innehållet i filen. Varje rad delas upp i två delar: ett namn och en lista med siffror. Sedan läggs dessa till i en ordbok, med namnet som nyckel och listan som värde.

Metoden använder en smidig Python-funktion: det är möjligt att först välja några objekt från en lista separat och sedan ta resten av objekten i en ny lista. Du kan se ett exempel på detta nedan. Du kanske minns från modul 6 att strängmetoden split returnerar en lista.

lista = [1, 2, 3, 4, 5]\nforsta, andra, *resten = lista\nprint(forsta)\nprint(andra)\nprint(resten)

1\n2\n[3, 4, 5]

Tecknet * framför variabelnamnet resten i ovanstående exempel betyder att den sista variabeln ska innehålla alla återstående poster i listan, från den tredje och framåt.

Vi bör absolut testa filhanteraren separat innan vi inkluderar den i vår applikation:

t = FilHanterare(\"katalog.txt\")\nprint(t.ladda())

{'Erik': ['02-1234567', '045-4356713'], 'Emilia': ['040-324344']}

Eftersom filhanteraren verkar fungera bra kan vi lägga till den i vår applikation. Låt oss anta att vi vill läsa filen som det första varje gång programmet körs. Den logiska platsen för att läsa filen skulle vara konstruktören för klassen TelefonkatalogApplikation:

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n        self.__fil = FilHanterare(\"katalog.txt\")\n\n        # lägg till namn och nummer från filen till telefonkatalogen\n        for namn, numren in self.__fil.ladda().items():\n            for nummer in numren:\n                self.__katalog.tillsatt_nummer(namn, nummer)\n\n    # annan kod

Denna funktionalitet bör också testas. När vi har försäkrat oss om att filens innehåll är tillgängligt via användargränssnittet i vår applikation kan vi gå vidare till nästa steg.

Steg 4: exportera data till en fil

Den sista funktionen i vår grundversion av applikationen är att spara innehållet i telefonkatalogen tillbaka i samma fil som data lästes från.

Detta innebär en förändring av Telefonkatalog-klassen. Vi måste kunna exportera innehållet i telefonkatalogen:

class Telefonkatalog:\n    def __init__(self):\n        self.__personer = {}\n\n    # ...\n\n    # returnera alla inlägg (i ordlistsformat)\n    def alla_uppgifter(self):\n        return self.__personer

Själva sparandet till filen bör hanteras av FilHanterare-klassen. Låt oss lägga till metoden spara som tar en ordlistsrepresentation av telefonkatalogen som argument:

class FilHanterare():\n    def __init__(self, fil):\n        self.__fil = fil\n\n    def ladda(self):\n        # ...\n\n    def spara(self, katalog: dict):\n        with open(self.__fil, \"w\") as f:\n            for namn, numren in katalog.items():\n                rad = [namn] + numren\n                f.write(\";\".join(rad) + \"\\n\")

Sparandet bör ske när programmet avslutas. Låt oss lägga till en metod för detta ändamål i användargränssnittet och anropa den innan vi bryter ut ur while-loopen:

\nclass TelefonkatalogApplikation:\n    # resten av koden för användargränssnittet\n\n    # metod som exekveras till programmet avslutas\n    def avsluta(self):\n        self.__fil.spara(self.__katalog.alla_uppgifter())\n\n    def exekvera(self):\n        self.hjalp()\n        while True:\n            print(\"\")\n            instruktion = input(\"instruktion: \")\n            if instruktion == \"0\":\n\n                self.avsluta()\n                break\n            elif instruktion == \"1\":\n                self.inlagg()\n            elif instruktion == \"2\":\n                self.sok()\n            else:\n                self.hjalp()

I den här övningen kommer du att skapa en liten utvidgning av telefonkatalogapplikationen. Koden från exemplet ovan finns i övningsmallen. Lägg till ett kommando som låter användaren söka efter nummer i telefonkatalogen. Efter tillägget ska applikationen fungera på följande sätt:

instruktioner:\n0 avsluta\n1 tillsätt inlägg\n2 sök\n3 sök enligt nummer

instruktion: 1\nnamn: Erik\nnummer: 02-123456

instruktion: 1\nnamn: Erik\nnummer: 045-4356713

instruktion: 3\nnummer: 02-123456\nErik

instruktion: 3\nnummer: 0100100\nokänt nummer

instruktion: 0

Implementera detta tillägg med hänsyn till den nuvarande strukturen i programmet. Detta innebär att du i klassen TelefonkatalogApplikation bör lägga till en lämplig hjälpmetod för att möjliggöra den nya funktionaliteten, och även lägga till en ny gren i while-loopen. I Telefonkatalog-klassen bör du lägga till en metod som gör det möjligt att söka med ett nummer.

Objekt i en ordlista

I nästa övning ombeds du att ändra din telefonkatalog så att värdena i ordlistan är objekt, inte listor.

Det är inget konstigt med det i sig, men det är första gången på den här kursen som något sådant föreslås, så låt oss gå igenom ett enklare exempel innan vi dyker ner i övningen.

Här har vi en applikation som håller reda på hur många övningar som studenterna har gjort på en kurs. Varje elevs antal övningar lagras i ett enkelt objekt:

class Uppgiftsraknare:\n    def __init__(self):\n        self.__uppgifter = 0\n\n    def klar(self):\n        self.__uppgifter += 1\n\n    def gjorda(self):\n        return self.__uppgifter

Följande huvudfunktion använder klassen ovan:

studeranden = {}\n\nprint(\"markerar uppgifter\")\nwhile True:\n    namn = input(\"studerande: \")\n    if len(namn) == 0:\n        break\n\n    # skapa ett nytt objekt ifall det inte finns ännu\n    if not namn in studeranden:\n        studeranden[namn] = Uppgiftsraknare()\n\n    # lägg till en ny utförd uppgift i räknaren\n    studeranden[namn].klar()\n\nprint()\nprint(\"gjorda uppgifter:\")\n\nfor studerande, uppgifter in studeranden.items():\n    print(f\"{studerande} uppgifter {uppgifter.gjorda()} st\")

Att exekvera koden ovan kunde se ut enligt följande:

markerar uppgifter\nstuderande: peter\nstuderande: sara\nstuderande: anton\nstuderande: sara\nstuderande: jonas\nstuderande: jonas\nstuderande: anton\nstuderande: sara\nstuderande:

gjorda uppgifter:\npeter uppgifter 1 st\nanton uppgifter 2 st\nsara uppgifter 3 st\njonas uppgifter 2 st

Det finns ett par saker att tänka på i exemplet ovan. När användaren matar in ett namn kontrollerar programmet först om namnet redan är en nyckel i ordlistan. Om namnet inte finns skapas ett nytt objekt som läggs till som en post i ordlistan:

if not namn in studeranden:\n    studeranden[namn] = Uppgiftsraknare()

Efter detta kan vi vara säkra på att objektet existerar, kopplat till namnet på den student som används som nyckel. Antingen skapades det precis, eller så fanns det redan från en tidigare iteration av loopen. I vilket fall som helst kan vi nu hämta objektet med nyckeln och kalla metoden klar:

studeranden[namn].klar()

Raden ovan innehåller egentligen två separata händelser. Vi kan lika gärna använda en hjälpvariabel och skriva den på två separata kodrader:

studerandes_raknare = studeranden[namn]\nstuderandes_raknare.klar()

OBS: Även om objektet här tilldelas en hjälpvariabel, så finns objektet kvar i ordlistan precis som tidigare. Hjälparvariabeln innehåller en referens till objektet i ordlistan.

Om du inte är helt säker på vad som egentligen händer i koden ovan kan du prova med visualiseringsverktyget.

I denna övning kommer du att skapa en annan version av TelefonkatalogApplikation. Du kommer att lägga till adresser i datan som kan kopplas till ett namn. För enkelhetens skull har funktionaliteten för att spara till fil tagits bort, och vissa andra metoder har bytt namn för att bättre passa in i förändringen.

En separat klass för en persons data

Ändra hur data om en person hanteras. Implementera en klass med namnet Person, som tar hand om personers telefonnummer och adresser. Klassen ska fungera på följande sätt:

person = Person(\"Erik\")\nprint(person.namn())\nprint(person.numren())\nprint(person.adress())\nperson.tillsatt_nummer(\"040-123456\")\nperson.tillsatt_adress(\"Mannerheimintie 10 Helsinki\")\nprint(person.numren())\nprint(person.adress())

Erik\n[]\nNone\n['040-123456']\nMannerheimintie 10 Helsinki

Telefonkatalogs användning av klassen Person

Ändra den interna implementeringen av din applikation så att din Telefonkatalog-klass använder objekt av klassen Person för att lagra data i telefonkatalogen. Det vill säga, attributet __personer ska fortfarande innehålla en ordlista, men värdena ska vara Person-objekt och inte listor. Användaren av din applikation ska inte märka någon skillnad; ändringarna får inte påverka användargränssnittet.

VARNING: när du gör strukturella ändringar i din kod, som beskrivs i den här övningen, ska du alltid ta små steg och testa i varje möjligt skede. Försök inte att göra alla ändringar på en gång. Det är ett säkert sätt att stöta på allvarliga problem med din kod.

Ett lämpligt första steg kan vara att skriva lite kod för att kontrollera funktionaliteten i Telefonkatalog-klassen direkt. Till exempel bör följande åtminstone inte orsaka några fel:

katalog = Telefonkatalog()\nkatalog.tillsatt_nummer(\"Erik\", \"02-123456\")\nprint(katalog.hamta_uppgifter(\"Erik\"))\nprint(katalog.hamta_uppgifter(\"Emilia\"))

Lägg märke till det nya namnet på metoden för att hämta ett inlägg från telefonkatalogen. De automatiska testerna kontrollerar inte vad utskriften från din hamta_uppgifter-metod är, utan ser till att inga fel uppstår av ovanstående kod och att resultatet är logiskt inom din implementation.

När du har gjort de nödvändiga ändringarna i ditt program och absolut har verifierat funktionaliteten i Telefonkatalog-klassen kan du gå vidare till användargränssnittet och se om allt fortfarande fungerar som förväntat.

Tillägg av adress

Vänligen implementera funktionaliteten för att lägga till en adress till ett inlägg i din telefonkatalog. Programmet ska fungera på följande sätt:

instruktioner:\n0 avsluta\n1 tillsätt nummer\n2 sök\n3 tillsätt adress

instruktion: 1\nnamn: Erik\nnummer: 02-123456

instruktion: 3\nnamn: Emilia\nadress: Viherlaaksontie 7, Espoo

instruktion: 2\nnamn: Erik\n02-123456\nadress okänd

instruktion: 2\nnamn: Emilia\nnummer okänd\nViherlaaksontie 7, Espoo

instruktion: 3\nnamn: Erik\nadress: Linnankatu 75, Turku

instruktion: 2\nnamn: Erik\n02-123456\nLinnankatu 75, Turku

instruktion: 2\nnamn: Wilhelm\nadress okänd\nnummer okänd

instruktion: 0

VARNING och tips: som nämndes ovan i den föregående övningen, försök inte att göra alla ändringar på en gång. Det är ett säkert sätt att stöta på allvarliga problem med din kod.

Kontrollera först att du på ett tillförlitligt sätt kan lägga till adresser med hjälp av Telefonkatalog-klassen direkt. När du har verifierat detta kan du gå vidare till de nödvändiga ändringarna i användargränssnittet.

Några avslutande anmärkningar

Strukturen i Telefonkatalog-exemplet ovan följer de grundläggande principerna för objektorienterad programmering ganska väl. Den centrala principen är att identifiera de olika ansvarsområdena i programmet och fördela dessa logiskt mellan de olika klasserna och metoderna. Ett av de viktigaste motiven för denna uppdelning är att hantera komplexiteten. Ett annat viktigt motiv är att en logisk uppdelning av ansvarsområden - modularitet i facktermer - ofta gör koden lättare att underhålla och bygga vidare på.

I de programvarupaket som utvecklas och används ute i världen är underhåll och utbyggnad, dvs felsökning av befintlig programvara och implementering av nya funktioner, den absolut dyraste delen av utvecklingen. Korrekt implementerad modularitet är ekonomiskt sett en mycket viktig funktion i programvaruutvecklingen.

Det finns några fler objektorienterade programmeringsprinciper som är värda att lyfta fram här. Telefonkatalog är ett bra exempel på hur den centrala programlogiken kan (och bör) separeras från både användargränssnitt och datalagring. Detta är viktigt på grund av ett par olika skäl. För det första gör denna separation det möjligt att testa koden i mindre enheter, en klass och metod i taget. För det andra, eftersom kärnlogiken nu är oberoende av gränssnitten som kommer till omvärlden, är det möjligt att i viss utsträckning ändra implementeringen av antingen kärnlogiken eller gränssnitten utan att hela applikationen går sönder.

Filhanteringen i Telefonkatalog-programmet går till på följande sätt: Programmet läser filen en enda gång, när det startar. Efter detta lagras all data i variabler i programmet. När programmet avslutas lagras alla data igen, vilket i praktiken innebär att filen skrivs över. I de flesta fall är detta det rekommenderade sättet att hantera externa filer, eftersom det ofta är mycket mer komplicerat att redigera data på plats.

Det finns många bra guideböcker för att lära sig om god programmeringspraxis. En sådan är Clean Code av Robert Martin. Kodexemplen i boken är dock implementerade i Java, så att arbeta igenom exemplen kan vara ganska besvärligt vid denna tidpunkt i din programmeringskarriär, även om boken i sig rekommenderas mycket av kurspersonalen. Teman som lätt underhållen, utbyggbar kod av god kvalitet kommer att utforskas ytterligare på kurserna Software Development Methods och Software Engineering.

Att skriva kod enligt etablerade principer för objektorienterad programmering har ett pris. Du kommer sannolikt att få skriva mer kod än om du skulle skriva din implementation i en enda kontinuerlig stöt av spaghettikod. En av de viktigaste färdigheterna hos en programmerare är att bestämma det bästa tillvägagångssättet för varje situation. Ibland är det nödvändigt att bara hacka ihop något snabbt för omedelbar användning. Å andra sidan, om det inom överskådlig framtid kan förväntas att koden kommer att återanvändas, underhållas eller vidareutvecklas, antingen av dig eller, mer kritiskt, av någon helt annan, blir programkodens läsbarhet och logiska modularitet väsentlig. Oftast är det så att om det är värt att göra, så är det värt att göra det bra, även i de allra tidigaste utvecklingsstadierna.

För att avsluta denna del av materialet kommer du att implementera ytterligare en större applikation.

Skriv en interaktiv applikation för att hålla koll på dina studier. Den interna strukturen är upp till dig, men det här skulle vara ett bra tillfälle att öva på att skapa en liknande struktur som i Telefonkatalog-exemplet ovan.

Ditt program bör fungera på följande sätt:

1 tillsätt prestation\n2 hämta prestation\n3 statistik\n0 avsluta

instruktion: 1\nkurs: ItP\nvitsord: 3\nstudiepoäng: 5

instruktion: 2\nkurs: ItP\nItP (5 sp) vitsord 3

instruktion: 1\nkurs: ItP\nvitsord: 5\nstudiepoäng: 5

instruktion: 2\nkurs: ItP\nItP (5 sp) vitsord 5

instruktion: 1\nkurs: ItP\nvitsord: 1\nstudiepoäng: 5

instruktion: 2\nkurs: ItP\nItP (5 sp) vitsord 5

instruktion: 2\nkurs: Introduktion till Java\ninga prestationer

instruktion: 1\nkurs: Tira\nvitsord: 1\nstudiepoäng: 10

instruktion: 1\nkurs: Tilpe\nvitsord: 2\nstudiepoäng: 5

instruktion: 1\nkurs: Lapio\nvitsord: 4\nstudiepoäng: 1

instruktion: 1\nkurs: Lama\nvitsord: 5\nstudiepoäng: 8

instruktion: 3\n5 avklarade kurser, totalt 29 studiepoäng\nmedeltal 3.4\nvitsordsdistribution\n5: xx\n4: x\n3:\n2: x\n1: x

instruktion: 0

Varje kursnamn ska resultera i ett enda tillägg i registret. Ett betyg kan höjas genom att kursuppgifterna skrivs in på nytt, men betyget får aldrig sänkas.

Denna övning är värd två övningspoäng. Den första ges om kommandona 1, 2 och 0 fungerar korrekt i ditt program. Det andra ges om även kommando 3 fungerar som förväntat.

Epilog

För att avsluta den här delen av materialet återvänder vi till användargränssnittet i telefonkatalogsexemplet för en stund.

class TelefonkatalogApplikation:\n    def __init__(self):\n        self.__katalog = Telefonkatalog()\n        self.__fil = FilHanterare(\"katalog.txt\")\n\n    # annan kod\n\napplikation = TelefonkatalogApplikation()\napplikation.exekvera()

Ett TelefonkatalogApplikation-objekt innehåller både ett Telefonkatalog-objekt och ett FilHanterare-objekt. Namnet på den fil som skickas till FilHanterare är för närvarande hårdkodat i TelefonkatalogApplikation-klassen. Detta är en helt irrelevant detalj när det gäller applikationens användargränssnitt. Faktum är att den bryter mot principen om separation of concerns: var ett Telefonkatalog-objekt sparar sitt innehåll ska inte vara en angelägenhet för en TelefonkatalogApplikation, men om vi vill ändra platsen måste vi ändra koden för TelefonkatalogApplikation.

Det skulle vara en bättre idé att skapa ett FilHanterare-objekt någonstans utanför TelefonkatalogApplikation-klassen och skicka det som ett argument till applikationen:

class TelefonkatalogApplikation:\n    def __init__(self, fil):\n        self.__katalog = Telefonkatalog()\n        self.__fil = fil\n\n    # annan kod\n\n# skapar ett objekt för att hantera sparning av filen\nforvaringstjanst = FilHanterare(\"katalog.txt\")\n# och passerar det som argument till TelefonkatalogApplikation-objektets konstruktor\napplikation = TelefonkatalogApplikation(forvaringstjanst)\napplikation.exekvera()

Detta tar bort ett onödigt beroende från TelefonkatalogApplikation-klassen. Om namnet på filen ändras behöver användargränssnittet inte längre ändras. Vi behöver bara skicka ett annat argument till konstruktören:

class TelefonkatalogApplikation:\n    def __init__(self, fil):\n        self.__katalog = Telefonkatalog()\n        self.__fil = fil\n\n    # annan kod\n\n# använd ett annat filnamn\nforvaringstjanst = FilHanterare(\"ny_katalog.txt\")\napplikation = TelefonkatalogApplikation(forvaringstjanst)\napplikation.exekvera()

Denna förändring gör det också möjligt för oss att överväga mer exotiska lagringsplatser, till exempel en molntjänst på internet. Vi behöver bara implementera en klass som använder molntjänsten och erbjuder TelefonkatalogApplikation exakt samma metoder som FilHanterare.

En instans av denna nya \"moln hanterar\"-klass kan skickas som ett argument till konstruktören och inte en enda rad kod behöver ändras i användargränssnittet:

class MolnHanterare:\n    # kod som sparar innehållet av telefonkatalogen i en molntjänst på internet\n\nforvaringstjanst = MolnHanterare(\"amazon-cloud\", \"anvandarnamn\", \"losenord\")\napplikation = TelefonkatalogApplikation(forvaringstjanst)\napplikation.exekvera()

Som du har sett tidigare har den här typen av tekniker ett pris, eftersom det blir mer kod att skriva, så en programmerare måste överväga om det är en acceptabel avvägning.

Den teknik som beskrivs ovan kallas beroendeinjektion. Som namnet antyder är tanken att alla beroenden som krävs av ett objekt ska tillhandahållas utanför objektet. Det är ett mycket användbart verktyg i en programmerares verktygslåda, eftersom det gör det lättare att implementera nya funktioner i program och underlättar automatisk testning. Detta tema kommer att utforskas ytterligare på de tidigare nämnda kurserna Software Development och Software Engineering.

Svara vänligen på en snabb enkät om denna del av kursen.

","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/4-storre-applikation.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-10/page-data.json b/page-data/osa-10/page-data.json index 3e8329133..7b1bab2a8 100644 --- a/page-data/osa-10/page-data.json +++ b/page-data/osa-10/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-10", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-10","title":"Modul 10"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-10","title":"Modul 10"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-10/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-11/1-list-comprehension/page-data.json b/page-data/osa-11/1-list-comprehension/page-data.json index 9a97a9910..8aa9e368e 100644 --- a/page-data/osa-11/1-list-comprehension/page-data.json +++ b/page-data/osa-11/1-list-comprehension/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-11/1-list-comprehension", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad list comprehension är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda list comprehensions för att skapa nya listor"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En av de situationer där programmering är som mest kraftfull är vid bearbetning av sekvenser av objekt och händelser. Datorer är bra på att upprepa saker. I de tidigare delarna av det här materialet har vi till exempel itererat strängar, listor och ordlistor på olika sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi har en lista med heltal och att vi skulle behöva samma lista med objekt i strängformat. Ett traditionellt sätt att utföra uppgiften skulle kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nstrangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n strangar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"list-comprehension","style":"position:relative;"},"children":[{"type":"text","value":"List comprehension"},{"type":"element","tagName":"a","properties":{"href":"#list-comprehension","ariaLabel":"list comprehension permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns också ett mer \"pythoniskt\" sätt att generera listor från befintliga listor. Dessa kallas list comprehensions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tanken är att på en enda rad få plats med både beskrivningen av vad som ska göras med varje objekt i listan och tilldelningen av resultatet till en ny lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan var operationen som utfördes på varje objekt i listan mycket enkel: varje heltal omvandlades till en sträng. Låt oss se hur detta skulle se ut implementerat med en list comprehension:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstrangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den andra raden ovan innehåller många av samma element som den mer traditionella iterativa metoden, men syntaxen är annorlunda. Ett sätt att generalisera en list comprehension skulle kunna vara"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ for in ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hakparenteserna runt list comprehensionsatsen signalerar till Python att resultatet ska vara en ny lista. En efter en bearbetas varje objekt i den ursprungliga listan och resultatet lagras i den nya listan, precis som i det iterativa tillvägagångssättet ovan. Som resultat har vi en ny lista med exakt lika många objekt som i originalet, och alla objekt har behandlats på ett identiskt sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(OBS: originalen till bilderna i denna del saknas tillfälligt, vilket är anledningen till att det finns en del finskt vokabulär i illustrationerna i denna del. Vi arbetar på att åtgärda detta)."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 301px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/bb2be65b36f7fdf4ea48dfde815e9fbf/fb933/11_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 131.7391304347826%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAACVUlEQVRIx42V247aQAyG5/0fibtWlZYLDqLAcgOoBDblHMIpgQRcfVYdDasku5as+ceneOwZx0kNPfJcRsOh9PsDSZJUvkPucDhIFcdxLNfrVRlcZ2vsVquVZFkmaZoKmJX9ZrOR8/mseL/fSxRFkue5ytCV+YAdhkY+JiOMoMvlogwhQ1fmA3a73a4QbLdbeT6fisnqdrspJisYQoYOwhYfI2K59Xqtqc7ncz0KvFgsdMUYjA0MRmbY7PElBnJnXyKD4/GoX2KlERAO6KmfnQadb4uvncwZgJIk0U5BrASxWuJEIJOZDT5GxHL+Brrf72pIrU6nkzrABERHRuhoAHvfVzP0BbY+Hg8NBlMrmBoRhIDIsSnzLQJ+Zo7V6XSk3W5Lo9GQVqslg8FAut2u6sp8ioB+UCu03bsy8pvi+70E9NPmaBzpbxjK21tTms2m/Pr5Q1rtrqS3u2TZvWieH6wyIMYU/Jameu9oir7n/1nRlC8D+qnb+6WzBBuPx8pJctWGoPM7XNtlv9MUfzabSa/XUwYj8ztc22Uju28clwA2HMDI0FmGn/1fXgpftnuGM2MJtoC2R4cNtpbty0vhOWFAoW2Y2lvm4aOHwXZtzAYffNFrhkyI6XSqNWIQLJdLvdBgXsdwONTVx+iwMYwvMXTa+POsah7aM/xqHoJrJ7Ydw/4rVp7aiR0EgRrwVTBC9jZwwWEYSvjxoU2wgYocW3zwZQ92tL+K/Q5adnX2sKv7xx6iSEfX6H0k/f7vb/2bXdkYKqZOHMufINAjTiaTImCVD/fwH00l3UL1OMHGAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bb2be65b36f7fdf4ea48dfde815e9fbf/a0b58/11_1_2.webp 230w","/static/bb2be65b36f7fdf4ea48dfde815e9fbf/c4c4c/11_1_2.webp 301w"],"sizes":"(max-width: 301px) 100vw, 301px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bb2be65b36f7fdf4ea48dfde815e9fbf/81c8e/11_1_2.png 230w","/static/bb2be65b36f7fdf4ea48dfde815e9fbf/fb933/11_1_2.png 301w"],"sizes":"(max-width: 301px) 100vw, 301px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/bb2be65b36f7fdf4ea48dfde815e9fbf/fb933/11_1_2.png","alt":"11 1 2","title":"11 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List comprehensions kan också hantera mycket mer komplicerade operationer. Vi kan utföra beräkningar, till exempel multiplicera de ursprungliga objekten med tio:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntalen_multiplicerade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen_multiplicerade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n[10, 20, 30, 40, 50, 60, 70, 80, 90]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Faktum är att uttrycket i list comprehension-satsen kan vara vilket Python-uttryck som helst. Du kan till och med anropa funktioner som du själv har definierat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakulteter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[120, 2, 24, 6, 1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med den mer välbekanta "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen skulle samma process kunna uttryckas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakulteter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List Comprehensions gör att vi kan uttrycka samma funktionalitet på ett mer konsekvent sätt, vanligtvis utan att förlora något av läsbarheten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också returnera en list comprehension-sats direkt från en funktion. Om vi behövde en funktion för att producera fakultettal för listor med tal, skulle vi kunna göra det på ett mycket kortfattat sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakulteter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kvadratrötter","tmcname":"osa11-01_kvadratrotter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kvadratrot(nummer: list)"}]},{"type":"text","value":" som tar en lista med heltal som sitt argument. Funktionen ska returnera en ny lista som innehåller kvadratrötterna av de ursprungliga heltalen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Modulen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" från Pythons standardbibliotek innehåller en lämplig funktion för att räkna ut kvadratrötter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör använda en list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rader "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kvadratrot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1.0\n1.4142135623730951\n1.7320508075688772\n2.0"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Stjärnrader","tmcname":"osa11-02_stjarnrader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"stjarnrader(nummer: list)"}]},{"type":"text","value":" som tar en lista med heltal som sitt argument. Funktionen ska returnera en ny lista som innehåller rader med stjärnor. Längden på varje rad ska motsvara heltalet på samma index i den ursprungliga listan. Funktionen bör använda en list comprehension för att uppnå detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rader "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stjarnrader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrader "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stjarnrader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*\n**\n***\n****\n\n****\n***\n**\n*\n**********\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bäst provresultat","tmcname":"osa11-03_bast_provresultat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns färdigt en klassdefinition för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Provresultat"}]},{"type":"text","value":", som har följane offentliga attribut:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vitsord1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vitsord2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vitsord3"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bast_resultat(resultat: list)"}]},{"type":"text","value":" som tar en lista med Provresultat-objekt som sitt argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en ny lista som endast innehåller det bästa resultatet från varje Provresultat-objekt. Funktionen bör använda en list comprehension för att uppnå detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paulus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprestationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"resultat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bast_resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[5, 4, 3]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Längder","tmcname":"osa11-04_langder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langder(listor: list)"}]},{"type":"text","value":" som tar en lista innehållande listor med heltal som argument. Funktionen ska returnera en ny lista som innehåller längderna på listorna i argumentlistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör använda en list comprehension för att uppnå detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"listat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"324"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"31"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"listat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[5, 4, 0]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-filtrera-foremal","style":"position:relative;"},"children":[{"type":"text","value":"Att filtrera föremål"},{"type":"element","tagName":"a","properties":{"href":"#att-filtrera-foremal","ariaLabel":"att filtrera foremal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan var alla våra listor lika långa före och efter en list comprehension-operation. I varje fall användes alla föremål i den ursprungliga listan som grund för den nya listan. Men ibland behöver vi bara några av de ursprungliga föremålen. Hur kan detta åstadkommas?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En list comprehension-sats kan också innehålla ett villkor, så att vi kan kontrollera objekten mot villkoret och bara välja ut dem som matchar. Den allmänna syntaxen är enligt följande:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ for in if ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Satsen ovan är i övrigt identisk med den allmänna form som introducerades i början av detta avsnitt, men nu finns det en if-sats i slutet. Endast de objekt från den ursprungliga listan för vilka det booleska uttrycket är sant används som grund för den nya listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet nedan väljer vi alla jämna objekt från den ursprungliga listan som bas för den nya listan. I själva verket bearbetas inte dessa objekt ytterligare på något sätt, utan de tilldelas den nya listan som de är:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\njamna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 4, 6, 4, 10, 12]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uttrycket i list comprehension-satsen ovan är bara ett enkelt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"foremal"}]},{"type":"text","value":", vilket innebär att inga operationer ska utföras på föremålen i listan. Uttrycket kan vara vilket Python-uttryck som helst, precis som i de tidigare exemplen. Följande list comprehension-sats tar till exempel alla jämna föremål i en lista, multiplicerar varje föremål med tio och lagrar resultatet i en ny lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\njamna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[20, 40, 60, 40, 100, 120]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du stöter på mer och mer komplicerade list comprehensions kan det vara bra att försöka läsa villkoret först. Föremålen bearbetas ändå bara om de klarar testet, så det är ofta vettigt att först ta reda på vilka objekt som klarar filtreringssteget. Ibland skulle uttrycket i en list comprehension-sats inte ens vara möjligt för alla föremål i den ursprungliga listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel är fakultettal bara definierat för icke-negativa heltal. Om vi inte kan vara säkra på att en lista bara innehåller värden på noll eller högre, måste innehållet filtreras innan det skickas vidare till den fakultetfunktion som vi skapade tidigare:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakulteter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[6, 24, 120, 1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som vi såg i vårt allra första exempel på list comprehension, där heltal omvandlades till strängar, behöver föremålen i den nya listan inte vara av samma typ som föremålen i den ursprungliga listan. Om vi fortsätter från fakultetexemplet ovan kan vi skapa en tupel från varje originella föremål och dess bearbetade motsvarighet och lagra dessa i en lista, vilket kombinerar allt vi har lärt oss hittills i en enda list comprehension-sats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakulteter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[(2, 2), (4, 24), (6, 720)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi plockar isär exemplet ovan har vi det booleska uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n > 0 and n % 2 == 0"}]},{"type":"text","value":". Detta innebär att endast föremål som är både positiva och delbara med två accepteras för vidare bearbetning från den ursprungliga listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa positiva, jämna tal bearbetas sedan i tur och ordning till formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(n, fakultet(n))"}]},{"type":"text","value":". Detta är en tupel, där det första objektet är själva talet och det andra objektet är resultatet som returneras av fakultetfunktionen."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ta bort mindre","tmcname":"osa11-05_ta_bort_mindre"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ta_bort_mindre(tal: list, grans: int)"}]},{"type":"text","value":" som tar en lista med heltal och ett gränsvärde (också i heltalsformat) som sina argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör använda en list comprehension för att producera en ny lista utan de värden som är mindre än gränsvärdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"65"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ta_bort_mindre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ta_bort_mindre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[65, 32, 11]\n[7, 8]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Börjar med vokal","tmcname":"osa11-06_borjar_med_vokal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"borjar_med_vokal(ord: list)"}]},{"type":"text","value":" som tar en lista med strängar som sitt argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska använda en list comprehension teknik för att skapa och returnera en ny lista som endast innehåller de ord från den ursprungliga listan som börjar med en vokal (a, e, i, o, u, y, å, ä, ö). Både små och stora bokstäver bör accepteras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ord_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"automobil\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"moped\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Enhörning\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hund\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ÄPPEL\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"appelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" vok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" borjar_med_vokal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"automobil\nEnhörning\nÄPPEL\nappelsin"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"alternativ-exekvering-med-list-comprehension","style":"position:relative;"},"children":[{"type":"text","value":"Alternativ exekvering med list comprehension"},{"type":"element","tagName":"a","properties":{"href":"#alternativ-exekvering-med-list-comprehension","ariaLabel":"alternativ exekvering med list comprehension permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ofta när vi har en villkorlig sats inkluderar vi också en else-gren. Eftersom vi kan använda villkor i list comprehensions är else-grenen också tillgänglig med list comprehension. Den allmänna syntaxen för villkoret som används med list comprehension ser ut så här:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":" if else "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi stötte på dessa enradiga villkor, eller ternära operatorer, redan i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-7/6-funktionaliteter-python","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 7"}]},{"type":"text","value":". Uttrycket ovan utvärderas till antingen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttryck 1"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttryck 2"}]},{"type":"text","value":", beroende på om villkoret är sant eller falskt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som en uppfräschning av ämnet kan vi säga att om vi behöver skriva ut det större av två tal och vi bara vill använda en enda utskriftssats, kan vi få plats med allt på en enda rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"tal1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange tal 1:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntal2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange tal 2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" tal2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" tal2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Genom att kombinera den ternära operatorssyntaxen med en list comprehension-sats får man följande allmänna struktur:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ if else for in ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kan se lite förvirrande ut, eftersom den villkorliga strukturen nu kommer före den faktiska list comprehensionen. Det är bara så här syntaxen har definierats, åtminstone för tillfället. Om det också finns en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":"-gren kommer villkoret först. Om det bara finns ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":", kommer det sist. Du kan prova att byta ut dem och se vad som händer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att inkludera en else-operator innebär att vi återigen kommer att bearbeta varje objekt från den ursprungliga listan. Beroende på om villkoret är sant eller falskt utförs antingen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttryck 1"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttryck 2"}]},{"type":"text","value":" på varje objekt i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel kontrolleras om föremåleni en lista är noll eller högre. Alla sådana föremål accepteras som de är, men alla negativa föremål negeras, så att tecknet ändras från negativt till positivt. Resultatet är en lista som innehåller de absoluta värdena för föremålen i den ursprungliga listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\ntalen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nabsolut_varde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"absolut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 45, 110, 2, 9, 11]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi upprepar vad som händer ovan: om villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer >= 0"}]},{"type":"text","value":" är sant, genomgår föremålet uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer"}]},{"type":"text","value":", och resultatet är själva föremålet. Om villkoret är falskt genomgår föremålet uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"–nummer"}]},{"type":"text","value":", så att det får ett positivt värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel har vi funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strang_langder"}]},{"type":"text","value":" som tar en lista som sitt argument och returnerar en annan lista med längderna på alla strängar i den ursprungliga listan. Den här funktionen är dock okej med listföremål av alla typer. Om föremålet är en sträng beräknar den dess längd. Om objektet är något annat infogar den -1 i listan som den returnerar."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"strang_langder"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen returnerar längden på strängarna i en ny lista \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n testlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"123.344"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hejsansvejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n langder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang_langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[3, -1, -1, 6, -1, 13, -1, -1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lotterinummer","tmcname":"osa11-07_lotterinummer"},"children":[{"type":"element","tagName":"h2","properties":{"id":"lotterinummer-matchade","style":"position:relative;"},"children":[{"type":"text","value":"LotteriNummer matchade"},{"type":"element","tagName":"a","properties":{"href":"#lotterinummer-matchade","ariaLabel":"lotterinummer matchade permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LotteriNummer"}]},{"type":"text","value":" som tar veckonumret (ett heltalsvärde) och en lista med sju heltal som sina konstruktorsargument. Listan ska innehålla de korrekta lotterinumren för den givna veckan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa också en metod med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"antalet_traffar(nummer: list)"}]},{"type":"text","value":" som tar en lista med heltal som sitt argument. Metoden returnerar antalet korrekta inlägg i parameterlistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden bör använda en list comprehension för att uppnå detta. Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ratta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteriNummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\negen_rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ratta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"antalet_traffar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"egen_rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lotterinummer-matchade-pa-plats","style":"position:relative;"},"children":[{"type":"text","value":"LotteriNummer matchade på plats"},{"type":"element","tagName":"a","properties":{"href":"#lotterinummer-matchade-pa-plats","ariaLabel":"lotterinummer matchade pa plats permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en metod med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"traffar_pa_plats(nummer)"}]},{"type":"text","value":" som tar en lista med sju heltal som sitt argument och returnerar en ny lista med sju heltal. Den nya listan innehåller endast de inlägg från den ursprungliga listan som matchar veckans korrekta nummer. Dessa måste ligga kvar på samma index som de gjorde i den ursprungliga listan. Resten av indexen bör fyllas med värdena "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör använda en list comprehension för att uppnå detta. Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ratta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteriNummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\negen_rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ratta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"traffar_pa_plats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"egen_rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, -1, -1, 10, -1, 20, 30]"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad list comprehension är
  • \n
  • Kommer du att kunna använda list comprehensions för att skapa nya listor
  • \n

En av de situationer där programmering är som mest kraftfull är vid bearbetning av sekvenser av objekt och händelser. Datorer är bra på att upprepa saker. I de tidigare delarna av det här materialet har vi till exempel itererat strängar, listor och ordlistor på olika sätt.

Låt oss anta att vi har en lista med heltal och att vi skulle behöva samma lista med objekt i strängformat. Ett traditionellt sätt att utföra uppgiften skulle kunna se ut så här:

talen = [1, 2, 3, 6, 5, 4, 7]\n\nstrangar = []\nfor tal in talen:\n    strangar.append(str(tal))

List comprehension

Det finns också ett mer \"pythoniskt\" sätt att generera listor från befintliga listor. Dessa kallas list comprehensions.

Tanken är att på en enda rad få plats med både beskrivningen av vad som ska göras med varje objekt i listan och tilldelningen av resultatet till en ny lista.

I exemplet ovan var operationen som utfördes på varje objekt i listan mycket enkel: varje heltal omvandlades till en sträng. Låt oss se hur detta skulle se ut implementerat med en list comprehension:

talen = [1, 2, 3, 6, 5, 4, 7]\nstrangar = [str(tal) for tal in talen]

Den andra raden ovan innehåller många av samma element som den mer traditionella iterativa metoden, men syntaxen är annorlunda. Ett sätt att generalisera en list comprehension skulle kunna vara

[<uttryck> for <föremål> in <serie>]

Hakparenteserna runt list comprehensionsatsen signalerar till Python att resultatet ska vara en ny lista. En efter en bearbetas varje objekt i den ursprungliga listan och resultatet lagras i den nya listan, precis som i det iterativa tillvägagångssättet ovan. Som resultat har vi en ny lista med exakt lika många objekt som i originalet, och alla objekt har behandlats på ett identiskt sätt.

(OBS: originalen till bilderna i denna del saknas tillfälligt, vilket är anledningen till att det finns en del finskt vokabulär i illustrationerna i denna del. Vi arbetar på att åtgärda detta).

\n \n \n \n \n \n \"11\n \n \n

List comprehensions kan också hantera mycket mer komplicerade operationer. Vi kan utföra beräkningar, till exempel multiplicera de ursprungliga objekten med tio:

talen = list(range(1,10))\nprint(talen)\n\ntalen_multiplicerade = [tal * 10 for tal in talen]\nprint(talen_multiplicerade)

[1, 2, 3, 4, 5, 6, 7, 8, 9]\n[10, 20, 30, 40, 50, 60, 70, 80, 90]

Faktum är att uttrycket i list comprehension-satsen kan vara vilket Python-uttryck som helst. Du kan till och med anropa funktioner som du själv har definierat:

def fakultet(n: int):\n    \"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [5, 2, 4, 3, 0]\n    fakulteter = [fakultet(tal) for tal in lista]\n    print(fakulteter)

[120, 2, 24, 6, 1]

Med den mer välbekanta for-loopen skulle samma process kunna uttryckas så här:

\ndef fakultet(n: int):\n    \"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [5, 2, 4, 3, 0]\n    fakulteter = []\n    for tal in lista:\n        fakulteter.append(fakultet(tal))\n    print(fakulteter)\n

List Comprehensions gör att vi kan uttrycka samma funktionalitet på ett mer konsekvent sätt, vanligtvis utan att förlora något av läsbarheten.

Vi kan också returnera en list comprehension-sats direkt från en funktion. Om vi behövde en funktion för att producera fakultettal för listor med tal, skulle vi kunna göra det på ett mycket kortfattat sätt:

def fakulteter(talen: list):\n    return [fakultet(tal) for tal in talen]

Skriv en funktion med namnet kvadratrot(nummer: list) som tar en lista med heltal som sitt argument. Funktionen ska returnera en ny lista som innehåller kvadratrötterna av de ursprungliga heltalen.

Modulen math från Pythons standardbibliotek innehåller en lämplig funktion för att räkna ut kvadratrötter.

Funktionen bör använda en list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet def.

Funktionen ska fungera enligt följande:

rader = kvadratrot([1,2,3,4])\nfor rad in rader:\n    print(rad)

1.0\n1.4142135623730951\n1.7320508075688772\n2.0

Skapa funktionen stjarnrader(nummer: list) som tar en lista med heltal som sitt argument. Funktionen ska returnera en ny lista som innehåller rader med stjärnor. Längden på varje rad ska motsvara heltalet på samma index i den ursprungliga listan. Funktionen bör använda en list comprehension för att uppnå detta.

Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

rader = stjarnrader([1,2,3,4])\nfor rad in rader:\n    print(rad)\n\nprint()\n\nrader = stjarnrader([4, 3, 2, 1, 10])\nfor rad in rader:\n    print(rad)
\n*\n**\n***\n****\n\n****\n***\n**\n*\n**********\n

I uppgiftsbotten finns färdigt en klassdefinition för Provresultat, som har följane offentliga attribut:

    \n
  • namn
  • \n
  • vitsord1
  • \n
  • vitsord2
  • \n
  • vitsord3
  • \n

Skapa en funktion med namnet bast_resultat(resultat: list) som tar en lista med Provresultat-objekt som sitt argument.

Funktionen ska returnera en ny lista som endast innehåller det bästa resultatet från varje Provresultat-objekt. Funktionen bör använda en list comprehension för att uppnå detta.

Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

resultat1 = Provresultat(\"Peter\",5,3,4)\nresultat2 = Provresultat(\"Pernilla\",3,4,1)\nresultat3 = Provresultat(\"Paulus\",2,1,3)\nprestationer = [resultat1, resultat2, resultat3]\nprint(bast_resultat(prestationer))

[5, 4, 3]

Skapa en funktion med namnet langder(listor: list) som tar en lista innehållande listor med heltal som argument. Funktionen ska returnera en ny lista som innehåller längderna på listorna i argumentlistan.

Funktionen bör använda en list comprehension för att uppnå detta.

Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

listat = [[1,2,3,4,5], [324, -1, 31, 7],[]]\nprint(langder(listat))

[5, 4, 0]

Att filtrera föremål

I exemplen ovan var alla våra listor lika långa före och efter en list comprehension-operation. I varje fall användes alla föremål i den ursprungliga listan som grund för den nya listan. Men ibland behöver vi bara några av de ursprungliga föremålen. Hur kan detta åstadkommas?

En list comprehension-sats kan också innehålla ett villkor, så att vi kan kontrollera objekten mot villkoret och bara välja ut dem som matchar. Den allmänna syntaxen är enligt följande:

[<uttryck> for <föremål> in <serie> if <boolskt uttryck>]

Satsen ovan är i övrigt identisk med den allmänna form som introducerades i början av detta avsnitt, men nu finns det en if-sats i slutet. Endast de objekt från den ursprungliga listan för vilka det booleska uttrycket är sant används som grund för den nya listan.

I exemplet nedan väljer vi alla jämna objekt från den ursprungliga listan som bas för den nya listan. I själva verket bearbetas inte dessa objekt ytterligare på något sätt, utan de tilldelas den nya listan som de är:

lista = [1, 1, 2, 3, 4, 6, 4, 5, 7, 10, 12, 3]\n\njamna = [foremal for foremal in lista if foremal % 2 == 0]\nprint(jamna)

[2, 4, 6, 4, 10, 12]

Uttrycket i list comprehension-satsen ovan är bara ett enkelt foremal, vilket innebär att inga operationer ska utföras på föremålen i listan. Uttrycket kan vara vilket Python-uttryck som helst, precis som i de tidigare exemplen. Följande list comprehension-sats tar till exempel alla jämna föremål i en lista, multiplicerar varje föremål med tio och lagrar resultatet i en ny lista:

lista = [1, 1, 2, 3, 4, 6, 4, 5, 7, 10, 12, 3]\n\njamna = [foremal * 10 for foremal in lista if foremal % 2 == 0]\nprint(jamna)

[20, 40, 60, 40, 100, 120]

När du stöter på mer och mer komplicerade list comprehensions kan det vara bra att försöka läsa villkoret först. Föremålen bearbetas ändå bara om de klarar testet, så det är ofta vettigt att först ta reda på vilka objekt som klarar filtreringssteget. Ibland skulle uttrycket i en list comprehension-sats inte ens vara möjligt för alla föremål i den ursprungliga listan.

Till exempel är fakultettal bara definierat för icke-negativa heltal. Om vi inte kan vara säkra på att en lista bara innehåller värden på noll eller högre, måste innehållet filtreras innan det skickas vidare till den fakultetfunktion som vi skapade tidigare:

def fakultet(n: int):\n    \"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [-2, 3, -1, 4, -10, 5, 1]\n    fakulteter = [fakultet(tal) for tal in lista if tal > 0]\n    print(fakulteter)

[6, 24, 120, 1]

Som vi såg i vårt allra första exempel på list comprehension, där heltal omvandlades till strängar, behöver föremålen i den nya listan inte vara av samma typ som föremålen i den ursprungliga listan. Om vi fortsätter från fakultetexemplet ovan kan vi skapa en tupel från varje originella föremål och dess bearbetade motsvarighet och lagra dessa i en lista, vilket kombinerar allt vi har lärt oss hittills i en enda list comprehension-sats:

\ndef fakultet(n: int):\n    \"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [-2, 3, 2, 1, 4, -10, 5, 1, 6]\n    fakulteter = [(tal, fakultet(tal)) for tal in lista if tal > 0 and tal % 2 == 0]\n    print(fakulteter)\n

[(2, 2), (4, 24), (6, 720)]

Om vi plockar isär exemplet ovan har vi det booleska uttrycket n > 0 and n % 2 == 0. Detta innebär att endast föremål som är både positiva och delbara med två accepteras för vidare bearbetning från den ursprungliga listan.

Dessa positiva, jämna tal bearbetas sedan i tur och ordning till formatet (n, fakultet(n)). Detta är en tupel, där det första objektet är själva talet och det andra objektet är resultatet som returneras av fakultetfunktionen.

Skapa en funktion med namnet ta_bort_mindre(tal: list, grans: int) som tar en lista med heltal och ett gränsvärde (också i heltalsformat) som sina argument.

Funktionen bör använda en list comprehension för att producera en ny lista utan de värden som är mindre än gränsvärdet.

Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

talen = [1,65, 32, -6, 9, 11]\nprint(ta_bort_mindre(talen, 10))\n\nprint(ta_bort_mindre([-4, 7, 8, -100], 0))

[65, 32, 11]\n[7, 8]

Skapa en funktion med namnet borjar_med_vokal(ord: list) som tar en lista med strängar som sitt argument.

Funktionen ska använda en list comprehension teknik för att skapa och returnera en ny lista som endast innehåller de ord från den ursprungliga listan som börjar med en vokal (a, e, i, o, u, y, å, ä, ö). Både små och stora bokstäver bör accepteras.

Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

ord_lista = [\"automobil\",\"moped\",\"Enhörning\",\"katt\",\"Hund\",\"ÄPPEL\",\"appelsin\"]\nfor vok in borjar_med_vokal(ord_lista):\n    print(vok)

automobil\nEnhörning\nÄPPEL\nappelsin

Alternativ exekvering med list comprehension

Ofta när vi har en villkorlig sats inkluderar vi också en else-gren. Eftersom vi kan använda villkor i list comprehensions är else-grenen också tillgänglig med list comprehension. Den allmänna syntaxen för villkoret som används med list comprehension ser ut så här:

<uttryck 1> if <villkor> else <uttryck 2>

Vi stötte på dessa enradiga villkor, eller ternära operatorer, redan i modul 7. Uttrycket ovan utvärderas till antingen uttryck 1 eller uttryck 2, beroende på om villkoret är sant eller falskt.

Som en uppfräschning av ämnet kan vi säga att om vi behöver skriva ut det större av två tal och vi bara vill använda en enda utskriftssats, kan vi få plats med allt på en enda rad:

tal1 = int(input(\"Ange tal 1:\"))\ntal2 = int(input(\"Ange tal 2:\"))\nprint (tal1 if tal1 > tal2 else tal2)

Genom att kombinera den ternära operatorssyntaxen med en list comprehension-sats får man följande allmänna struktur:

[<uttryck 1> if <villkor> else <uttryck 2> for <föremål> in <serie>]

Det här kan se lite förvirrande ut, eftersom den villkorliga strukturen nu kommer före den faktiska list comprehensionen. Det är bara så här syntaxen har definierats, åtminstone för tillfället. Om det också finns en else-gren kommer villkoret först. Om det bara finns ett if, kommer det sist. Du kan prova att byta ut dem och se vad som händer.

Att inkludera en else-operator innebär att vi återigen kommer att bearbeta varje objekt från den ursprungliga listan. Beroende på om villkoret är sant eller falskt utförs antingen uttryck 1 eller uttryck 2 på varje objekt i listan.

I följande exempel kontrolleras om föremåleni en lista är noll eller högre. Alla sådana föremål accepteras som de är, men alla negativa föremål negeras, så att tecknet ändras från negativt till positivt. Resultatet är en lista som innehåller de absoluta värdena för föremålen i den ursprungliga listan.

\ntalen = [1, -3, 45, -110, 2, 9, -11]\nabsolut_varde = [tal if tal >= 0 else -tal for tal in talen]\nprint(absolut_varde)\n

[1, 3, 45, 110, 2, 9, 11]

Vi upprepar vad som händer ovan: om villkoret nummer >= 0 är sant, genomgår föremålet uttrycket nummer, och resultatet är själva föremålet. Om villkoret är falskt genomgår föremålet uttrycket –nummer, så att det får ett positivt värde.

I följande exempel har vi funktionen strang_langder som tar en lista som sitt argument och returnerar en annan lista med längderna på alla strängar i den ursprungliga listan. Den här funktionen är dock okej med listföremål av alla typer. Om föremålet är en sträng beräknar den dess längd. Om objektet är något annat infogar den -1 i listan som den returnerar.

\ndef strang_langder(lista: list):\n    \"\"\" Funktionen returnerar längden på strängarna i en ny lista \"\"\"\n    return [len(foremal) if type(foremal) == str else -1 for foremal in lista]\n\nif __name__ == \"__main__\":\n    testlista = [\"hej\", 3, True, \"hejsan\", -123.344, \"hejsansvejsan\", 2, False]\n    langder = strang_langder(testlista)\n    print(langder)\n

[3, -1, -1, 6, -1, 13, -1, -1]

LotteriNummer matchade

Skapa en klass med namnet LotteriNummer som tar veckonumret (ett heltalsvärde) och en lista med sju heltal som sina konstruktorsargument. Listan ska innehålla de korrekta lotterinumren för den givna veckan.

Skapa också en metod med namnet antalet_traffar(nummer: list) som tar en lista med heltal som sitt argument. Metoden returnerar antalet korrekta inlägg i parameterlistan.

Metoden bör använda en list comprehension för att uppnå detta. Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

ratta = LotteriNummer(5, [1,2,3,4,5,6,7])\negen_rad = [1,4,7,11,13,19,24]\n\nprint(ratta.antalet_traffar(egen_rad))

3

LotteriNummer matchade på plats

Skapa en metod med namnet traffar_pa_plats(nummer) som tar en lista med sju heltal som sitt argument och returnerar en ny lista med sju heltal. Den nya listan innehåller endast de inlägg från den ursprungliga listan som matchar veckans korrekta nummer. Dessa måste ligga kvar på samma index som de gjorde i den ursprungliga listan. Resten av indexen bör fyllas med värdena -1.

Funktionen bör använda en list comprehension för att uppnå detta. Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

ratta = LotteriNummer(8, [1,2,3,10,20,30,33])\negen_rad = [1,4,7,10,11,20,30]\n\nprint(ratta.traffar_pa_plats(egen_rad))

[1, -1, -1, 10, -1, 20, 30]

","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-11/1-list-comprehension.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad list comprehension är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda list comprehensions för att skapa nya listor"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En av de situationer där programmering är som mest kraftfull är vid bearbetning av sekvenser av objekt och händelser. Datorer är bra på att upprepa saker. I de tidigare delarna av det här materialet har vi till exempel itererat strängar, listor och ordlistor på olika sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi har en lista med heltal och att vi skulle behöva samma lista med objekt i strängformat. Ett traditionellt sätt att utföra uppgiften skulle kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nstrangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n strangar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"list-comprehension","style":"position:relative;"},"children":[{"type":"text","value":"List comprehension"},{"type":"element","tagName":"a","properties":{"href":"#list-comprehension","ariaLabel":"list comprehension permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns också ett mer \"pythoniskt\" sätt att generera listor från befintliga listor. Dessa kallas list comprehensions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tanken är att på en enda rad få plats med både beskrivningen av vad som ska göras med varje objekt i listan och tilldelningen av resultatet till en ny lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan var operationen som utfördes på varje objekt i listan mycket enkel: varje heltal omvandlades till en sträng. Låt oss se hur detta skulle se ut implementerat med en list comprehension:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstrangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den andra raden ovan innehåller många av samma element som den mer traditionella iterativa metoden, men syntaxen är annorlunda. Ett sätt att generalisera en list comprehension skulle kunna vara"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ for in ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hakparenteserna runt list comprehensionsatsen signalerar till Python att resultatet ska vara en ny lista. En efter en bearbetas varje objekt i den ursprungliga listan och resultatet lagras i den nya listan, precis som i det iterativa tillvägagångssättet ovan. Som resultat har vi en ny lista med exakt lika många objekt som i originalet, och alla objekt har behandlats på ett identiskt sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(OBS: originalen till bilderna i denna del saknas tillfälligt, vilket är anledningen till att det finns en del finskt vokabulär i illustrationerna i denna del. Vi arbetar på att åtgärda detta)."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 301px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/bb2be65b36f7fdf4ea48dfde815e9fbf/fb933/11_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 131.7391304347826%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAACVUlEQVRIx42V247aQAyG5/0fibtWlZYLDqLAcgOoBDblHMIpgQRcfVYdDasku5as+ceneOwZx0kNPfJcRsOh9PsDSZJUvkPucDhIFcdxLNfrVRlcZ2vsVquVZFkmaZoKmJX9ZrOR8/mseL/fSxRFkue5ytCV+YAdhkY+JiOMoMvlogwhQ1fmA3a73a4QbLdbeT6fisnqdrspJisYQoYOwhYfI2K59Xqtqc7ncz0KvFgsdMUYjA0MRmbY7PElBnJnXyKD4/GoX2KlERAO6KmfnQadb4uvncwZgJIk0U5BrASxWuJEIJOZDT5GxHL+Brrf72pIrU6nkzrABERHRuhoAHvfVzP0BbY+Hg8NBlMrmBoRhIDIsSnzLQJ+Zo7V6XSk3W5Lo9GQVqslg8FAut2u6sp8ioB+UCu03bsy8pvi+70E9NPmaBzpbxjK21tTms2m/Pr5Q1rtrqS3u2TZvWieH6wyIMYU/Jameu9oir7n/1nRlC8D+qnb+6WzBBuPx8pJctWGoPM7XNtlv9MUfzabSa/XUwYj8ztc22Uju28clwA2HMDI0FmGn/1fXgpftnuGM2MJtoC2R4cNtpbty0vhOWFAoW2Y2lvm4aOHwXZtzAYffNFrhkyI6XSqNWIQLJdLvdBgXsdwONTVx+iwMYwvMXTa+POsah7aM/xqHoJrJ7Ydw/4rVp7aiR0EgRrwVTBC9jZwwWEYSvjxoU2wgYocW3zwZQ92tL+K/Q5adnX2sKv7xx6iSEfX6H0k/f7vb/2bXdkYKqZOHMufINAjTiaTImCVD/fwH00l3UL1OMHGAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bb2be65b36f7fdf4ea48dfde815e9fbf/a0b58/11_1_2.webp 230w","/static/bb2be65b36f7fdf4ea48dfde815e9fbf/c4c4c/11_1_2.webp 301w"],"sizes":"(max-width: 301px) 100vw, 301px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bb2be65b36f7fdf4ea48dfde815e9fbf/81c8e/11_1_2.png 230w","/static/bb2be65b36f7fdf4ea48dfde815e9fbf/fb933/11_1_2.png 301w"],"sizes":"(max-width: 301px) 100vw, 301px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/bb2be65b36f7fdf4ea48dfde815e9fbf/fb933/11_1_2.png","alt":"11 1 2","title":"11 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List comprehensions kan också hantera mycket mer komplicerade operationer. Vi kan utföra beräkningar, till exempel multiplicera de ursprungliga objekten med tio:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntalen_multiplicerade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen_multiplicerade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n[10, 20, 30, 40, 50, 60, 70, 80, 90]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Faktum är att uttrycket i list comprehension-satsen kan vara vilket Python-uttryck som helst. Du kan till och med anropa funktioner som du själv har definierat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakulteter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[120, 2, 24, 6, 1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med den mer välbekanta "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen skulle samma process kunna uttryckas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakulteter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List Comprehensions gör att vi kan uttrycka samma funktionalitet på ett mer konsekvent sätt, vanligtvis utan att förlora något av läsbarheten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också returnera en list comprehension-sats direkt från en funktion. Om vi behövde en funktion för att producera fakultettal för listor med tal, skulle vi kunna göra det på ett mycket kortfattat sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakulteter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kvadratrötter","tmcname":"osa11-01_kvadratrotter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kvadratrot(nummer: list)"}]},{"type":"text","value":" som tar en lista med heltal som sitt argument. Funktionen ska returnera en ny lista som innehåller kvadratrötterna av de ursprungliga heltalen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Modulen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" från Pythons standardbibliotek innehåller en lämplig funktion för att räkna ut kvadratrötter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör använda en list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rader "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kvadratrot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1.0\n1.4142135623730951\n1.7320508075688772\n2.0"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Stjärnrader","tmcname":"osa11-02_stjarnrader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"stjarnrader(nummer: list)"}]},{"type":"text","value":" som tar en lista med heltal som sitt argument. Funktionen ska returnera en ny lista som innehåller rader med stjärnor. Längden på varje rad ska motsvara heltalet på samma index i den ursprungliga listan. Funktionen bör använda en list comprehension för att uppnå detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rader "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stjarnrader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrader "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stjarnrader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*\n**\n***\n****\n\n****\n***\n**\n*\n**********\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bäst provresultat","tmcname":"osa11-03_bast_provresultat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns färdigt en klassdefinition för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Provresultat"}]},{"type":"text","value":", som har följane offentliga attribut:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vitsord1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vitsord2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vitsord3"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bast_resultat(resultat: list)"}]},{"type":"text","value":" som tar en lista med Provresultat-objekt som sitt argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en ny lista som endast innehåller det bästa resultatet från varje Provresultat-objekt. Funktionen bör använda en list comprehension för att uppnå detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paulus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprestationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"resultat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bast_resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[5, 4, 3]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Längder","tmcname":"osa11-04_langder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langder(listor: list)"}]},{"type":"text","value":" som tar en lista innehållande listor med heltal som argument. Funktionen ska returnera en ny lista som innehåller längderna på listorna i argumentlistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör använda en list comprehension för att uppnå detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"listat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"324"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"31"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"listat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[5, 4, 0]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-filtrera-foremal","style":"position:relative;"},"children":[{"type":"text","value":"Att filtrera föremål"},{"type":"element","tagName":"a","properties":{"href":"#att-filtrera-foremal","ariaLabel":"att filtrera foremal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan var alla våra listor lika långa före och efter en list comprehension-operation. I varje fall användes alla föremål i den ursprungliga listan som grund för den nya listan. Men ibland behöver vi bara några av de ursprungliga föremålen. Hur kan detta åstadkommas?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En list comprehension-sats kan också innehålla ett villkor, så att vi kan kontrollera objekten mot villkoret och bara välja ut dem som matchar. Den allmänna syntaxen är enligt följande:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ for in if ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Satsen ovan är i övrigt identisk med den allmänna form som introducerades i början av detta avsnitt, men nu finns det en if-sats i slutet. Endast de objekt från den ursprungliga listan för vilka det booleska uttrycket är sant används som grund för den nya listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet nedan väljer vi alla jämna objekt från den ursprungliga listan som bas för den nya listan. I själva verket bearbetas inte dessa objekt ytterligare på något sätt, utan de tilldelas den nya listan som de är:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\njamna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 4, 6, 4, 10, 12]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uttrycket i list comprehension-satsen ovan är bara ett enkelt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"foremal"}]},{"type":"text","value":", vilket innebär att inga operationer ska utföras på föremålen i listan. Uttrycket kan vara vilket Python-uttryck som helst, precis som i de tidigare exemplen. Följande list comprehension-sats tar till exempel alla jämna föremål i en lista, multiplicerar varje föremål med tio och lagrar resultatet i en ny lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\njamna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[20, 40, 60, 40, 100, 120]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du stöter på mer och mer komplicerade list comprehensions kan det vara bra att försöka läsa villkoret först. Föremålen bearbetas ändå bara om de klarar testet, så det är ofta vettigt att först ta reda på vilka objekt som klarar filtreringssteget. Ibland skulle uttrycket i en list comprehension-sats inte ens vara möjligt för alla föremål i den ursprungliga listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel är fakultettal bara definierat för icke-negativa heltal. Om vi inte kan vara säkra på att en lista bara innehåller värden på noll eller högre, måste innehållet filtreras innan det skickas vidare till den fakultetfunktion som vi skapade tidigare:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakulteter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[6, 24, 120, 1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som vi såg i vårt allra första exempel på list comprehension, där heltal omvandlades till strängar, behöver föremålen i den nya listan inte vara av samma typ som föremålen i den ursprungliga listan. Om vi fortsätter från fakultetexemplet ovan kan vi skapa en tupel från varje originella föremål och dess bearbetade motsvarighet och lagra dessa i en lista, vilket kombinerar allt vi har lärt oss hittills i en enda list comprehension-sats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakulteter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[(2, 2), (4, 24), (6, 720)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi plockar isär exemplet ovan har vi det booleska uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n > 0 and n % 2 == 0"}]},{"type":"text","value":". Detta innebär att endast föremål som är både positiva och delbara med två accepteras för vidare bearbetning från den ursprungliga listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa positiva, jämna tal bearbetas sedan i tur och ordning till formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(n, fakultet(n))"}]},{"type":"text","value":". Detta är en tupel, där det första objektet är själva talet och det andra objektet är resultatet som returneras av fakultetfunktionen."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ta bort mindre","tmcname":"osa11-05_ta_bort_mindre"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ta_bort_mindre(tal: list, grans: int)"}]},{"type":"text","value":" som tar en lista med heltal och ett gränsvärde (också i heltalsformat) som sina argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör använda en list comprehension för att producera en ny lista utan de värden som är mindre än gränsvärdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"65"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ta_bort_mindre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ta_bort_mindre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[65, 32, 11]\n[7, 8]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Börjar med vokal","tmcname":"osa11-06_borjar_med_vokal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"borjar_med_vokal(ord: list)"}]},{"type":"text","value":" som tar en lista med strängar som sitt argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska använda en list comprehension teknik för att skapa och returnera en ny lista som endast innehåller de ord från den ursprungliga listan som börjar med en vokal (a, e, i, o, u, y, å, ä, ö). Både små och stora bokstäver bör accepteras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ord_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"automobil\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"moped\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Enhörning\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hund\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ÄPPEL\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"appelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" vok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" borjar_med_vokal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"automobil\nEnhörning\nÄPPEL\nappelsin"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"alternativ-exekvering-med-list-comprehension","style":"position:relative;"},"children":[{"type":"text","value":"Alternativ exekvering med list comprehension"},{"type":"element","tagName":"a","properties":{"href":"#alternativ-exekvering-med-list-comprehension","ariaLabel":"alternativ exekvering med list comprehension permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ofta när vi har en villkorlig sats inkluderar vi också en else-gren. Eftersom vi kan använda villkor i list comprehensions är else-grenen också tillgänglig med list comprehension. Den allmänna syntaxen för villkoret som används med list comprehension ser ut så här:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":" if else "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi stötte på dessa enradiga villkor, eller ternära operatorer, redan i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-7/6-funktionaliteter-python","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 7"}]},{"type":"text","value":". Uttrycket ovan utvärderas till antingen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttryck 1"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttryck 2"}]},{"type":"text","value":", beroende på om villkoret är sant eller falskt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som en uppfräschning av ämnet kan vi säga att om vi behöver skriva ut det större av två tal och vi bara vill använda en enda utskriftssats, kan vi få plats med allt på en enda rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"tal1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange tal 1:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntal2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange tal 2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" tal2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" tal2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Genom att kombinera den ternära operatorssyntaxen med en list comprehension-sats får man följande allmänna struktur:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ if else for in ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kan se lite förvirrande ut, eftersom den villkorliga strukturen nu kommer före den faktiska list comprehensionen. Det är bara så här syntaxen har definierats, åtminstone för tillfället. Om det också finns en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":"-gren kommer villkoret först. Om det bara finns ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":", kommer det sist. Du kan prova att byta ut dem och se vad som händer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att inkludera en else-operator innebär att vi återigen kommer att bearbeta varje objekt från den ursprungliga listan. Beroende på om villkoret är sant eller falskt utförs antingen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttryck 1"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttryck 2"}]},{"type":"text","value":" på varje objekt i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel kontrolleras om föremåleni en lista är noll eller högre. Alla sådana föremål accepteras som de är, men alla negativa föremål negeras, så att tecknet ändras från negativt till positivt. Resultatet är en lista som innehåller de absoluta värdena för föremålen i den ursprungliga listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\ntalen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nabsolut_varde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"absolut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 45, 110, 2, 9, 11]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi upprepar vad som händer ovan: om villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer >= 0"}]},{"type":"text","value":" är sant, genomgår föremålet uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer"}]},{"type":"text","value":", och resultatet är själva föremålet. Om villkoret är falskt genomgår föremålet uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"–nummer"}]},{"type":"text","value":", så att det får ett positivt värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel har vi funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strang_langder"}]},{"type":"text","value":" som tar en lista som sitt argument och returnerar en annan lista med längderna på alla strängar i den ursprungliga listan. Den här funktionen är dock okej med listföremål av alla typer. Om föremålet är en sträng beräknar den dess längd. Om objektet är något annat infogar den -1 i listan som den returnerar."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"strang_langder"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen returnerar längden på strängarna i en ny lista \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n testlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"123.344"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hejsansvejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n langder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang_langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[3, -1, -1, 6, -1, 13, -1, -1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lotterinummer","tmcname":"osa11-07_lotterinummer"},"children":[{"type":"element","tagName":"h2","properties":{"id":"lotterinummer-matchade","style":"position:relative;"},"children":[{"type":"text","value":"LotteriNummer matchade"},{"type":"element","tagName":"a","properties":{"href":"#lotterinummer-matchade","ariaLabel":"lotterinummer matchade permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LotteriNummer"}]},{"type":"text","value":" som tar veckonumret (ett heltalsvärde) och en lista med sju heltal som sina konstruktorsargument. Listan ska innehålla de korrekta lotterinumren för den givna veckan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa också en metod med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"antalet_traffar(nummer: list)"}]},{"type":"text","value":" som tar en lista med heltal som sitt argument. Metoden returnerar antalet korrekta inlägg i parameterlistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden bör använda en list comprehension för att uppnå detta. Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ratta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteriNummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\negen_rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ratta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"antalet_traffar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"egen_rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lotterinummer-matchade-pa-plats","style":"position:relative;"},"children":[{"type":"text","value":"LotteriNummer matchade på plats"},{"type":"element","tagName":"a","properties":{"href":"#lotterinummer-matchade-pa-plats","ariaLabel":"lotterinummer matchade pa plats permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en metod med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"traffar_pa_plats(nummer)"}]},{"type":"text","value":" som tar en lista med sju heltal som sitt argument och returnerar en ny lista med sju heltal. Den nya listan innehåller endast de inlägg från den ursprungliga listan som matchar veckans korrekta nummer. Dessa måste ligga kvar på samma index som de gjorde i den ursprungliga listan. Resten av indexen bör fyllas med värdena "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör använda en list comprehension för att uppnå detta. Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ratta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteriNummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\negen_rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ratta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"traffar_pa_plats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"egen_rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, -1, -1, 10, -1, 20, 30]"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad list comprehension är
  • \n
  • Kommer du att kunna använda list comprehensions för att skapa nya listor
  • \n

En av de situationer där programmering är som mest kraftfull är vid bearbetning av sekvenser av objekt och händelser. Datorer är bra på att upprepa saker. I de tidigare delarna av det här materialet har vi till exempel itererat strängar, listor och ordlistor på olika sätt.

Låt oss anta att vi har en lista med heltal och att vi skulle behöva samma lista med objekt i strängformat. Ett traditionellt sätt att utföra uppgiften skulle kunna se ut så här:

talen = [1, 2, 3, 6, 5, 4, 7]\n\nstrangar = []\nfor tal in talen:\n    strangar.append(str(tal))

List comprehension

Det finns också ett mer \"pythoniskt\" sätt att generera listor från befintliga listor. Dessa kallas list comprehensions.

Tanken är att på en enda rad få plats med både beskrivningen av vad som ska göras med varje objekt i listan och tilldelningen av resultatet till en ny lista.

I exemplet ovan var operationen som utfördes på varje objekt i listan mycket enkel: varje heltal omvandlades till en sträng. Låt oss se hur detta skulle se ut implementerat med en list comprehension:

talen = [1, 2, 3, 6, 5, 4, 7]\nstrangar = [str(tal) for tal in talen]

Den andra raden ovan innehåller många av samma element som den mer traditionella iterativa metoden, men syntaxen är annorlunda. Ett sätt att generalisera en list comprehension skulle kunna vara

[<uttryck> for <föremål> in <serie>]

Hakparenteserna runt list comprehensionsatsen signalerar till Python att resultatet ska vara en ny lista. En efter en bearbetas varje objekt i den ursprungliga listan och resultatet lagras i den nya listan, precis som i det iterativa tillvägagångssättet ovan. Som resultat har vi en ny lista med exakt lika många objekt som i originalet, och alla objekt har behandlats på ett identiskt sätt.

(OBS: originalen till bilderna i denna del saknas tillfälligt, vilket är anledningen till att det finns en del finskt vokabulär i illustrationerna i denna del. Vi arbetar på att åtgärda detta).

\n \n \n \n \n \n \"11\n \n \n

List comprehensions kan också hantera mycket mer komplicerade operationer. Vi kan utföra beräkningar, till exempel multiplicera de ursprungliga objekten med tio:

talen = list(range(1,10))\nprint(talen)\n\ntalen_multiplicerade = [tal * 10 for tal in talen]\nprint(talen_multiplicerade)

[1, 2, 3, 4, 5, 6, 7, 8, 9]\n[10, 20, 30, 40, 50, 60, 70, 80, 90]

Faktum är att uttrycket i list comprehension-satsen kan vara vilket Python-uttryck som helst. Du kan till och med anropa funktioner som du själv har definierat:

def fakultet(n: int):\n    \"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [5, 2, 4, 3, 0]\n    fakulteter = [fakultet(tal) for tal in lista]\n    print(fakulteter)

[120, 2, 24, 6, 1]

Med den mer välbekanta for-loopen skulle samma process kunna uttryckas så här:

\ndef fakultet(n: int):\n    \"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [5, 2, 4, 3, 0]\n    fakulteter = []\n    for tal in lista:\n        fakulteter.append(fakultet(tal))\n    print(fakulteter)\n

List Comprehensions gör att vi kan uttrycka samma funktionalitet på ett mer konsekvent sätt, vanligtvis utan att förlora något av läsbarheten.

Vi kan också returnera en list comprehension-sats direkt från en funktion. Om vi behövde en funktion för att producera fakultettal för listor med tal, skulle vi kunna göra det på ett mycket kortfattat sätt:

def fakulteter(talen: list):\n    return [fakultet(tal) for tal in talen]

Skriv en funktion med namnet kvadratrot(nummer: list) som tar en lista med heltal som sitt argument. Funktionen ska returnera en ny lista som innehåller kvadratrötterna av de ursprungliga heltalen.

Modulen math från Pythons standardbibliotek innehåller en lämplig funktion för att räkna ut kvadratrötter.

Funktionen bör använda en list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet def.

Funktionen ska fungera enligt följande:

rader = kvadratrot([1,2,3,4])\nfor rad in rader:\n    print(rad)

1.0\n1.4142135623730951\n1.7320508075688772\n2.0

Skapa funktionen stjarnrader(nummer: list) som tar en lista med heltal som sitt argument. Funktionen ska returnera en ny lista som innehåller rader med stjärnor. Längden på varje rad ska motsvara heltalet på samma index i den ursprungliga listan. Funktionen bör använda en list comprehension för att uppnå detta.

Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

rader = stjarnrader([1,2,3,4])\nfor rad in rader:\n    print(rad)\n\nprint()\n\nrader = stjarnrader([4, 3, 2, 1, 10])\nfor rad in rader:\n    print(rad)
\n*\n**\n***\n****\n\n****\n***\n**\n*\n**********\n

I uppgiftsbotten finns färdigt en klassdefinition för Provresultat, som har följane offentliga attribut:

    \n
  • namn
  • \n
  • vitsord1
  • \n
  • vitsord2
  • \n
  • vitsord3
  • \n

Skapa en funktion med namnet bast_resultat(resultat: list) som tar en lista med Provresultat-objekt som sitt argument.

Funktionen ska returnera en ny lista som endast innehåller det bästa resultatet från varje Provresultat-objekt. Funktionen bör använda en list comprehension för att uppnå detta.

Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

resultat1 = Provresultat(\"Peter\",5,3,4)\nresultat2 = Provresultat(\"Pernilla\",3,4,1)\nresultat3 = Provresultat(\"Paulus\",2,1,3)\nprestationer = [resultat1, resultat2, resultat3]\nprint(bast_resultat(prestationer))

[5, 4, 3]

Skapa en funktion med namnet langder(listor: list) som tar en lista innehållande listor med heltal som argument. Funktionen ska returnera en ny lista som innehåller längderna på listorna i argumentlistan.

Funktionen bör använda en list comprehension för att uppnå detta.

Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

listat = [[1,2,3,4,5], [324, -1, 31, 7],[]]\nprint(langder(listat))

[5, 4, 0]

Att filtrera föremål

I exemplen ovan var alla våra listor lika långa före och efter en list comprehension-operation. I varje fall användes alla föremål i den ursprungliga listan som grund för den nya listan. Men ibland behöver vi bara några av de ursprungliga föremålen. Hur kan detta åstadkommas?

En list comprehension-sats kan också innehålla ett villkor, så att vi kan kontrollera objekten mot villkoret och bara välja ut dem som matchar. Den allmänna syntaxen är enligt följande:

[<uttryck> for <föremål> in <serie> if <boolskt uttryck>]

Satsen ovan är i övrigt identisk med den allmänna form som introducerades i början av detta avsnitt, men nu finns det en if-sats i slutet. Endast de objekt från den ursprungliga listan för vilka det booleska uttrycket är sant används som grund för den nya listan.

I exemplet nedan väljer vi alla jämna objekt från den ursprungliga listan som bas för den nya listan. I själva verket bearbetas inte dessa objekt ytterligare på något sätt, utan de tilldelas den nya listan som de är:

lista = [1, 1, 2, 3, 4, 6, 4, 5, 7, 10, 12, 3]\n\njamna = [foremal for foremal in lista if foremal % 2 == 0]\nprint(jamna)

[2, 4, 6, 4, 10, 12]

Uttrycket i list comprehension-satsen ovan är bara ett enkelt foremal, vilket innebär att inga operationer ska utföras på föremålen i listan. Uttrycket kan vara vilket Python-uttryck som helst, precis som i de tidigare exemplen. Följande list comprehension-sats tar till exempel alla jämna föremål i en lista, multiplicerar varje föremål med tio och lagrar resultatet i en ny lista:

lista = [1, 1, 2, 3, 4, 6, 4, 5, 7, 10, 12, 3]\n\njamna = [foremal * 10 for foremal in lista if foremal % 2 == 0]\nprint(jamna)

[20, 40, 60, 40, 100, 120]

När du stöter på mer och mer komplicerade list comprehensions kan det vara bra att försöka läsa villkoret först. Föremålen bearbetas ändå bara om de klarar testet, så det är ofta vettigt att först ta reda på vilka objekt som klarar filtreringssteget. Ibland skulle uttrycket i en list comprehension-sats inte ens vara möjligt för alla föremål i den ursprungliga listan.

Till exempel är fakultettal bara definierat för icke-negativa heltal. Om vi inte kan vara säkra på att en lista bara innehåller värden på noll eller högre, måste innehållet filtreras innan det skickas vidare till den fakultetfunktion som vi skapade tidigare:

def fakultet(n: int):\n    \"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [-2, 3, -1, 4, -10, 5, 1]\n    fakulteter = [fakultet(tal) for tal in lista if tal > 0]\n    print(fakulteter)

[6, 24, 120, 1]

Som vi såg i vårt allra första exempel på list comprehension, där heltal omvandlades till strängar, behöver föremålen i den nya listan inte vara av samma typ som föremålen i den ursprungliga listan. Om vi fortsätter från fakultetexemplet ovan kan vi skapa en tupel från varje originella föremål och dess bearbetade motsvarighet och lagra dessa i en lista, vilket kombinerar allt vi har lärt oss hittills i en enda list comprehension-sats:

\ndef fakultet(n: int):\n    \"\"\" Funktionen räknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [-2, 3, 2, 1, 4, -10, 5, 1, 6]\n    fakulteter = [(tal, fakultet(tal)) for tal in lista if tal > 0 and tal % 2 == 0]\n    print(fakulteter)\n

[(2, 2), (4, 24), (6, 720)]

Om vi plockar isär exemplet ovan har vi det booleska uttrycket n > 0 and n % 2 == 0. Detta innebär att endast föremål som är både positiva och delbara med två accepteras för vidare bearbetning från den ursprungliga listan.

Dessa positiva, jämna tal bearbetas sedan i tur och ordning till formatet (n, fakultet(n)). Detta är en tupel, där det första objektet är själva talet och det andra objektet är resultatet som returneras av fakultetfunktionen.

Skapa en funktion med namnet ta_bort_mindre(tal: list, grans: int) som tar en lista med heltal och ett gränsvärde (också i heltalsformat) som sina argument.

Funktionen bör använda en list comprehension för att producera en ny lista utan de värden som är mindre än gränsvärdet.

Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

talen = [1,65, 32, -6, 9, 11]\nprint(ta_bort_mindre(talen, 10))\n\nprint(ta_bort_mindre([-4, 7, 8, -100], 0))

[65, 32, 11]\n[7, 8]

Skapa en funktion med namnet borjar_med_vokal(ord: list) som tar en lista med strängar som sitt argument.

Funktionen ska använda en list comprehension teknik för att skapa och returnera en ny lista som endast innehåller de ord från den ursprungliga listan som börjar med en vokal (a, e, i, o, u, y, å, ä, ö). Både små och stora bokstäver bör accepteras.

Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

ord_lista = [\"automobil\",\"moped\",\"Enhörning\",\"katt\",\"Hund\",\"ÄPPEL\",\"appelsin\"]\nfor vok in borjar_med_vokal(ord_lista):\n    print(vok)

automobil\nEnhörning\nÄPPEL\nappelsin

Alternativ exekvering med list comprehension

Ofta när vi har en villkorlig sats inkluderar vi också en else-gren. Eftersom vi kan använda villkor i list comprehensions är else-grenen också tillgänglig med list comprehension. Den allmänna syntaxen för villkoret som används med list comprehension ser ut så här:

<uttryck 1> if <villkor> else <uttryck 2>

Vi stötte på dessa enradiga villkor, eller ternära operatorer, redan i modul 7. Uttrycket ovan utvärderas till antingen uttryck 1 eller uttryck 2, beroende på om villkoret är sant eller falskt.

Som en uppfräschning av ämnet kan vi säga att om vi behöver skriva ut det större av två tal och vi bara vill använda en enda utskriftssats, kan vi få plats med allt på en enda rad:

tal1 = int(input(\"Ange tal 1:\"))\ntal2 = int(input(\"Ange tal 2:\"))\nprint (tal1 if tal1 > tal2 else tal2)

Genom att kombinera den ternära operatorssyntaxen med en list comprehension-sats får man följande allmänna struktur:

[<uttryck 1> if <villkor> else <uttryck 2> for <föremål> in <serie>]

Det här kan se lite förvirrande ut, eftersom den villkorliga strukturen nu kommer före den faktiska list comprehensionen. Det är bara så här syntaxen har definierats, åtminstone för tillfället. Om det också finns en else-gren kommer villkoret först. Om det bara finns ett if, kommer det sist. Du kan prova att byta ut dem och se vad som händer.

Att inkludera en else-operator innebär att vi återigen kommer att bearbeta varje objekt från den ursprungliga listan. Beroende på om villkoret är sant eller falskt utförs antingen uttryck 1 eller uttryck 2 på varje objekt i listan.

I följande exempel kontrolleras om föremåleni en lista är noll eller högre. Alla sådana föremål accepteras som de är, men alla negativa föremål negeras, så att tecknet ändras från negativt till positivt. Resultatet är en lista som innehåller de absoluta värdena för föremålen i den ursprungliga listan.

\ntalen = [1, -3, 45, -110, 2, 9, -11]\nabsolut_varde = [tal if tal >= 0 else -tal for tal in talen]\nprint(absolut_varde)\n

[1, 3, 45, 110, 2, 9, 11]

Vi upprepar vad som händer ovan: om villkoret nummer >= 0 är sant, genomgår föremålet uttrycket nummer, och resultatet är själva föremålet. Om villkoret är falskt genomgår föremålet uttrycket –nummer, så att det får ett positivt värde.

I följande exempel har vi funktionen strang_langder som tar en lista som sitt argument och returnerar en annan lista med längderna på alla strängar i den ursprungliga listan. Den här funktionen är dock okej med listföremål av alla typer. Om föremålet är en sträng beräknar den dess längd. Om objektet är något annat infogar den -1 i listan som den returnerar.

\ndef strang_langder(lista: list):\n    \"\"\" Funktionen returnerar längden på strängarna i en ny lista \"\"\"\n    return [len(foremal) if type(foremal) == str else -1 for foremal in lista]\n\nif __name__ == \"__main__\":\n    testlista = [\"hej\", 3, True, \"hejsan\", -123.344, \"hejsansvejsan\", 2, False]\n    langder = strang_langder(testlista)\n    print(langder)\n

[3, -1, -1, 6, -1, 13, -1, -1]

LotteriNummer matchade

Skapa en klass med namnet LotteriNummer som tar veckonumret (ett heltalsvärde) och en lista med sju heltal som sina konstruktorsargument. Listan ska innehålla de korrekta lotterinumren för den givna veckan.

Skapa också en metod med namnet antalet_traffar(nummer: list) som tar en lista med heltal som sitt argument. Metoden returnerar antalet korrekta inlägg i parameterlistan.

Metoden bör använda en list comprehension för att uppnå detta. Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

ratta = LotteriNummer(5, [1,2,3,4,5,6,7])\negen_rad = [1,4,7,11,13,19,24]\n\nprint(ratta.antalet_traffar(egen_rad))

3

LotteriNummer matchade på plats

Skapa en metod med namnet traffar_pa_plats(nummer) som tar en lista med sju heltal som sitt argument och returnerar en ny lista med sju heltal. Den nya listan innehåller endast de inlägg från den ursprungliga listan som matchar veckans korrekta nummer. Dessa måste ligga kvar på samma index som de gjorde i den ursprungliga listan. Resten av indexen bör fyllas med värdena -1.

Funktionen bör använda en list comprehension för att uppnå detta. Funktionen får maximalt omfatta två rader kod, inklusive den rubrikrad som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

ratta = LotteriNummer(8, [1,2,3,10,20,30,33])\negen_rad = [1,4,7,10,11,20,30]\n\nprint(ratta.traffar_pa_plats(egen_rad))

[1, -1, -1, 10, -1, 20, 30]

","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-11/1-list-comprehension.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-11/2-mer-om-comprehensions/page-data.json b/page-data/osa-11/2-mer-om-comprehensions/page-data.json index 54dce65f7..5d144585d 100644 --- a/page-data/osa-11/2-mer-om-comprehensions/page-data.json +++ b/page-data/osa-11/2-mer-om-comprehensions/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-11/2-mer-om-comprehensions", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda comprehensions med strängar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du använder comprehensions med dina egna klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa ordlistscomprehensions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listor är kanske det vanligaste målet för comprehensions, men comprehensions fungerar på alla serier av föremål, inklusive strängar. Liksom listexemplen i föregående avsnitt, ifall en list comprehension utförs på en sträng, plockas föremålen (dvs. tecknen) i strängen en efter en, bearbetas enligt det givna uttrycket och lagras i en lista."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"text","value":"\n\nstora_bokstaver "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tecken"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stora_bokstaver"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['P', 'E', 'T', 'E', 'R', ' ', 'P', 'Y', 'T', 'H', 'O', 'N']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet är en lista, vilket dikteras av parentesnotationen runt comprehension-satsen. Om vi ville ha en sträng istället skulle vi kunna använda strängmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" för att tolka listan till en sträng. Kom ihåg att metoden anropas på den sträng som vi vill använda som \"lim\" mellan tecknen. Låt oss ta en titt på några exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"text","value":"\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" och \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['P', 'e', 't', 'e', 'r']\nPeter\nP e t e r\nP,e,t,e,r\nP och e och t och e och r"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List comprehensions och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":"-metoden gör det enkelt att skapa nya strängar baserade på andra strängar. Vi kan t.ex. skapa en sträng som bara innehåller vokalerna från en annan sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nteststrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Halloj allihopa, det här är ett test\""}]},{"type":"text","value":"\n\nvokaler "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" teststrang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiouyåäö\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nnystrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vokaler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nystrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"aoaioaeääee"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan står list comprehension och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":"-metoden på separata rader, men de kan kombineras till ett enda uttryck:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nteststrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Halloj allihopa, det här är ett test\""}]},{"type":"text","value":"\n\nvokalstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" teststrang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiouyåäö\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vokalstrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Många Python-programmerare står trogna vid dessa oneliners, så det är väl värt besväret att lära sig läsa dem. Vi kan till och med lägga till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":"-metoden i mixen, så att vi kan bearbeta hela meningar effektivt med ett enda uttalande. I exemplet nedan tas det första tecknet från varje ord i en mening bort:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sju sjösjuka sjömän på skeppet Shang Hai.\""}]},{"type":"text","value":"\n\nmening_utan_initialer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening_utan_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ju jösjuka jömän å keppet hang ai"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss gå igenom detta steg för steg:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord[1:]"}]},{"type":"text","value":" extraherar en delsträng från det andra tecknet (vid index 1) och framåt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mening.split()"}]},{"type":"text","value":" delar upp meningen i avsnitt vid det angivna tecknet. I det här fallet ges inget argument till metoden, så meningen delas upp vid mellanslagstecken som standard"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\" \".join()"}]},{"type":"text","value":" kombinerar föremålen i listan till en ny sträng med ett mellanslag mellan föremålen"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En mer traditionell iterativ metod skulle kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sju sjösjuka sjömän på skeppet Shang Hai.\""}]},{"type":"text","value":"\n\nordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\norden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ord_utan_initialer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_utan_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmening_utan_initialer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening_utan_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filtrera förbjudna","tmcname":"osa11-08_filtrera_forbjudna"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filtrera_forbjudna(strang: str, forbjuden: str)"}]},{"type":"text","value":" som tar två strängar som argument. Funktionen ska returnera en ny version av den första strängen. Den ska inte innehålla tecken från den andra strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst tre rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"mening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det! var, en gång: en python!??!?!\""}]},{"type":"text","value":"\nfiltrerad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filtrera_forbjudna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!?:,.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filtrerad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det var en gång en python"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"egna-klasser-och-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Egna klasser och comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#egna-klasser-och-comprehensions","ariaLabel":"egna klasser och comprehensions permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Comprehensions kan vara ett användbart verktyg för att bearbeta eller formulera instanser av dina egna klasser, vilket vi kommer att se i följande exempel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss först ta en titt på klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Land"}]},{"type":"text","value":" som är en enkel modell för ett enda land, med attribut för namn och befolkning. I huvudfunktionen nedan skapar vi först några Land-objekt och använder sedan en list comprehension för att bara välja dem vars befolkning är större än fem miljoner."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Land"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Denna klass modellerar ett enkelt land med befolkning \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" befolkningsmangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"befolkningsmangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" befolkningsmangd\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n finland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n malta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sverige "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n island "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Island\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"350000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lander "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"finland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" malta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sverige"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" island"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n storre_land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lander "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"befolkningsmangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" storre_land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finland\nSverige"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I list comprehension ovan valde vi bara namnattributet från Land-objekten, så innehållet i listan kunde skrivas ut direkt. Vi skulle också kunna skapa en ny lista med länderna och komma åt namnattributet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen. Detta skulle vara användbart om samma lista med länder skulle användas senare i programmet, eller om vi behövde befolkningsattributet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen också:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n finland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n malta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sverige "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n island "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Island\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"350000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lander "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"finland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" malta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sverige"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" island"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n storre_land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lander "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"befolkningsmangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" storre_land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I nästa exempel har vi en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fotlopp"}]},{"type":"text","value":" som modellerar ett enskilt lopp med attribut för loppets längd och namn. Vi kommer att använda list comprehension för att skapa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fotlopp"}]},{"type":"text","value":"-objekt baserat på en lista med tävlingslängder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" har ett standardvärde i konstruktorn för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fotlopp"}]},{"type":"text","value":"-klassen, vilket är varför vi inte behöver skicka namnet som ett argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Fotlopp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar ett fotloppsevenemang med längden n meter \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"inget namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"stracka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stracka\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" m. ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n langder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42195"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n strackor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"Fotlopp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" langd "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut alla"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strackor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ta en från listan och ge den ett namn"}]},{"type":"text","value":"\n maraton "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strackor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# sista föremålet i listan"}]},{"type":"text","value":"\n maraton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maraton\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut alla igen, inkluderandes det nya namnet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strackor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[100 m. (inget namn), 200 m. (inget namn), 1500 m. (inget namn), 3000 m. (inget namn), 42195 m. (inget namn)]\n[100 m. (inget namn), 200 m. (inget namn), 1500 m. (inget namn), 3000 m. (inget namn), 42195 m. (Maraton)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss nu ta reda på vad som gör en serie objekt \"begripliga\" (“comprehendible”). I föregående del lärde vi oss hur vi kan göra våra egna klasser itererbara. Det är exakt samma funktion som också möjliggör list comprehension. Om din egen klass är itererbar kan den användas som grund för en list comprehension. Följande klassdefinitioner är kopierade direkt från "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-10/3-objektorienterade-programmeringstekniker","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 10"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sidor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" forfattare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sidor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sidor\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Iteratorns initialiseringsmetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Här bör iterationsvariabeln(variablerna) initialiseras"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar en referens till själva objektet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# eftersom iteratorn är implementerad inom samma klassdefinition"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna metod returnerar nästa föremål inom objektet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Om alla föremål har genomgåtts åstadkomms StopIteration"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__next__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ta det aktuella föremålet från listan i objektet"}]},{"type":"text","value":"\n bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Öka räknaren med ett"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ...och returnera föremålet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bok\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Inga fler böcker"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" StopIteration\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Livet av en Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"123"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den gamle och Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ernest Hemingjava\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"204"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C-värdheter på nätet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Karl Kodare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n hylla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bokhylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skapa en lista innehållandes namnet på alla böcker"}]},{"type":"text","value":"\n bockernas_namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bockernas_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Affärslistans produkter","tmcname":"osa11-09_affarslistans_produkter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I modul 10 skapade du en "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-10/3-objektorienterade-programmeringstekniker","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"itererbar affärslista"}]},{"type":"text","value":". Objekt av en itererbar klass kan användas med list comprehensions. Uppgiftsmallen innehåller en avskalad version av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" med knappt tillräckligt funktion för denna övning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"affarslistans_produkter(affarslista, antal: int)"}]},{"type":"text","value":" som tar som argument ett affarslista-objekt och ett heltalsvärde. Funktionen returnerar en lista av produktnamn. Listan borde inkludera endast produkter som har åtminstone ett lika stort antal som parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"antal"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":". Klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" bör "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inte"}]},{"type":"text","value":" modifieras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"alkoholfri öl\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ananas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Affärslistan har minst 8 av följande:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" affarslistans_produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Affärslistan har minst 8 av följande:\nbananer\nalkoholfri öl"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Billigare prisskillnad","tmcname":"osa11-10_billigare_prisskillnad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna övning innehåller en aning modifierad version av klassen "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/1-objekt-och-referenser","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"text","value":" från modul 9."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"billigare(bostader: list, jamforelse: Bostad)"}]},{"type":"text","value":" som tar en lista med bostäder och ett enda Bostad-objekt som sina argument. Funktionen ska returnera en lista som endast innehåller de bostäder i den ursprungliga listan som är billigare än jämförelsebostaden, tillsammans med prisskillnaden. Föremålen i den returnerade listan bör vara tupler, där det första föremålet är själva bostaden och den andra är prisskillnaden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden för Bostad-klassen ingår i övningsmallen och ska inte ändras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på funktionen i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eira etta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Berghäll tvåa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jakobacka trea\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"215"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Suomussalmi egnahemshus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"105"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1700"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kerava 4r och kök\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na6 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Haikon kartano\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbostader "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a6"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"billigare alternativ än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beskrivning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":":\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" billigare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bostader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beskrivning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" prisskillnad "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"billigare alternativ än Jakobacka trea:\nEira etta prisskillnad 107000 euro\nBerghäll tvåa prisskillnad 35400 euro\nSuomussalmi egnahemshus prisskillnad 87500 euro\nKerava 4r och kök prisskillnad 16500 euro"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"comprehensions-och-ordlistor","style":"position:relative;"},"children":[{"type":"text","value":"Comprehensions och ordlistor"},{"type":"element","tagName":"a","properties":{"href":"#comprehensions-och-ordlistor","ariaLabel":"comprehensions och ordlistor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns inget i sig \"list-aktigt\" med comprehensions. Resultatet är en lista eftersom comprehension-satsen är inkapslad i hakparenteser, som indikerar en Python-lista. Förståelser fungerar lika bra med Python-ordlistor om du använder rundparenteser istället. Kom dock ihåg att ordlistor kräver nyckel-värde-par. Båda måste anges när en ordlista skapas, även när det gäller comprehensions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Grunden för en comprehension kan vara vilken itererbar serie som helst, vare sig det är en lista, en sträng, en tupel, en ordlista, någon av dina egna itererbara klasser och så vidare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel använder vi en sträng som bas för en ordlista. Ordlistan innehåller alla unika tecken i strängen, tillsammans med antalet gånger de förekommer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla\""}]},{"type":"text","value":"\n\ntecken_antal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bokstav "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bokstav"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bokstav "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tecken_antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'H': 1, 'e': 1, 'j': 1, ' ': 1, 'a': 2, 'l': 2}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Principen för comprehension-satsen är exakt densamma som för listor, men i stället för ett enda värde består uttrycket nu av en nyckel och ett värde. Den allmänna syntaxen ser ut så här:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{ : för i }"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som avslutning på det här avsnittet tittar vi på faktorialtal igen. Den här gången lagrar vi resultaten i en ordlista. Själva talet är nyckeln, medan värdet är resultatet av faktorn från vår funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen beräknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakultett "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Strängarnas längder","tmcname":"osa11-11_strangarnas_langder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langder(strangar: list)"}]},{"type":"text","value":" som tar en lista med strängar som sitt argument. Funktionen ska returnera en ordlista med strängarna i listan som nycklar och deras längder som värden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör implementeras med en ordlistscomprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som inleds med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"det\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"var\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"en\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"gång\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsanojen_langder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sanojen_langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'det': 3, 'var': 3, 'en': 2, 'gång': 4, 'python': 6}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vanligaste orden","tmcname":"osa11-12_vanligaste_orden"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vanligaste_orden(filnamn: str, nedre_grans: int)"}]},{"type":"text","value":" som tar ett filnamn och ett heltalsvärde som en nedre gräns som sina argument. Funktionen ska returnera en ordlista som innehåller antalet förekomster av de ord som förekommer minst det antal gånger som anges i parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nedre_grans"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anta t.ex. att funktionen används för att bearbeta en fil med namnet comprehensions.txt med följande innehåll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"txt"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-txt"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-txt"]},"children":[{"type":"text","value":"List comprehension is an elegant way to define and create lists based on existing lists.\nList comprehension is generally more compact and faster than normal functions and loops for creating list.\nHowever, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.\nRemember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När funktionen anropas med argumenten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vanligaste_orden(\"comprehensions.txt\", 3)"}]},{"type":"text","value":" ska den returnera"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'comprehension': 4, 'is': 3, 'and': 3, 'for': 3, 'list': 4, 'in': 3}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att bokstävernas versaler påverkar resultatet och att alla böjda former är unika ord i den här övningen. Det vill säga att orden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"List"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lists"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" är separata ord här, och endast "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" har tillräckligt många förekomster för att komma med i den returnerade listan. Alla skiljetecken ska tas bort innan du räknar upp förekomsterna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är upp till dig att bestämma hur detta ska implementeras. Det enklaste sättet skulle troligen vara att använda list- och ordlistsscomprehension."}]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att kunna använda comprehensions med strängar
  • \n
  • Vet du hur du använder comprehensions med dina egna klasser
  • \n
  • Kommer du att kunna skapa ordlistscomprehensions
  • \n

Listor är kanske det vanligaste målet för comprehensions, men comprehensions fungerar på alla serier av föremål, inklusive strängar. Liksom listexemplen i föregående avsnitt, ifall en list comprehension utförs på en sträng, plockas föremålen (dvs. tecknen) i strängen en efter en, bearbetas enligt det givna uttrycket och lagras i en lista.

\nnamn = \"Peter Python\"\n\nstora_bokstaver = [tecken.upper() for tecken in namn]\nprint(stora_bokstaver)\n

['P', 'E', 'T', 'E', 'R', ' ', 'P', 'Y', 'T', 'H', 'O', 'N']

Resultatet är en lista, vilket dikteras av parentesnotationen runt comprehension-satsen. Om vi ville ha en sträng istället skulle vi kunna använda strängmetoden join för att tolka listan till en sträng. Kom ihåg att metoden anropas på den sträng som vi vill använda som \"lim\" mellan tecknen. Låt oss ta en titt på några exempel:

\nnamn = \"Peter\"\nlista = list(namn)\nprint(lista)\n\nprint(\"\".join(lista))\nprint(\" \".join(lista))\nprint(\",\".join(lista))\nprint(\" och \".join(lista))\n

['P', 'e', 't', 'e', 'r']\nPeter\nP e t e r\nP,e,t,e,r\nP och e och t och e och r

List comprehensions och join-metoden gör det enkelt att skapa nya strängar baserade på andra strängar. Vi kan t.ex. skapa en sträng som bara innehåller vokalerna från en annan sträng:

\nteststrang = \"Halloj allihopa, det här är ett test\"\n\nvokaler = [tecken for tecken in teststrang if tecken in \"aeiouyåäö\"]\nnystrang = \"\".join(vokaler)\n\nprint(nystrang)\n

aoaioaeääee

I exemplet ovan står list comprehension och join-metoden på separata rader, men de kan kombineras till ett enda uttryck:

\nteststrang = \"Halloj allihopa, det här är ett test\"\n\nvokalstrang = \"\".join([tecken for tecken in teststrang if tecken in \"aeiouyåäö\"])\n\nprint(vokalstrang)\n

Många Python-programmerare står trogna vid dessa oneliners, så det är väl värt besväret att lära sig läsa dem. Vi kan till och med lägga till split-metoden i mixen, så att vi kan bearbeta hela meningar effektivt med ett enda uttalande. I exemplet nedan tas det första tecknet från varje ord i en mening bort:

\nmening = \"Sju sjösjuka sjömän på skeppet Shang Hai.\"\n\nmening_utan_initialer = \" \".join([ord[1:] for ord in mening.split()])\nprint(mening_utan_initialer)\n

ju jösjuka jömän å keppet hang ai

Låt oss gå igenom detta steg för steg:

    \n
  • ord[1:] extraherar en delsträng från det andra tecknet (vid index 1) och framåt
  • \n
  • mening.split() delar upp meningen i avsnitt vid det angivna tecknet. I det här fallet ges inget argument till metoden, så meningen delas upp vid mellanslagstecken som standard
  • \n
  • \" \".join() kombinerar föremålen i listan till en ny sträng med ett mellanslag mellan föremålen
  • \n

En mer traditionell iterativ metod skulle kunna se ut så här:

\nmening = \"Sju sjösjuka sjömän på skeppet Shang Hai.\"\n\nordlista = []\norden = mening.split()\nfor ord in orden:\n    ord_utan_initialer = ord[1:]\n    ordlista.append(ord_utan_initialer)\n\nmening_utan_initialer = \" \".join(ordlista)\n\n\nprint(mening_utan_initialer)\n

Skapa funktionen filtrera_forbjudna(strang: str, forbjuden: str) som tar två strängar som argument. Funktionen ska returnera en ny version av den första strängen. Den ska inte innehålla tecken från den andra strängen.

Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst tre rader lång, inklusive rubrikraden som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

mening = \"Det! var, en gång: en python!??!?!\"\nfiltrerad = filtrera_forbjudna(mening, \"!?:,.\")\nprint(filtrerad)

Det var en gång en python

Egna klasser och comprehensions

Comprehensions kan vara ett användbart verktyg för att bearbeta eller formulera instanser av dina egna klasser, vilket vi kommer att se i följande exempel.

Låt oss först ta en titt på klassen Land som är en enkel modell för ett enda land, med attribut för namn och befolkning. I huvudfunktionen nedan skapar vi först några Land-objekt och använder sedan en list comprehension för att bara välja dem vars befolkning är större än fem miljoner.

\nclass Land:\n    \"\"\" Denna klass modellerar ett enkelt land med befolkning \"\"\"\n    def __init__(self, namn: str, befolkningsmangd: int):\n        self.namn = namn\n        self.befolkningsmangd = befolkningsmangd\n\nif __name__ == \"__main__\":\n    finland = Land(\"Finland\", 6000000)\n    malta = Land(\"Malta\", 500000)\n    sverige = Land(\"Sverige\", 10000000)\n    island = Land(\"Island\", 350000)\n\n    lander = [finland, malta, sverige, island]\n\n    storre_land = [land.namn for land in lander if land.befolkningsmangd > 5000000]\n    for land in storre_land:\n        print(land)\n\n

Finland\nSverige

I list comprehension ovan valde vi bara namnattributet från Land-objekten, så innehållet i listan kunde skrivas ut direkt. Vi skulle också kunna skapa en ny lista med länderna och komma åt namnattributet i for-loopen. Detta skulle vara användbart om samma lista med länder skulle användas senare i programmet, eller om vi behövde befolkningsattributet i for-loopen också:

\nif __name__ == \"__main__\":\n    finland = Land(\"Finland\", 6000000)\n    malta = Land(\"Malta\", 500000)\n    sverige = Land(\"Sverige\", 10000000)\n    island = Land(\"Island\", 350000)\n\n    lander = [finland, malta, sverige, island]\n\n    storre_land = [land for land in lander if land.befolkningsmangd > 5000000]\n    for land in storre_land:\n        print(land.namn)

I nästa exempel har vi en klass som heter Fotlopp som modellerar ett enskilt lopp med attribut för loppets längd och namn. Vi kommer att använda list comprehension för att skapa Fotlopp-objekt baserat på en lista med tävlingslängder.

Parametern namn har ett standardvärde i konstruktorn för Fotlopp-klassen, vilket är varför vi inte behöver skicka namnet som ett argument.

\nclass Fotlopp:\n    \"\"\" Klassen modellerar ett fotloppsevenemang med längden n meter \"\"\"\n    def __init__(self, stracka:int, namn:str = \"inget namn\"):\n        self.stracka = stracka\n        self.namn = namn\n\n    def __repr__(self):\n        return f\"{self.stracka} m. ({self.namn})\"\n\nif __name__ == \"__main__\":\n    langder = [100, 200, 1500, 3000, 42195]\n    strackor = [Fotlopp(langd) for langd in langder]\n\n    # Skriv ut alla\n    print(strackor)\n\n    # Ta en från listan och ge den ett namn\n    maraton = strackor[-1] # sista föremålet i listan\n    maraton.namn = \"Maraton\"\n\n    # Skriv ut alla igen, inkluderandes det nya namnet\n    print(strackor)\n

[100 m. (inget namn), 200 m. (inget namn), 1500 m. (inget namn), 3000 m. (inget namn), 42195 m. (inget namn)]\n[100 m. (inget namn), 200 m. (inget namn), 1500 m. (inget namn), 3000 m. (inget namn), 42195 m. (Maraton)]

Låt oss nu ta reda på vad som gör en serie objekt \"begripliga\" (“comprehendible”). I föregående del lärde vi oss hur vi kan göra våra egna klasser itererbara. Det är exakt samma funktion som också möjliggör list comprehension. Om din egen klass är itererbar kan den användas som grund för en list comprehension. Följande klassdefinitioner är kopierade direkt från modul 10:

\nclass Bok:\n    def __init__(self, namn: str, forfattare: str, sidor: int):\n        self.namn = namn\n        self.forfattare = forfattare\n        self.sidor = sidor\n\nclass Bokhylla:\n    def __init__(self):\n        self._bocker = []\n\n    def tillsatt_bok(self, bok: Bok):\n        self._bocker.append(bok)\n\n    # Iteratorns initialiseringsmetod\n    # Här bör iterationsvariabeln(variablerna) initialiseras\n    def __iter__(self):\n        self.n = 0\n        # Metoden returnerar en referens till själva objektet\n        # eftersom iteratorn är implementerad inom samma klassdefinition\n        return self\n\n    # Denna metod returnerar nästa föremål inom objektet\n    # Om alla föremål har genomgåtts åstadkomms StopIteration\n    def __next__(self):\n        if self.n < len(self._bocker):\n            # Ta det aktuella föremålet från listan i objektet\n            bok = self._bocker[self.n]\n            # Öka räknaren med ett\n            self.n += 1\n            # ...och returnera föremålet\n            return bok\n        else:\n            # Inga fler böcker\n            raise StopIteration\n\n# Testar\nif __name__ == \"__main__\":\n    b1 = Bok(\"Livet av en Python\", \"Peter Python\", 123)\n    b2 = Bok(\"Den gamle och Java\", \"Ernest Hemingjava\", 204)\n    b3 = Bok(\"C-värdheter på nätet\", \"Karl Kodare\", 997)\n\n    hylla = Bokhylla()\n    hylla.tillsatt_bok(b1)\n    hylla.tillsatt_bok(b2)\n    hylla.tillsatt_bok(b3)\n\n    # Skapa en lista innehållandes namnet på alla böcker\n    bockernas_namn = [bok.namn for bok in hylla]\n    print(bockernas_namn)\n

I modul 10 skapade du en itererbar affärslista. Objekt av en itererbar klass kan användas med list comprehensions. Uppgiftsmallen innehåller en avskalad version av Affarslista med knappt tillräckligt funktion för denna övning.

Skapa en funktion med namnet affarslistans_produkter(affarslista, antal: int) som tar som argument ett affarslista-objekt och ett heltalsvärde. Funktionen returnerar en lista av produktnamn. Listan borde inkludera endast produkter som har åtminstone ett lika stort antal som parametern antal.

Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet def. Klassdefinitionen för Affarslista bör inte modifieras.

Funktionen ska fungera enligt följande:

lista = Affarslista()\nlista.tillsatt(\"bananer\", 10)\nlista.tillsatt(\"äppel\", 5)\nlista.tillsatt(\"alkoholfri öl\", 24)\nlista.tillsatt(\"ananas\", 1)\n\nprint(\"Affärslistan har minst 8 av följande:\")\nfor produkt in affarslistans_produkter(lista, 8):\n    print(produkt)

Affärslistan har minst 8 av följande:\nbananer\nalkoholfri öl

Denna övning innehåller en aning modifierad version av klassen Bostad från modul 9.

Skapa en funktion med namnet billigare(bostader: list, jamforelse: Bostad) som tar en lista med bostäder och ett enda Bostad-objekt som sina argument. Funktionen ska returnera en lista som endast innehåller de bostäder i den ursprungliga listan som är billigare än jämförelsebostaden, tillsammans med prisskillnaden. Föremålen i den returnerade listan bör vara tupler, där det första föremålet är själva bostaden och den andra är prisskillnaden.

Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet def.

Koden för Bostad-klassen ingår i övningsmallen och ska inte ändras.

Ett exempel på funktionen i användning:

a1 = Bostad(1, 16, 5500, \"Eira etta\")\na2 = Bostad(2, 38, 4200, \"Berghäll tvåa\")\na3 = Bostad(3, 78, 2500, \"Jakobacka trea\")\na4 = Bostad(6, 215, 500, \"Suomussalmi egnahemshus\")\na5 = Bostad(4, 105, 1700, \"Kerava 4r och kök\")\na6 = Bostad(25, 1200, 2500, \"Haikon kartano\")\n\nbostader = [a1, a2, a3, a4, a5, a6]\n\nprint(f\"billigare alternativ än {a3.beskrivning}:\")\nfor foremal in billigare(bostader, a3):\n    print(f\"{foremal[0].beskrivning:30} prisskillnad {foremal[1]} euro\")

billigare alternativ än Jakobacka trea:\nEira etta prisskillnad 107000 euro\nBerghäll tvåa prisskillnad 35400 euro\nSuomussalmi egnahemshus prisskillnad 87500 euro\nKerava 4r och kök prisskillnad 16500 euro

Comprehensions och ordlistor

Det finns inget i sig \"list-aktigt\" med comprehensions. Resultatet är en lista eftersom comprehension-satsen är inkapslad i hakparenteser, som indikerar en Python-lista. Förståelser fungerar lika bra med Python-ordlistor om du använder rundparenteser istället. Kom dock ihåg att ordlistor kräver nyckel-värde-par. Båda måste anges när en ordlista skapas, även när det gäller comprehensions.

Grunden för en comprehension kan vara vilken itererbar serie som helst, vare sig det är en lista, en sträng, en tupel, en ordlista, någon av dina egna itererbara klasser och så vidare.

I följande exempel använder vi en sträng som bas för en ordlista. Ordlistan innehåller alla unika tecken i strängen, tillsammans med antalet gånger de förekommer:

\nmening = \"Hej alla\"\n\ntecken_antal = {bokstav : mening.count(bokstav) for bokstav in mening}\nprint(tecken_antal)\n

{'H': 1, 'e': 1, 'j': 1, ' ': 1, 'a': 2, 'l': 2}

Principen för comprehension-satsen är exakt densamma som för listor, men i stället för ett enda värde består uttrycket nu av en nyckel och ett värde. Den allmänna syntaxen ser ut så här:

{<nyckeluttryck> : <värdeuttryck> för <föremål> i <serie>}

Som avslutning på det här avsnittet tittar vi på faktorialtal igen. Den här gången lagrar vi resultaten i en ordlista. Själva talet är nyckeln, medan värdet är resultatet av faktorn från vår funktion:

\ndef fakultet(n: int):\n    \"\"\" Funktionen beräknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [-2, 3, 2, 1, 4, -10, 5, 1, 6]\n    fakultett = {tal : fakultet(tal) for tal in lista if tal > 0}\n    print(fakulteter)\n

{3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}

Skapa en funktion med namnet langder(strangar: list) som tar en lista med strängar som sitt argument. Funktionen ska returnera en ordlista med strängarna i listan som nycklar och deras längder som värden.

Funktionen bör implementeras med en ordlistscomprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som inleds med nyckelordet def.

Funktionen ska fungera på följande sätt:

ordlista = [\"det\", \"var\" , \"en\", \"gång\", \"python\"]\n\nsanojen_langder = langder(ordlista)\nprint(sanojen_langder)

{'det': 3, 'var': 3, 'en': 2, 'gång': 4, 'python': 6}

Skapa en funktion med namnet vanligaste_orden(filnamn: str, nedre_grans: int) som tar ett filnamn och ett heltalsvärde som en nedre gräns som sina argument. Funktionen ska returnera en ordlista som innehåller antalet förekomster av de ord som förekommer minst det antal gånger som anges i parametern nedre_grans.

Anta t.ex. att funktionen används för att bearbeta en fil med namnet comprehensions.txt med följande innehåll:

List comprehension is an elegant way to define and create lists based on existing lists.\nList comprehension is generally more compact and faster than normal functions and loops for creating list.\nHowever, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.\nRemember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension.

När funktionen anropas med argumenten vanligaste_orden(\"comprehensions.txt\", 3) ska den returnera

{'comprehension': 4, 'is': 3, 'and': 3, 'for': 3, 'list': 4, 'in': 3}

Observera att bokstävernas versaler påverkar resultatet och att alla böjda former är unika ord i den här övningen. Det vill säga att orden List, lists och list är separata ord här, och endast list har tillräckligt många förekomster för att komma med i den returnerade listan. Alla skiljetecken ska tas bort innan du räknar upp förekomsterna.

Det är upp till dig att bestämma hur detta ska implementeras. Det enklaste sättet skulle troligen vara att använda list- och ordlistsscomprehension.

","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-11/2-mer-om-comprehensions.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda comprehensions med strängar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du använder comprehensions med dina egna klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa ordlistscomprehensions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listor är kanske det vanligaste målet för comprehensions, men comprehensions fungerar på alla serier av föremål, inklusive strängar. Liksom listexemplen i föregående avsnitt, ifall en list comprehension utförs på en sträng, plockas föremålen (dvs. tecknen) i strängen en efter en, bearbetas enligt det givna uttrycket och lagras i en lista."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"text","value":"\n\nstora_bokstaver "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tecken"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stora_bokstaver"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['P', 'E', 'T', 'E', 'R', ' ', 'P', 'Y', 'T', 'H', 'O', 'N']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet är en lista, vilket dikteras av parentesnotationen runt comprehension-satsen. Om vi ville ha en sträng istället skulle vi kunna använda strängmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" för att tolka listan till en sträng. Kom ihåg att metoden anropas på den sträng som vi vill använda som \"lim\" mellan tecknen. Låt oss ta en titt på några exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"text","value":"\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" och \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['P', 'e', 't', 'e', 'r']\nPeter\nP e t e r\nP,e,t,e,r\nP och e och t och e och r"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List comprehensions och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":"-metoden gör det enkelt att skapa nya strängar baserade på andra strängar. Vi kan t.ex. skapa en sträng som bara innehåller vokalerna från en annan sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nteststrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Halloj allihopa, det här är ett test\""}]},{"type":"text","value":"\n\nvokaler "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" teststrang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiouyåäö\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nnystrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vokaler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nystrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"aoaioaeääee"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan står list comprehension och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":"-metoden på separata rader, men de kan kombineras till ett enda uttryck:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nteststrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Halloj allihopa, det här är ett test\""}]},{"type":"text","value":"\n\nvokalstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" teststrang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiouyåäö\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vokalstrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Många Python-programmerare står trogna vid dessa oneliners, så det är väl värt besväret att lära sig läsa dem. Vi kan till och med lägga till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":"-metoden i mixen, så att vi kan bearbeta hela meningar effektivt med ett enda uttalande. I exemplet nedan tas det första tecknet från varje ord i en mening bort:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sju sjösjuka sjömän på skeppet Shang Hai.\""}]},{"type":"text","value":"\n\nmening_utan_initialer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening_utan_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ju jösjuka jömän å keppet hang ai"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss gå igenom detta steg för steg:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord[1:]"}]},{"type":"text","value":" extraherar en delsträng från det andra tecknet (vid index 1) och framåt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mening.split()"}]},{"type":"text","value":" delar upp meningen i avsnitt vid det angivna tecknet. I det här fallet ges inget argument till metoden, så meningen delas upp vid mellanslagstecken som standard"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\" \".join()"}]},{"type":"text","value":" kombinerar föremålen i listan till en ny sträng med ett mellanslag mellan föremålen"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En mer traditionell iterativ metod skulle kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sju sjösjuka sjömän på skeppet Shang Hai.\""}]},{"type":"text","value":"\n\nordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\norden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ord_utan_initialer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_utan_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmening_utan_initialer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening_utan_initialer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filtrera förbjudna","tmcname":"osa11-08_filtrera_forbjudna"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filtrera_forbjudna(strang: str, forbjuden: str)"}]},{"type":"text","value":" som tar två strängar som argument. Funktionen ska returnera en ny version av den första strängen. Den ska inte innehålla tecken från den andra strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst tre rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"mening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det! var, en gång: en python!??!?!\""}]},{"type":"text","value":"\nfiltrerad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filtrera_forbjudna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!?:,.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filtrerad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det var en gång en python"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"egna-klasser-och-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Egna klasser och comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#egna-klasser-och-comprehensions","ariaLabel":"egna klasser och comprehensions permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Comprehensions kan vara ett användbart verktyg för att bearbeta eller formulera instanser av dina egna klasser, vilket vi kommer att se i följande exempel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss först ta en titt på klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Land"}]},{"type":"text","value":" som är en enkel modell för ett enda land, med attribut för namn och befolkning. I huvudfunktionen nedan skapar vi först några Land-objekt och använder sedan en list comprehension för att bara välja dem vars befolkning är större än fem miljoner."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Land"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Denna klass modellerar ett enkelt land med befolkning \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" befolkningsmangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"befolkningsmangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" befolkningsmangd\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n finland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n malta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sverige "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n island "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Island\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"350000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lander "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"finland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" malta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sverige"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" island"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n storre_land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lander "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"befolkningsmangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" storre_land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finland\nSverige"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I list comprehension ovan valde vi bara namnattributet från Land-objekten, så innehållet i listan kunde skrivas ut direkt. Vi skulle också kunna skapa en ny lista med länderna och komma åt namnattributet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen. Detta skulle vara användbart om samma lista med länder skulle användas senare i programmet, eller om vi behövde befolkningsattributet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen också:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n finland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n malta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sverige "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n island "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Island\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"350000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lander "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"finland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" malta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sverige"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" island"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n storre_land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lander "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"befolkningsmangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" storre_land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I nästa exempel har vi en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fotlopp"}]},{"type":"text","value":" som modellerar ett enskilt lopp med attribut för loppets längd och namn. Vi kommer att använda list comprehension för att skapa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fotlopp"}]},{"type":"text","value":"-objekt baserat på en lista med tävlingslängder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" har ett standardvärde i konstruktorn för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fotlopp"}]},{"type":"text","value":"-klassen, vilket är varför vi inte behöver skicka namnet som ett argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Fotlopp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar ett fotloppsevenemang med längden n meter \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"inget namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"stracka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stracka\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" m. ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n langder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42195"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n strackor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"Fotlopp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" langd "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut alla"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strackor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ta en från listan och ge den ett namn"}]},{"type":"text","value":"\n maraton "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strackor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# sista föremålet i listan"}]},{"type":"text","value":"\n maraton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maraton\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut alla igen, inkluderandes det nya namnet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strackor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[100 m. (inget namn), 200 m. (inget namn), 1500 m. (inget namn), 3000 m. (inget namn), 42195 m. (inget namn)]\n[100 m. (inget namn), 200 m. (inget namn), 1500 m. (inget namn), 3000 m. (inget namn), 42195 m. (Maraton)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss nu ta reda på vad som gör en serie objekt \"begripliga\" (“comprehendible”). I föregående del lärde vi oss hur vi kan göra våra egna klasser itererbara. Det är exakt samma funktion som också möjliggör list comprehension. Om din egen klass är itererbar kan den användas som grund för en list comprehension. Följande klassdefinitioner är kopierade direkt från "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-10/3-objektorienterade-programmeringstekniker","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 10"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sidor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" forfattare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sidor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sidor\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bokhylla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Iteratorns initialiseringsmetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Här bör iterationsvariabeln(variablerna) initialiseras"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar en referens till själva objektet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# eftersom iteratorn är implementerad inom samma klassdefinition"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna metod returnerar nästa föremål inom objektet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Om alla föremål har genomgåtts åstadkomms StopIteration"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__next__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ta det aktuella föremålet från listan i objektet"}]},{"type":"text","value":"\n bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Öka räknaren med ett"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ...och returnera föremålet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bok\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Inga fler böcker"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" StopIteration\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Livet av en Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"123"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den gamle och Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ernest Hemingjava\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"204"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C-värdheter på nätet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Karl Kodare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n hylla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bokhylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skapa en lista innehållandes namnet på alla böcker"}]},{"type":"text","value":"\n bockernas_namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hylla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bockernas_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Affärslistans produkter","tmcname":"osa11-09_affarslistans_produkter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I modul 10 skapade du en "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-10/3-objektorienterade-programmeringstekniker","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"itererbar affärslista"}]},{"type":"text","value":". Objekt av en itererbar klass kan användas med list comprehensions. Uppgiftsmallen innehåller en avskalad version av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" med knappt tillräckligt funktion för denna övning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"affarslistans_produkter(affarslista, antal: int)"}]},{"type":"text","value":" som tar som argument ett affarslista-objekt och ett heltalsvärde. Funktionen returnerar en lista av produktnamn. Listan borde inkludera endast produkter som har åtminstone ett lika stort antal som parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"antal"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":". Klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" bör "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inte"}]},{"type":"text","value":" modifieras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"alkoholfri öl\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ananas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Affärslistan har minst 8 av följande:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" affarslistans_produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Affärslistan har minst 8 av följande:\nbananer\nalkoholfri öl"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Billigare prisskillnad","tmcname":"osa11-10_billigare_prisskillnad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna övning innehåller en aning modifierad version av klassen "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/1-objekt-och-referenser","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"text","value":" från modul 9."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"billigare(bostader: list, jamforelse: Bostad)"}]},{"type":"text","value":" som tar en lista med bostäder och ett enda Bostad-objekt som sina argument. Funktionen ska returnera en lista som endast innehåller de bostäder i den ursprungliga listan som är billigare än jämförelsebostaden, tillsammans med prisskillnaden. Föremålen i den returnerade listan bör vara tupler, där det första föremålet är själva bostaden och den andra är prisskillnaden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden för Bostad-klassen ingår i övningsmallen och ska inte ändras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på funktionen i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eira etta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Berghäll tvåa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jakobacka trea\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"215"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Suomussalmi egnahemshus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"105"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1700"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kerava 4r och kök\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na6 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Haikon kartano\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbostader "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a6"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"billigare alternativ än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beskrivning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":":\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" billigare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bostader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beskrivning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" prisskillnad "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"billigare alternativ än Jakobacka trea:\nEira etta prisskillnad 107000 euro\nBerghäll tvåa prisskillnad 35400 euro\nSuomussalmi egnahemshus prisskillnad 87500 euro\nKerava 4r och kök prisskillnad 16500 euro"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"comprehensions-och-ordlistor","style":"position:relative;"},"children":[{"type":"text","value":"Comprehensions och ordlistor"},{"type":"element","tagName":"a","properties":{"href":"#comprehensions-och-ordlistor","ariaLabel":"comprehensions och ordlistor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns inget i sig \"list-aktigt\" med comprehensions. Resultatet är en lista eftersom comprehension-satsen är inkapslad i hakparenteser, som indikerar en Python-lista. Förståelser fungerar lika bra med Python-ordlistor om du använder rundparenteser istället. Kom dock ihåg att ordlistor kräver nyckel-värde-par. Båda måste anges när en ordlista skapas, även när det gäller comprehensions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Grunden för en comprehension kan vara vilken itererbar serie som helst, vare sig det är en lista, en sträng, en tupel, en ordlista, någon av dina egna itererbara klasser och så vidare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel använder vi en sträng som bas för en ordlista. Ordlistan innehåller alla unika tecken i strängen, tillsammans med antalet gånger de förekommer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla\""}]},{"type":"text","value":"\n\ntecken_antal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bokstav "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bokstav"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bokstav "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tecken_antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'H': 1, 'e': 1, 'j': 1, ' ': 1, 'a': 2, 'l': 2}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Principen för comprehension-satsen är exakt densamma som för listor, men i stället för ett enda värde består uttrycket nu av en nyckel och ett värde. Den allmänna syntaxen ser ut så här:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{ : för i }"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som avslutning på det här avsnittet tittar vi på faktorialtal igen. Den här gången lagrar vi resultaten i en ordlista. Själva talet är nyckeln, medan värdet är resultatet av faktorn från vår funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen beräknar fakulteten n! för positiva heltal \"\"\""}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fakultett "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Strängarnas längder","tmcname":"osa11-11_strangarnas_langder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langder(strangar: list)"}]},{"type":"text","value":" som tar en lista med strängar som sitt argument. Funktionen ska returnera en ordlista med strängarna i listan som nycklar och deras längder som värden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen bör implementeras med en ordlistscomprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som inleds med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"det\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"var\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"en\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"gång\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsanojen_langder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sanojen_langder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'det': 3, 'var': 3, 'en': 2, 'gång': 4, 'python': 6}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vanligaste orden","tmcname":"osa11-12_vanligaste_orden"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vanligaste_orden(filnamn: str, nedre_grans: int)"}]},{"type":"text","value":" som tar ett filnamn och ett heltalsvärde som en nedre gräns som sina argument. Funktionen ska returnera en ordlista som innehåller antalet förekomster av de ord som förekommer minst det antal gånger som anges i parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nedre_grans"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anta t.ex. att funktionen används för att bearbeta en fil med namnet comprehensions.txt med följande innehåll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"txt"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-txt"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-txt"]},"children":[{"type":"text","value":"List comprehension is an elegant way to define and create lists based on existing lists.\nList comprehension is generally more compact and faster than normal functions and loops for creating list.\nHowever, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.\nRemember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När funktionen anropas med argumenten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vanligaste_orden(\"comprehensions.txt\", 3)"}]},{"type":"text","value":" ska den returnera"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'comprehension': 4, 'is': 3, 'and': 3, 'for': 3, 'list': 4, 'in': 3}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att bokstävernas versaler påverkar resultatet och att alla böjda former är unika ord i den här övningen. Det vill säga att orden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"List"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lists"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" är separata ord här, och endast "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" har tillräckligt många förekomster för att komma med i den returnerade listan. Alla skiljetecken ska tas bort innan du räknar upp förekomsterna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är upp till dig att bestämma hur detta ska implementeras. Det enklaste sättet skulle troligen vara att använda list- och ordlistsscomprehension."}]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att kunna använda comprehensions med strängar
  • \n
  • Vet du hur du använder comprehensions med dina egna klasser
  • \n
  • Kommer du att kunna skapa ordlistscomprehensions
  • \n

Listor är kanske det vanligaste målet för comprehensions, men comprehensions fungerar på alla serier av föremål, inklusive strängar. Liksom listexemplen i föregående avsnitt, ifall en list comprehension utförs på en sträng, plockas föremålen (dvs. tecknen) i strängen en efter en, bearbetas enligt det givna uttrycket och lagras i en lista.

\nnamn = \"Peter Python\"\n\nstora_bokstaver = [tecken.upper() for tecken in namn]\nprint(stora_bokstaver)\n

['P', 'E', 'T', 'E', 'R', ' ', 'P', 'Y', 'T', 'H', 'O', 'N']

Resultatet är en lista, vilket dikteras av parentesnotationen runt comprehension-satsen. Om vi ville ha en sträng istället skulle vi kunna använda strängmetoden join för att tolka listan till en sträng. Kom ihåg att metoden anropas på den sträng som vi vill använda som \"lim\" mellan tecknen. Låt oss ta en titt på några exempel:

\nnamn = \"Peter\"\nlista = list(namn)\nprint(lista)\n\nprint(\"\".join(lista))\nprint(\" \".join(lista))\nprint(\",\".join(lista))\nprint(\" och \".join(lista))\n

['P', 'e', 't', 'e', 'r']\nPeter\nP e t e r\nP,e,t,e,r\nP och e och t och e och r

List comprehensions och join-metoden gör det enkelt att skapa nya strängar baserade på andra strängar. Vi kan t.ex. skapa en sträng som bara innehåller vokalerna från en annan sträng:

\nteststrang = \"Halloj allihopa, det här är ett test\"\n\nvokaler = [tecken for tecken in teststrang if tecken in \"aeiouyåäö\"]\nnystrang = \"\".join(vokaler)\n\nprint(nystrang)\n

aoaioaeääee

I exemplet ovan står list comprehension och join-metoden på separata rader, men de kan kombineras till ett enda uttryck:

\nteststrang = \"Halloj allihopa, det här är ett test\"\n\nvokalstrang = \"\".join([tecken for tecken in teststrang if tecken in \"aeiouyåäö\"])\n\nprint(vokalstrang)\n

Många Python-programmerare står trogna vid dessa oneliners, så det är väl värt besväret att lära sig läsa dem. Vi kan till och med lägga till split-metoden i mixen, så att vi kan bearbeta hela meningar effektivt med ett enda uttalande. I exemplet nedan tas det första tecknet från varje ord i en mening bort:

\nmening = \"Sju sjösjuka sjömän på skeppet Shang Hai.\"\n\nmening_utan_initialer = \" \".join([ord[1:] for ord in mening.split()])\nprint(mening_utan_initialer)\n

ju jösjuka jömän å keppet hang ai

Låt oss gå igenom detta steg för steg:

    \n
  • ord[1:] extraherar en delsträng från det andra tecknet (vid index 1) och framåt
  • \n
  • mening.split() delar upp meningen i avsnitt vid det angivna tecknet. I det här fallet ges inget argument till metoden, så meningen delas upp vid mellanslagstecken som standard
  • \n
  • \" \".join() kombinerar föremålen i listan till en ny sträng med ett mellanslag mellan föremålen
  • \n

En mer traditionell iterativ metod skulle kunna se ut så här:

\nmening = \"Sju sjösjuka sjömän på skeppet Shang Hai.\"\n\nordlista = []\norden = mening.split()\nfor ord in orden:\n    ord_utan_initialer = ord[1:]\n    ordlista.append(ord_utan_initialer)\n\nmening_utan_initialer = \" \".join(ordlista)\n\n\nprint(mening_utan_initialer)\n

Skapa funktionen filtrera_forbjudna(strang: str, forbjuden: str) som tar två strängar som argument. Funktionen ska returnera en ny version av den första strängen. Den ska inte innehålla tecken från den andra strängen.

Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst tre rader lång, inklusive rubrikraden som börjar med nyckelordet def.

Funktionen ska fungera på följande sätt:

mening = \"Det! var, en gång: en python!??!?!\"\nfiltrerad = filtrera_forbjudna(mening, \"!?:,.\")\nprint(filtrerad)

Det var en gång en python

Egna klasser och comprehensions

Comprehensions kan vara ett användbart verktyg för att bearbeta eller formulera instanser av dina egna klasser, vilket vi kommer att se i följande exempel.

Låt oss först ta en titt på klassen Land som är en enkel modell för ett enda land, med attribut för namn och befolkning. I huvudfunktionen nedan skapar vi först några Land-objekt och använder sedan en list comprehension för att bara välja dem vars befolkning är större än fem miljoner.

\nclass Land:\n    \"\"\" Denna klass modellerar ett enkelt land med befolkning \"\"\"\n    def __init__(self, namn: str, befolkningsmangd: int):\n        self.namn = namn\n        self.befolkningsmangd = befolkningsmangd\n\nif __name__ == \"__main__\":\n    finland = Land(\"Finland\", 6000000)\n    malta = Land(\"Malta\", 500000)\n    sverige = Land(\"Sverige\", 10000000)\n    island = Land(\"Island\", 350000)\n\n    lander = [finland, malta, sverige, island]\n\n    storre_land = [land.namn for land in lander if land.befolkningsmangd > 5000000]\n    for land in storre_land:\n        print(land)\n\n

Finland\nSverige

I list comprehension ovan valde vi bara namnattributet från Land-objekten, så innehållet i listan kunde skrivas ut direkt. Vi skulle också kunna skapa en ny lista med länderna och komma åt namnattributet i for-loopen. Detta skulle vara användbart om samma lista med länder skulle användas senare i programmet, eller om vi behövde befolkningsattributet i for-loopen också:

\nif __name__ == \"__main__\":\n    finland = Land(\"Finland\", 6000000)\n    malta = Land(\"Malta\", 500000)\n    sverige = Land(\"Sverige\", 10000000)\n    island = Land(\"Island\", 350000)\n\n    lander = [finland, malta, sverige, island]\n\n    storre_land = [land for land in lander if land.befolkningsmangd > 5000000]\n    for land in storre_land:\n        print(land.namn)

I nästa exempel har vi en klass som heter Fotlopp som modellerar ett enskilt lopp med attribut för loppets längd och namn. Vi kommer att använda list comprehension för att skapa Fotlopp-objekt baserat på en lista med tävlingslängder.

Parametern namn har ett standardvärde i konstruktorn för Fotlopp-klassen, vilket är varför vi inte behöver skicka namnet som ett argument.

\nclass Fotlopp:\n    \"\"\" Klassen modellerar ett fotloppsevenemang med längden n meter \"\"\"\n    def __init__(self, stracka:int, namn:str = \"inget namn\"):\n        self.stracka = stracka\n        self.namn = namn\n\n    def __repr__(self):\n        return f\"{self.stracka} m. ({self.namn})\"\n\nif __name__ == \"__main__\":\n    langder = [100, 200, 1500, 3000, 42195]\n    strackor = [Fotlopp(langd) for langd in langder]\n\n    # Skriv ut alla\n    print(strackor)\n\n    # Ta en från listan och ge den ett namn\n    maraton = strackor[-1] # sista föremålet i listan\n    maraton.namn = \"Maraton\"\n\n    # Skriv ut alla igen, inkluderandes det nya namnet\n    print(strackor)\n

[100 m. (inget namn), 200 m. (inget namn), 1500 m. (inget namn), 3000 m. (inget namn), 42195 m. (inget namn)]\n[100 m. (inget namn), 200 m. (inget namn), 1500 m. (inget namn), 3000 m. (inget namn), 42195 m. (Maraton)]

Låt oss nu ta reda på vad som gör en serie objekt \"begripliga\" (“comprehendible”). I föregående del lärde vi oss hur vi kan göra våra egna klasser itererbara. Det är exakt samma funktion som också möjliggör list comprehension. Om din egen klass är itererbar kan den användas som grund för en list comprehension. Följande klassdefinitioner är kopierade direkt från modul 10:

\nclass Bok:\n    def __init__(self, namn: str, forfattare: str, sidor: int):\n        self.namn = namn\n        self.forfattare = forfattare\n        self.sidor = sidor\n\nclass Bokhylla:\n    def __init__(self):\n        self._bocker = []\n\n    def tillsatt_bok(self, bok: Bok):\n        self._bocker.append(bok)\n\n    # Iteratorns initialiseringsmetod\n    # Här bör iterationsvariabeln(variablerna) initialiseras\n    def __iter__(self):\n        self.n = 0\n        # Metoden returnerar en referens till själva objektet\n        # eftersom iteratorn är implementerad inom samma klassdefinition\n        return self\n\n    # Denna metod returnerar nästa föremål inom objektet\n    # Om alla föremål har genomgåtts åstadkomms StopIteration\n    def __next__(self):\n        if self.n < len(self._bocker):\n            # Ta det aktuella föremålet från listan i objektet\n            bok = self._bocker[self.n]\n            # Öka räknaren med ett\n            self.n += 1\n            # ...och returnera föremålet\n            return bok\n        else:\n            # Inga fler böcker\n            raise StopIteration\n\n# Testar\nif __name__ == \"__main__\":\n    b1 = Bok(\"Livet av en Python\", \"Peter Python\", 123)\n    b2 = Bok(\"Den gamle och Java\", \"Ernest Hemingjava\", 204)\n    b3 = Bok(\"C-värdheter på nätet\", \"Karl Kodare\", 997)\n\n    hylla = Bokhylla()\n    hylla.tillsatt_bok(b1)\n    hylla.tillsatt_bok(b2)\n    hylla.tillsatt_bok(b3)\n\n    # Skapa en lista innehållandes namnet på alla böcker\n    bockernas_namn = [bok.namn for bok in hylla]\n    print(bockernas_namn)\n

I modul 10 skapade du en itererbar affärslista. Objekt av en itererbar klass kan användas med list comprehensions. Uppgiftsmallen innehåller en avskalad version av Affarslista med knappt tillräckligt funktion för denna övning.

Skapa en funktion med namnet affarslistans_produkter(affarslista, antal: int) som tar som argument ett affarslista-objekt och ett heltalsvärde. Funktionen returnerar en lista av produktnamn. Listan borde inkludera endast produkter som har åtminstone ett lika stort antal som parametern antal.

Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet def. Klassdefinitionen för Affarslista bör inte modifieras.

Funktionen ska fungera enligt följande:

lista = Affarslista()\nlista.tillsatt(\"bananer\", 10)\nlista.tillsatt(\"äppel\", 5)\nlista.tillsatt(\"alkoholfri öl\", 24)\nlista.tillsatt(\"ananas\", 1)\n\nprint(\"Affärslistan har minst 8 av följande:\")\nfor produkt in affarslistans_produkter(lista, 8):\n    print(produkt)

Affärslistan har minst 8 av följande:\nbananer\nalkoholfri öl

Denna övning innehåller en aning modifierad version av klassen Bostad från modul 9.

Skapa en funktion med namnet billigare(bostader: list, jamforelse: Bostad) som tar en lista med bostäder och ett enda Bostad-objekt som sina argument. Funktionen ska returnera en lista som endast innehåller de bostäder i den ursprungliga listan som är billigare än jämförelsebostaden, tillsammans med prisskillnaden. Föremålen i den returnerade listan bör vara tupler, där det första föremålet är själva bostaden och den andra är prisskillnaden.

Funktionen bör implementeras med hjälp av list comprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som börjar med nyckelordet def.

Koden för Bostad-klassen ingår i övningsmallen och ska inte ändras.

Ett exempel på funktionen i användning:

a1 = Bostad(1, 16, 5500, \"Eira etta\")\na2 = Bostad(2, 38, 4200, \"Berghäll tvåa\")\na3 = Bostad(3, 78, 2500, \"Jakobacka trea\")\na4 = Bostad(6, 215, 500, \"Suomussalmi egnahemshus\")\na5 = Bostad(4, 105, 1700, \"Kerava 4r och kök\")\na6 = Bostad(25, 1200, 2500, \"Haikon kartano\")\n\nbostader = [a1, a2, a3, a4, a5, a6]\n\nprint(f\"billigare alternativ än {a3.beskrivning}:\")\nfor foremal in billigare(bostader, a3):\n    print(f\"{foremal[0].beskrivning:30} prisskillnad {foremal[1]} euro\")

billigare alternativ än Jakobacka trea:\nEira etta prisskillnad 107000 euro\nBerghäll tvåa prisskillnad 35400 euro\nSuomussalmi egnahemshus prisskillnad 87500 euro\nKerava 4r och kök prisskillnad 16500 euro

Comprehensions och ordlistor

Det finns inget i sig \"list-aktigt\" med comprehensions. Resultatet är en lista eftersom comprehension-satsen är inkapslad i hakparenteser, som indikerar en Python-lista. Förståelser fungerar lika bra med Python-ordlistor om du använder rundparenteser istället. Kom dock ihåg att ordlistor kräver nyckel-värde-par. Båda måste anges när en ordlista skapas, även när det gäller comprehensions.

Grunden för en comprehension kan vara vilken itererbar serie som helst, vare sig det är en lista, en sträng, en tupel, en ordlista, någon av dina egna itererbara klasser och så vidare.

I följande exempel använder vi en sträng som bas för en ordlista. Ordlistan innehåller alla unika tecken i strängen, tillsammans med antalet gånger de förekommer:

\nmening = \"Hej alla\"\n\ntecken_antal = {bokstav : mening.count(bokstav) for bokstav in mening}\nprint(tecken_antal)\n

{'H': 1, 'e': 1, 'j': 1, ' ': 1, 'a': 2, 'l': 2}

Principen för comprehension-satsen är exakt densamma som för listor, men i stället för ett enda värde består uttrycket nu av en nyckel och ett värde. Den allmänna syntaxen ser ut så här:

{<nyckeluttryck> : <värdeuttryck> för <föremål> i <serie>}

Som avslutning på det här avsnittet tittar vi på faktorialtal igen. Den här gången lagrar vi resultaten i en ordlista. Själva talet är nyckeln, medan värdet är resultatet av faktorn från vår funktion:

\ndef fakultet(n: int):\n    \"\"\" Funktionen beräknar fakulteten n! för positiva heltal \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    lista = [-2, 3, 2, 1, 4, -10, 5, 1, 6]\n    fakultett = {tal : fakultet(tal) for tal in lista if tal > 0}\n    print(fakulteter)\n

{3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}

Skapa en funktion med namnet langder(strangar: list) som tar en lista med strängar som sitt argument. Funktionen ska returnera en ordlista med strängarna i listan som nycklar och deras längder som värden.

Funktionen bör implementeras med en ordlistscomprehension. Funktionen får vara högst två rader lång, inklusive rubrikraden som inleds med nyckelordet def.

Funktionen ska fungera på följande sätt:

ordlista = [\"det\", \"var\" , \"en\", \"gång\", \"python\"]\n\nsanojen_langder = langder(ordlista)\nprint(sanojen_langder)

{'det': 3, 'var': 3, 'en': 2, 'gång': 4, 'python': 6}

Skapa en funktion med namnet vanligaste_orden(filnamn: str, nedre_grans: int) som tar ett filnamn och ett heltalsvärde som en nedre gräns som sina argument. Funktionen ska returnera en ordlista som innehåller antalet förekomster av de ord som förekommer minst det antal gånger som anges i parametern nedre_grans.

Anta t.ex. att funktionen används för att bearbeta en fil med namnet comprehensions.txt med följande innehåll:

List comprehension is an elegant way to define and create lists based on existing lists.\nList comprehension is generally more compact and faster than normal functions and loops for creating list.\nHowever, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.\nRemember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension.

När funktionen anropas med argumenten vanligaste_orden(\"comprehensions.txt\", 3) ska den returnera

{'comprehension': 4, 'is': 3, 'and': 3, 'for': 3, 'list': 4, 'in': 3}

Observera att bokstävernas versaler påverkar resultatet och att alla böjda former är unika ord i den här övningen. Det vill säga att orden List, lists och list är separata ord här, och endast list har tillräckligt många förekomster för att komma med i den returnerade listan. Alla skiljetecken ska tas bort innan du räknar upp förekomsterna.

Det är upp till dig att bestämma hur detta ska implementeras. Det enklaste sättet skulle troligen vara att använda list- och ordlistsscomprehension.

","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-11/2-mer-om-comprehensions.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-11/3-rekursion/page-data.json b/page-data/osa-11/3-rekursion/page-data.json index 599d31c2b..71f940447 100644 --- a/page-data/osa-11/3-rekursion/page-data.json +++ b/page-data/osa-11/3-rekursion/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-11/3-rekursion", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad rekursion innebär"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva en enkel rekursiv funktion"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som vi har sett många gånger tidigare kan funktioner anropa andra funktioner. Till exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hej"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hej_flera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hej"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion kan också anropa sig själv, men vi som programmerare måste vara försiktiga när vi gör det. Det är lätt att hamna i en oändlig loop av funktionsanrop, precis som vi hamnade i en oändlig loop av upprepningar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loopar om vi utelämnade lämpliga brytvillkor. Så om man försöker anropa en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hej"}]},{"type":"text","value":"-funktion med följande definition"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hej"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hej"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"skulle skapa ett nytt sort av fel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"RecursionError: maximum recursion depth exceeded"}]}]},{"type":"element","tagName":"h2","properties":{"id":"vad-betyder-rekursion","style":"position:relative;"},"children":[{"type":"text","value":"Vad betyder rekursion?"},{"type":"element","tagName":"a","properties":{"href":"#vad-betyder-rekursion","ariaLabel":"vad betyder rekursion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den rekursion som nämns i felet ovan innebär att man definierar något i termer av sig självt. I programmeringssammanhang handlar det oftast om en funktion som anropar sig själv. För att detta ska fungera utan att orsaka oändliga loopar måste de argument som skickas till funktionen ändras varje gång, så att de nästlade funktionsanropen slutar vid något skede. Grundprincipen här är densamma som i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loopar: det måste alltid finnas ett stoppvillkor av något slag, och det villkoret måste utlösas vid någon tidpunkt i exekveringen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en enkel funktion som lägger till nollor i en lista så länge det finns färre än 10 objekt i listan. Den här gången använder vi dock inte en loop. Om villkoret ännu inte är uppfyllt anropar funktionen sig själv"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fyll_lista"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Lägger till föremål till listan ifall listans längd är kortare än 10 \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# anropa funktionen igen"}]},{"type":"text","value":"\n fyll_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n test "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fyll_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna funktionalitet kunde lika väl bli uppnådd genom en vanlig "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fyll_lista"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Lägger till föremål till listan ifall listans längd är kortare än 10 \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n test "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fyll_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det mer traditionella iterativa tillvägagångssättet ger ett kortare program som förmodligen också är lättare att förstå. Med den rekursiva versionen är det inte lika tydligt att vi under hela processen arbetar med exakt samma lista. Så är det dock och därför fungerar den rekursiva funktionen lika bra."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Iterativ eller rekursiv?"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inom datavetenskapsteori skiljer man ofta mellan iterativa och rekursiva algoritmer, så det är bäst att bekanta sig med dessa termer redan från början. Iterativa lösningar är sådana som baseras på sekventiell bearbetning av objekt, ofta med hjälp av loopar. Hittills har vi behandlat iterativa metoder ganska exklusivt. Rekursiv, å andra sidan, avser en metod där funktionen anropar sig själv med förändrade parametervärden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I princip borde det vara möjligt att lösa alla problem med antingen iterativa eller rekursiva metoder. I praktiken är det dock oftast så att den ena eller den andra metoden är klart bättre lämpad för varje problem. Förmågan att avgöra vilken som är bäst kommer till stor del med övning."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Större tal","tmcname":"osa11-13_tal_till_listan"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"rekursiv funktion"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tal_till_listan(tal: list)"}]},{"type":"text","value":". Funktionen tar en lista med tal som sitt argument och lägger till nya tal i listan tills längden på listan är delbar med fem. Varje tal som läggs till i listan ska vara ett större tal än det sista talet i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen måste anropa sig själv rekursivt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fxempel på funktionen i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\ntal_till_listan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 4, 5, 10, 11, 12, 13, 14, 15]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"rekursion-och-returvarden","style":"position:relative;"},"children":[{"type":"text","value":"Rekursion och returvärden"},{"type":"element","tagName":"a","properties":{"href":"#rekursion-och-returvarden","ariaLabel":"rekursion och returvarden permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rekursiva funktioner kan också ha returvärden. I de senaste avsnitten har vi arbetat med fakultettal, så låt oss skriva en rekursiv fakultetfunktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen räknar ut fakulteten n! för n>= 0 \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Fakulteten av 0 och 1 är 1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# anropa funktionen igen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Fakulteten av "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fakulteten av 1 är 1\nFakulteten av 2 är 2\nFakulteten av 3 är 6\nFakulteten av 4 är 24\nFakulteten av 5 är 120\nFakulteten av 6 är 720"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om parametern för den rekursiva faktoriella funktionen är 0 eller 1, returnerar funktionen 1, eftersom det är så faktoriell operation definieras. I alla andra fall returnerar funktionen värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n * fakultet(n - 1)"}]},{"type":"text","value":", vilket är värdet av dess parameter n multiplicerat med returvärdet av funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fakultet(n - 1)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det avgörande här är att funktionsdefinitionen innehåller ett stoppvillkor. Om detta uppfylls avslutas rekursionen. I det här fallet är villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n < 2"}]},{"type":"text","value":". Vi vet att det kommer att nås så småningom, eftersom det värde som skickas som argument till funktionen minskas med ett på varje nivå i rekursionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" kan vara till stor hjälp när det gäller att förstå rekursiva program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exemplet ovan skulle kanske bli lite tydligare om vi använde oss av hjälpvariabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n forra_talets_fakultet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fakultet_nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" forra_talets_fakultet\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" fakultet_nu\n\nfakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ta en titt på hur"},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#code=def%20kertoma%28n%3A%20int%29%3A%0A%20%20%20%20if%20n%20%3C%202%3A%0A%20%20%20%20%20%20%20%20return%201%0A%0A%20%20%20%20edellisen_luvun_kertoma%20%3D%20kertoma%28n%20-%201%29%0A%20%20%20%20luvun_n_kertoma%20%3D%20n%20*%20edellisen_luvun_kertoma%0A%20%20%20%20return%20luvun_n_kertoma%0A%20%20%20%20%0Akertoma%285%29&cumulative=false&curInstr=5&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyget"}]},{"type":"text","value":" visar hur rekursionen fortskrider."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Visualiseringsverktyget har en liten finess i hur det hanterar anropsstacken, eftersom den verkar \"växa\" nedåt. Vanligtvis avbildas anropsstaplar som just staplar, där de nya anropen placeras överst. I visualiseringsverktyget är det aktiva funktionsanropet det skuggade blocket längst ner, som har sina egna kopior av de variabler som syns."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När den rekursiva faktorfunktionen anropas byggs anropsstapeln upp tills den gräns som utgörs av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n < 2"}]},{"type":"text","value":" nås. Då återkommer det sista funktionsanropet i stacken med ett värde - det är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", eftersom "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" nu är mindre än 2. Detta återkomstvärde skickas till det föregående funktionsanropet i stacken, där det används för att beräkna det funktionsanropets återkomstvärde, och så vidare tillbaka ut ur stacken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Returvärdet för varje funktionsanrop lagras i hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fakultet_nu"}]},{"type":"text","value":". Gå igenom visualiseringen noggrant tills du förstår vad som händer i varje steg, och var särskilt uppmärksam på det värde som returneras i varje steg."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9d75a2d6b42864178f98c237765dbc86/6976b/11_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 43.913043478260875%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABKklEQVQoz42Ry04DMQxF+/+fxgapGwQSQtBKhcx08rJnbMeOUWcKpTuOoiwSnST3ZpcrlYq1lgJLKpeRCwCA/4Odu/cVNQtjHM7ZrG+L26ym3b2tiMg8zze5946I0zSllBaSpv1Xc3dZaKmwAJaYlEVEzuezqt5kMyuljOOIAIhYV4jIepcQecqOJBksVudm3brZVTZhJgor4zDEONUfmLmlyk3c3bzrTE5yl9lUAaCU0loDJJj57zaPaangaiZN6+zcfI10lWvOKaWtBhZjtt59y+LuMaSP18/xVE6HqXxGX+5v1tbMDAFCCMPl7V8hBADo6xkh4ePzcf9yfHh6D4fB9292HPtUvF062wFKRSFAYUorIqKq2/+xaEGKGWIGknYpayZjMTUz+wbqwQrKy5mcywAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9d75a2d6b42864178f98c237765dbc86/a0b58/11_1_1.webp 230w","/static/9d75a2d6b42864178f98c237765dbc86/bc10c/11_1_1.webp 460w","/static/9d75a2d6b42864178f98c237765dbc86/966d8/11_1_1.webp 920w","/static/9d75a2d6b42864178f98c237765dbc86/445df/11_1_1.webp 1380w","/static/9d75a2d6b42864178f98c237765dbc86/78de1/11_1_1.webp 1840w","/static/9d75a2d6b42864178f98c237765dbc86/129b4/11_1_1.webp 2172w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9d75a2d6b42864178f98c237765dbc86/81c8e/11_1_1.png 230w","/static/9d75a2d6b42864178f98c237765dbc86/08a84/11_1_1.png 460w","/static/9d75a2d6b42864178f98c237765dbc86/c0255/11_1_1.png 920w","/static/9d75a2d6b42864178f98c237765dbc86/b1001/11_1_1.png 1380w","/static/9d75a2d6b42864178f98c237765dbc86/161ec/11_1_1.png 1840w","/static/9d75a2d6b42864178f98c237765dbc86/6976b/11_1_1.png 2172w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9d75a2d6b42864178f98c237765dbc86/c0255/11_1_1.png","alt":"11 1 1","title":"11 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ett annat vanligt rekursivt exempel: Fibonacci-talen. I en Fibonacci-sekvens är varje tal summan av de två föregående talen. De två första talen definieras här som 1 och 1, och sekvensen börjar då så här: 1, 1, 2, 3, 5, 8, 13, 21, 34."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fibonacci"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen returnerar det n:e talet i Fibonaccis talföljd \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# de första två är ettor"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Alla andra tal blir summan av de två föregående talen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" fibonacci"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" fibonacci"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar att allting fungerar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Fibonaccis "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":". tal är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"fibonacci"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fibonaccis 1. tal är 1\nFibonaccis 2. tal är 1\nFibonaccis 3. tal är 2\nFibonaccis 4. tal är 3\nFibonaccis 5. tal är 5\nFibonaccis 6. tal är 8\nFibonaccis 7. tal är 13\nFibonaccis 8. tal är 21\nFibonaccis 9. tal är 34\nFibonaccis 10. tal är 55"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här gången är stoppvillkoret att parametern är mindre än eller lika med 2, eftersom hela sekvensen definieras från de två första siffrorna och framåt, och vi definierade de två första siffrorna som lika med 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur fungerar då den här funktionen i praktiken?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om funktionen anropas med 1 eller 2 som argument returnerar den 1, vilket dikteras av villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n <= 2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om argumentet är 3 eller större returnerar funktionen värdet av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(n - 1) + fibonacci(n - 2)"}]},{"type":"text","value":". Om argumentet är exakt 3 är detta värde lika med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(2) + fibonacci(1)"}]},{"type":"text","value":", och vi vet redan resultatet av båda dessa från föregående steg. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 + 1"}]},{"type":"text","value":" är lika med 2, som alltså är det tredje talet i Fibonacci-sekvensen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om argumentet är 4 är returvärdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(3) + fibonacci(2)"}]},{"type":"text","value":", som vi nu vet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 1"}]},{"type":"text","value":", vilket är lika med 3."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om argumentet är 5 är returvärdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(4) + fibonacci(3)"}]},{"type":"text","value":", vilket vi nu vet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"3 + 2"}]},{"type":"text","value":", vilket är lika med 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Och så vidare, och så vidare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan i varje steg verifiera att funktionen ger rätt resultat, vilket ofta är tillräckligt i grundläggande programmeringsuppgifter. Den formella verifierbarheten av algoritmer är ett ämne för mer avancerade kurser, till exempel Data Structures and Algorithms."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Rekursiv summa","tmcname":"osa11-14_rekursiv_summa"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en rekursiv funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa(tal: int)"}]},{"type":"text","value":", som räknar summan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 + 2 + ... + tal"}]},{"type":"text","value":". Funktionens mall är följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ifall talet är 1, finns det inget att tillägga"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" tal\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# fyll i resten"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Några exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"6\n15\n55"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Balanserade parenteser","tmcname":"osa11-15_balanserade_parenteser"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns den färdiga funtkionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balanserade_parenteser"}]},{"type":"text","value":" som tar en sträng som argument. Funktionen kollar ifall "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"runda"}]},{"type":"text","value":" parenteser inom strängen är balanserade. Med andra ord, för varje öppnande parentes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":" ska det finnas en stängande parentes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":", och alla parenteser ska vara i matchande ordning, alltså får parentesparen inte korsas."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"balanserade_parenteser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'('"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"')'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ta bort första och sista tecknet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"(((())))\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# finns en parentes för mycket, alltså False"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"()())\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# börjar med en stängande parentes, alltså False igen"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\")()\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# är inte giltig, eftersom funktionen bara kan hantera helt inkapslade parenteser"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"()(())\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse\nFalse\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utöka funktionen så att den även fungerar med hakparenteser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":". Funktionen bör också ignorera alla tecken som inte är parenteser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"()"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":". De olika typerna av parenteser måste matchas korrekt i tur och ordning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett par exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"([([])])\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"(python version [3.7]) använd denna!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# felaktigt stängande parentes"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"(()]\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# felaktiga matchningar av parenteser"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanserade_parenteser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"([dåligt)]\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs: Funktionen behöver endast hantera helt inkapslade parenteser. Strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(x+1)(y+1)"}]},{"type":"text","value":" ska producera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", eftersom parenteserna inte är inkapslade inom varandra."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nFalse\nFalse"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"binar-sokning","style":"position:relative;"},"children":[{"type":"text","value":"Binär sökning"},{"type":"element","tagName":"a","properties":{"href":"#binar-sokning","ariaLabel":"binar sokning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I en binär sökning har vi en sorterad lista med objekt och vi försöker hitta ett visst objekt i den. Ordningen på objekten kan till exempel vara siffror från minst till störst, eller strängar från alfabetiskt först till sist. Sorteringsmetoden spelar ingen roll, så länge den är känd och relevant för det objekt som vi försöker hitta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tanken med en binär sökning är att alltid titta på objektet i mitten av listan. Vi har då tre möjliga scenarier. Om objektet i mitten är"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"det vi letar efter: vi kan returnera en indikation på att vi hittade objektet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"mindre än det vi letar efter: vi kan göra om sökningen i den större halvan av listan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"större än den vi letar efter: vi kan göra om sökningen i den mindre halvan av listan."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om listan är tom kan vi fastställa att objektet inte hittades och returnera en indikation på det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande bild kan vi se hur en binär sökning fortskrider när den letar efter talet 24:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 777px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/33f03fd48c215b216b48b8d09c1f757a/108f8/11_3_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 43.47826086956522%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAA7CAAAOwgEVKEqAAAABkUlEQVQoz52SS0sbURTH8+mkfgBXhboQqlBaXIhLlxZcCBZcdCFFqILGF4oiao2BPkhUfEdBE2PGzCRtmEwmM8kkM/f+ytzm4aJU6IH/PZzXn3PPORH+S6R6DcMgm82i6wZCCOWLCCkJRBdCaYEvAqWDlv0UUkoF3/fxPI9Go6FsRdg3u8Gb5S8MRnd5Hd1haGGfVxuL9PwYY2BzmbfROMNrMd6txhhZjzMU3SN+p6niX6USpmliWdafvqUkcvJY5KpQImWYXOctrnST0595EpUUSV0jkS2QzBkc5goca0USDwZF21UEjuPgui71er1LOBk/4uPXS8YTu4zezDCV/MbM9xQfYufolvPPSYadlctlbNvuEs4dp1g5yfDp4pDp+03mLs5YOr1l9uiSB7OiEv3OLFtzbs3LsiqKtFqtdgk9P8CRLjXh0fT+ss9W8XP+zlJeft7ixcF7evcn6J/fJnabU4FmEHSSnha3NxyKpmlkMhnS6XTn2xG94pCvmQoF26XW9J/trB0Lzya8v7YOCX8DpFagRpKgRFAAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/33f03fd48c215b216b48b8d09c1f757a/a0b58/11_3_1.webp 230w","/static/33f03fd48c215b216b48b8d09c1f757a/bc10c/11_3_1.webp 460w","/static/33f03fd48c215b216b48b8d09c1f757a/2e4ba/11_3_1.webp 777w"],"sizes":"(max-width: 777px) 100vw, 777px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/33f03fd48c215b216b48b8d09c1f757a/81c8e/11_3_1.png 230w","/static/33f03fd48c215b216b48b8d09c1f757a/08a84/11_3_1.png 460w","/static/33f03fd48c215b216b48b8d09c1f757a/108f8/11_3_1.png 777w"],"sizes":"(max-width: 777px) 100vw, 777px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/33f03fd48c215b216b48b8d09c1f757a/108f8/11_3_1.png","alt":"11 3 1","title":"11 3 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är en rekursiv algoritm för en binär sökning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"binarsokning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vanster "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hoger "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Funktionen returnerar True ifall föremålet finns i listan, annars False \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Om sökfältet är tomt hittas inget föremål"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" vanster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" hoger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kalkylerar mitten av sökområdet"}]},{"type":"text","value":"\n mitten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vanster"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"hoger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"//"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall föremålet hittas i mitten"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"mitten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall föremålet är större, sök andra halvan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"mitten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" binarsokning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mitten"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hoger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Annars måste föremålet vara mindre, sök mindre halvan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" binarsokning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vanster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mitten"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Testar"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"18"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"binarsokning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"binarsokning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"binarsokning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"binarsokning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nFalse\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"binar_sokning"}]},{"type":"text","value":" tar fyra argument: mållistan, det objekt som söks samt vänster och höger kant på sökområdet. När funktionen anropas första gången täcker sökområdet hela mållistan. Den vänstra kanten ligger på index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" och den högra kanten ligger på index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(lista)-1"}]},{"type":"text","value":". Funktionen beräknar det centrala indexet och kontrollerar den positionen på listan. Antingen har objektet hittats eller så fortsätter sökningen till den mindre eller större halvan av mållistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss jämföra detta med en enkel linjär sökning. Vid en linjär sökning är sökområdet från början och framåt, tills antingen objektet hittas eller sökområdet tar slut. Antalet steg som behövs för att täcka hela sökområdet växer linjärt i samma takt som sökområdets storlek. Varje söksteg täcker endast en sökkandidat från början av sökområdet. Låt oss anta att det sökta objektet inte hittas. Om sökområdet är en miljon objekt långt måste vi ta en miljon söksteg för att försäkra oss om att objektet inte finns i sökområdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vid en binär sökning växer däremot antalet steg som behövs logaritmiskt. Låt oss återigen anta att det sökta objektet inte hittas. Sökområdet halveras för varje steg, eftersom vi vet att objektet antingen är mindre eller större än den aktuella sökkandidaten i mitten. 2 gånger 20 (2^20) är redan långt över 1 miljon, så det tar som mest 20 steg att täcka hela sökområdet med en binär sökning. När vi har att göra med sorterade sökområden, vilket ofta är fallet när vi har att göra med datorer och material som ska bearbetas automatiskt, är en binär sökning alltså mycket effektivare än en linjär sökning."}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad rekursion innebär
  • \n
  • Kommer du att kunna skriva en enkel rekursiv funktion
  • \n

Som vi har sett många gånger tidigare kan funktioner anropa andra funktioner. Till exempel:

def hej(namn : str):\n    print(\"Hejsan,\", namn)\n\ndef hej_flera(namn : str, ganger : int):\n    for i in range(ganger):\n        hej(namn)

En funktion kan också anropa sig själv, men vi som programmerare måste vara försiktiga när vi gör det. Det är lätt att hamna i en oändlig loop av funktionsanrop, precis som vi hamnade i en oändlig loop av upprepningar med while-loopar om vi utelämnade lämpliga brytvillkor. Så om man försöker anropa en hej-funktion med följande definition

def hej(namn : str):\n    print(\"Hejsan,\", namn)\n    hej(namn)

skulle skapa ett nytt sort av fel:

RecursionError: maximum recursion depth exceeded

Vad betyder rekursion?

Den rekursion som nämns i felet ovan innebär att man definierar något i termer av sig självt. I programmeringssammanhang handlar det oftast om en funktion som anropar sig själv. För att detta ska fungera utan att orsaka oändliga loopar måste de argument som skickas till funktionen ändras varje gång, så att de nästlade funktionsanropen slutar vid något skede. Grundprincipen här är densamma som i while-loopar: det måste alltid finnas ett stoppvillkor av något slag, och det villkoret måste utlösas vid någon tidpunkt i exekveringen.

Låt oss ta en titt på en enkel funktion som lägger till nollor i en lista så länge det finns färre än 10 objekt i listan. Den här gången använder vi dock inte en loop. Om villkoret ännu inte är uppfyllt anropar funktionen sig själv

def fyll_lista(tal: list):\n    \"\"\" Lägger till föremål till listan ifall listans längd är kortare än 10 \"\"\"\n    if len(tal) < 10:\n        tal.append(0)\n        # anropa funktionen igen\n        fyll_lista(tal)\n\n\nif __name__ == \"__main__\":\n    test = [1,2,3,4]\n    fyll_lista(test)\n    print(test)

[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]

Denna funktionalitet kunde lika väl bli uppnådd genom en vanlig while-loop:

\ndef fyll_lista(tal: list):\n    \"\"\" Lägger till föremål till listan ifall listans längd är kortare än 10 \"\"\"\n    while len(tal) < 10:\n        tal.append(0)\n\nif __name__ == \"__main__\":\n    test = [1,2,3,4]\n    fyll_lista(test)\n    print(test)\n

Det mer traditionella iterativa tillvägagångssättet ger ett kortare program som förmodligen också är lättare att förstå. Med den rekursiva versionen är det inte lika tydligt att vi under hela processen arbetar med exakt samma lista. Så är det dock och därför fungerar den rekursiva funktionen lika bra.

Inom datavetenskapsteori skiljer man ofta mellan iterativa och rekursiva algoritmer, så det är bäst att bekanta sig med dessa termer redan från början. Iterativa lösningar är sådana som baseras på sekventiell bearbetning av objekt, ofta med hjälp av loopar. Hittills har vi behandlat iterativa metoder ganska exklusivt. Rekursiv, å andra sidan, avser en metod där funktionen anropar sig själv med förändrade parametervärden.

I princip borde det vara möjligt att lösa alla problem med antingen iterativa eller rekursiva metoder. I praktiken är det dock oftast så att den ena eller den andra metoden är klart bättre lämpad för varje problem. Förmågan att avgöra vilken som är bäst kommer till stor del med övning.

Skapa en rekursiv funktion tal_till_listan(tal: list). Funktionen tar en lista med tal som sitt argument och lägger till nya tal i listan tills längden på listan är delbar med fem. Varje tal som läggs till i listan ska vara ett större tal än det sista talet i listan.

Funktionen måste anropa sig själv rekursivt.

Fxempel på funktionen i användning:

tal = [1,3,4,5,10,11]\ntal_till_listan(tal)\nprint(tal)

[1, 3, 4, 5, 10, 11, 12, 13, 14, 15]

Rekursion och returvärden

Rekursiva funktioner kan också ha returvärden. I de senaste avsnitten har vi arbetat med fakultettal, så låt oss skriva en rekursiv fakultetfunktion:

\ndef fakultet(n: int):\n    \"\"\" Funktionen räknar ut fakulteten n! för n>= 0 \"\"\"\n    if n < 2:\n        # Fakulteten av 0 och 1 är 1\n        return 1\n\n    # anropa funktionen igen\n    return n * fakultet(n - 1)\n\nif __name__ == \"__main__\":\n    # Testar\n    for i in range(1, 7):\n        print(f\"Fakulteten av {i} är {fakultet(i)}\")\n

Fakulteten av 1 är 1\nFakulteten av 2 är 2\nFakulteten av 3 är 6\nFakulteten av 4 är 24\nFakulteten av 5 är 120\nFakulteten av 6 är 720

Om parametern för den rekursiva faktoriella funktionen är 0 eller 1, returnerar funktionen 1, eftersom det är så faktoriell operation definieras. I alla andra fall returnerar funktionen värdet n * fakultet(n - 1), vilket är värdet av dess parameter n multiplicerat med returvärdet av funktionsanropet fakultet(n - 1).

Det avgörande här är att funktionsdefinitionen innehåller ett stoppvillkor. Om detta uppfylls avslutas rekursionen. I det här fallet är villkoret n < 2. Vi vet att det kommer att nås så småningom, eftersom det värde som skickas som argument till funktionen minskas med ett på varje nivå i rekursionen.

Visualiseringsverktyget kan vara till stor hjälp när det gäller att förstå rekursiva program.

Exemplet ovan skulle kanske bli lite tydligare om vi använde oss av hjälpvariabler:

def fakultet(n: int):\n    if n < 2:\n        return 1\n\n    forra_talets_fakultet = fakultet(n - 1)\n    fakultet_nu = n * forra_talets_fakultet\n    return fakultet_nu\n\nfakultet(5)

Ta en titt på hurvisualiseringsverktyget visar hur rekursionen fortskrider.

Visualiseringsverktyget har en liten finess i hur det hanterar anropsstacken, eftersom den verkar \"växa\" nedåt. Vanligtvis avbildas anropsstaplar som just staplar, där de nya anropen placeras överst. I visualiseringsverktyget är det aktiva funktionsanropet det skuggade blocket längst ner, som har sina egna kopior av de variabler som syns.

När den rekursiva faktorfunktionen anropas byggs anropsstapeln upp tills den gräns som utgörs av n < 2 nås. Då återkommer det sista funktionsanropet i stacken med ett värde - det är 1, eftersom n nu är mindre än 2. Detta återkomstvärde skickas till det föregående funktionsanropet i stacken, där det används för att beräkna det funktionsanropets återkomstvärde, och så vidare tillbaka ut ur stacken.

Returvärdet för varje funktionsanrop lagras i hjälpvariabeln fakultet_nu. Gå igenom visualiseringen noggrant tills du förstår vad som händer i varje steg, och var särskilt uppmärksam på det värde som returneras i varje steg.

\n \n \n \n \n \n \"11\n \n \n

Låt oss ta en titt på ett annat vanligt rekursivt exempel: Fibonacci-talen. I en Fibonacci-sekvens är varje tal summan av de två föregående talen. De två första talen definieras här som 1 och 1, och sekvensen börjar då så här: 1, 1, 2, 3, 5, 8, 13, 21, 34.

def fibonacci(n: int):\n    \"\"\" Funktionen returnerar det n:e talet i Fibonaccis talföljd \"\"\"\n\n    if n <= 2:\n        # de första två är ettor\n        return 1\n\n    # Alla andra tal blir summan av de två föregående talen\n    return fibonacci(n - 1) + fibonacci(n - 2)\n\n# Testar att allting fungerar\nif __name__ == \"__main__\":\n    for i in range(1, 11):\n        print(f\"Fibonaccis {i}. tal är {fibonacci(i)}\")

Fibonaccis 1. tal är 1\nFibonaccis 2. tal är 1\nFibonaccis 3. tal är 2\nFibonaccis 4. tal är 3\nFibonaccis 5. tal är 5\nFibonaccis 6. tal är 8\nFibonaccis 7. tal är 13\nFibonaccis 8. tal är 21\nFibonaccis 9. tal är 34\nFibonaccis 10. tal är 55

Den här gången är stoppvillkoret att parametern är mindre än eller lika med 2, eftersom hela sekvensen definieras från de två första siffrorna och framåt, och vi definierade de två första siffrorna som lika med 1.

Hur fungerar då den här funktionen i praktiken?

Om funktionen anropas med 1 eller 2 som argument returnerar den 1, vilket dikteras av villkoret n <= 2.

Om argumentet är 3 eller större returnerar funktionen värdet av fibonacci(n - 1) + fibonacci(n - 2). Om argumentet är exakt 3 är detta värde lika med fibonacci(2) + fibonacci(1), och vi vet redan resultatet av båda dessa från föregående steg. 1 + 1 är lika med 2, som alltså är det tredje talet i Fibonacci-sekvensen.

Om argumentet är 4 är returvärdet fibonacci(3) + fibonacci(2), som vi nu vet är 2 + 1, vilket är lika med 3.

Om argumentet är 5 är returvärdet fibonacci(4) + fibonacci(3), vilket vi nu vet är 3 + 2, vilket är lika med 5.

Och så vidare, och så vidare.

Vi kan i varje steg verifiera att funktionen ger rätt resultat, vilket ofta är tillräckligt i grundläggande programmeringsuppgifter. Den formella verifierbarheten av algoritmer är ett ämne för mer avancerade kurser, till exempel Data Structures and Algorithms.

Skapa en rekursiv funktion med namnet summa(tal: int), som räknar summan 1 + 2 + ... + tal. Funktionens mall är följande:

def summa(tal: int):\n    # ifall talet är 1, finns det inget att tillägga\n    if tal <= 1:\n        return tal\n\n    # fyll i resten

Några exempel:

resultat = summa(3)\nprint(resultat)\n\nprint(summa(5))\nprint(summa(10))

6\n15\n55

I uppgiftsbotten finns den färdiga funtkionen balanserade_parenteser som tar en sträng som argument. Funktionen kollar ifall runda parenteser inom strängen är balanserade. Med andra ord, för varje öppnande parentes ( ska det finnas en stängande parentes ), och alla parenteser ska vara i matchande ordning, alltså får parentesparen inte korsas.

def balanserade_parenteser(strang: str):\n    if len(strang) == 0:\n        return True\n    if not (strang[0] == '(' and strang[-1] == ')'):\n        return False\n\n    # ta bort första och sista tecknet\n    return balanserade_parenteser(strang[1:-1])\n\nok = balanserade_parenteser(\"(((())))\")\nprint(ok)\n\n# finns en parentes för mycket, alltså False\nok = balanserade_parenteser(\"()())\")\nprint(ok)\n\n# börjar med en stängande parentes, alltså False igen\nok = balanserade_parenteser(\")()\")\nprint(ok)\n\n# är inte giltig, eftersom funktionen bara kan hantera helt inkapslade parenteser\nok = balanserade_parenteser(\"()(())\")\nprint(ok)

True\nFalse\nFalse\nFalse

Utöka funktionen så att den även fungerar med hakparenteser []. Funktionen bör också ignorera alla tecken som inte är parenteser () eller []. De olika typerna av parenteser måste matchas korrekt i tur och ordning.

Ett par exempel:

ok = balanserade_parenteser(\"([([])])\")\nprint(ok)\n\nok = balanserade_parenteser(\"(python version [3.7]) använd denna!\")\nprint(ok)\n\n# felaktigt stängande parentes\nok = balanserade_parenteser(\"(()]\")\nprint(ok)\n\n\n# felaktiga matchningar av parenteser\nok = balanserade_parenteser(\"([dåligt)]\")\nprint(ok)

Obs: Funktionen behöver endast hantera helt inkapslade parenteser. Strängen (x+1)(y+1) ska producera False, eftersom parenteserna inte är inkapslade inom varandra.

True\nTrue\nFalse\nFalse

Binär sökning

I en binär sökning har vi en sorterad lista med objekt och vi försöker hitta ett visst objekt i den. Ordningen på objekten kan till exempel vara siffror från minst till störst, eller strängar från alfabetiskt först till sist. Sorteringsmetoden spelar ingen roll, så länge den är känd och relevant för det objekt som vi försöker hitta.

Tanken med en binär sökning är att alltid titta på objektet i mitten av listan. Vi har då tre möjliga scenarier. Om objektet i mitten är

    \n
  • det vi letar efter: vi kan returnera en indikation på att vi hittade objektet
  • \n
  • mindre än det vi letar efter: vi kan göra om sökningen i den större halvan av listan
  • \n
  • större än den vi letar efter: vi kan göra om sökningen i den mindre halvan av listan.
  • \n

Om listan är tom kan vi fastställa att objektet inte hittades och returnera en indikation på det.

I följande bild kan vi se hur en binär sökning fortskrider när den letar efter talet 24:

\n \n

Efter den här delen

    \n
  • Vet du vad rekursion innebär
  • \n
  • Kommer du att kunna skriva en enkel rekursiv funktion
  • \n

Som vi har sett många gånger tidigare kan funktioner anropa andra funktioner. Till exempel:

def hej(namn : str):\n    print(\"Hejsan,\", namn)\n\ndef hej_flera(namn : str, ganger : int):\n    for i in range(ganger):\n        hej(namn)

En funktion kan också anropa sig själv, men vi som programmerare måste vara försiktiga när vi gör det. Det är lätt att hamna i en oändlig loop av funktionsanrop, precis som vi hamnade i en oändlig loop av upprepningar med while-loopar om vi utelämnade lämpliga brytvillkor. Så om man försöker anropa en hej-funktion med följande definition

def hej(namn : str):\n    print(\"Hejsan,\", namn)\n    hej(namn)

skulle skapa ett nytt sort av fel:

RecursionError: maximum recursion depth exceeded

Vad betyder rekursion?

Den rekursion som nämns i felet ovan innebär att man definierar något i termer av sig självt. I programmeringssammanhang handlar det oftast om en funktion som anropar sig själv. För att detta ska fungera utan att orsaka oändliga loopar måste de argument som skickas till funktionen ändras varje gång, så att de nästlade funktionsanropen slutar vid något skede. Grundprincipen här är densamma som i while-loopar: det måste alltid finnas ett stoppvillkor av något slag, och det villkoret måste utlösas vid någon tidpunkt i exekveringen.

Låt oss ta en titt på en enkel funktion som lägger till nollor i en lista så länge det finns färre än 10 objekt i listan. Den här gången använder vi dock inte en loop. Om villkoret ännu inte är uppfyllt anropar funktionen sig själv

def fyll_lista(tal: list):\n    \"\"\" Lägger till föremål till listan ifall listans längd är kortare än 10 \"\"\"\n    if len(tal) < 10:\n        tal.append(0)\n        # anropa funktionen igen\n        fyll_lista(tal)\n\n\nif __name__ == \"__main__\":\n    test = [1,2,3,4]\n    fyll_lista(test)\n    print(test)

[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]

Denna funktionalitet kunde lika väl bli uppnådd genom en vanlig while-loop:

\ndef fyll_lista(tal: list):\n    \"\"\" Lägger till föremål till listan ifall listans längd är kortare än 10 \"\"\"\n    while len(tal) < 10:\n        tal.append(0)\n\nif __name__ == \"__main__\":\n    test = [1,2,3,4]\n    fyll_lista(test)\n    print(test)\n

Det mer traditionella iterativa tillvägagångssättet ger ett kortare program som förmodligen också är lättare att förstå. Med den rekursiva versionen är det inte lika tydligt att vi under hela processen arbetar med exakt samma lista. Så är det dock och därför fungerar den rekursiva funktionen lika bra.

Inom datavetenskapsteori skiljer man ofta mellan iterativa och rekursiva algoritmer, så det är bäst att bekanta sig med dessa termer redan från början. Iterativa lösningar är sådana som baseras på sekventiell bearbetning av objekt, ofta med hjälp av loopar. Hittills har vi behandlat iterativa metoder ganska exklusivt. Rekursiv, å andra sidan, avser en metod där funktionen anropar sig själv med förändrade parametervärden.

I princip borde det vara möjligt att lösa alla problem med antingen iterativa eller rekursiva metoder. I praktiken är det dock oftast så att den ena eller den andra metoden är klart bättre lämpad för varje problem. Förmågan att avgöra vilken som är bäst kommer till stor del med övning.

Skapa en rekursiv funktion tal_till_listan(tal: list). Funktionen tar en lista med tal som sitt argument och lägger till nya tal i listan tills längden på listan är delbar med fem. Varje tal som läggs till i listan ska vara ett större tal än det sista talet i listan.

Funktionen måste anropa sig själv rekursivt.

Fxempel på funktionen i användning:

tal = [1,3,4,5,10,11]\ntal_till_listan(tal)\nprint(tal)

[1, 3, 4, 5, 10, 11, 12, 13, 14, 15]

Rekursion och returvärden

Rekursiva funktioner kan också ha returvärden. I de senaste avsnitten har vi arbetat med fakultettal, så låt oss skriva en rekursiv fakultetfunktion:

\ndef fakultet(n: int):\n    \"\"\" Funktionen räknar ut fakulteten n! för n>= 0 \"\"\"\n    if n < 2:\n        # Fakulteten av 0 och 1 är 1\n        return 1\n\n    # anropa funktionen igen\n    return n * fakultet(n - 1)\n\nif __name__ == \"__main__\":\n    # Testar\n    for i in range(1, 7):\n        print(f\"Fakulteten av {i} är {fakultet(i)}\")\n

Fakulteten av 1 är 1\nFakulteten av 2 är 2\nFakulteten av 3 är 6\nFakulteten av 4 är 24\nFakulteten av 5 är 120\nFakulteten av 6 är 720

Om parametern för den rekursiva faktoriella funktionen är 0 eller 1, returnerar funktionen 1, eftersom det är så faktoriell operation definieras. I alla andra fall returnerar funktionen värdet n * fakultet(n - 1), vilket är värdet av dess parameter n multiplicerat med returvärdet av funktionsanropet fakultet(n - 1).

Det avgörande här är att funktionsdefinitionen innehåller ett stoppvillkor. Om detta uppfylls avslutas rekursionen. I det här fallet är villkoret n < 2. Vi vet att det kommer att nås så småningom, eftersom det värde som skickas som argument till funktionen minskas med ett på varje nivå i rekursionen.

Visualiseringsverktyget kan vara till stor hjälp när det gäller att förstå rekursiva program.

Exemplet ovan skulle kanske bli lite tydligare om vi använde oss av hjälpvariabler:

def fakultet(n: int):\n    if n < 2:\n        return 1\n\n    forra_talets_fakultet = fakultet(n - 1)\n    fakultet_nu = n * forra_talets_fakultet\n    return fakultet_nu\n\nfakultet(5)

Ta en titt på hurvisualiseringsverktyget visar hur rekursionen fortskrider.

Visualiseringsverktyget har en liten finess i hur det hanterar anropsstacken, eftersom den verkar \"växa\" nedåt. Vanligtvis avbildas anropsstaplar som just staplar, där de nya anropen placeras överst. I visualiseringsverktyget är det aktiva funktionsanropet det skuggade blocket längst ner, som har sina egna kopior av de variabler som syns.

När den rekursiva faktorfunktionen anropas byggs anropsstapeln upp tills den gräns som utgörs av n < 2 nås. Då återkommer det sista funktionsanropet i stacken med ett värde - det är 1, eftersom n nu är mindre än 2. Detta återkomstvärde skickas till det föregående funktionsanropet i stacken, där det används för att beräkna det funktionsanropets återkomstvärde, och så vidare tillbaka ut ur stacken.

Returvärdet för varje funktionsanrop lagras i hjälpvariabeln fakultet_nu. Gå igenom visualiseringen noggrant tills du förstår vad som händer i varje steg, och var särskilt uppmärksam på det värde som returneras i varje steg.

\n \n \n \n \n \n \"11\n \n \n

Låt oss ta en titt på ett annat vanligt rekursivt exempel: Fibonacci-talen. I en Fibonacci-sekvens är varje tal summan av de två föregående talen. De två första talen definieras här som 1 och 1, och sekvensen börjar då så här: 1, 1, 2, 3, 5, 8, 13, 21, 34.

def fibonacci(n: int):\n    \"\"\" Funktionen returnerar det n:e talet i Fibonaccis talföljd \"\"\"\n\n    if n <= 2:\n        # de första två är ettor\n        return 1\n\n    # Alla andra tal blir summan av de två föregående talen\n    return fibonacci(n - 1) + fibonacci(n - 2)\n\n# Testar att allting fungerar\nif __name__ == \"__main__\":\n    for i in range(1, 11):\n        print(f\"Fibonaccis {i}. tal är {fibonacci(i)}\")

Fibonaccis 1. tal är 1\nFibonaccis 2. tal är 1\nFibonaccis 3. tal är 2\nFibonaccis 4. tal är 3\nFibonaccis 5. tal är 5\nFibonaccis 6. tal är 8\nFibonaccis 7. tal är 13\nFibonaccis 8. tal är 21\nFibonaccis 9. tal är 34\nFibonaccis 10. tal är 55

Den här gången är stoppvillkoret att parametern är mindre än eller lika med 2, eftersom hela sekvensen definieras från de två första siffrorna och framåt, och vi definierade de två första siffrorna som lika med 1.

Hur fungerar då den här funktionen i praktiken?

Om funktionen anropas med 1 eller 2 som argument returnerar den 1, vilket dikteras av villkoret n <= 2.

Om argumentet är 3 eller större returnerar funktionen värdet av fibonacci(n - 1) + fibonacci(n - 2). Om argumentet är exakt 3 är detta värde lika med fibonacci(2) + fibonacci(1), och vi vet redan resultatet av båda dessa från föregående steg. 1 + 1 är lika med 2, som alltså är det tredje talet i Fibonacci-sekvensen.

Om argumentet är 4 är returvärdet fibonacci(3) + fibonacci(2), som vi nu vet är 2 + 1, vilket är lika med 3.

Om argumentet är 5 är returvärdet fibonacci(4) + fibonacci(3), vilket vi nu vet är 3 + 2, vilket är lika med 5.

Och så vidare, och så vidare.

Vi kan i varje steg verifiera att funktionen ger rätt resultat, vilket ofta är tillräckligt i grundläggande programmeringsuppgifter. Den formella verifierbarheten av algoritmer är ett ämne för mer avancerade kurser, till exempel Data Structures and Algorithms.

Skapa en rekursiv funktion med namnet summa(tal: int), som räknar summan 1 + 2 + ... + tal. Funktionens mall är följande:

def summa(tal: int):\n    # ifall talet är 1, finns det inget att tillägga\n    if tal <= 1:\n        return tal\n\n    # fyll i resten

Några exempel:

resultat = summa(3)\nprint(resultat)\n\nprint(summa(5))\nprint(summa(10))

6\n15\n55

I uppgiftsbotten finns den färdiga funtkionen balanserade_parenteser som tar en sträng som argument. Funktionen kollar ifall runda parenteser inom strängen är balanserade. Med andra ord, för varje öppnande parentes ( ska det finnas en stängande parentes ), och alla parenteser ska vara i matchande ordning, alltså får parentesparen inte korsas.

def balanserade_parenteser(strang: str):\n    if len(strang) == 0:\n        return True\n    if not (strang[0] == '(' and strang[-1] == ')'):\n        return False\n\n    # ta bort första och sista tecknet\n    return balanserade_parenteser(strang[1:-1])\n\nok = balanserade_parenteser(\"(((())))\")\nprint(ok)\n\n# finns en parentes för mycket, alltså False\nok = balanserade_parenteser(\"()())\")\nprint(ok)\n\n# börjar med en stängande parentes, alltså False igen\nok = balanserade_parenteser(\")()\")\nprint(ok)\n\n# är inte giltig, eftersom funktionen bara kan hantera helt inkapslade parenteser\nok = balanserade_parenteser(\"()(())\")\nprint(ok)

True\nFalse\nFalse\nFalse

Utöka funktionen så att den även fungerar med hakparenteser []. Funktionen bör också ignorera alla tecken som inte är parenteser () eller []. De olika typerna av parenteser måste matchas korrekt i tur och ordning.

Ett par exempel:

ok = balanserade_parenteser(\"([([])])\")\nprint(ok)\n\nok = balanserade_parenteser(\"(python version [3.7]) använd denna!\")\nprint(ok)\n\n# felaktigt stängande parentes\nok = balanserade_parenteser(\"(()]\")\nprint(ok)\n\n\n# felaktiga matchningar av parenteser\nok = balanserade_parenteser(\"([dåligt)]\")\nprint(ok)

Obs: Funktionen behöver endast hantera helt inkapslade parenteser. Strängen (x+1)(y+1) ska producera False, eftersom parenteserna inte är inkapslade inom varandra.

True\nTrue\nFalse\nFalse

Binär sökning

I en binär sökning har vi en sorterad lista med objekt och vi försöker hitta ett visst objekt i den. Ordningen på objekten kan till exempel vara siffror från minst till störst, eller strängar från alfabetiskt först till sist. Sorteringsmetoden spelar ingen roll, så länge den är känd och relevant för det objekt som vi försöker hitta.

Tanken med en binär sökning är att alltid titta på objektet i mitten av listan. Vi har då tre möjliga scenarier. Om objektet i mitten är

    \n
  • det vi letar efter: vi kan returnera en indikation på att vi hittade objektet
  • \n
  • mindre än det vi letar efter: vi kan göra om sökningen i den större halvan av listan
  • \n
  • större än den vi letar efter: vi kan göra om sökningen i den mindre halvan av listan.
  • \n

Om listan är tom kan vi fastställa att objektet inte hittades och returnera en indikation på det.

I följande bild kan vi se hur en binär sökning fortskrider när den letar efter talet 24:

\n \n "}]},{"type":"text","value":" rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" sok_nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" sok_nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Chefer och underordnade","tmcname":"osa11-17_chefer_och_underordnade"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Arbetare"}]},{"type":"text","value":" modellerar en arbetare på ett företag:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Arbetare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"underordnade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_underordnad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Arbetare'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"underordnade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rakna_underordnade(arbetare: Arbetare)"}]},{"type":"text","value":", som rekursivt räknar mängden underordnade som en arbetare har."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på funktionen i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n a1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sussi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a6 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kjell\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a6"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_underordnade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_underordnade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_underordnade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3\n0"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"besok-till-tiden-innan-rekursion","style":"position:relative;"},"children":[{"type":"text","value":"Besök till tiden innan rekursion"},{"type":"element","tagName":"a","properties":{"href":"#besok-till-tiden-innan-rekursion","ariaLabel":"besok till tiden innan rekursion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss avsluta denna del av materialet med en lite större övning som koncentrerar sig på objektorienterade programmeringsprinciper. Vi rekommenderar inte att du använder rekursion i denna serie av uppgifter, men tekniker för list comprehension kommer att vara användbara."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Uppgiftsbok","tmcname":"osa11-18_uppgiftsbok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen kommer du skriva två olika klasser, vilka i sin tur kommer att vara ryggraden för övningen som följer denna, i vilken du kommer att skapa en interaktiv applikation."}]},{"type":"element","tagName":"h2","properties":{"id":"uppgift","style":"position:relative;"},"children":[{"type":"text","value":"Uppgift"},{"type":"element","tagName":"a","properties":{"href":"#uppgift","ariaLabel":"uppgift permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förverkliga klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgift"}]},{"type":"text","value":", som modellerar en uppgift i ett mjukvaruföretags lista av uppgifter. Uppgifterna har"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en beskrivning"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en beräkning av mängden timmar uppgiften tar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"namnet på programmeraren som har fått uppgiften"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett fält som håller koll på ifall uppgiften är gjord"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en unik id"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda hello world\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beskrivning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"programmerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arbetsmangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda webbutik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda mobilapp för räknande av arbetsmängd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 koda hello world Erik 3\n1: koda hello world (3 timmar), programmerare Erik INTE FÄRDIG\nFalse\n1: koda hello world (3 timmar), programmerare Erik FÄRDIG\nTrue\n2: koda webbutik (10 timmar), programmerare Anton INTE FÄRDIG\n3: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik INTE FÄRDIG"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klarifikationer:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"uppgiftens läge (färdig eller inte färdig) kan kollas med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar_fardig(self)"}]},{"type":"text","value":", som returnerar ett booleskt värde"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"uppgiften är inte färdig när den skapas"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"uppgiften markeras som färdig genom att anropa på metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"markera_fardig(self)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"uppgifternas id är ett löpande nummer som börjar med 1. Id av första uppgiften är 1, andra uppgiften 2 osv."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" id kan implementeras med en "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/5-klassattribut","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"klassvariabel"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"uppgiftsbok","style":"position:relative;"},"children":[{"type":"text","value":"Uppgiftsbok"},{"type":"element","tagName":"a","properties":{"href":"#uppgiftsbok","ariaLabel":"uppgiftsbok permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa nu en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftsbok"}]},{"type":"text","value":", som samlar ihop alla uppgifter som delegerats av mjukvaruföretaget. Uppgifterna ska modelleras med klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgift"}]},{"type":"text","value":" som du just gjorde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Basversionen av Uppgiftsboken används enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bestallningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda webbutik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda mobilapp för räknande av arbetsmängd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda app för övande av matematik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bestallning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alla_bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" programmerare "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kodare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"programmerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1: koda webbutik (10 timmar), programmerare Anton INTE FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik INTE FÄRDIG\n3: koda app för övande av matematik (100 timmar), programmerare Anton INTE FÄRDIG"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anton\nErik"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I detta skede borde din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftsbok"}]},{"type":"text","value":" erbjuda tre metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_bestallning(self, beskrivning, programmerare, arbetsmangd)"}]},{"type":"text","value":", som lägger till en ny uppgift till Uppgiftsboken. En Uppgiftsbok förvarar uppgifterna internt som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgift"}]},{"type":"text","value":"-objekt. OBS: metoden ska ta exakt de argument som nämns eller så fungerar inte de automatiserade testerna korrekt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alla_bestallningar(self)"}]},{"type":"text","value":" returnerar en lista på alla uppgifter som lagras i en Uppgiftsbok"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kodare(self)"}]},{"type":"text","value":" returnerar en lista på namnen av alla kodare som har uppgifter lagrade i Uppgiftsboken. Listan ska innehålla varje programmerare endast en gång."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" En enkel metod för att göra sig av med multipler är att ursprungligen hantera listan som en "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3.8/library/stdtypes.html#set","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"mängd"}]},{"type":"text","value":" (eng: set). En mängd är en samling av saker där varje unikt föremål endast förekommer en gång. En "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set"}]},{"type":"text","value":" kan sedan konverteras tillbaks till en lista, och vi kan då vara säkra på att varje föremål nu är unikt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"set"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 1, 3, 6, 4, 1, 3]\n[1, 3, 4, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"nagra-fler-funktioner-for-uppgiftsboken","style":"position:relative;"},"children":[{"type":"text","value":"Några fler funktioner för Uppgiftsboken"},{"type":"element","tagName":"a","properties":{"href":"#nagra-fler-funktioner-for-uppgiftsboken","ariaLabel":"nagra fler funktioner for uppgiftsboken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa tre till metoder till din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftsbok"}]},{"type":"text","value":"-klass."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"markera_fardig(self, id: int)"}]},{"type":"text","value":" får som argument uppgiftens id och markerar den relevanta uppgiften som färdig:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bestallningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda webbutik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda mobilapp för räknande av arbetsmängd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda app för övande av matematik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bestallning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alla_bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1: koda webbutik (10 timmar), programmerare Anton FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik FÄRDIG\n3: koda app för övande av matematik (100 timmar), programmerare Anton INTE FÄRDIG"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall det inte finns någon uppgift med det givna id:t, ska metoden åstadkomma ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag. Ifall du behöver en påminnelse om att åstadkomma undantag kan du kolla in "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/3-fel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fardiga_bestallningar(self)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ofardiga_bestallningar(self)"}]},{"type":"text","value":" fungerar som förväntat: båda returnerar en lista innehållande de relevanta uppgifterna från Uppgiftsboken."}]},{"type":"element","tagName":"h2","properties":{"id":"projektets-sista-detaljer","style":"position:relative;"},"children":[{"type":"text","value":"Projektets sista detaljer"},{"type":"element","tagName":"a","properties":{"href":"#projektets-sista-detaljer","ariaLabel":"projektets sista detaljer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skapar en sista metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"programmerarens_status(self, programmerare: str)"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftsbok"}]},{"type":"text","value":"-klassen, vilken returnar en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tupel"}]},{"type":"text","value":". Tupeln ska innehålla antalet färdiga och ofärdiga uppgifter en programmerare har tilldelats samt den uppskattade mängden timmar i båda kategorierna."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bestallningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda webbutik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda mobilapp för räknande av arbetsmängd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda app för övande av matematik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda nya facebook\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstatus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"programmerarens_status"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"status"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(2, 1, 35, 100)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det första föremålet i tupeln är antalet "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"färdiga"}]},{"type":"text","value":" uppgifter, medan det andra föremålet är antalet "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ofärdiga"}]},{"type":"text","value":" uppgifter. Det tredje och fjärde föremålet är summan av uppskattningarna av arbetsmängden för de färdiga respektive ofärdiga uppgifterna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall det inte finns någon programmerare med det angivna namnet, ska metoden åstadkomma ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Uppgiftsboksapplikation","tmcname":"osa11-19_uppgiftsboksapplikation"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen ska du skapa en interaktiv applikation för att administrera de uppgifter som beställts från ett programvaruföretag. Implementeringen är helt upp till dig, men du kan använda byggstenarna från föregående övning i din applikation. Exemplen i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-10/4-storre-applikation","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"det sista avsnittet av modul 10"}]},{"type":"text","value":" kan också vara till hjälp."}]},{"type":"element","tagName":"h2","properties":{"id":"utan-felhantering","style":"position:relative;"},"children":[{"type":"text","value":"Utan felhantering"},{"type":"element","tagName":"a","properties":{"href":"#utan-felhantering","ariaLabel":"utan felhantering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Applikationen ska fungera "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exakt"}]},{"type":"text","value":" enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 tillsätt beställning\n2 lista färdiga\n3 lista ofärdiga\n4 markera uppgift färdig\n5 programmerare\n6 programmerarens status"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda nya facebook"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas 1000"}]},{"type":"text","value":"\ntillsatt!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda mobilapp för räknande av arbetsmängd"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"erik 25"}]},{"type":"text","value":"\ntillsatt!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program för att öva musikteori"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nina 12"}]},{"type":"text","value":"\ntillsatt!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda nya twitter"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas 55"}]},{"type":"text","value":"\ntillsatt!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ninga färdiga uppgifter"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1: koda nya facebook (1000 timmar), programmerare jonas INTE FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare erik INTE FÄRDIG\n3: ohjelma musiikin teorian harjoitteluun (12 timmar), programmerare nina INTE FÄRDIG\n4: koda nya twitter (55 timmar), programmerare jonas INTE FÄRDIG"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nmarkerad som färdig"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nmarkerad som färdig"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare erik FÄRDIG\n4: koda nya twitter (55 timmar), programmerare jonas FÄRDIG"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1: koda nya facebook (1000 timmar), programmerare jonas INTE FÄRDIG\n3: program för att öva musikteori (12 timmar), programmerare nina INTE FÄRDIG"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\njonas\nerik\nnina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nprogrammerare: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas"}]},{"type":"text","value":"\nuppgifter: färdiga 2 ofärdiga 1, timmar: gjorda 55 ogjorda 1000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den första övningspoängen ges för en fungerande applikation när alla användarinmatningar är felfria."}]},{"type":"element","tagName":"h2","properties":{"id":"hantering-av-inamtningsfel","style":"position:relative;"},"children":[{"type":"text","value":"Hantering av inamtningsfel"},{"type":"element","tagName":"a","properties":{"href":"#hantering-av-inamtningsfel","ariaLabel":"hantering av inamtningsfel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att få den andra övningspoängen för denna övning förväntas din applikation återhämta sig från felaktig användarinmatning. All inmatning som inte följer det angivna formatet ska ge ett felmeddelande "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"felaktig inmatning"}]},{"type":"text","value":" och resultera i ännu en upprepning av loopen med begäran om en ny instruktion:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda mobilapp för räknande av arbetsmängd"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"erik xxx"}]},{"type":"text","value":"\nfelaktig inmatning"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda mobilapp för räknande av arbetsmängd"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"erik"}]},{"type":"text","value":"\nfelaktig inmatning"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1000000"}]},{"type":"text","value":"\nfelaktig inmatning"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"XXXX"}]},{"type":"text","value":"\nfelaktig inmatning"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nprogrammerare: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"okändprogrammerare"}]},{"type":"text","value":"\nfelaktig inmatning"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara slutligen på en snabb enkät:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"3435ec4c-3236-59f6-82c1-664967213776"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att känna till binära träd och några rekursiva algoritmer som används för att bearbeta dem
  • \n

De verkliga fördelarna med rekursion blir uppenbara när vi stöter på problem där iterativa lösningar är svåra att skriva. Låt oss ta en titt på binära träd, till exempel. Ett binärt träd är en förgrenad struktur där vi har noder och vid varje nod förgrenar sig strukturen, som mest, i två underordnade grenar med egna noder. Ett binärt träd skulle då kunna se ut så här (datavetenskap betraktas ofta som en gren av naturvetenskapen, men vår förståelse av träd är lite upp och ner, som du kommer att märka):

\n
\n \n \n \n \n \"11\n \n \n

Binära träd bör åtminstone teoretiskt sett vara lätta att hantera rekursivt: om vi vill utföra någon operation på varje nod i trädet behöver vår algoritm helt enkelt

    \n
  1. Behandla den aktuella noden
  2. \n
  3. Anropa sig själv på barnnoden till vänster
  4. \n
  5. Anropa sig själv på barnnoden till höger
  6. \n
\n \n = None, hoger_barn:'Nod' = None):\n self.varde = varde\n self.vanster_barn = vanster_barn\n self.hoger_barn = hoger_barn

Låt oss anta att vi vill modellera följande träd:

\n \n \n \n \n \n \"11\n \n \n

Vi kunde uppnå detta med följande kod:

if __name__ == \"__main__\":\n    trad = Nod(2)\n\n    trad.vanster_barn = Nod(3)\n    trad.vanster_barn.vanster_barn = Nod(5)\n    trad.vanster_barn.hoger_barn = Nod(8)\n\n    trad.hoger_barn = Nod(4)\n    trad.hoger_barn.hoger_barn = Nod(11)\n

Algoritmer för rekursiva binära träd

Låt oss först ta en titt på en algoritm som skriver ut alla noder i ett binärt träd en efter en. I de följande exemplen kommer vi att arbeta med det binära träd som definieras ovan.

Argumentet till utskriftsfunktionen är rotnoden i det binära trädet. Detta är noden högst upp i vår illustration ovan. Alla andra noder är barn till den här noden:

\ndef skriv_ut_noder(rot: Nod):\n    print(rot.varde)\n\n    if rot.vanster_barn is not None:\n        skriv_ut_noder(rot.vanster_barn)\n\n    if rot.hoger_barn is not None:\n        skriv_ut_noder(rot.hoger_barn)\n

Funktionen skriver ut värdet på den nod som skickas som argument och anropar sedan sig själv på de vänstra och högra underordnade noderna, förutsatt att noderna är definierade. Det här är en mycket enkel algoritm, men den går på ett effektivt och tillförlitligt sätt igenom alla noder i trädet, oavsett trädets storlek. Avgörande är att ingen nod besöks två gånger. Varje värde skrivs bara ut en gång.

Om vi skickar rotnoden trad i det binära trädet som illustreras ovan som ett argument till funktionen, skriver den ut

2\n3\n5\n8\n4\n11

Som du kan se av ordningen på noderna i utskriften rör sig algoritmen först längs trädets \"vänstra ben\" ner till botten, och därifrån går den igenom de andra noderna i ordning.

På samma sätt kan vi skriva en algoritm för att beräkna summan av alla de värden som finns lagrade i trädets noder:

\ndef nodernas_summa(rot: Nod):\n    summa = rot.varde\n\n    if rot.vanster_barn is not None:\n        summa += nodernas_summa(rot.vanster_barn)\n\n    if rot.hoger_barn is not None:\n        summa += nodernas_summa(rot.hoger_barn)\n\n    return summa\n

Variabeln summa initieras till att vara lika med värdet för den aktuella noden. Värdet i variabeln ökas sedan genom rekursiva anrop till nodens summor i det vänstra och högra underordnade trädet (först kontrolleras naturligtvis att de finns). Detta resultat returneras sedan.

Skapa funktionen storsta_nod(rot: Nod), som tar rotnoden av ett binärt träd som argument.

Funktionen returnerar värdet på den nod som har det största värdet i trädet. Trädet ska genomkorsas rekursivt.

Tips: funktionen nodernas_summa i exemplet ovan kan vara användbart.

Exempel på hur funktionen ska fungera:

\nif __name__ == \"__main__\":\n    trad = Nod(2)\n\n    trad.vanster_barn = Nod(3)\n    trad.vanster_barn.vanster_barn = Nod(5)\n    trad.vanster_barn.hoger_barn = Nod(8)\n\n    trad.hoger_barn = Nod(4)\n    trad.hoger_barn.hoger_barn = Nod(11)\n\n    print(storsta_nod(trad))\n

11

Sorterat binärt träd

Ett binärt träd är särskilt användbart när noderna är sorterade på ett visst sätt. Det gör att det går snabbt och effektivt att hitta noder i trädet.

Låt oss ta en titt på ett träd som är sorterat på följande sätt: det vänstra barnet till varje nod är mindre än själva noden och det högra barnet är på motsvarande sätt större.

\n \n \n \n \n \n \"11\n \n \n

Nu kan vi skriva en rekursiv algoritm för att söka efter noder. Idén är mycket lik den binära sökningen från föregående avsnitt: om den aktuella noden är den nod vi letar efter, returnera True. Annars fortsätter vi rekursivt med antingen det vänstra eller det högra underordnade trädet. Om noden inte är definierad returneras False.

\ndef sok_nod(rot: Nod, varde):\n    if rot is None:\n        return False\n\n    if varde == rot.varde:\n        return True\n\n    if varde > rot.varde:\n        return sok_nod(rot.hoger_barn, varde)\n\n    return sok_nod(rot.vanster_barn, varde)\n

Klassen Arbetare modellerar en arbetare på ett företag:

class Arbetare:\n    def __init__(self, namn: str):\n        self.namn = namn\n        self.underordnade = []\n\n    def tillsatt_underordnad(self, arbetare: 'Arbetare'):\n        self.underordnade.append(arbetare)

Skapa funktionen rakna_underordnade(arbetare: Arbetare), som rekursivt räknar mängden underordnade som en arbetare har.

Exempel på funktionen i användning:

if __name__ == \"__main__\":\n    a1 = Arbetare(\"Sussi\")\n    a2 = Arbetare(\"Erik\")\n    a3 = Arbetare(\"Matte\")\n    a4 = Arbetare(\"Emilia\")\n    a5 = Arbetare(\"Anton\")\n    a6 = Arbetare(\"Kjell\")\n    a1.tillsatt_underordnad(a4)\n    a1.tillsatt_underordnad(a6)\n    a4.tillsatt_underordnad(a2)\n    a4.tillsatt_underordnad(a3)\n    a4.tillsatt_underordnad(a5)\n    print(rakna_underordnade(a1))\n    print(rakna_underordnade(a4))\n    print(rakna_underordnade(a5))

5\n3\n0

Besök till tiden innan rekursion

Låt oss avsluta denna del av materialet med en lite större övning som koncentrerar sig på objektorienterade programmeringsprinciper. Vi rekommenderar inte att du använder rekursion i denna serie av uppgifter, men tekniker för list comprehension kommer att vara användbara.

I den här övningen kommer du skriva två olika klasser, vilka i sin tur kommer att vara ryggraden för övningen som följer denna, i vilken du kommer att skapa en interaktiv applikation.

Uppgift

Förverkliga klassen Uppgift, som modellerar en uppgift i ett mjukvaruföretags lista av uppgifter. Uppgifterna har

    \n
  • en beskrivning
  • \n
  • en beräkning av mängden timmar uppgiften tar
  • \n
  • namnet på programmeraren som har fått uppgiften
  • \n
  • ett fält som håller koll på ifall uppgiften är gjord
  • \n
  • en unik id
  • \n

Klassen fungerar enligt följande:

a1 = Uppgift(\"koda hello world\", \"Erik\", 3)\nprint(a1.id, a1.beskrivning, a1.programmerare, a1.arbetsmangd)\nprint(a1)\nprint(a1.ar_fardig())\na1.markera_fardig()\nprint(a1)\nprint(a1.ar_fardig())\na2 = Uppgift(\"koda webbutik\", \"Anton\", 10)\na3 = Uppgift(\"koda mobilapp för räknande av arbetsmängd\", \"Erik\", 25)\nprint(a2)\nprint(a3)

1 koda hello world Erik 3\n1: koda hello world (3 timmar), programmerare Erik INTE FÄRDIG\nFalse\n1: koda hello world (3 timmar), programmerare Erik FÄRDIG\nTrue\n2: koda webbutik (10 timmar), programmerare Anton INTE FÄRDIG\n3: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik INTE FÄRDIG

Klarifikationer:

    \n
  • uppgiftens läge (färdig eller inte färdig) kan kollas med funktionen ar_fardig(self), som returnerar ett booleskt värde
  • \n
  • uppgiften är inte färdig när den skapas
  • \n
  • uppgiften markeras som färdig genom att anropa på metoden markera_fardig(self)
  • \n
  • uppgifternas id är ett löpande nummer som börjar med 1. Id av första uppgiften är 1, andra uppgiften 2 osv.
  • \n

Tips: id kan implementeras med en klassvariabel.

Uppgiftsbok

Skapa nu en klass med namnet Uppgiftsbok, som samlar ihop alla uppgifter som delegerats av mjukvaruföretaget. Uppgifterna ska modelleras med klassen Uppgift som du just gjorde.

Basversionen av Uppgiftsboken används enligt följande:

bestallningar = Uppgiftsbok()\nbestallningar.tillsatt_bestallning(\"koda webbutik\", \"Anton\", 10)\nbestallningar.tillsatt_bestallning(\"koda mobilapp för räknande av arbetsmängd\", \"Erik\", 25)\nbestallningar.tillsatt_bestallning(\"koda app för övande av matematik\", \"Anton\", 100)\n\nfor bestallning in bestallningar.alla_bestallningar():\n    print(bestallning)\n\nprint()\n\nfor programmerare in bestallningar.kodare():\n    print(programmerare)

1: koda webbutik (10 timmar), programmerare Anton INTE FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik INTE FÄRDIG\n3: koda app för övande av matematik (100 timmar), programmerare Anton INTE FÄRDIG

Anton\nErik

I detta skede borde din Uppgiftsbok erbjuda tre metoder:

    \n
  • tillsatt_bestallning(self, beskrivning, programmerare, arbetsmangd), som lägger till en ny uppgift till Uppgiftsboken. En Uppgiftsbok förvarar uppgifterna internt som Uppgift-objekt. OBS: metoden ska ta exakt de argument som nämns eller så fungerar inte de automatiserade testerna korrekt.
  • \n
  • alla_bestallningar(self) returnerar en lista på alla uppgifter som lagras i en Uppgiftsbok
  • \n
  • kodare(self) returnerar en lista på namnen av alla kodare som har uppgifter lagrade i Uppgiftsboken. Listan ska innehålla varje programmerare endast en gång.
  • \n

Tips: En enkel metod för att göra sig av med multipler är att ursprungligen hantera listan som en mängd (eng: set). En mängd är en samling av saker där varje unikt föremål endast förekommer en gång. En set kan sedan konverteras tillbaks till en lista, och vi kan då vara säkra på att varje föremål nu är unikt:

lista = [1,1,3,6,4,1,3]\nlista2 = list(set(lista))\nprint(lista)\nprint(lista2)

[1, 1, 3, 6, 4, 1, 3]\n[1, 3, 4, 6]

Några fler funktioner för Uppgiftsboken

Skapa tre till metoder till din Uppgiftsbok-klass.

Metoden markera_fardig(self, id: int) får som argument uppgiftens id och markerar den relevanta uppgiften som färdig:

bestallningar = Uppgiftsbok()\nbestallningar.tillsatt_bestallning(\"koda webbutik\", \"Anton\", 10)\nbestallningar.tillsatt_bestallning(\"koda mobilapp för räknande av arbetsmängd\", \"Erik\", 25)\nbestallningar.tillsatt_bestallning(\"koda app för övande av matematik\", \"Anton\", 100)\n\nbestallningar.markera_fardig(1)\nbestallningar.markera_fardig(2)\n\nfor bestallning in bestallningar.alla_bestallningar():\n    print(bestallning)

1: koda webbutik (10 timmar), programmerare Anton FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik FÄRDIG\n3: koda app för övande av matematik (100 timmar), programmerare Anton INTE FÄRDIG

Ifall det inte finns någon uppgift med det givna id:t, ska metoden åstadkomma ett ValueError-undantag. Ifall du behöver en påminnelse om att åstadkomma undantag kan du kolla in modul 6.

Metoderna fardiga_bestallningar(self) och ofardiga_bestallningar(self) fungerar som förväntat: båda returnerar en lista innehållande de relevanta uppgifterna från Uppgiftsboken.

Projektets sista detaljer

Vi skapar en sista metod programmerarens_status(self, programmerare: str) till Uppgiftsbok-klassen, vilken returnar en tupel. Tupeln ska innehålla antalet färdiga och ofärdiga uppgifter en programmerare har tilldelats samt den uppskattade mängden timmar i båda kategorierna.

bestallningar = Uppgiftsbok()\nbestallningar.tillsatt_bestallning(\"koda webbutik\", \"Anton\", 10)\nbestallningar.tillsatt_bestallning(\"koda mobilapp för räknande av arbetsmängd\", \"Anton\", 25)\nbestallningar.tillsatt_bestallning(\"koda app för övande av matematik\", \"Anton\", 100)\nbestallningar.tillsatt_bestallning(\"koda nya facebook\", \"Erik\", 1000)\n\nbestallningar.markera_fardig(1)\nbestallningar.markera_fardig(2)\n\nstatus = bestallningar.programmerarens_status(\"Anton\")\nprint(status)

(2, 1, 35, 100)

Det första föremålet i tupeln är antalet färdiga uppgifter, medan det andra föremålet är antalet ofärdiga uppgifter. Det tredje och fjärde föremålet är summan av uppskattningarna av arbetsmängden för de färdiga respektive ofärdiga uppgifterna.

Ifall det inte finns någon programmerare med det angivna namnet, ska metoden åstadkomma ett ValueError-undantag.

I den här övningen ska du skapa en interaktiv applikation för att administrera de uppgifter som beställts från ett programvaruföretag. Implementeringen är helt upp till dig, men du kan använda byggstenarna från föregående övning i din applikation. Exemplen i det sista avsnittet av modul 10 kan också vara till hjälp.

Utan felhantering

Applikationen ska fungera exakt enligt följande:

instruktioner:\n0 avsluta\n1 tillsätt beställning\n2 lista färdiga\n3 lista ofärdiga\n4 markera uppgift färdig\n5 programmerare\n6 programmerarens status

instruktion: 1\nbeskrivning: koda nya facebook\nprogrammerare och uppskattad arbetsmängd: jonas 1000\ntillsatt!

instruktion: 1\nbeskrivning: koda mobilapp för räknande av arbetsmängd\nprogrammerare och uppskattad arbetsmängd: erik 25\ntillsatt!

instruktion: 1\nbeskrivning: program för att öva musikteori\nprogrammerare och uppskattad arbetsmängd: nina 12\ntillsatt!

instruktion: 1\nbeskrivning: koda nya twitter\nprogrammerare och uppskattad arbetsmängd: jonas 55\ntillsatt!

instruktion: 2\ninga färdiga uppgifter

instruktion: 3\n1: koda nya facebook (1000 timmar), programmerare jonas INTE FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare erik INTE FÄRDIG\n3: ohjelma musiikin teorian harjoitteluun (12 timmar), programmerare nina INTE FÄRDIG\n4: koda nya twitter (55 timmar), programmerare jonas INTE FÄRDIG

instruktion: 4\nid: 2\nmarkerad som färdig

instruktion: 4\nid: 4\nmarkerad som färdig

instruktion: 2\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare erik FÄRDIG\n4: koda nya twitter (55 timmar), programmerare jonas FÄRDIG

instruktion: 3\n1: koda nya facebook (1000 timmar), programmerare jonas INTE FÄRDIG\n3: program för att öva musikteori (12 timmar), programmerare nina INTE FÄRDIG

instruktion: 5\njonas\nerik\nnina

instruktion: 6\nprogrammerare: jonas\nuppgifter: färdiga 2 ofärdiga 1, timmar: gjorda 55 ogjorda 1000

Den första övningspoängen ges för en fungerande applikation när alla användarinmatningar är felfria.

Hantering av inamtningsfel

För att få den andra övningspoängen för denna övning förväntas din applikation återhämta sig från felaktig användarinmatning. All inmatning som inte följer det angivna formatet ska ge ett felmeddelande felaktig inmatning och resultera i ännu en upprepning av loopen med begäran om en ny instruktion:

instruktion: 1\nbeskrivning: koda mobilapp för räknande av arbetsmängd\nprogrammerare och uppskattad arbetsmängd: erik xxx\nfelaktig inmatning

instruktion: 1\nbeskrivning: koda mobilapp för räknande av arbetsmängd\nprogrammerare och uppskattad arbetsmängd: erik\nfelaktig inmatning

instruktion: 4\nid: 1000000\nfelaktig inmatning

instruktion: 4\nid: XXXX\nfelaktig inmatning

instruktion: 6\nprogrammerare: okändprogrammerare\nfelaktig inmatning

Svara slutligen på en snabb enkät:

","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-11/4-fler-exempel-pa-rekursion.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att känna till binära träd och några rekursiva algoritmer som används för att bearbeta dem"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De verkliga fördelarna med rekursion blir uppenbara när vi stöter på problem där iterativa lösningar är svåra att skriva. Låt oss ta en titt på binära träd, till exempel. Ett binärt träd är en förgrenad struktur där vi har noder och vid varje nod förgrenar sig strukturen, som mest, i två underordnade grenar med egna noder. Ett binärt träd skulle då kunna se ut så här (datavetenskap betraktas ofta som en gren av naturvetenskapen, men vår förståelse av träd är lite upp och ner, som du kommer att märka):"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 307px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsElEQVQ4y3WU54rDQAyE/f5PlUD+JIT03ntvpHcdn0C+9Z5PYFxWO5rRaB2IF9/vV++fz0fa7bZ0u13pdDryer3CdcuJi8AHs+Tr9apgAHHn3c35DzRwF7nDjHg8HpJOp6XX60k2m5XNZhMy53L3uOCBL9WiXq/L7XYL35E9n89j2xMBdCvsdjsZDodSLBb12RhZjEYjLTQYDMJ1n0hgH5CICSQ2m82QnVvwcrlIo9GQ9XqtuX5fI4DP51Oq1arKqlQqcr/f/ySfz2ft6Ww2k1KppCR8o0JAAABarVZSq9UiJrjSDoeDFiWHZz8nMMfoC5IskH08Hv9tvhnnylbJzBjMuKyaycec7XYbC0qP2VMul2W/3/8yHI/HcjqddCwslsulOkrPYOqfIGI6ncpkMtFcJiMEZAPOsWiBjFwup98xIO6EULBQKChL1IRjgzy3d7BCZr/fD110Gb7fbzUFtynMflhikjJ0mwx1zMlkMhHn/PPNyCDZgsngG2uBm9xqtfQZdq7DlgMI7DDEcm0iMAaWkaNHhUQioQlxhx8gfhSpVEpPi8VisZBkMin5fF5+ALPgzivet7o7AAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42ada93f7fad74a8205a13a46dc4de57/a0b58/11_4_1.webp 230w","/static/42ada93f7fad74a8205a13a46dc4de57/b1a97/11_4_1.webp 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42ada93f7fad74a8205a13a46dc4de57/81c8e/11_4_1.png 230w","/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png","alt":"11 4 1","title":"11 4 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Binära träd bör åtminstone teoretiskt sett vara lätta att hantera rekursivt: om vi vill utföra någon operation på varje nod i trädet behöver vår algoritm helt enkelt"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Behandla den aktuella noden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Anropa sig själv på barnnoden till vänster"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Anropa sig själv på barnnoden till höger"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 362px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/32e25a73b5e04b72265ba7ccd2d765b1/10600/11_4_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 77.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB1klEQVQ4y4VU2Y7aQBD0/39V9mFhEwcJCZv79oUPbpsbKqrONhp7l6Sl1sjDTHVVTQnr8Xig2iyu0+kUnU4HURQ99/7XFiqlgJfLBYPBAIfDQVYT8F9lfceOtd/v0W63MZ/P0e/35dsc+pLharXCdrvFZrPBbrdDkiQIwxDNZhPr9Vr22JROCxaLBcw71baOx2OJFRm5rovlcvlko8O4P5lM4HneS+klwCzLhMVwOBQGJiCbv/m+j16vh9vt9r1kmq4Vx7GAjUYjkV1lmOe57FO+3jNTIQzP5zPYlEi5WpSmoMrQLMqvggogDaYMRiMIAtnkgNlsJmw5iN4SkDJ1gCpRy56SyYJAXJURpRGQ5vM3fpuAPEc/6SWtYN3v978MT6cT0jSVy3pBJXGPAdeImHYQUB/JzGfpla/XqxxwHOcZG5VIUA5utVolkG63K02bvsRGQ83JDLUWwTiAFxktfTz6z7NkrEOswohNURQYj8fSOojeEFxzyMtkqo9Xza2VxLGwIjtO5D+LH/jI0hRRFCL7fGUOIDBflizJmOfDMIDn+YiTWPatosjRaDRQr9XhOC7aroNfto2PWl3WvDiU8tfvdfH29kN8Ztfe3/HbbuCnbWOf5/gDQCDEjSWuT0cAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/32e25a73b5e04b72265ba7ccd2d765b1/a0b58/11_4_2.webp 230w","/static/32e25a73b5e04b72265ba7ccd2d765b1/c2de8/11_4_2.webp 362w"],"sizes":"(max-width: 362px) 100vw, 362px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/32e25a73b5e04b72265ba7ccd2d765b1/81c8e/11_4_2.png 230w","/static/32e25a73b5e04b72265ba7ccd2d765b1/10600/11_4_2.png 362w"],"sizes":"(max-width: 362px) 100vw, 362px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/32e25a73b5e04b72265ba7ccd2d765b1/10600/11_4_2.png","alt":"11 4 2","title":"11 4 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se på bilden ovan är både de vänstra och högra \"underträden\" fullfjädrade binära träd i sig, och den enda nod som lämnas utanför de rekursiva anropen är den överordnade noden, som bearbetas i steg 1 innan funktionen anropas rekursivt. På så sätt kan vi vara säkra på att varje nod har besökts exakt en gång när funktionen är klar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En iterativ version av en binär trädtraversering skulle vara mycket mer komplicerad, eftersom vi på något sätt skulle behöva hålla reda på alla noder som vi redan har besökt. Samma principer gäller för alla beräkningsbara trädstrukturer, inte bara binära."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett binärt träd är också lätt att modellera i Python-kod. Vi behöver bara skriva en klassdefinition för en enda nod. Den har ett värdeattribut och attribut för de vänstra och högra underordnade noderna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Nod"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen representerar en enkel nod i ett binärt träd \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Nod'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hoger_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Nod'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" varde\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vanster_barn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hoger_barn"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi vill modellera följande träd:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 267px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 68.26086956521739%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABfUlEQVQ4y4VTWcuDQAzc///sDyr0wRdrRVuqvbx6aA96gtArHxPIsm4t34LEI5lMZqKijvP5fDjebjfyPI+v+Xyuv8n3rqO6wKQgjmO63+/0er0oyzI6nU7/giqTkcT3+80xz3MKw5CKomCWAO7KN8GV2U3u5Xk0GvGo6/WakiTRY0tTu1YD2l1wAFBVVevdZrPhJr80/xoZAIvFgobDIaVpyu8wJtiIDMfjkXzf57zZbEZN03QzPJ/PbILE7Xb7ZYBoGEURy1DXNU0mk1aeBtzv9+wkOoIpDLF1lVzIAaZwHc1bDGWU5/NJ/X6fx0CEXqbjJiDAwAxMB4NByyS9h8vlkhOlcDwe0+PxaIGaTEAApyxLWq1WOk/hAXpMp9NWwfV6pV6vx3+L7aS9Ea7rskk8MoSFq9BPktEJI2FsMDX/EHt8EIJM0BwSKDDY7XZ6TYQdlvhwOLDb0szUU1yHQaiHBI7jkEIx2NhrEgQBXS4XXmQA/2IIIriwJTDoD1qJNDsmtBRAAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/2d27ece550ee70e685bb54be04c9ea65/a0b58/11_4_3.webp 230w","/static/2d27ece550ee70e685bb54be04c9ea65/9156e/11_4_3.webp 267w"],"sizes":"(max-width: 267px) 100vw, 267px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/2d27ece550ee70e685bb54be04c9ea65/81c8e/11_4_3.png 230w","/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.png 267w"],"sizes":"(max-width: 267px) 100vw, 267px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.png","alt":"11 4 3","title":"11 4 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kunde uppnå detta med följande kod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n trad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"algoritmer-for-rekursiva-binara-trad","style":"position:relative;"},"children":[{"type":"text","value":"Algoritmer för rekursiva binära träd"},{"type":"element","tagName":"a","properties":{"href":"#algoritmer-for-rekursiva-binara-trad","ariaLabel":"algoritmer for rekursiva binara trad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss först ta en titt på en algoritm som skriver ut alla noder i ett binärt träd en efter en. I de följande exemplen kommer vi att arbeta med det binära träd som definieras ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Argumentet till utskriftsfunktionen är rotnoden i det binära trädet. Detta är noden högst upp i vår illustration ovan. Alla andra noder är barn till den här noden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut_noder"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n skriv_ut_noder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n skriv_ut_noder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen skriver ut värdet på den nod som skickas som argument och anropar sedan sig själv på de vänstra och högra underordnade noderna, förutsatt att noderna är definierade. Det här är en mycket enkel algoritm, men den går på ett effektivt och tillförlitligt sätt igenom alla noder i trädet, oavsett trädets storlek. Avgörande är att ingen nod besöks två gånger. Varje värde skrivs bara ut en gång."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi skickar rotnoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"trad"}]},{"type":"text","value":" i det binära trädet som illustreras ovan som ett argument till funktionen, skriver den ut"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n5\n8\n4\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se av ordningen på noderna i utskriften rör sig algoritmen först längs trädets \"vänstra ben\" ner till botten, och därifrån går den igenom de andra noderna i ordning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt kan vi skriva en algoritm för att beräkna summan av alla de värden som finns lagrade i trädets noder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nodernas_summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" nodernas_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" nodernas_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" summa\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" initieras till att vara lika med värdet för den aktuella noden. Värdet i variabeln ökas sedan genom rekursiva anrop till nodens summor i det vänstra och högra underordnade trädet (först kontrolleras naturligtvis att de finns). Detta resultat returneras sedan."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Största noden","tmcname":"osa11-16_storsta_nod"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storsta_nod(rot: Nod)"}]},{"type":"text","value":", som tar rotnoden av ett binärt träd som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar värdet på den nod som har det största värdet i trädet. Trädet ska genomkorsas rekursivt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nodernas_summa"}]},{"type":"text","value":" i exemplet ovan kan vara användbart."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n trad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storsta_nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"trad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"11"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"sorterat-binart-trad","style":"position:relative;"},"children":[{"type":"text","value":"Sorterat binärt träd"},{"type":"element","tagName":"a","properties":{"href":"#sorterat-binart-trad","ariaLabel":"sorterat binart trad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett binärt träd är särskilt användbart när noderna är sorterade på ett visst sätt. Det gör att det går snabbt och effektivt att hitta noder i trädet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ett träd som är sorterat på följande sätt: det vänstra barnet till varje nod är mindre än själva noden och det högra barnet är på motsvarande sätt större."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 307px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsElEQVQ4y3WU54rDQAyE/f5PlUD+JIT03ntvpHcdn0C+9Z5PYFxWO5rRaB2IF9/vV++fz0fa7bZ0u13pdDryer3CdcuJi8AHs+Tr9apgAHHn3c35DzRwF7nDjHg8HpJOp6XX60k2m5XNZhMy53L3uOCBL9WiXq/L7XYL35E9n89j2xMBdCvsdjsZDodSLBb12RhZjEYjLTQYDMJ1n0hgH5CICSQ2m82QnVvwcrlIo9GQ9XqtuX5fI4DP51Oq1arKqlQqcr/f/ySfz2ft6Ww2k1KppCR8o0JAAABarVZSq9UiJrjSDoeDFiWHZz8nMMfoC5IskH08Hv9tvhnnylbJzBjMuKyaycec7XYbC0qP2VMul2W/3/8yHI/HcjqddCwslsulOkrPYOqfIGI6ncpkMtFcJiMEZAPOsWiBjFwup98xIO6EULBQKChL1IRjgzy3d7BCZr/fD110Gb7fbzUFtynMflhikjJ0mwx1zMlkMhHn/PPNyCDZgsngG2uBm9xqtfQZdq7DlgMI7DDEcm0iMAaWkaNHhUQioQlxhx8gfhSpVEpPi8VisZBkMin5fF5+ALPgzivet7o7AAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42ada93f7fad74a8205a13a46dc4de57/a0b58/11_4_1.webp 230w","/static/42ada93f7fad74a8205a13a46dc4de57/b1a97/11_4_1.webp 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42ada93f7fad74a8205a13a46dc4de57/81c8e/11_4_1.png 230w","/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png","alt":"11 4 1","title":"11 4 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu kan vi skriva en rekursiv algoritm för att söka efter noder. Idén är mycket lik den binära sökningen från föregående avsnitt: om den aktuella noden är den nod vi letar efter, returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". Annars fortsätter vi rekursivt med antingen det vänstra eller det högra underordnade trädet. Om noden inte är definierad returneras "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sok_nod"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" rot "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" sok_nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" sok_nod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_barn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Chefer och underordnade","tmcname":"osa11-17_chefer_och_underordnade"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Arbetare"}]},{"type":"text","value":" modellerar en arbetare på ett företag:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Arbetare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"underordnade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_underordnad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Arbetare'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"underordnade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rakna_underordnade(arbetare: Arbetare)"}]},{"type":"text","value":", som rekursivt räknar mängden underordnade som en arbetare har."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på funktionen i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n a1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sussi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a6 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Arbetare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kjell\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a6"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_underordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_underordnade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_underordnade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_underordnade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3\n0"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"besok-till-tiden-innan-rekursion","style":"position:relative;"},"children":[{"type":"text","value":"Besök till tiden innan rekursion"},{"type":"element","tagName":"a","properties":{"href":"#besok-till-tiden-innan-rekursion","ariaLabel":"besok till tiden innan rekursion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss avsluta denna del av materialet med en lite större övning som koncentrerar sig på objektorienterade programmeringsprinciper. Vi rekommenderar inte att du använder rekursion i denna serie av uppgifter, men tekniker för list comprehension kommer att vara användbara."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Uppgiftsbok","tmcname":"osa11-18_uppgiftsbok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen kommer du skriva två olika klasser, vilka i sin tur kommer att vara ryggraden för övningen som följer denna, i vilken du kommer att skapa en interaktiv applikation."}]},{"type":"element","tagName":"h2","properties":{"id":"uppgift","style":"position:relative;"},"children":[{"type":"text","value":"Uppgift"},{"type":"element","tagName":"a","properties":{"href":"#uppgift","ariaLabel":"uppgift permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förverkliga klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgift"}]},{"type":"text","value":", som modellerar en uppgift i ett mjukvaruföretags lista av uppgifter. Uppgifterna har"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en beskrivning"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en beräkning av mängden timmar uppgiften tar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"namnet på programmeraren som har fått uppgiften"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett fält som håller koll på ifall uppgiften är gjord"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en unik id"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda hello world\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beskrivning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"programmerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arbetsmangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda webbutik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda mobilapp för räknande av arbetsmängd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 koda hello world Erik 3\n1: koda hello world (3 timmar), programmerare Erik INTE FÄRDIG\nFalse\n1: koda hello world (3 timmar), programmerare Erik FÄRDIG\nTrue\n2: koda webbutik (10 timmar), programmerare Anton INTE FÄRDIG\n3: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik INTE FÄRDIG"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klarifikationer:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"uppgiftens läge (färdig eller inte färdig) kan kollas med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar_fardig(self)"}]},{"type":"text","value":", som returnerar ett booleskt värde"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"uppgiften är inte färdig när den skapas"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"uppgiften markeras som färdig genom att anropa på metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"markera_fardig(self)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"uppgifternas id är ett löpande nummer som börjar med 1. Id av första uppgiften är 1, andra uppgiften 2 osv."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" id kan implementeras med en "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/5-klassattribut","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"klassvariabel"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"uppgiftsbok","style":"position:relative;"},"children":[{"type":"text","value":"Uppgiftsbok"},{"type":"element","tagName":"a","properties":{"href":"#uppgiftsbok","ariaLabel":"uppgiftsbok permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa nu en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftsbok"}]},{"type":"text","value":", som samlar ihop alla uppgifter som delegerats av mjukvaruföretaget. Uppgifterna ska modelleras med klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgift"}]},{"type":"text","value":" som du just gjorde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Basversionen av Uppgiftsboken används enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bestallningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda webbutik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda mobilapp för räknande av arbetsmängd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda app för övande av matematik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bestallning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alla_bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" programmerare "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kodare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"programmerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1: koda webbutik (10 timmar), programmerare Anton INTE FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik INTE FÄRDIG\n3: koda app för övande av matematik (100 timmar), programmerare Anton INTE FÄRDIG"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anton\nErik"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I detta skede borde din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftsbok"}]},{"type":"text","value":" erbjuda tre metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_bestallning(self, beskrivning, programmerare, arbetsmangd)"}]},{"type":"text","value":", som lägger till en ny uppgift till Uppgiftsboken. En Uppgiftsbok förvarar uppgifterna internt som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgift"}]},{"type":"text","value":"-objekt. OBS: metoden ska ta exakt de argument som nämns eller så fungerar inte de automatiserade testerna korrekt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alla_bestallningar(self)"}]},{"type":"text","value":" returnerar en lista på alla uppgifter som lagras i en Uppgiftsbok"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kodare(self)"}]},{"type":"text","value":" returnerar en lista på namnen av alla kodare som har uppgifter lagrade i Uppgiftsboken. Listan ska innehålla varje programmerare endast en gång."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" En enkel metod för att göra sig av med multipler är att ursprungligen hantera listan som en "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3.8/library/stdtypes.html#set","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"mängd"}]},{"type":"text","value":" (eng: set). En mängd är en samling av saker där varje unikt föremål endast förekommer en gång. En "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set"}]},{"type":"text","value":" kan sedan konverteras tillbaks till en lista, och vi kan då vara säkra på att varje föremål nu är unikt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"set"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 1, 3, 6, 4, 1, 3]\n[1, 3, 4, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"nagra-fler-funktioner-for-uppgiftsboken","style":"position:relative;"},"children":[{"type":"text","value":"Några fler funktioner för Uppgiftsboken"},{"type":"element","tagName":"a","properties":{"href":"#nagra-fler-funktioner-for-uppgiftsboken","ariaLabel":"nagra fler funktioner for uppgiftsboken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa tre till metoder till din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftsbok"}]},{"type":"text","value":"-klass."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"markera_fardig(self, id: int)"}]},{"type":"text","value":" får som argument uppgiftens id och markerar den relevanta uppgiften som färdig:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bestallningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda webbutik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda mobilapp för räknande av arbetsmängd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda app för övande av matematik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bestallning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"alla_bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1: koda webbutik (10 timmar), programmerare Anton FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik FÄRDIG\n3: koda app för övande av matematik (100 timmar), programmerare Anton INTE FÄRDIG"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall det inte finns någon uppgift med det givna id:t, ska metoden åstadkomma ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag. Ifall du behöver en påminnelse om att åstadkomma undantag kan du kolla in "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/3-fel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fardiga_bestallningar(self)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ofardiga_bestallningar(self)"}]},{"type":"text","value":" fungerar som förväntat: båda returnerar en lista innehållande de relevanta uppgifterna från Uppgiftsboken."}]},{"type":"element","tagName":"h2","properties":{"id":"projektets-sista-detaljer","style":"position:relative;"},"children":[{"type":"text","value":"Projektets sista detaljer"},{"type":"element","tagName":"a","properties":{"href":"#projektets-sista-detaljer","ariaLabel":"projektets sista detaljer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skapar en sista metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"programmerarens_status(self, programmerare: str)"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftsbok"}]},{"type":"text","value":"-klassen, vilken returnar en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tupel"}]},{"type":"text","value":". Tupeln ska innehålla antalet färdiga och ofärdiga uppgifter en programmerare har tilldelats samt den uppskattade mängden timmar i båda kategorierna."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bestallningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftsbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda webbutik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda mobilapp för räknande av arbetsmängd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda app för övande av matematik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_bestallning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"koda nya facebook\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"markera_fardig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstatus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" bestallningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"programmerarens_status"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"status"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(2, 1, 35, 100)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det första föremålet i tupeln är antalet "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"färdiga"}]},{"type":"text","value":" uppgifter, medan det andra föremålet är antalet "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ofärdiga"}]},{"type":"text","value":" uppgifter. Det tredje och fjärde föremålet är summan av uppskattningarna av arbetsmängden för de färdiga respektive ofärdiga uppgifterna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall det inte finns någon programmerare med det angivna namnet, ska metoden åstadkomma ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Uppgiftsboksapplikation","tmcname":"osa11-19_uppgiftsboksapplikation"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen ska du skapa en interaktiv applikation för att administrera de uppgifter som beställts från ett programvaruföretag. Implementeringen är helt upp till dig, men du kan använda byggstenarna från föregående övning i din applikation. Exemplen i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-10/4-storre-applikation","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"det sista avsnittet av modul 10"}]},{"type":"text","value":" kan också vara till hjälp."}]},{"type":"element","tagName":"h2","properties":{"id":"utan-felhantering","style":"position:relative;"},"children":[{"type":"text","value":"Utan felhantering"},{"type":"element","tagName":"a","properties":{"href":"#utan-felhantering","ariaLabel":"utan felhantering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Applikationen ska fungera "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exakt"}]},{"type":"text","value":" enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 tillsätt beställning\n2 lista färdiga\n3 lista ofärdiga\n4 markera uppgift färdig\n5 programmerare\n6 programmerarens status"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda nya facebook"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas 1000"}]},{"type":"text","value":"\ntillsatt!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda mobilapp för räknande av arbetsmängd"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"erik 25"}]},{"type":"text","value":"\ntillsatt!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program för att öva musikteori"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nina 12"}]},{"type":"text","value":"\ntillsatt!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda nya twitter"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas 55"}]},{"type":"text","value":"\ntillsatt!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ninga färdiga uppgifter"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1: koda nya facebook (1000 timmar), programmerare jonas INTE FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare erik INTE FÄRDIG\n3: ohjelma musiikin teorian harjoitteluun (12 timmar), programmerare nina INTE FÄRDIG\n4: koda nya twitter (55 timmar), programmerare jonas INTE FÄRDIG"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nmarkerad som färdig"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nmarkerad som färdig"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare erik FÄRDIG\n4: koda nya twitter (55 timmar), programmerare jonas FÄRDIG"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1: koda nya facebook (1000 timmar), programmerare jonas INTE FÄRDIG\n3: program för att öva musikteori (12 timmar), programmerare nina INTE FÄRDIG"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\njonas\nerik\nnina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nprogrammerare: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonas"}]},{"type":"text","value":"\nuppgifter: färdiga 2 ofärdiga 1, timmar: gjorda 55 ogjorda 1000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den första övningspoängen ges för en fungerande applikation när alla användarinmatningar är felfria."}]},{"type":"element","tagName":"h2","properties":{"id":"hantering-av-inamtningsfel","style":"position:relative;"},"children":[{"type":"text","value":"Hantering av inamtningsfel"},{"type":"element","tagName":"a","properties":{"href":"#hantering-av-inamtningsfel","ariaLabel":"hantering av inamtningsfel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att få den andra övningspoängen för denna övning förväntas din applikation återhämta sig från felaktig användarinmatning. All inmatning som inte följer det angivna formatet ska ge ett felmeddelande "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"felaktig inmatning"}]},{"type":"text","value":" och resultera i ännu en upprepning av loopen med begäran om en ny instruktion:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda mobilapp för räknande av arbetsmängd"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"erik xxx"}]},{"type":"text","value":"\nfelaktig inmatning"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nbeskrivning: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koda mobilapp för räknande av arbetsmängd"}]},{"type":"text","value":"\nprogrammerare och uppskattad arbetsmängd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"erik"}]},{"type":"text","value":"\nfelaktig inmatning"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1000000"}]},{"type":"text","value":"\nfelaktig inmatning"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"XXXX"}]},{"type":"text","value":"\nfelaktig inmatning"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nprogrammerare: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"okändprogrammerare"}]},{"type":"text","value":"\nfelaktig inmatning"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara slutligen på en snabb enkät:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"3435ec4c-3236-59f6-82c1-664967213776"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att känna till binära träd och några rekursiva algoritmer som används för att bearbeta dem
  • \n

De verkliga fördelarna med rekursion blir uppenbara när vi stöter på problem där iterativa lösningar är svåra att skriva. Låt oss ta en titt på binära träd, till exempel. Ett binärt träd är en förgrenad struktur där vi har noder och vid varje nod förgrenar sig strukturen, som mest, i två underordnade grenar med egna noder. Ett binärt träd skulle då kunna se ut så här (datavetenskap betraktas ofta som en gren av naturvetenskapen, men vår förståelse av träd är lite upp och ner, som du kommer att märka):

\n \n \n \n \n \n \"11\n \n \n

Binära träd bör åtminstone teoretiskt sett vara lätta att hantera rekursivt: om vi vill utföra någon operation på varje nod i trädet behöver vår algoritm helt enkelt

    \n
  1. Behandla den aktuella noden
  2. \n
  3. Anropa sig själv på barnnoden till vänster
  4. \n
  5. Anropa sig själv på barnnoden till höger
  6. \n
\n \n = None, hoger_barn:'Nod' = None):\n self.varde = varde\n self.vanster_barn = vanster_barn\n self.hoger_barn = hoger_barn

Låt oss anta att vi vill modellera följande träd:

\n \n \n \n \n \n \"11\n \n \n

Vi kunde uppnå detta med följande kod:

if __name__ == \"__main__\":\n    trad = Nod(2)\n\n    trad.vanster_barn = Nod(3)\n    trad.vanster_barn.vanster_barn = Nod(5)\n    trad.vanster_barn.hoger_barn = Nod(8)\n\n    trad.hoger_barn = Nod(4)\n    trad.hoger_barn.hoger_barn = Nod(11)\n

Algoritmer för rekursiva binära träd

Låt oss först ta en titt på en algoritm som skriver ut alla noder i ett binärt träd en efter en. I de följande exemplen kommer vi att arbeta med det binära träd som definieras ovan.

Argumentet till utskriftsfunktionen är rotnoden i det binära trädet. Detta är noden högst upp i vår illustration ovan. Alla andra noder är barn till den här noden:

\ndef skriv_ut_noder(rot: Nod):\n    print(rot.varde)\n\n    if rot.vanster_barn is not None:\n        skriv_ut_noder(rot.vanster_barn)\n\n    if rot.hoger_barn is not None:\n        skriv_ut_noder(rot.hoger_barn)\n

Funktionen skriver ut värdet på den nod som skickas som argument och anropar sedan sig själv på de vänstra och högra underordnade noderna, förutsatt att noderna är definierade. Det här är en mycket enkel algoritm, men den går på ett effektivt och tillförlitligt sätt igenom alla noder i trädet, oavsett trädets storlek. Avgörande är att ingen nod besöks två gånger. Varje värde skrivs bara ut en gång.

Om vi skickar rotnoden trad i det binära trädet som illustreras ovan som ett argument till funktionen, skriver den ut

2\n3\n5\n8\n4\n11

Som du kan se av ordningen på noderna i utskriften rör sig algoritmen först längs trädets \"vänstra ben\" ner till botten, och därifrån går den igenom de andra noderna i ordning.

På samma sätt kan vi skriva en algoritm för att beräkna summan av alla de värden som finns lagrade i trädets noder:

\ndef nodernas_summa(rot: Nod):\n    summa = rot.varde\n\n    if rot.vanster_barn is not None:\n        summa += nodernas_summa(rot.vanster_barn)\n\n    if rot.hoger_barn is not None:\n        summa += nodernas_summa(rot.hoger_barn)\n\n    return summa\n

Variabeln summa initieras till att vara lika med värdet för den aktuella noden. Värdet i variabeln ökas sedan genom rekursiva anrop till nodens summor i det vänstra och högra underordnade trädet (först kontrolleras naturligtvis att de finns). Detta resultat returneras sedan.

Skapa funktionen storsta_nod(rot: Nod), som tar rotnoden av ett binärt träd som argument.

Funktionen returnerar värdet på den nod som har det största värdet i trädet. Trädet ska genomkorsas rekursivt.

Tips: funktionen nodernas_summa i exemplet ovan kan vara användbart.

Exempel på hur funktionen ska fungera:

\nif __name__ == \"__main__\":\n    trad = Nod(2)\n\n    trad.vanster_barn = Nod(3)\n    trad.vanster_barn.vanster_barn = Nod(5)\n    trad.vanster_barn.hoger_barn = Nod(8)\n\n    trad.hoger_barn = Nod(4)\n    trad.hoger_barn.hoger_barn = Nod(11)\n\n    print(storsta_nod(trad))\n

11

Sorterat binärt träd

Ett binärt träd är särskilt användbart när noderna är sorterade på ett visst sätt. Det gör att det går snabbt och effektivt att hitta noder i trädet.

Låt oss ta en titt på ett träd som är sorterat på följande sätt: det vänstra barnet till varje nod är mindre än själva noden och det högra barnet är på motsvarande sätt större.

\n \n \n \n \n \n \"11\n \n \n

Nu kan vi skriva en rekursiv algoritm för att söka efter noder. Idén är mycket lik den binära sökningen från föregående avsnitt: om den aktuella noden är den nod vi letar efter, returnera True. Annars fortsätter vi rekursivt med antingen det vänstra eller det högra underordnade trädet. Om noden inte är definierad returneras False.

\ndef sok_nod(rot: Nod, varde):\n    if rot is None:\n        return False\n\n    if varde == rot.varde:\n        return True\n\n    if varde > rot.varde:\n        return sok_nod(rot.hoger_barn, varde)\n\n    return sok_nod(rot.vanster_barn, varde)\n

Klassen Arbetare modellerar en arbetare på ett företag:

class Arbetare:\n    def __init__(self, namn: str):\n        self.namn = namn\n        self.underordnade = []\n\n    def tillsatt_underordnad(self, arbetare: 'Arbetare'):\n        self.underordnade.append(arbetare)

Skapa funktionen rakna_underordnade(arbetare: Arbetare), som rekursivt räknar mängden underordnade som en arbetare har.

Exempel på funktionen i användning:

if __name__ == \"__main__\":\n    a1 = Arbetare(\"Sussi\")\n    a2 = Arbetare(\"Erik\")\n    a3 = Arbetare(\"Matte\")\n    a4 = Arbetare(\"Emilia\")\n    a5 = Arbetare(\"Anton\")\n    a6 = Arbetare(\"Kjell\")\n    a1.tillsatt_underordnad(a4)\n    a1.tillsatt_underordnad(a6)\n    a4.tillsatt_underordnad(a2)\n    a4.tillsatt_underordnad(a3)\n    a4.tillsatt_underordnad(a5)\n    print(rakna_underordnade(a1))\n    print(rakna_underordnade(a4))\n    print(rakna_underordnade(a5))

5\n3\n0

Besök till tiden innan rekursion

Låt oss avsluta denna del av materialet med en lite större övning som koncentrerar sig på objektorienterade programmeringsprinciper. Vi rekommenderar inte att du använder rekursion i denna serie av uppgifter, men tekniker för list comprehension kommer att vara användbara.

I den här övningen kommer du skriva två olika klasser, vilka i sin tur kommer att vara ryggraden för övningen som följer denna, i vilken du kommer att skapa en interaktiv applikation.

Uppgift

Förverkliga klassen Uppgift, som modellerar en uppgift i ett mjukvaruföretags lista av uppgifter. Uppgifterna har

    \n
  • en beskrivning
  • \n
  • en beräkning av mängden timmar uppgiften tar
  • \n
  • namnet på programmeraren som har fått uppgiften
  • \n
  • ett fält som håller koll på ifall uppgiften är gjord
  • \n
  • en unik id
  • \n

Klassen fungerar enligt följande:

a1 = Uppgift(\"koda hello world\", \"Erik\", 3)\nprint(a1.id, a1.beskrivning, a1.programmerare, a1.arbetsmangd)\nprint(a1)\nprint(a1.ar_fardig())\na1.markera_fardig()\nprint(a1)\nprint(a1.ar_fardig())\na2 = Uppgift(\"koda webbutik\", \"Anton\", 10)\na3 = Uppgift(\"koda mobilapp för räknande av arbetsmängd\", \"Erik\", 25)\nprint(a2)\nprint(a3)

1 koda hello world Erik 3\n1: koda hello world (3 timmar), programmerare Erik INTE FÄRDIG\nFalse\n1: koda hello world (3 timmar), programmerare Erik FÄRDIG\nTrue\n2: koda webbutik (10 timmar), programmerare Anton INTE FÄRDIG\n3: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik INTE FÄRDIG

Klarifikationer:

    \n
  • uppgiftens läge (färdig eller inte färdig) kan kollas med funktionen ar_fardig(self), som returnerar ett booleskt värde
  • \n
  • uppgiften är inte färdig när den skapas
  • \n
  • uppgiften markeras som färdig genom att anropa på metoden markera_fardig(self)
  • \n
  • uppgifternas id är ett löpande nummer som börjar med 1. Id av första uppgiften är 1, andra uppgiften 2 osv.
  • \n

Tips: id kan implementeras med en klassvariabel.

Uppgiftsbok

Skapa nu en klass med namnet Uppgiftsbok, som samlar ihop alla uppgifter som delegerats av mjukvaruföretaget. Uppgifterna ska modelleras med klassen Uppgift som du just gjorde.

Basversionen av Uppgiftsboken används enligt följande:

bestallningar = Uppgiftsbok()\nbestallningar.tillsatt_bestallning(\"koda webbutik\", \"Anton\", 10)\nbestallningar.tillsatt_bestallning(\"koda mobilapp för räknande av arbetsmängd\", \"Erik\", 25)\nbestallningar.tillsatt_bestallning(\"koda app för övande av matematik\", \"Anton\", 100)\n\nfor bestallning in bestallningar.alla_bestallningar():\n    print(bestallning)\n\nprint()\n\nfor programmerare in bestallningar.kodare():\n    print(programmerare)

1: koda webbutik (10 timmar), programmerare Anton INTE FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik INTE FÄRDIG\n3: koda app för övande av matematik (100 timmar), programmerare Anton INTE FÄRDIG

Anton\nErik

I detta skede borde din Uppgiftsbok erbjuda tre metoder:

    \n
  • tillsatt_bestallning(self, beskrivning, programmerare, arbetsmangd), som lägger till en ny uppgift till Uppgiftsboken. En Uppgiftsbok förvarar uppgifterna internt som Uppgift-objekt. OBS: metoden ska ta exakt de argument som nämns eller så fungerar inte de automatiserade testerna korrekt.
  • \n
  • alla_bestallningar(self) returnerar en lista på alla uppgifter som lagras i en Uppgiftsbok
  • \n
  • kodare(self) returnerar en lista på namnen av alla kodare som har uppgifter lagrade i Uppgiftsboken. Listan ska innehålla varje programmerare endast en gång.
  • \n

Tips: En enkel metod för att göra sig av med multipler är att ursprungligen hantera listan som en mängd (eng: set). En mängd är en samling av saker där varje unikt föremål endast förekommer en gång. En set kan sedan konverteras tillbaks till en lista, och vi kan då vara säkra på att varje föremål nu är unikt:

lista = [1,1,3,6,4,1,3]\nlista2 = list(set(lista))\nprint(lista)\nprint(lista2)

[1, 1, 3, 6, 4, 1, 3]\n[1, 3, 4, 6]

Några fler funktioner för Uppgiftsboken

Skapa tre till metoder till din Uppgiftsbok-klass.

Metoden markera_fardig(self, id: int) får som argument uppgiftens id och markerar den relevanta uppgiften som färdig:

bestallningar = Uppgiftsbok()\nbestallningar.tillsatt_bestallning(\"koda webbutik\", \"Anton\", 10)\nbestallningar.tillsatt_bestallning(\"koda mobilapp för räknande av arbetsmängd\", \"Erik\", 25)\nbestallningar.tillsatt_bestallning(\"koda app för övande av matematik\", \"Anton\", 100)\n\nbestallningar.markera_fardig(1)\nbestallningar.markera_fardig(2)\n\nfor bestallning in bestallningar.alla_bestallningar():\n    print(bestallning)

1: koda webbutik (10 timmar), programmerare Anton FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare Erik FÄRDIG\n3: koda app för övande av matematik (100 timmar), programmerare Anton INTE FÄRDIG

Ifall det inte finns någon uppgift med det givna id:t, ska metoden åstadkomma ett ValueError-undantag. Ifall du behöver en påminnelse om att åstadkomma undantag kan du kolla in modul 6.

Metoderna fardiga_bestallningar(self) och ofardiga_bestallningar(self) fungerar som förväntat: båda returnerar en lista innehållande de relevanta uppgifterna från Uppgiftsboken.

Projektets sista detaljer

Vi skapar en sista metod programmerarens_status(self, programmerare: str) till Uppgiftsbok-klassen, vilken returnar en tupel. Tupeln ska innehålla antalet färdiga och ofärdiga uppgifter en programmerare har tilldelats samt den uppskattade mängden timmar i båda kategorierna.

bestallningar = Uppgiftsbok()\nbestallningar.tillsatt_bestallning(\"koda webbutik\", \"Anton\", 10)\nbestallningar.tillsatt_bestallning(\"koda mobilapp för räknande av arbetsmängd\", \"Anton\", 25)\nbestallningar.tillsatt_bestallning(\"koda app för övande av matematik\", \"Anton\", 100)\nbestallningar.tillsatt_bestallning(\"koda nya facebook\", \"Erik\", 1000)\n\nbestallningar.markera_fardig(1)\nbestallningar.markera_fardig(2)\n\nstatus = bestallningar.programmerarens_status(\"Anton\")\nprint(status)

(2, 1, 35, 100)

Det första föremålet i tupeln är antalet färdiga uppgifter, medan det andra föremålet är antalet ofärdiga uppgifter. Det tredje och fjärde föremålet är summan av uppskattningarna av arbetsmängden för de färdiga respektive ofärdiga uppgifterna.

Ifall det inte finns någon programmerare med det angivna namnet, ska metoden åstadkomma ett ValueError-undantag.

I den här övningen ska du skapa en interaktiv applikation för att administrera de uppgifter som beställts från ett programvaruföretag. Implementeringen är helt upp till dig, men du kan använda byggstenarna från föregående övning i din applikation. Exemplen i det sista avsnittet av modul 10 kan också vara till hjälp.

Utan felhantering

Applikationen ska fungera exakt enligt följande:

instruktioner:\n0 avsluta\n1 tillsätt beställning\n2 lista färdiga\n3 lista ofärdiga\n4 markera uppgift färdig\n5 programmerare\n6 programmerarens status

instruktion: 1\nbeskrivning: koda nya facebook\nprogrammerare och uppskattad arbetsmängd: jonas 1000\ntillsatt!

instruktion: 1\nbeskrivning: koda mobilapp för räknande av arbetsmängd\nprogrammerare och uppskattad arbetsmängd: erik 25\ntillsatt!

instruktion: 1\nbeskrivning: program för att öva musikteori\nprogrammerare och uppskattad arbetsmängd: nina 12\ntillsatt!

instruktion: 1\nbeskrivning: koda nya twitter\nprogrammerare och uppskattad arbetsmängd: jonas 55\ntillsatt!

instruktion: 2\ninga färdiga uppgifter

instruktion: 3\n1: koda nya facebook (1000 timmar), programmerare jonas INTE FÄRDIG\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare erik INTE FÄRDIG\n3: ohjelma musiikin teorian harjoitteluun (12 timmar), programmerare nina INTE FÄRDIG\n4: koda nya twitter (55 timmar), programmerare jonas INTE FÄRDIG

instruktion: 4\nid: 2\nmarkerad som färdig

instruktion: 4\nid: 4\nmarkerad som färdig

instruktion: 2\n2: koda mobilapp för räknande av arbetsmängd (25 timmar), programmerare erik FÄRDIG\n4: koda nya twitter (55 timmar), programmerare jonas FÄRDIG

instruktion: 3\n1: koda nya facebook (1000 timmar), programmerare jonas INTE FÄRDIG\n3: program för att öva musikteori (12 timmar), programmerare nina INTE FÄRDIG

instruktion: 5\njonas\nerik\nnina

instruktion: 6\nprogrammerare: jonas\nuppgifter: färdiga 2 ofärdiga 1, timmar: gjorda 55 ogjorda 1000

Den första övningspoängen ges för en fungerande applikation när alla användarinmatningar är felfria.

Hantering av inamtningsfel

För att få den andra övningspoängen för denna övning förväntas din applikation återhämta sig från felaktig användarinmatning. All inmatning som inte följer det angivna formatet ska ge ett felmeddelande felaktig inmatning och resultera i ännu en upprepning av loopen med begäran om en ny instruktion:

instruktion: 1\nbeskrivning: koda mobilapp för räknande av arbetsmängd\nprogrammerare och uppskattad arbetsmängd: erik xxx\nfelaktig inmatning

instruktion: 1\nbeskrivning: koda mobilapp för räknande av arbetsmängd\nprogrammerare och uppskattad arbetsmängd: erik\nfelaktig inmatning

instruktion: 4\nid: 1000000\nfelaktig inmatning

instruktion: 4\nid: XXXX\nfelaktig inmatning

instruktion: 6\nprogrammerare: okändprogrammerare\nfelaktig inmatning

Svara slutligen på en snabb enkät:

","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-11/4-fler-exempel-pa-rekursion.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-11/page-data.json b/page-data/osa-11/page-data.json index 1c662b844..4b568be68 100644 --- a/page-data/osa-11/page-data.json +++ b/page-data/osa-11/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-11", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-11","title":"Modul 11"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-11/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-11","title":"Modul 11"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-11/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-12/1-funktioner-som-argument/page-data.json b/page-data/osa-12/1-funktioner-som-argument/page-data.json index dca50411b..631df04ee 100644 --- a/page-data/osa-12/1-funktioner-som-argument/page-data.json +++ b/page-data/osa-12/1-funktioner-som-argument/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-12/1-funktioner-som-argument", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kan du sortera listor enligt olika kriterier"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att veta vad ett lambdauttryck är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kan du använda lambda-uttryck med andra Python-funktioner"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur en funktion skickas som argument till en annan funktion"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi är redan bekanta med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" och funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":", som används för att sortera listor i deras naturliga ordning. För siffror och strängar fungerar detta vanligtvis bra. För allt som är mer komplicerat än dem så är dock den naturliga ordningen på föremål enligt Python inte alltid det som vi som programmerare avser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel sorteras som standard en lista med tupler baserat på det första objektet i varje tupel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nprodukter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('apelsin', 4.5)\n('banan', 5.95)\n('vattenmelon', 4.95)\n('äppel', 3.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men vad händer om vi vill sortera listan baserat på priset?"}]},{"type":"element","tagName":"h2","properties":{"id":"funktioner-som-argument","style":"position:relative;"},"children":[{"type":"text","value":"Funktioner som argument"},{"type":"element","tagName":"a","properties":{"href":"#funktioner-som-argument","ariaLabel":"funktioner som argument permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En sorteringsmetod eller -funktion accepterar vanligtvis ett valfritt andra argument som gör att du kan kringgå standardsorteringskriterierna. Detta andra argument är en funktion som definierar hur värdet på varje föremål i listan bestäms. När listan sorteras anropar Python denna funktion när den jämför föremålen med varandra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ett exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Returnerar tupelns andra föremål, alltså priset"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Använd funktionen prisordning för sortering"}]},{"type":"text","value":"\n produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu är listan sorterad utifrån artiklarnas priser, men vad händer egentligen i programmet?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"text","value":" är faktiskt ganska enkel. Den tar ett objekt som sitt argument och returnerar ett värde för det objektet. Mer specifikt returnerar den det andra objektet i tupeln, som representerar priset. Men sedan har vi den här kodraden, där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"-metoden anropas:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"produkter.sort(key=prisordning)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här anropas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"-metoden med en funktion som argument. Detta är inte en referens till funktionens returvärde, utan en referens till själva funktionen. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sort"}]},{"type":"text","value":"-metoden anropar denna funktion flera gånger och använder varje objekt i listan som argument i tur och ordning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi inkluderar en extra print-sats i funktionsdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"text","value":" kan vi verifiera att funktionen verkligen anropas en gång för varje objekt i listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriver ut föremålet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Anropade prisordning("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Returnerar tupelns andra föremål, alltså priset"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n\nprodukter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Använd funktionen prisordning för sortering"}]},{"type":"text","value":"\nprodukter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anropade prisordning(('banan', 5.95))\nAnropade prisordning(('äppel', 3.95))\nAnropade prisordning(('apelsin', 4.5))\nAnropade prisordning(('vattenmelon', 4.95))\n('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordningen kan vändas med ett annat nyckelordsargument; "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse"}]},{"type":"text","value":", som är tillgängligt med både "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"-metoden och funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" reverse"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nt2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" reverse"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-funktionsdefinition-inom-en-funktionsdefinition","style":"position:relative;"},"children":[{"type":"text","value":"En funktionsdefinition inom en funktionsdefinition"},{"type":"element","tagName":"a","properties":{"href":"#en-funktionsdefinition-inom-en-funktionsdefinition","ariaLabel":"en funktionsdefinition inom en funktionsdefinition permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också inkludera en namngiven funktion för den nya prisbaserade sorteringsfunktionen som vi har skapat. Låt oss lägga till en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_pris"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sortera_enligt_pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremalen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Använd funktionen prisordning här"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremalen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nprodukter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sortera_enligt_pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vet att hjälpfunktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"text","value":" inte används någonstans utanför funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_pris"}]},{"type":"text","value":", kan vi placera den första funktionsdefinitionen inom den senare:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sortera_enligt_pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremalen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpfunktion definierad inom funktionen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremalen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sortering enligt återstående lager","tmcname":"osa12-01_aterstaende_lager"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_aterstaende_lager(foremal: list)"}]},{"type":"text","value":". Funktionen tar en lista med tupler som sitt argument. Tuplerna består av namn, pris och återstående lager för en produkt. Funktionen ska returnera en ny lista, där artiklarna är sorterade enligt återstående lager, med det lägsta värdet först. Den ursprungliga listan ska inte ändras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sortera_enligt_aterstaende_lager"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" st\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\napelsin 2 st\näppel 3 st\nbanan 12 st\nvattenmelon 22 st\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sortering enligt produktionssäsonger","tmcname":"osa12-02_produktionssasong"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_sasonger(foremal: list)"}]},{"type":"text","value":" som tar en lista med ordlistor som sitt argument. Varje ordlista innehåller information om ett enda TV-program. Funktionen ska sortera listan efter antalet säsonger som varje program har, i stigande ordning. Funktionen ska inte ändra den ursprungliga listan, utan istället returnera en ny lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"serier "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Friends\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Simpsons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" serie "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sortera_enligt_sasonger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"serier"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'namn'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'säsonger'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" säsonger\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\nDexter 9 säsonger\nFriends 10 säsonger\nSimpsons 32 säsonger\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sortering enligt poäng","tmcname":"osa12-03_enligt_poang"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_betyg(foremal: list)"}]},{"type":"text","value":" som tar en lista med ordlistor som sitt argument. Ordlistornas struktur är identisk med den i den föregående övningen. Denna funktion ska sortera ordlistorna i fallande ordning baserat på programmens betyg. Funktionen ska inte ändra den ursprungliga listan, utan returnera en ny lista istället."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"serier "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Friends\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Simpsons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betygsättning enligt IMDB\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" serie "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sortera_enligt_betyg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"serier"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'namn'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'betyg'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\nBetygsättning enligt IMDB\nFriends 8.9\nSimpsons 8.7\nDexter 8.6\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortering-av-samlingar-av-egna-objekt","style":"position:relative;"},"children":[{"type":"text","value":"Sortering av samlingar av egna objekt"},{"type":"element","tagName":"a","properties":{"href":"#sortering-av-samlingar-av-egna-objekt","ariaLabel":"sortering av samlingar av egna objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss med samma princip skriva ett program som sorterar en lista med objekt från vår egen klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" på två olika sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en enkel studerande \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" poang\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"), "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" sp.\""}]}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"enligt_id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"enligt_poang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"220"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"m321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a999\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"131"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n studeranden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Enligt id:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"enligt_id"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Enligt poäng:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"enligt_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enligt id:\nAnton (a123), 220 sp.\nAnna (a999), 131 sp.\nMaja (m321), 210 sp."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enligt poäng:\nAnna (a999), 131 sp.\nMaja (m321), 210 sp.\nAnton (a123), 220 sp."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se ovan fungerar sortering efter olika kriterier precis som det är tänkt. Om funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_id"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_studiepoang"}]},{"type":"text","value":" inte behövs någon annanstans finns det sätt att göra implementeringen enklare. Vi återkommer till detta ämne efter dessa övningar."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Klättringsrutt","tmcname":"osa12-04_klattringsrutt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbottnet innehåller en klassdefinition för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Klattringsrutt"}]},{"type":"text","value":", som fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rutt1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kantti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrutt2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrutt3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Syncro\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rutt3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rutt3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kantti, längd 38 meter, grade 6A+\nSmooth operator, längd 11 meter, grade 7A\nSyncro 14 8C+"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortera-enligt-langd","style":"position:relative;"},"children":[{"type":"text","value":"Sortera enligt längd"},{"type":"element","tagName":"a","properties":{"href":"#sortera-enligt-langd","ariaLabel":"sortera enligt langd permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_langd(rutter: list)"}]},{"type":"text","value":", som returnerar en ny lista av rutter sorterade enligt längd från längsta till kortaste."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kantti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Syncro\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Små steg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrutter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rutt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" enligt_langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kantti, längd 38 meter, grade 6A+\nSyncro, längd 14 meter, grade 8C+\nSmå steg, längd 12 meter, grade 6A+\nSmooth operator, längd 11 meter, grade 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortera-enligt-svarighetsgrad","style":"position:relative;"},"children":[{"type":"text","value":"Sortera enligt svårighetsgrad"},{"type":"element","tagName":"a","properties":{"href":"#sortera-enligt-svarighetsgrad","ariaLabel":"sortera enligt svarighetsgrad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_svarighet(rutter: list)"}]},{"type":"text","value":", som returnerar en ny lista av rutter sorterade enligt svårighet från svåraste till lättaste. För rutter med samma svårighet är den längre svårare. Skalan för svårighet är "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"4, 4+, 5, 5+, 6A, 6A+, ..."}]},{"type":"text","value":" som i praktiken fungerar enligt alfabetiska ordningen för strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kantti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Syncro\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Små steg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrutter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rutt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" enligt_svarighet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Syncro, längd 14 meter, grade 8C+\nSmooth operator, längd 11 meter, grade 7A\nKantti, längd 38 meter, grade 6A+\nSmå steg, längd 12 meter, grade 6A+"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" Ifall ordningen är baserad på en lista eller tupel, sorterar Python föremål per standard baserat på första föremålet, sedan baserat på det andra och så vidare:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"b\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"b\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[('a', 2), ('a', 4), ('b', 0), ('b', 30)]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Klättringsområde","tmcname":"osa12-05_klattringsomrade/"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns förutom klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Klattringsrutt"}]},{"type":"text","value":" dessutom klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Klattringsomrade"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"o1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsomrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olhava\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kantti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Stora snittet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"36"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Svensk rutt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"5+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\no2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsomrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nummi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Syncro\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\no3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsomrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nalkkilan släbi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Små steg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grisen gillar inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6B+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fruktträdgård\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rutter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"svaraste_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olhava, 3 rutter, svåraste 6B\nNalkkilan släbi 4\nSmooth operator, längd 9 meter, grade 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortera-enligt-antalet-rutter","style":"position:relative;"},"children":[{"type":"text","value":"Sortera enligt antalet rutter"},{"type":"element","tagName":"a","properties":{"href":"#sortera-enligt-antalet-rutter","ariaLabel":"sortera enligt antalet rutter permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_antal_rutter"}]},{"type":"text","value":", som sorterar klättringsområdena enligt antalet rutter de har i ökande ordning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# o1, o2 och o3 definierade enligt ovan"}]},{"type":"text","value":"\nomraden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"o1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" omrade "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" enligt_antal_rutter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"omraden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"omrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nummi, 1 rutter, svåraste 8C+\nOlhava, 3 rutter, svåraste 6B\nNalkkilan slabi, 4 rutter, svåraste 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortera-enligt-svaraste-rutt","style":"position:relative;"},"children":[{"type":"text","value":"Sortera enligt svåraste rutt"},{"type":"element","tagName":"a","properties":{"href":"#sortera-enligt-svaraste-rutt","ariaLabel":"sortera enligt svaraste rutt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_svaraste_rutt"}]},{"type":"text","value":", som sorterar klättringsområdena enligt högsta svårighetsgraden de har i "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"minskande"}]},{"type":"text","value":" ordning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# o1, o2 och o3 definierade enligt ovan"}]},{"type":"text","value":"\nomraden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"o1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" omrade "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" enligt_svaraste_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"omraden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"omrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nummi, 1 rutter, svåraste 8C+\nNalkkilan slabi, 4 rutter, svåraste 7A\nOlhava, 3 rutter, svåraste 6B"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"lambda-uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Lambda-uttryck"},{"type":"element","tagName":"a","properties":{"href":"#lambda-uttryck","ariaLabel":"lambda uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har mest arbetat med funktioner ur modularitetssynpunkt. Det är sant att funktioner spelar en viktig roll när det gäller att hantera komplexiteten i dina program och undvika upprepning av kod. Funktioner skrivs vanligtvis så att de kan användas många gånger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men ibland behöver man något som liknar en funktion som man bara använder en gång. Med lambda-uttryck kan du skapa små, anonyma funktioner som skapas (och kasseras) när de behövs i koden. Den allmänna syntaxen är som följer:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda : "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att sortera en lista med tupler efter det andra objektet i varje tupel skulle se ut så här implementerat med ett lambda-uttryck:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Funktionen skapas \"i farten\" med ett lambda-uttryck:"}]},{"type":"text","value":"\nprodukter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uttrycket"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda föremål: föremål[1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Är ekvivalent med funktionsdefinitionen"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"förutom det faktum att en lambdafunktion inte har något namn. Det är därför lambda-funktioner kallas anonyma funktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I alla andra avseenden skiljer sig inte en lambda-funktion från någon annan funktion, och de kan användas i alla samma sammanhang som en motsvarande namngiven funktion. Följande program sorterar till exempel en lista med strängar i alfabetisk ordning enligt det sista tecknet i varje sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mikael\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Makke\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Markus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strangar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Maja\nMinna\nMakke\nMikael\nMarkus"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också kombinera list comprehensions, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":"-metoden och lambda-uttryck. Vi kan till exempel sortera strängar baserat på enbart vokalerna i dem och ignorera alla andra tecken:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mikael\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Makke\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Markus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strangar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"m "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" m "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" m "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiouyäö\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Maja\nMakke\nMarkus\nMinna\nMikael"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anonyma funktioner kan också användas med andra inbyggda Python-funktioner, inte bara de som används för sortering. Till exempel tar funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" också ett nyckelordsargument som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"key"}]},{"type":"text","value":". Det används som kriterium för att jämföra objekten när det lägsta eller högsta värdet väljs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel handlar det om ljudinspelningar. Först väljer vi den äldsta inspelningen och sedan den längsta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Skiva"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en enkel skiva \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" artist"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"artist "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" artist\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ar\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"artist"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"), "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":". "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" min.\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n l1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nevermind\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nirvana\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1991"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"43"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n l2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Let It Be\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Beatles\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1969"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n l3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Joshua Tree\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"U2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1986"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n skivor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"l1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" l2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" l3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Äldsta skiva:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skivor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Längsta skiva: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skivor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Äldsta skiva:\nLet It Be (Beatles), 1969. 35 min.\nLängsta skiva:\nU2 (Joshua Tree), 1986. 50 min."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bollspelare","tmcname":"osa12-06_bollspelare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbotten innehåller en definition för en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bollspelare"}]},{"type":"text","value":", som har följande offentliga attribut:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"spelnummer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"mängden gjorda mål "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mal"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"mängden målpassningar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"malpassningar"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"minuter spelade "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minuter"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera följande funktioner. OBS: varje funktion har olika typer av returvärden."}]},{"type":"element","tagName":"h2","properties":{"id":"flest-mal","style":"position:relative;"},"children":[{"type":"text","value":"Flest mål"},{"type":"element","tagName":"a","properties":{"href":"#flest-mal","ariaLabel":"flest mal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flest_mal"}]},{"type":"text","value":", som får bollspelare som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera namnet av spelaren som gjort flest mål, i strängformat."}]},{"type":"element","tagName":"h2","properties":{"id":"flest-poang","style":"position:relative;"},"children":[{"type":"text","value":"Flest poäng"},{"type":"element","tagName":"a","properties":{"href":"#flest-poang","ariaLabel":"flest poang permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flest_poang"}]},{"type":"text","value":", som får en bollspelare som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en tupel som innehåller namnet och skjortnumret av spelaren som fått flest poäng. Den totala mängden av poäng är antalet mål och antalet målpassningar ihopslaget."}]},{"type":"element","tagName":"h2","properties":{"id":"minst-minuter","style":"position:relative;"},"children":[{"type":"text","value":"Minst minuter"},{"type":"element","tagName":"a","properties":{"href":"#minst-minuter","ariaLabel":"minst minuter permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minst_minuter"}]},{"type":"text","value":", som får en bollspelare som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bollspelare"}]},{"type":"text","value":"-objektet som har den minsta mängden minuter spelade."}]},{"type":"element","tagName":"h2","properties":{"id":"testprogram","style":"position:relative;"},"children":[{"type":"text","value":"Testprogram"},{"type":"element","tagName":"a","properties":{"href":"#testprogram","ariaLabel":"testprogram permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan testa dina funktioner med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n spelare1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kalle Ankka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"46"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n spelare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Långben\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n spelare3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Musse Pigg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n spelare4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n spelare5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nalle Puh\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"spelare1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"flest_mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"flest_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minst_minuter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta ska skriva ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kalle Ankka\n('Musse Pigg', 9)\nBollspelare(namn=Nalle Puh, spelnummer=4, mål=3, målpassningar=9, minuter=12)"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"funktioner-som-argument-inom-egna-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Funktioner som argument inom egna funktioner"},{"type":"element","tagName":"a","properties":{"href":"#funktioner-som-argument-inom-egna-funktioner","ariaLabel":"funktioner som argument inom egna funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi konstaterade ovan att det är möjligt att skicka en referens till en funktion som argument till en annan funktion. Som avslutning på detta avsnitt skriver vi en egen funktion som tar en funktion som argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# typledtråden callable refererar till en funktion"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"utfor_operation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"callable"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Anropa funktionen som passerades som argument"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" b\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"utfor_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"utfor_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"utfor_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"15\n50\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det värde som returneras av funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"utfor_operation"}]},{"type":"text","value":" beror på vilken funktion som skickades som argument. Vilken funktion som helst som tar emot två argument skulle duga, oavsett om den är anonym eller namngiven."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skicka referenser till funktioner som argument till andra funktioner är kanske inte något som du kommer att göra dagligen under din programmeringskarriär, men det kan vara en användbar teknik. Följande program väljer ut några rader från en fil och skriver dem till en annan fil. Hur raderna väljs ut bestäms av en funktion som returnerar True endast om raderna ska kopieras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kopiera_rader"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kalla_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mal_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kriterie"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kalla_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" kalla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mal_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kalla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ta bort all tomrum från början och slutet av raden"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kriterie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Exempel"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall tredje parametern inte är angiven, kopiera alla rader"}]},{"type":"text","value":"\n kopiera_rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kopiera alla icke-tomma rader"}]},{"type":"text","value":"\n kopiera_rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kopierar alla rader som innehåller ordet \"Python\""}]},{"type":"text","value":"\n kopiera_rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kopierar alla rader som inte slutar med en punkt"}]},{"type":"text","value":"\n kopiera_rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionsdefinitionen innehåller ett standardvärde för nyckelordsparametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kriterie"}]},{"type":"text","value":": "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda x: True"}]},{"type":"text","value":". Denna anonyma funktion returnerar alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" oavsett indata. Standardbeteendet är alltså att kopiera alla rader. Som vanligt gäller att om ett värde anges för en parameter med ett standardvärde, ersätter det nya värdet standardvärdet."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Söking av produkter","tmcname":"osa12-07_sokning_av_produkter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här övningen hanterar produkter som förvaras som tupler. Exemplen antar en variabel med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"produkter"}]},{"type":"text","value":" som förses med följande värde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kål\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje tupel innehåller tre föremål: namn, pris och antal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sok(produkter: list, kriterie: callable)"}]},{"type":"text","value":". Det andra argumentet till funktionen är en funktion i sig, och den ska kunna bearbeta en tupel enligt definitionen ovan och returnera ett booleskt värde. Sökfunktionen ska returnera en ny lista som innehåller de tupler från originalet som uppfyller kriteriet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett passande kriterie kunde exempelvis vara följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris_under_4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar alltså "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" ifall produktens pris, tupelns andra föremål, är mindre än 4."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sok"}]},{"type":"text","value":" fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris_under_4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('äppel', 3.95, 3)\n('kål', 0.99, 1)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kriteriefunktionen kan också vara en lambda-funktion. Om vi bara ville söka efter de produkter vars belopp är minst 11, skulle vi kunna skapa följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('banan', 5.95, 12)\n('vattenmelon', 4.95, 22)"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Kan du sortera listor enligt olika kriterier
  • \n
  • Kommer du att veta vad ett lambdauttryck är
  • \n
  • Kan du använda lambda-uttryck med andra Python-funktioner
  • \n
  • Vet du hur en funktion skickas som argument till en annan funktion
  • \n

Vi är redan bekanta med metoden sort och funktionen sorted, som används för att sortera listor i deras naturliga ordning. För siffror och strängar fungerar detta vanligtvis bra. För allt som är mer komplicerat än dem så är dock den naturliga ordningen på föremål enligt Python inte alltid det som vi som programmerare avser.

Till exempel sorteras som standard en lista med tupler baserat på det första objektet i varje tupel:

produkter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\nprodukter.sort()\n\nfor produkt in produkter:\n    print(produkt)

('apelsin', 4.5)\n('banan', 5.95)\n('vattenmelon', 4.95)\n('äppel', 3.95)

Men vad händer om vi vill sortera listan baserat på priset?

Funktioner som argument

En sorteringsmetod eller -funktion accepterar vanligtvis ett valfritt andra argument som gör att du kan kringgå standardsorteringskriterierna. Detta andra argument är en funktion som definierar hur värdet på varje föremål i listan bestäms. När listan sorteras anropar Python denna funktion när den jämför föremålen med varandra.

Låt oss ta en titt på ett exempel:

def prisordning(foremal: tuple):\n    # Returnerar tupelns andra föremål, alltså priset\n    return foremal[1]\n\nif __name__ == \"__main__\":\n    produkter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\n    # Använd funktionen prisordning för sortering\n    produkter.sort(key=prisordning)\n\n    for produkt in produkter:\n        print(produkt)

('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)

Nu är listan sorterad utifrån artiklarnas priser, men vad händer egentligen i programmet?

Funktionen prisordning är faktiskt ganska enkel. Den tar ett objekt som sitt argument och returnerar ett värde för det objektet. Mer specifikt returnerar den det andra objektet i tupeln, som representerar priset. Men sedan har vi den här kodraden, där sort-metoden anropas:

produkter.sort(key=prisordning)

Här anropas sort-metoden med en funktion som argument. Detta är inte en referens till funktionens returvärde, utan en referens till själva funktionen. Sort-metoden anropar denna funktion flera gånger och använder varje objekt i listan som argument i tur och ordning.

Om vi inkluderar en extra print-sats i funktionsdefinitionen för prisordning kan vi verifiera att funktionen verkligen anropas en gång för varje objekt i listan:

def prisordning(foremal: tuple):\n    # Skriver ut föremålet\n    print(f\"Anropade prisordning({foremal})\")\n\n    # Returnerar tupelns andra föremål, alltså priset\n    return foremal[1]\n\n\nprodukter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\n# Använd funktionen prisordning för sortering\nprodukter.sort(key=prisordning)\n\nfor produkt in produkter:\n    print(produkt)

Anropade prisordning(('banan', 5.95))\nAnropade prisordning(('äppel', 3.95))\nAnropade prisordning(('apelsin', 4.5))\nAnropade prisordning(('vattenmelon', 4.95))\n('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)

Ordningen kan vändas med ett annat nyckelordsargument; reverse, som är tillgängligt med både sort-metoden och funktionen sorted:

produkter.sort(key=prisordning, reverse=True)\n\nt2 = sorted(produkter, key=prisordning, reverse=True)

En funktionsdefinition inom en funktionsdefinition

Vi kan också inkludera en namngiven funktion för den nya prisbaserade sorteringsfunktionen som vi har skapat. Låt oss lägga till en funktion med namnet sortera_enligt_pris:

def prisordning(foremal: tuple):\n    return foremal[1]\n\ndef sortera_enligt_pris(foremalen: list):\n    # Använd funktionen prisordning här\n    return sorted(foremalen, key=prisordning)\n\nprodukter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\nfor produkt in sortera_enligt_pris(produkter):\n    print(produkt)

Om vi vet att hjälpfunktionen prisordning inte används någonstans utanför funktionen sortera_enligt_pris, kan vi placera den första funktionsdefinitionen inom den senare:

def sortera_enligt_pris(foremalen: list):\n    # hjälpfunktion definierad inom funktionen\n    def prisordning(foremal: tuple):\n        return foremal[1]\n\n    return sorted(foremalen, key=prisordning)

Skapa en funktion med namnet sortera_enligt_aterstaende_lager(foremal: list). Funktionen tar en lista med tupler som sitt argument. Tuplerna består av namn, pris och återstående lager för en produkt. Funktionen ska returnera en ny lista, där artiklarna är sorterade enligt återstående lager, med det lägsta värdet först. Den ursprungliga listan ska inte ändras.

Funktionen ska fungera på följande sätt:

produkter = [(\"banan\", 5.95, 12), (\"äppel\", 3.95, 3), (\"apelsin\", 4.50, 2), (\"vattenmelon\", 4.95, 22)]\n\nfor produkt in sortera_enligt_aterstaende_lager(produkter):\n    print(f\"{produkt[0]} {produkt[2]} st\")
\napelsin 2 st\näppel 3 st\nbanan 12 st\nvattenmelon 22 st\n

Skapa en funktion med namnet sortera_enligt_sasonger(foremal: list) som tar en lista med ordlistor som sitt argument. Varje ordlista innehåller information om ett enda TV-program. Funktionen ska sortera listan efter antalet säsonger som varje program har, i stigande ordning. Funktionen ska inte ändra den ursprungliga listan, utan istället returnera en ny lista.

Funktionen ska fungera på följande sätt:

serier = [{ \"namn\": \"Dexter\", \"betyg\" : 8.6, \"säsonger\":9 }, { \"namn\": \"Friends\", \"betyg\" : 8.9, \"säsonger\":10 },  { \"namn\": \"Simpsons\", \"betyg\" : 8.7, \"säsonger\":32 }  ]\n\nfor serie in sortera_enligt_sasonger(serier):\n    print(f\"{serie['namn']}  {serie['säsonger']} säsonger\")
\nDexter 9 säsonger\nFriends 10 säsonger\nSimpsons 32 säsonger\n

Skapa en funktion med namnet sortera_enligt_betyg(foremal: list) som tar en lista med ordlistor som sitt argument. Ordlistornas struktur är identisk med den i den föregående övningen. Denna funktion ska sortera ordlistorna i fallande ordning baserat på programmens betyg. Funktionen ska inte ändra den ursprungliga listan, utan returnera en ny lista istället.

serier = [{ \"namn\": \"Dexter\", \"betyg\" : 8.6, \"säsonger\":9 }, { \"namn\": \"Friends\", \"betyg\" : 8.9, \"säsonger\":10 },  { \"namn\": \"Simpsons\", \"betyg\" : 8.7, \"säsonger\":32 }  ]\n\nprint(\"Betygsättning enligt IMDB\")\nfor serie in sortera_enligt_betyg(serier):\n    print(f\"{serie['namn']}  {serie['betyg']}\")
\nBetygsättning enligt IMDB\nFriends 8.9\nSimpsons 8.7\nDexter 8.6\n

Sortering av samlingar av egna objekt

Låt oss med samma princip skriva ett program som sorterar en lista med objekt från vår egen klass Studerande på två olika sätt:

class Studerande:\n    \"\"\" Klassen modellerar en enkel studerande \"\"\"\n    def __init__(self, namn: str, id: str, poang: int):\n        self.namn = namn\n        self.id = id\n        self.poang = poang\n\n    def __str__(self):\n        return f\"{self.namn} ({self.id}), {self.poang} sp.\"\n\n\ndef enligt_id(foremal: Studerande):\n    return foremal.id\n\ndef enligt_poang(foremal: Studerande):\n    return foremal.poang\n\n\nif __name__ == \"__main__\":\n    s1 = Studerande(\"Anton\", \"a123\", 220)\n    s2 = Studerande(\"Maja\", \"m321\", 210)\n    s3 = Studerande(\"Anna\", \"a999\", 131)\n\n    studeranden = [s1, s2, s3]\n\n    print(\"Enligt id:\")\n    for studerande in sorted(studeranden, key=enligt_id):\n        print(studerande)\n\n    print()\n\n    print(\"Enligt poäng:\")\n    for studerande in sorted(studeranden, key=enligt_poang):\n        print(studerande)

Enligt id:\nAnton (a123), 220 sp.\nAnna (a999), 131 sp.\nMaja (m321), 210 sp.

Enligt poäng:\nAnna (a999), 131 sp.\nMaja (m321), 210 sp.\nAnton (a123), 220 sp.

Som du kan se ovan fungerar sortering efter olika kriterier precis som det är tänkt. Om funktionerna enligt_id och enligt_studiepoang inte behövs någon annanstans finns det sätt att göra implementeringen enklare. Vi återkommer till detta ämne efter dessa övningar.

Uppgiftsbottnet innehåller en klassdefinition för Klattringsrutt, som fungerar enligt följande:

rutt1 = Klattringsrutt(\"Kantti\", 38, \"6A+\")\nrutt2 = Klattringsrutt(\"Smooth operator\", 11, \"7A\")\nrutt3 = Klattringsrutt(\"Syncro\", 14, \"8C+\")\n\n\nprint(rutt1)\nprint(rutt2)\nprint(rutt3.namn, rutt3.langd, rutt3.grade)

Kantti, längd 38 meter, grade 6A+\nSmooth operator, längd 11 meter, grade 7A\nSyncro 14 8C+

Sortera enligt längd

Skapa funktionen enligt_langd(rutter: list), som returnerar en ny lista av rutter sorterade enligt längd från längsta till kortaste.

Funktionen ska fungera enligt följande:

r1 = Klattringsrutt(\"Kantti\", 38, \"6A+\")\nr2 = Klattringsrutt(\"Smooth operator\", 11, \"7A\")\nr3 = Klattringsrutt(\"Syncro\", 14, \"8C+\")\nr4 = Klattringsrutt(\"Små steg\", 12, \"6A+\")\n\nrutter = [r1, r2, r3, r4]\n\nfor rutt in enligt_langd(rutter):\n    print(rutt)

Kantti, längd 38 meter, grade 6A+\nSyncro, längd 14 meter, grade 8C+\nSmå steg, längd 12 meter, grade 6A+\nSmooth operator, längd 11 meter, grade 7A

Sortera enligt svårighetsgrad

Skapa funktionen enligt_svarighet(rutter: list), som returnerar en ny lista av rutter sorterade enligt svårighet från svåraste till lättaste. För rutter med samma svårighet är den längre svårare. Skalan för svårighet är 4, 4+, 5, 5+, 6A, 6A+, ... som i praktiken fungerar enligt alfabetiska ordningen för strängar.

Funktionen ska fungera enligt följande:

r1 = Klattringsrutt(\"Kantti\", 38, \"6A+\")\nr2 = Klattringsrutt(\"Smooth operator\", 11, \"7A\")\nr3 = Klattringsrutt(\"Syncro\", 14, \"8C+\")\nr4 = Klattringsrutt(\"Små steg\", 12, \"6A+\")\n\nrutter = [r1, r2, r3, r4]\nfor rutt in enligt_svarighet(rutter):\n    print(rutt)

Syncro, längd 14 meter, grade 8C+\nSmooth operator, längd 11 meter, grade 7A\nKantti, längd 38 meter, grade 6A+\nSmå steg, längd 12 meter, grade 6A+

Tips: Ifall ordningen är baserad på en lista eller tupel, sorterar Python föremål per standard baserat på första föremålet, sedan baserat på det andra och så vidare:

lista = [(\"a\", 4),(\"a\", 2),(\"b\", 30), (\"b\", 0) ]\nprint(sorted(lista))

[('a', 2), ('a', 4), ('b', 0), ('b', 30)]

I uppgiftsbotten finns förutom klassen Klattringsrutt dessutom klassen Klattringsomrade.

o1 = Klattringsomrade(\"Olhava\")\no1.tillsatt_rutt(Klattringsrutt(\"Kantti\", 38, \"6A+\"))\no1.tillsatt_rutt(Klattringsrutt(\"Stora snittet\", 36, \"6B\"))\no1.tillsatt_rutt(Klattringsrutt(\"Svensk rutt\", 42, \"5+\"))\n\no2 = Klattringsomrade(\"Nummi\")\no2.tillsatt_rutt(Klattringsrutt(\"Syncro\", 14, \"8C+\"))\n\no3 = Klattringsomrade(\"Nalkkilan släbi\")\no3.tillsatt_rutt(Klattringsrutt(\"Små steg\", 12, \"6A+\"))\no3.tillsatt_rutt(Klattringsrutt(\"Smooth operator\", 11, \"7A\"))\no3.tillsatt_rutt(Klattringsrutt(\"Grisen gillar inte\", 12 , \"6B+\"))\no3.tillsatt_rutt(Klattringsrutt(\"Fruktträdgård\", 8, \"6A\"))\n\nprint(o1)\nprint(o3.namn, o3.rutter())\nprint(o3.svaraste_rutt())

Olhava, 3 rutter, svåraste 6B\nNalkkilan släbi 4\nSmooth operator, längd 9 meter, grade 7A

Sortera enligt antalet rutter

Skapa funktionen enligt_antal_rutter, som sorterar klättringsområdena enligt antalet rutter de har i ökande ordning.

# o1, o2 och o3 definierade enligt ovan\nomraden = [o1, o2, o3]\nfor omrade in enligt_antal_rutter(omraden):\n    print(omrade)\n

Nummi, 1 rutter, svåraste 8C+\nOlhava, 3 rutter, svåraste 6B\nNalkkilan slabi, 4 rutter, svåraste 7A

Sortera enligt svåraste rutt

Skapa funktionen enligt_svaraste_rutt, som sorterar klättringsområdena enligt högsta svårighetsgraden de har i minskande ordning.

# o1, o2 och o3 definierade enligt ovan\nomraden = [o1, o2, o3]\nfor omrade in enligt_svaraste_rutt(omraden):\n    print(omrade)\n

Nummi, 1 rutter, svåraste 8C+\nNalkkilan slabi, 4 rutter, svåraste 7A\nOlhava, 3 rutter, svåraste 6B

Lambda-uttryck

Vi har mest arbetat med funktioner ur modularitetssynpunkt. Det är sant att funktioner spelar en viktig roll när det gäller att hantera komplexiteten i dina program och undvika upprepning av kod. Funktioner skrivs vanligtvis så att de kan användas många gånger.

Men ibland behöver man något som liknar en funktion som man bara använder en gång. Med lambda-uttryck kan du skapa små, anonyma funktioner som skapas (och kasseras) när de behövs i koden. Den allmänna syntaxen är som följer:

lambda <parametrar> : <uttryck>

Att sortera en lista med tupler efter det andra objektet i varje tupel skulle se ut så här implementerat med ett lambda-uttryck:

produkter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\n# Funktionen skapas \"i farten\" med ett lambda-uttryck:\nprodukter.sort(key=lambda foremal: foremal[1])\n\nfor produkt in produkter:\n    print(produkt)

('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)

Uttrycket

lambda föremål: föremål[1]

Är ekvivalent med funktionsdefinitionen

\ndef pris(foremal):\n    return foremal[1]

förutom det faktum att en lambdafunktion inte har något namn. Det är därför lambda-funktioner kallas anonyma funktioner.

I alla andra avseenden skiljer sig inte en lambda-funktion från någon annan funktion, och de kan användas i alla samma sammanhang som en motsvarande namngiven funktion. Följande program sorterar till exempel en lista med strängar i alfabetisk ordning enligt det sista tecknet i varje sträng:

strangar = [\"Mikael\", \"Makke\", \"Maja\", \"Markus\", \"Minna\"]\n\nfor strang in sorted(strangar, key=lambda strang: strang[-1]):\n    print(strang)

Maja\nMinna\nMakke\nMikael\nMarkus

Vi kan också kombinera list comprehensions, join-metoden och lambda-uttryck. Vi kan till exempel sortera strängar baserat på enbart vokalerna i dem och ignorera alla andra tecken:

strangar = [\"Mikael\", \"Makke\", \"Maja\", \"Markus\", \"Minna\"]\n\nfor strang in sorted(strangar, key=lambda strang: \"\".join([m for m in strang if m in \"aeiouyäö\"])):\n    print(strang)

Maja\nMakke\nMarkus\nMinna\nMikael

Anonyma funktioner kan också användas med andra inbyggda Python-funktioner, inte bara de som används för sortering. Till exempel tar funktionerna min och max också ett nyckelordsargument som heter key. Det används som kriterium för att jämföra objekten när det lägsta eller högsta värdet väljs.

I följande exempel handlar det om ljudinspelningar. Först väljer vi den äldsta inspelningen och sedan den längsta:

\nclass Skiva:\n    \"\"\" Klassen modellerar en enkel skiva \"\"\"\n    def __init__(self, namn: str, artist: str, ar: int, langd: int):\n        self.namn = namn\n        self.artist = artist\n        self.ar = ar\n        self.langd = langd\n\n\n    def __str__(self):\n        return f\"{self.namn} ({self.artist}), {self.ar}. {self.langd} min.\"\n\nif __name__ == \"__main__\":\n    l1 = Skiva(\"Nevermind\", \"Nirvana\", 1991, 43)\n    l2 = Skiva(\"Let It Be\", \"Beatles\", 1969, 35)\n    l3 = Skiva(\"Joshua Tree\", \"U2\", 1986, 50)\n\n    skivor = [l1, l2, l3]\n\n\n    print(\"Äldsta skiva:\")\n    print(min(skivor, key=lambda skiva: skiva.ar))\n\n    print(\"Längsta skiva: \")\n    print(max(skivor, key=lambda skiva: skiva.langd))

Äldsta skiva:\nLet It Be (Beatles), 1969. 35 min.\nLängsta skiva:\nU2 (Joshua Tree), 1986. 50 min.

Uppgiftsbotten innehåller en definition för en klass med namnet Bollspelare, som har följande offentliga attribut:

    \n
  • namn
  • \n
  • spelnummer
  • \n
  • mängden gjorda mål mal
  • \n
  • mängden målpassningar malpassningar
  • \n
  • minuter spelade minuter
  • \n

Implementera följande funktioner. OBS: varje funktion har olika typer av returvärden.

Flest mål

Skapa funktionen flest_mal, som får bollspelare som argument.

Funktionen ska returnera namnet av spelaren som gjort flest mål, i strängformat.

Flest poäng

Skapa funktionen flest_poang, som får en bollspelare som argument.

Funktionen ska returnera en tupel som innehåller namnet och skjortnumret av spelaren som fått flest poäng. Den totala mängden av poäng är antalet mål och antalet målpassningar ihopslaget.

Minst minuter

Skapa funktionen minst_minuter, som får en bollspelare som argument.

Funktionen ska returnera Bollspelare-objektet som har den minsta mängden minuter spelade.

Testprogram

Du kan testa dina funktioner med följande program:

if __name__ == \"__main__\":\n    spelare1 = Bollspelare(\"Kalle Ankka\", 13, 5, 12, 46)\n    spelare2 = Bollspelare(\"Långben\", 7, 2, 26, 55)\n    spelare3 = Bollspelare(\"Musse Pigg\", 9, 1, 32, 26)\n    spelare4 = Bollspelare(\"Peter Pan\", 12, 1, 11, 41)\n    spelare5 = Bollspelare(\"Nalle Puh\", 4, 3, 9, 12)\n\n    lag = [spelare1, spelare2, spelare3, spelare4, spelare5]\n    print(flest_mal(lag))\n    print(flest_poang(lag))\n    print(minst_minuter(lag))

Detta ska skriva ut:

Kalle Ankka\n('Musse Pigg', 9)\nBollspelare(namn=Nalle Puh, spelnummer=4, mål=3, målpassningar=9, minuter=12)

Funktioner som argument inom egna funktioner

Vi konstaterade ovan att det är möjligt att skicka en referens till en funktion som argument till en annan funktion. Som avslutning på detta avsnitt skriver vi en egen funktion som tar en funktion som argument.

# typledtråden callable refererar till en funktion\ndef utfor_operation(operation: callable):\n    # Anropa funktionen som passerades som argument\n    return operation(10, 5)\n\ndef summa(a: int, b: int):\n    return a + b\n\ndef produkt(a: int, b: int):\n    return a * b\n\n\nif __name__ == \"__main__\":\n    print(utfor_operation(summa))\n    print(utfor_operation(produkt))\n    print(utfor_operation(lambda x,y: x - y))\n

15\n50\n5

Det värde som returneras av funktionen utfor_operation beror på vilken funktion som skickades som argument. Vilken funktion som helst som tar emot två argument skulle duga, oavsett om den är anonym eller namngiven.

Att skicka referenser till funktioner som argument till andra funktioner är kanske inte något som du kommer att göra dagligen under din programmeringskarriär, men det kan vara en användbar teknik. Följande program väljer ut några rader från en fil och skriver dem till en annan fil. Hur raderna väljs ut bestäms av en funktion som returnerar True endast om raderna ska kopieras:

def kopiera_rader(kalla_namn: str, mal_namn: str, kriterie= lambda x: True):\n    with open(kalla_namn) as kalla, open(mal_namn, \"w\") as mal:\n        for rad in kalla:\n            # Ta bort all tomrum från början och slutet av raden\n            rad = rad.strip()\n\n            if kriterie(rad):\n                mal.write(rad + \"\\n\")\n\n# Exempel\nif __name__ == \"__main__\":\n    # Ifall tredje parametern inte är angiven, kopiera alla rader\n    kopiera_rader(\"första.txt\", \"andra.txt\")\n\n    # Kopiera alla icke-tomma rader\n    kopiera_rader(\"första.txt\", \"andra.txt\", lambda rad: len(rad) > 0)\n\n    # Kopierar alla rader som innehåller ordet \"Python\"\n    kopiera_rader(\"första.txt\", \"andra.txt\", lambda rad: \"Python\" in rad)\n\n    # Kopierar alla rader som inte slutar med en punkt\n    kopiera_rader(\"första.txt\", \"andra.txt\", lambda rad: rad[-1] != \".\")

Funktionsdefinitionen innehåller ett standardvärde för nyckelordsparametern kriterie: lambda x: True. Denna anonyma funktion returnerar alltid True oavsett indata. Standardbeteendet är alltså att kopiera alla rader. Som vanligt gäller att om ett värde anges för en parameter med ett standardvärde, ersätter det nya värdet standardvärdet.

Den här övningen hanterar produkter som förvaras som tupler. Exemplen antar en variabel med namnet produkter som förses med följande värde:

produkter = [(\"banan\", 5.95, 12), (\"äppel\", 3.95, 3), (\"apelsin\", 4.50, 2), (\"vattenmelon\", 4.95, 22), (\"kål\", 0.99, 1)]

Varje tupel innehåller tre föremål: namn, pris och antal.

Skapa funktionen sok(produkter: list, kriterie: callable). Det andra argumentet till funktionen är en funktion i sig, och den ska kunna bearbeta en tupel enligt definitionen ovan och returnera ett booleskt värde. Sökfunktionen ska returnera en ny lista som innehåller de tupler från originalet som uppfyller kriteriet.

Ett passande kriterie kunde exempelvis vara följande:

def pris_under_4(produkt):\n    return produkt[1] < 4

Funktionen returnerar alltså True ifall produktens pris, tupelns andra föremål, är mindre än 4.

Funktionen sok fungerar enligt följande:

for produkt in sok(produkter, pris_under_4):\n    print(produkt)

('äppel', 3.95, 3)\n('kål', 0.99, 1)

Kriteriefunktionen kan också vara en lambda-funktion. Om vi bara ville söka efter de produkter vars belopp är minst 11, skulle vi kunna skapa följande:

for produkt in sok(produkter, lambda t: t[2]>10):\n    print(produkt)

('banan', 5.95, 12)\n('vattenmelon', 4.95, 22)

","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/1-funktioner-som-argument.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kan du sortera listor enligt olika kriterier"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att veta vad ett lambdauttryck är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kan du använda lambda-uttryck med andra Python-funktioner"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur en funktion skickas som argument till en annan funktion"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi är redan bekanta med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" och funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":", som används för att sortera listor i deras naturliga ordning. För siffror och strängar fungerar detta vanligtvis bra. För allt som är mer komplicerat än dem så är dock den naturliga ordningen på föremål enligt Python inte alltid det som vi som programmerare avser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel sorteras som standard en lista med tupler baserat på det första objektet i varje tupel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nprodukter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('apelsin', 4.5)\n('banan', 5.95)\n('vattenmelon', 4.95)\n('äppel', 3.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men vad händer om vi vill sortera listan baserat på priset?"}]},{"type":"element","tagName":"h2","properties":{"id":"funktioner-som-argument","style":"position:relative;"},"children":[{"type":"text","value":"Funktioner som argument"},{"type":"element","tagName":"a","properties":{"href":"#funktioner-som-argument","ariaLabel":"funktioner som argument permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En sorteringsmetod eller -funktion accepterar vanligtvis ett valfritt andra argument som gör att du kan kringgå standardsorteringskriterierna. Detta andra argument är en funktion som definierar hur värdet på varje föremål i listan bestäms. När listan sorteras anropar Python denna funktion när den jämför föremålen med varandra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ett exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Returnerar tupelns andra föremål, alltså priset"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Använd funktionen prisordning för sortering"}]},{"type":"text","value":"\n produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu är listan sorterad utifrån artiklarnas priser, men vad händer egentligen i programmet?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"text","value":" är faktiskt ganska enkel. Den tar ett objekt som sitt argument och returnerar ett värde för det objektet. Mer specifikt returnerar den det andra objektet i tupeln, som representerar priset. Men sedan har vi den här kodraden, där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"-metoden anropas:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"produkter.sort(key=prisordning)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här anropas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"-metoden med en funktion som argument. Detta är inte en referens till funktionens returvärde, utan en referens till själva funktionen. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sort"}]},{"type":"text","value":"-metoden anropar denna funktion flera gånger och använder varje objekt i listan som argument i tur och ordning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi inkluderar en extra print-sats i funktionsdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"text","value":" kan vi verifiera att funktionen verkligen anropas en gång för varje objekt i listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriver ut föremålet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Anropade prisordning("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Returnerar tupelns andra föremål, alltså priset"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n\nprodukter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Använd funktionen prisordning för sortering"}]},{"type":"text","value":"\nprodukter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anropade prisordning(('banan', 5.95))\nAnropade prisordning(('äppel', 3.95))\nAnropade prisordning(('apelsin', 4.5))\nAnropade prisordning(('vattenmelon', 4.95))\n('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordningen kan vändas med ett annat nyckelordsargument; "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse"}]},{"type":"text","value":", som är tillgängligt med både "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"-metoden och funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" reverse"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nt2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" reverse"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-funktionsdefinition-inom-en-funktionsdefinition","style":"position:relative;"},"children":[{"type":"text","value":"En funktionsdefinition inom en funktionsdefinition"},{"type":"element","tagName":"a","properties":{"href":"#en-funktionsdefinition-inom-en-funktionsdefinition","ariaLabel":"en funktionsdefinition inom en funktionsdefinition permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också inkludera en namngiven funktion för den nya prisbaserade sorteringsfunktionen som vi har skapat. Låt oss lägga till en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_pris"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sortera_enligt_pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremalen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Använd funktionen prisordning här"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremalen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nprodukter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sortera_enligt_pris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vet att hjälpfunktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"text","value":" inte används någonstans utanför funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_pris"}]},{"type":"text","value":", kan vi placera den första funktionsdefinitionen inom den senare:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sortera_enligt_pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremalen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpfunktion definierad inom funktionen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"prisordning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremalen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"prisordning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sortering enligt återstående lager","tmcname":"osa12-01_aterstaende_lager"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_aterstaende_lager(foremal: list)"}]},{"type":"text","value":". Funktionen tar en lista med tupler som sitt argument. Tuplerna består av namn, pris och återstående lager för en produkt. Funktionen ska returnera en ny lista, där artiklarna är sorterade enligt återstående lager, med det lägsta värdet först. Den ursprungliga listan ska inte ändras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sortera_enligt_aterstaende_lager"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" st\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\napelsin 2 st\näppel 3 st\nbanan 12 st\nvattenmelon 22 st\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sortering enligt produktionssäsonger","tmcname":"osa12-02_produktionssasong"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_sasonger(foremal: list)"}]},{"type":"text","value":" som tar en lista med ordlistor som sitt argument. Varje ordlista innehåller information om ett enda TV-program. Funktionen ska sortera listan efter antalet säsonger som varje program har, i stigande ordning. Funktionen ska inte ändra den ursprungliga listan, utan istället returnera en ny lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"serier "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Friends\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Simpsons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" serie "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sortera_enligt_sasonger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"serier"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'namn'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'säsonger'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" säsonger\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\nDexter 9 säsonger\nFriends 10 säsonger\nSimpsons 32 säsonger\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sortering enligt poäng","tmcname":"osa12-03_enligt_poang"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sortera_enligt_betyg(foremal: list)"}]},{"type":"text","value":" som tar en lista med ordlistor som sitt argument. Ordlistornas struktur är identisk med den i den föregående övningen. Denna funktion ska sortera ordlistorna i fallande ordning baserat på programmens betyg. Funktionen ska inte ändra den ursprungliga listan, utan returnera en ny lista istället."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"serier "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Friends\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Simpsons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"säsonger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betygsättning enligt IMDB\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" serie "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sortera_enligt_betyg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"serier"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'namn'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'betyg'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\nBetygsättning enligt IMDB\nFriends 8.9\nSimpsons 8.7\nDexter 8.6\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortering-av-samlingar-av-egna-objekt","style":"position:relative;"},"children":[{"type":"text","value":"Sortering av samlingar av egna objekt"},{"type":"element","tagName":"a","properties":{"href":"#sortering-av-samlingar-av-egna-objekt","ariaLabel":"sortering av samlingar av egna objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss med samma princip skriva ett program som sorterar en lista med objekt från vår egen klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" på två olika sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en enkel studerande \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" poang\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"), "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" sp.\""}]}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"enligt_id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"enligt_poang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"220"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"m321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a999\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"131"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n studeranden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Enligt id:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"enligt_id"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Enligt poäng:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"enligt_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enligt id:\nAnton (a123), 220 sp.\nAnna (a999), 131 sp.\nMaja (m321), 210 sp."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enligt poäng:\nAnna (a999), 131 sp.\nMaja (m321), 210 sp.\nAnton (a123), 220 sp."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se ovan fungerar sortering efter olika kriterier precis som det är tänkt. Om funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_id"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_studiepoang"}]},{"type":"text","value":" inte behövs någon annanstans finns det sätt att göra implementeringen enklare. Vi återkommer till detta ämne efter dessa övningar."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Klättringsrutt","tmcname":"osa12-04_klattringsrutt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbottnet innehåller en klassdefinition för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Klattringsrutt"}]},{"type":"text","value":", som fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rutt1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kantti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrutt2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrutt3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Syncro\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rutt3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rutt3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kantti, längd 38 meter, grade 6A+\nSmooth operator, längd 11 meter, grade 7A\nSyncro 14 8C+"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortera-enligt-langd","style":"position:relative;"},"children":[{"type":"text","value":"Sortera enligt längd"},{"type":"element","tagName":"a","properties":{"href":"#sortera-enligt-langd","ariaLabel":"sortera enligt langd permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_langd(rutter: list)"}]},{"type":"text","value":", som returnerar en ny lista av rutter sorterade enligt längd från längsta till kortaste."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kantti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Syncro\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Små steg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrutter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rutt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" enligt_langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kantti, längd 38 meter, grade 6A+\nSyncro, längd 14 meter, grade 8C+\nSmå steg, längd 12 meter, grade 6A+\nSmooth operator, längd 11 meter, grade 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortera-enligt-svarighetsgrad","style":"position:relative;"},"children":[{"type":"text","value":"Sortera enligt svårighetsgrad"},{"type":"element","tagName":"a","properties":{"href":"#sortera-enligt-svarighetsgrad","ariaLabel":"sortera enligt svarighetsgrad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_svarighet(rutter: list)"}]},{"type":"text","value":", som returnerar en ny lista av rutter sorterade enligt svårighet från svåraste till lättaste. För rutter med samma svårighet är den längre svårare. Skalan för svårighet är "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"4, 4+, 5, 5+, 6A, 6A+, ..."}]},{"type":"text","value":" som i praktiken fungerar enligt alfabetiska ordningen för strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kantti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Syncro\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Små steg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrutter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rutt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" enligt_svarighet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Syncro, längd 14 meter, grade 8C+\nSmooth operator, längd 11 meter, grade 7A\nKantti, längd 38 meter, grade 6A+\nSmå steg, längd 12 meter, grade 6A+"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" Ifall ordningen är baserad på en lista eller tupel, sorterar Python föremål per standard baserat på första föremålet, sedan baserat på det andra och så vidare:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"b\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"b\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[('a', 2), ('a', 4), ('b', 0), ('b', 30)]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Klättringsområde","tmcname":"osa12-05_klattringsomrade/"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns förutom klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Klattringsrutt"}]},{"type":"text","value":" dessutom klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Klattringsomrade"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"o1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsomrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olhava\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kantti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Stora snittet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"36"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Svensk rutt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"5+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\no2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsomrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nummi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Syncro\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\no3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klattringsomrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nalkkilan släbi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Små steg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grisen gillar inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6B+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\no3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Klattringsrutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fruktträdgård\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"6A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rutter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"svaraste_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olhava, 3 rutter, svåraste 6B\nNalkkilan släbi 4\nSmooth operator, längd 9 meter, grade 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortera-enligt-antalet-rutter","style":"position:relative;"},"children":[{"type":"text","value":"Sortera enligt antalet rutter"},{"type":"element","tagName":"a","properties":{"href":"#sortera-enligt-antalet-rutter","ariaLabel":"sortera enligt antalet rutter permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_antal_rutter"}]},{"type":"text","value":", som sorterar klättringsområdena enligt antalet rutter de har i ökande ordning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# o1, o2 och o3 definierade enligt ovan"}]},{"type":"text","value":"\nomraden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"o1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" omrade "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" enligt_antal_rutter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"omraden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"omrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nummi, 1 rutter, svåraste 8C+\nOlhava, 3 rutter, svåraste 6B\nNalkkilan slabi, 4 rutter, svåraste 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sortera-enligt-svaraste-rutt","style":"position:relative;"},"children":[{"type":"text","value":"Sortera enligt svåraste rutt"},{"type":"element","tagName":"a","properties":{"href":"#sortera-enligt-svaraste-rutt","ariaLabel":"sortera enligt svaraste rutt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enligt_svaraste_rutt"}]},{"type":"text","value":", som sorterar klättringsområdena enligt högsta svårighetsgraden de har i "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"minskande"}]},{"type":"text","value":" ordning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# o1, o2 och o3 definierade enligt ovan"}]},{"type":"text","value":"\nomraden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"o1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" omrade "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" enligt_svaraste_rutt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"omraden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"omrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nummi, 1 rutter, svåraste 8C+\nNalkkilan slabi, 4 rutter, svåraste 7A\nOlhava, 3 rutter, svåraste 6B"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"lambda-uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Lambda-uttryck"},{"type":"element","tagName":"a","properties":{"href":"#lambda-uttryck","ariaLabel":"lambda uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har mest arbetat med funktioner ur modularitetssynpunkt. Det är sant att funktioner spelar en viktig roll när det gäller att hantera komplexiteten i dina program och undvika upprepning av kod. Funktioner skrivs vanligtvis så att de kan användas många gånger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men ibland behöver man något som liknar en funktion som man bara använder en gång. Med lambda-uttryck kan du skapa små, anonyma funktioner som skapas (och kasseras) när de behövs i koden. Den allmänna syntaxen är som följer:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda : "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att sortera en lista med tupler efter det andra objektet i varje tupel skulle se ut så här implementerat med ett lambda-uttryck:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Funktionen skapas \"i farten\" med ett lambda-uttryck:"}]},{"type":"text","value":"\nprodukter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uttrycket"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda föremål: föremål[1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Är ekvivalent med funktionsdefinitionen"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"förutom det faktum att en lambdafunktion inte har något namn. Det är därför lambda-funktioner kallas anonyma funktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I alla andra avseenden skiljer sig inte en lambda-funktion från någon annan funktion, och de kan användas i alla samma sammanhang som en motsvarande namngiven funktion. Följande program sorterar till exempel en lista med strängar i alfabetisk ordning enligt det sista tecknet i varje sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mikael\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Makke\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Markus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strangar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Maja\nMinna\nMakke\nMikael\nMarkus"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också kombinera list comprehensions, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":"-metoden och lambda-uttryck. Vi kan till exempel sortera strängar baserat på enbart vokalerna i dem och ignorera alla andra tecken:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mikael\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Makke\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Markus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strangar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"m "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" m "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" m "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiouyäö\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Maja\nMakke\nMarkus\nMinna\nMikael"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anonyma funktioner kan också användas med andra inbyggda Python-funktioner, inte bara de som används för sortering. Till exempel tar funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" också ett nyckelordsargument som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"key"}]},{"type":"text","value":". Det används som kriterium för att jämföra objekten när det lägsta eller högsta värdet väljs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel handlar det om ljudinspelningar. Först väljer vi den äldsta inspelningen och sedan den längsta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Skiva"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en enkel skiva \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" artist"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"artist "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" artist\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ar\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"artist"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"), "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":". "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" min.\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n l1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nevermind\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nirvana\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1991"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"43"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n l2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Let It Be\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Beatles\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1969"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n l3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Joshua Tree\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"U2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1986"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n skivor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"l1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" l2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" l3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Äldsta skiva:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skivor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Längsta skiva: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skivor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Äldsta skiva:\nLet It Be (Beatles), 1969. 35 min.\nLängsta skiva:\nU2 (Joshua Tree), 1986. 50 min."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bollspelare","tmcname":"osa12-06_bollspelare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbotten innehåller en definition för en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bollspelare"}]},{"type":"text","value":", som har följande offentliga attribut:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"spelnummer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"mängden gjorda mål "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mal"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"mängden målpassningar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"malpassningar"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"minuter spelade "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minuter"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera följande funktioner. OBS: varje funktion har olika typer av returvärden."}]},{"type":"element","tagName":"h2","properties":{"id":"flest-mal","style":"position:relative;"},"children":[{"type":"text","value":"Flest mål"},{"type":"element","tagName":"a","properties":{"href":"#flest-mal","ariaLabel":"flest mal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flest_mal"}]},{"type":"text","value":", som får bollspelare som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera namnet av spelaren som gjort flest mål, i strängformat."}]},{"type":"element","tagName":"h2","properties":{"id":"flest-poang","style":"position:relative;"},"children":[{"type":"text","value":"Flest poäng"},{"type":"element","tagName":"a","properties":{"href":"#flest-poang","ariaLabel":"flest poang permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flest_poang"}]},{"type":"text","value":", som får en bollspelare som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en tupel som innehåller namnet och skjortnumret av spelaren som fått flest poäng. Den totala mängden av poäng är antalet mål och antalet målpassningar ihopslaget."}]},{"type":"element","tagName":"h2","properties":{"id":"minst-minuter","style":"position:relative;"},"children":[{"type":"text","value":"Minst minuter"},{"type":"element","tagName":"a","properties":{"href":"#minst-minuter","ariaLabel":"minst minuter permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minst_minuter"}]},{"type":"text","value":", som får en bollspelare som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bollspelare"}]},{"type":"text","value":"-objektet som har den minsta mängden minuter spelade."}]},{"type":"element","tagName":"h2","properties":{"id":"testprogram","style":"position:relative;"},"children":[{"type":"text","value":"Testprogram"},{"type":"element","tagName":"a","properties":{"href":"#testprogram","ariaLabel":"testprogram permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan testa dina funktioner med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n spelare1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kalle Ankka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"46"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n spelare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Långben\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n spelare3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Musse Pigg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n spelare4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n spelare5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bollspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nalle Puh\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"spelare1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"flest_mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"flest_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minst_minuter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta ska skriva ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kalle Ankka\n('Musse Pigg', 9)\nBollspelare(namn=Nalle Puh, spelnummer=4, mål=3, målpassningar=9, minuter=12)"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"funktioner-som-argument-inom-egna-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Funktioner som argument inom egna funktioner"},{"type":"element","tagName":"a","properties":{"href":"#funktioner-som-argument-inom-egna-funktioner","ariaLabel":"funktioner som argument inom egna funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi konstaterade ovan att det är möjligt att skicka en referens till en funktion som argument till en annan funktion. Som avslutning på detta avsnitt skriver vi en egen funktion som tar en funktion som argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# typledtråden callable refererar till en funktion"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"utfor_operation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"callable"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Anropa funktionen som passerades som argument"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" b\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"utfor_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"utfor_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"utfor_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"15\n50\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det värde som returneras av funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"utfor_operation"}]},{"type":"text","value":" beror på vilken funktion som skickades som argument. Vilken funktion som helst som tar emot två argument skulle duga, oavsett om den är anonym eller namngiven."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skicka referenser till funktioner som argument till andra funktioner är kanske inte något som du kommer att göra dagligen under din programmeringskarriär, men det kan vara en användbar teknik. Följande program väljer ut några rader från en fil och skriver dem till en annan fil. Hur raderna väljs ut bestäms av en funktion som returnerar True endast om raderna ska kopieras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kopiera_rader"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kalla_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mal_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kriterie"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kalla_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" kalla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mal_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kalla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ta bort all tomrum från början och slutet av raden"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kriterie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Exempel"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall tredje parametern inte är angiven, kopiera alla rader"}]},{"type":"text","value":"\n kopiera_rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kopiera alla icke-tomma rader"}]},{"type":"text","value":"\n kopiera_rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kopierar alla rader som innehåller ordet \"Python\""}]},{"type":"text","value":"\n kopiera_rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kopierar alla rader som inte slutar med en punkt"}]},{"type":"text","value":"\n kopiera_rader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionsdefinitionen innehåller ett standardvärde för nyckelordsparametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kriterie"}]},{"type":"text","value":": "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda x: True"}]},{"type":"text","value":". Denna anonyma funktion returnerar alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" oavsett indata. Standardbeteendet är alltså att kopiera alla rader. Som vanligt gäller att om ett värde anges för en parameter med ett standardvärde, ersätter det nya värdet standardvärdet."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Söking av produkter","tmcname":"osa12-07_sokning_av_produkter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här övningen hanterar produkter som förvaras som tupler. Exemplen antar en variabel med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"produkter"}]},{"type":"text","value":" som förses med följande värde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"produkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"äppel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apelsin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vattenmelon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kål\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje tupel innehåller tre föremål: namn, pris och antal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sok(produkter: list, kriterie: callable)"}]},{"type":"text","value":". Det andra argumentet till funktionen är en funktion i sig, och den ska kunna bearbeta en tupel enligt definitionen ovan och returnera ett booleskt värde. Sökfunktionen ska returnera en ny lista som innehåller de tupler från originalet som uppfyller kriteriet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett passande kriterie kunde exempelvis vara följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pris_under_4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar alltså "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" ifall produktens pris, tupelns andra föremål, är mindre än 4."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sok"}]},{"type":"text","value":" fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pris_under_4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('äppel', 3.95, 3)\n('kål', 0.99, 1)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kriteriefunktionen kan också vara en lambda-funktion. Om vi bara ville söka efter de produkter vars belopp är minst 11, skulle vi kunna skapa följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"('banan', 5.95, 12)\n('vattenmelon', 4.95, 22)"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Kan du sortera listor enligt olika kriterier
  • \n
  • Kommer du att veta vad ett lambdauttryck är
  • \n
  • Kan du använda lambda-uttryck med andra Python-funktioner
  • \n
  • Vet du hur en funktion skickas som argument till en annan funktion
  • \n

Vi är redan bekanta med metoden sort och funktionen sorted, som används för att sortera listor i deras naturliga ordning. För siffror och strängar fungerar detta vanligtvis bra. För allt som är mer komplicerat än dem så är dock den naturliga ordningen på föremål enligt Python inte alltid det som vi som programmerare avser.

Till exempel sorteras som standard en lista med tupler baserat på det första objektet i varje tupel:

produkter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\nprodukter.sort()\n\nfor produkt in produkter:\n    print(produkt)

('apelsin', 4.5)\n('banan', 5.95)\n('vattenmelon', 4.95)\n('äppel', 3.95)

Men vad händer om vi vill sortera listan baserat på priset?

Funktioner som argument

En sorteringsmetod eller -funktion accepterar vanligtvis ett valfritt andra argument som gör att du kan kringgå standardsorteringskriterierna. Detta andra argument är en funktion som definierar hur värdet på varje föremål i listan bestäms. När listan sorteras anropar Python denna funktion när den jämför föremålen med varandra.

Låt oss ta en titt på ett exempel:

def prisordning(foremal: tuple):\n    # Returnerar tupelns andra föremål, alltså priset\n    return foremal[1]\n\nif __name__ == \"__main__\":\n    produkter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\n    # Använd funktionen prisordning för sortering\n    produkter.sort(key=prisordning)\n\n    for produkt in produkter:\n        print(produkt)

('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)

Nu är listan sorterad utifrån artiklarnas priser, men vad händer egentligen i programmet?

Funktionen prisordning är faktiskt ganska enkel. Den tar ett objekt som sitt argument och returnerar ett värde för det objektet. Mer specifikt returnerar den det andra objektet i tupeln, som representerar priset. Men sedan har vi den här kodraden, där sort-metoden anropas:

produkter.sort(key=prisordning)

Här anropas sort-metoden med en funktion som argument. Detta är inte en referens till funktionens returvärde, utan en referens till själva funktionen. Sort-metoden anropar denna funktion flera gånger och använder varje objekt i listan som argument i tur och ordning.

Om vi inkluderar en extra print-sats i funktionsdefinitionen för prisordning kan vi verifiera att funktionen verkligen anropas en gång för varje objekt i listan:

def prisordning(foremal: tuple):\n    # Skriver ut föremålet\n    print(f\"Anropade prisordning({foremal})\")\n\n    # Returnerar tupelns andra föremål, alltså priset\n    return foremal[1]\n\n\nprodukter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\n# Använd funktionen prisordning för sortering\nprodukter.sort(key=prisordning)\n\nfor produkt in produkter:\n    print(produkt)

Anropade prisordning(('banan', 5.95))\nAnropade prisordning(('äppel', 3.95))\nAnropade prisordning(('apelsin', 4.5))\nAnropade prisordning(('vattenmelon', 4.95))\n('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)

Ordningen kan vändas med ett annat nyckelordsargument; reverse, som är tillgängligt med både sort-metoden och funktionen sorted:

produkter.sort(key=prisordning, reverse=True)\n\nt2 = sorted(produkter, key=prisordning, reverse=True)

En funktionsdefinition inom en funktionsdefinition

Vi kan också inkludera en namngiven funktion för den nya prisbaserade sorteringsfunktionen som vi har skapat. Låt oss lägga till en funktion med namnet sortera_enligt_pris:

def prisordning(foremal: tuple):\n    return foremal[1]\n\ndef sortera_enligt_pris(foremalen: list):\n    # Använd funktionen prisordning här\n    return sorted(foremalen, key=prisordning)\n\nprodukter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\nfor produkt in sortera_enligt_pris(produkter):\n    print(produkt)

Om vi vet att hjälpfunktionen prisordning inte används någonstans utanför funktionen sortera_enligt_pris, kan vi placera den första funktionsdefinitionen inom den senare:

def sortera_enligt_pris(foremalen: list):\n    # hjälpfunktion definierad inom funktionen\n    def prisordning(foremal: tuple):\n        return foremal[1]\n\n    return sorted(foremalen, key=prisordning)

Skapa en funktion med namnet sortera_enligt_aterstaende_lager(foremal: list). Funktionen tar en lista med tupler som sitt argument. Tuplerna består av namn, pris och återstående lager för en produkt. Funktionen ska returnera en ny lista, där artiklarna är sorterade enligt återstående lager, med det lägsta värdet först. Den ursprungliga listan ska inte ändras.

Funktionen ska fungera på följande sätt:

produkter = [(\"banan\", 5.95, 12), (\"äppel\", 3.95, 3), (\"apelsin\", 4.50, 2), (\"vattenmelon\", 4.95, 22)]\n\nfor produkt in sortera_enligt_aterstaende_lager(produkter):\n    print(f\"{produkt[0]} {produkt[2]} st\")
\napelsin 2 st\näppel 3 st\nbanan 12 st\nvattenmelon 22 st\n

Skapa en funktion med namnet sortera_enligt_sasonger(foremal: list) som tar en lista med ordlistor som sitt argument. Varje ordlista innehåller information om ett enda TV-program. Funktionen ska sortera listan efter antalet säsonger som varje program har, i stigande ordning. Funktionen ska inte ändra den ursprungliga listan, utan istället returnera en ny lista.

Funktionen ska fungera på följande sätt:

serier = [{ \"namn\": \"Dexter\", \"betyg\" : 8.6, \"säsonger\":9 }, { \"namn\": \"Friends\", \"betyg\" : 8.9, \"säsonger\":10 },  { \"namn\": \"Simpsons\", \"betyg\" : 8.7, \"säsonger\":32 }  ]\n\nfor serie in sortera_enligt_sasonger(serier):\n    print(f\"{serie['namn']}  {serie['säsonger']} säsonger\")
\nDexter 9 säsonger\nFriends 10 säsonger\nSimpsons 32 säsonger\n

Skapa en funktion med namnet sortera_enligt_betyg(foremal: list) som tar en lista med ordlistor som sitt argument. Ordlistornas struktur är identisk med den i den föregående övningen. Denna funktion ska sortera ordlistorna i fallande ordning baserat på programmens betyg. Funktionen ska inte ändra den ursprungliga listan, utan returnera en ny lista istället.

serier = [{ \"namn\": \"Dexter\", \"betyg\" : 8.6, \"säsonger\":9 }, { \"namn\": \"Friends\", \"betyg\" : 8.9, \"säsonger\":10 },  { \"namn\": \"Simpsons\", \"betyg\" : 8.7, \"säsonger\":32 }  ]\n\nprint(\"Betygsättning enligt IMDB\")\nfor serie in sortera_enligt_betyg(serier):\n    print(f\"{serie['namn']}  {serie['betyg']}\")
\nBetygsättning enligt IMDB\nFriends 8.9\nSimpsons 8.7\nDexter 8.6\n

Sortering av samlingar av egna objekt

Låt oss med samma princip skriva ett program som sorterar en lista med objekt från vår egen klass Studerande på två olika sätt:

class Studerande:\n    \"\"\" Klassen modellerar en enkel studerande \"\"\"\n    def __init__(self, namn: str, id: str, poang: int):\n        self.namn = namn\n        self.id = id\n        self.poang = poang\n\n    def __str__(self):\n        return f\"{self.namn} ({self.id}), {self.poang} sp.\"\n\n\ndef enligt_id(foremal: Studerande):\n    return foremal.id\n\ndef enligt_poang(foremal: Studerande):\n    return foremal.poang\n\n\nif __name__ == \"__main__\":\n    s1 = Studerande(\"Anton\", \"a123\", 220)\n    s2 = Studerande(\"Maja\", \"m321\", 210)\n    s3 = Studerande(\"Anna\", \"a999\", 131)\n\n    studeranden = [s1, s2, s3]\n\n    print(\"Enligt id:\")\n    for studerande in sorted(studeranden, key=enligt_id):\n        print(studerande)\n\n    print()\n\n    print(\"Enligt poäng:\")\n    for studerande in sorted(studeranden, key=enligt_poang):\n        print(studerande)

Enligt id:\nAnton (a123), 220 sp.\nAnna (a999), 131 sp.\nMaja (m321), 210 sp.

Enligt poäng:\nAnna (a999), 131 sp.\nMaja (m321), 210 sp.\nAnton (a123), 220 sp.

Som du kan se ovan fungerar sortering efter olika kriterier precis som det är tänkt. Om funktionerna enligt_id och enligt_studiepoang inte behövs någon annanstans finns det sätt att göra implementeringen enklare. Vi återkommer till detta ämne efter dessa övningar.

Uppgiftsbottnet innehåller en klassdefinition för Klattringsrutt, som fungerar enligt följande:

rutt1 = Klattringsrutt(\"Kantti\", 38, \"6A+\")\nrutt2 = Klattringsrutt(\"Smooth operator\", 11, \"7A\")\nrutt3 = Klattringsrutt(\"Syncro\", 14, \"8C+\")\n\n\nprint(rutt1)\nprint(rutt2)\nprint(rutt3.namn, rutt3.langd, rutt3.grade)

Kantti, längd 38 meter, grade 6A+\nSmooth operator, längd 11 meter, grade 7A\nSyncro 14 8C+

Sortera enligt längd

Skapa funktionen enligt_langd(rutter: list), som returnerar en ny lista av rutter sorterade enligt längd från längsta till kortaste.

Funktionen ska fungera enligt följande:

r1 = Klattringsrutt(\"Kantti\", 38, \"6A+\")\nr2 = Klattringsrutt(\"Smooth operator\", 11, \"7A\")\nr3 = Klattringsrutt(\"Syncro\", 14, \"8C+\")\nr4 = Klattringsrutt(\"Små steg\", 12, \"6A+\")\n\nrutter = [r1, r2, r3, r4]\n\nfor rutt in enligt_langd(rutter):\n    print(rutt)

Kantti, längd 38 meter, grade 6A+\nSyncro, längd 14 meter, grade 8C+\nSmå steg, längd 12 meter, grade 6A+\nSmooth operator, längd 11 meter, grade 7A

Sortera enligt svårighetsgrad

Skapa funktionen enligt_svarighet(rutter: list), som returnerar en ny lista av rutter sorterade enligt svårighet från svåraste till lättaste. För rutter med samma svårighet är den längre svårare. Skalan för svårighet är 4, 4+, 5, 5+, 6A, 6A+, ... som i praktiken fungerar enligt alfabetiska ordningen för strängar.

Funktionen ska fungera enligt följande:

r1 = Klattringsrutt(\"Kantti\", 38, \"6A+\")\nr2 = Klattringsrutt(\"Smooth operator\", 11, \"7A\")\nr3 = Klattringsrutt(\"Syncro\", 14, \"8C+\")\nr4 = Klattringsrutt(\"Små steg\", 12, \"6A+\")\n\nrutter = [r1, r2, r3, r4]\nfor rutt in enligt_svarighet(rutter):\n    print(rutt)

Syncro, längd 14 meter, grade 8C+\nSmooth operator, längd 11 meter, grade 7A\nKantti, längd 38 meter, grade 6A+\nSmå steg, längd 12 meter, grade 6A+

Tips: Ifall ordningen är baserad på en lista eller tupel, sorterar Python föremål per standard baserat på första föremålet, sedan baserat på det andra och så vidare:

lista = [(\"a\", 4),(\"a\", 2),(\"b\", 30), (\"b\", 0) ]\nprint(sorted(lista))

[('a', 2), ('a', 4), ('b', 0), ('b', 30)]

I uppgiftsbotten finns förutom klassen Klattringsrutt dessutom klassen Klattringsomrade.

o1 = Klattringsomrade(\"Olhava\")\no1.tillsatt_rutt(Klattringsrutt(\"Kantti\", 38, \"6A+\"))\no1.tillsatt_rutt(Klattringsrutt(\"Stora snittet\", 36, \"6B\"))\no1.tillsatt_rutt(Klattringsrutt(\"Svensk rutt\", 42, \"5+\"))\n\no2 = Klattringsomrade(\"Nummi\")\no2.tillsatt_rutt(Klattringsrutt(\"Syncro\", 14, \"8C+\"))\n\no3 = Klattringsomrade(\"Nalkkilan släbi\")\no3.tillsatt_rutt(Klattringsrutt(\"Små steg\", 12, \"6A+\"))\no3.tillsatt_rutt(Klattringsrutt(\"Smooth operator\", 11, \"7A\"))\no3.tillsatt_rutt(Klattringsrutt(\"Grisen gillar inte\", 12 , \"6B+\"))\no3.tillsatt_rutt(Klattringsrutt(\"Fruktträdgård\", 8, \"6A\"))\n\nprint(o1)\nprint(o3.namn, o3.rutter())\nprint(o3.svaraste_rutt())

Olhava, 3 rutter, svåraste 6B\nNalkkilan släbi 4\nSmooth operator, längd 9 meter, grade 7A

Sortera enligt antalet rutter

Skapa funktionen enligt_antal_rutter, som sorterar klättringsområdena enligt antalet rutter de har i ökande ordning.

# o1, o2 och o3 definierade enligt ovan\nomraden = [o1, o2, o3]\nfor omrade in enligt_antal_rutter(omraden):\n    print(omrade)\n

Nummi, 1 rutter, svåraste 8C+\nOlhava, 3 rutter, svåraste 6B\nNalkkilan slabi, 4 rutter, svåraste 7A

Sortera enligt svåraste rutt

Skapa funktionen enligt_svaraste_rutt, som sorterar klättringsområdena enligt högsta svårighetsgraden de har i minskande ordning.

# o1, o2 och o3 definierade enligt ovan\nomraden = [o1, o2, o3]\nfor omrade in enligt_svaraste_rutt(omraden):\n    print(omrade)\n

Nummi, 1 rutter, svåraste 8C+\nNalkkilan slabi, 4 rutter, svåraste 7A\nOlhava, 3 rutter, svåraste 6B

Lambda-uttryck

Vi har mest arbetat med funktioner ur modularitetssynpunkt. Det är sant att funktioner spelar en viktig roll när det gäller att hantera komplexiteten i dina program och undvika upprepning av kod. Funktioner skrivs vanligtvis så att de kan användas många gånger.

Men ibland behöver man något som liknar en funktion som man bara använder en gång. Med lambda-uttryck kan du skapa små, anonyma funktioner som skapas (och kasseras) när de behövs i koden. Den allmänna syntaxen är som följer:

lambda <parametrar> : <uttryck>

Att sortera en lista med tupler efter det andra objektet i varje tupel skulle se ut så här implementerat med ett lambda-uttryck:

produkter = [(\"banan\", 5.95), (\"äppel\", 3.95), (\"apelsin\", 4.50), (\"vattenmelon\", 4.95)]\n\n# Funktionen skapas \"i farten\" med ett lambda-uttryck:\nprodukter.sort(key=lambda foremal: foremal[1])\n\nfor produkt in produkter:\n    print(produkt)

('äppel', 3.95)\n('apelsin', 4.5)\n('vattenmelon', 4.95)\n('banan', 5.95)

Uttrycket

lambda föremål: föremål[1]

Är ekvivalent med funktionsdefinitionen

\ndef pris(foremal):\n    return foremal[1]

förutom det faktum att en lambdafunktion inte har något namn. Det är därför lambda-funktioner kallas anonyma funktioner.

I alla andra avseenden skiljer sig inte en lambda-funktion från någon annan funktion, och de kan användas i alla samma sammanhang som en motsvarande namngiven funktion. Följande program sorterar till exempel en lista med strängar i alfabetisk ordning enligt det sista tecknet i varje sträng:

strangar = [\"Mikael\", \"Makke\", \"Maja\", \"Markus\", \"Minna\"]\n\nfor strang in sorted(strangar, key=lambda strang: strang[-1]):\n    print(strang)

Maja\nMinna\nMakke\nMikael\nMarkus

Vi kan också kombinera list comprehensions, join-metoden och lambda-uttryck. Vi kan till exempel sortera strängar baserat på enbart vokalerna i dem och ignorera alla andra tecken:

strangar = [\"Mikael\", \"Makke\", \"Maja\", \"Markus\", \"Minna\"]\n\nfor strang in sorted(strangar, key=lambda strang: \"\".join([m for m in strang if m in \"aeiouyäö\"])):\n    print(strang)

Maja\nMakke\nMarkus\nMinna\nMikael

Anonyma funktioner kan också användas med andra inbyggda Python-funktioner, inte bara de som används för sortering. Till exempel tar funktionerna min och max också ett nyckelordsargument som heter key. Det används som kriterium för att jämföra objekten när det lägsta eller högsta värdet väljs.

I följande exempel handlar det om ljudinspelningar. Först väljer vi den äldsta inspelningen och sedan den längsta:

\nclass Skiva:\n    \"\"\" Klassen modellerar en enkel skiva \"\"\"\n    def __init__(self, namn: str, artist: str, ar: int, langd: int):\n        self.namn = namn\n        self.artist = artist\n        self.ar = ar\n        self.langd = langd\n\n\n    def __str__(self):\n        return f\"{self.namn} ({self.artist}), {self.ar}. {self.langd} min.\"\n\nif __name__ == \"__main__\":\n    l1 = Skiva(\"Nevermind\", \"Nirvana\", 1991, 43)\n    l2 = Skiva(\"Let It Be\", \"Beatles\", 1969, 35)\n    l3 = Skiva(\"Joshua Tree\", \"U2\", 1986, 50)\n\n    skivor = [l1, l2, l3]\n\n\n    print(\"Äldsta skiva:\")\n    print(min(skivor, key=lambda skiva: skiva.ar))\n\n    print(\"Längsta skiva: \")\n    print(max(skivor, key=lambda skiva: skiva.langd))

Äldsta skiva:\nLet It Be (Beatles), 1969. 35 min.\nLängsta skiva:\nU2 (Joshua Tree), 1986. 50 min.

Uppgiftsbotten innehåller en definition för en klass med namnet Bollspelare, som har följande offentliga attribut:

    \n
  • namn
  • \n
  • spelnummer
  • \n
  • mängden gjorda mål mal
  • \n
  • mängden målpassningar malpassningar
  • \n
  • minuter spelade minuter
  • \n

Implementera följande funktioner. OBS: varje funktion har olika typer av returvärden.

Flest mål

Skapa funktionen flest_mal, som får bollspelare som argument.

Funktionen ska returnera namnet av spelaren som gjort flest mål, i strängformat.

Flest poäng

Skapa funktionen flest_poang, som får en bollspelare som argument.

Funktionen ska returnera en tupel som innehåller namnet och skjortnumret av spelaren som fått flest poäng. Den totala mängden av poäng är antalet mål och antalet målpassningar ihopslaget.

Minst minuter

Skapa funktionen minst_minuter, som får en bollspelare som argument.

Funktionen ska returnera Bollspelare-objektet som har den minsta mängden minuter spelade.

Testprogram

Du kan testa dina funktioner med följande program:

if __name__ == \"__main__\":\n    spelare1 = Bollspelare(\"Kalle Ankka\", 13, 5, 12, 46)\n    spelare2 = Bollspelare(\"Långben\", 7, 2, 26, 55)\n    spelare3 = Bollspelare(\"Musse Pigg\", 9, 1, 32, 26)\n    spelare4 = Bollspelare(\"Peter Pan\", 12, 1, 11, 41)\n    spelare5 = Bollspelare(\"Nalle Puh\", 4, 3, 9, 12)\n\n    lag = [spelare1, spelare2, spelare3, spelare4, spelare5]\n    print(flest_mal(lag))\n    print(flest_poang(lag))\n    print(minst_minuter(lag))

Detta ska skriva ut:

Kalle Ankka\n('Musse Pigg', 9)\nBollspelare(namn=Nalle Puh, spelnummer=4, mål=3, målpassningar=9, minuter=12)

Funktioner som argument inom egna funktioner

Vi konstaterade ovan att det är möjligt att skicka en referens till en funktion som argument till en annan funktion. Som avslutning på detta avsnitt skriver vi en egen funktion som tar en funktion som argument.

# typledtråden callable refererar till en funktion\ndef utfor_operation(operation: callable):\n    # Anropa funktionen som passerades som argument\n    return operation(10, 5)\n\ndef summa(a: int, b: int):\n    return a + b\n\ndef produkt(a: int, b: int):\n    return a * b\n\n\nif __name__ == \"__main__\":\n    print(utfor_operation(summa))\n    print(utfor_operation(produkt))\n    print(utfor_operation(lambda x,y: x - y))\n

15\n50\n5

Det värde som returneras av funktionen utfor_operation beror på vilken funktion som skickades som argument. Vilken funktion som helst som tar emot två argument skulle duga, oavsett om den är anonym eller namngiven.

Att skicka referenser till funktioner som argument till andra funktioner är kanske inte något som du kommer att göra dagligen under din programmeringskarriär, men det kan vara en användbar teknik. Följande program väljer ut några rader från en fil och skriver dem till en annan fil. Hur raderna väljs ut bestäms av en funktion som returnerar True endast om raderna ska kopieras:

def kopiera_rader(kalla_namn: str, mal_namn: str, kriterie= lambda x: True):\n    with open(kalla_namn) as kalla, open(mal_namn, \"w\") as mal:\n        for rad in kalla:\n            # Ta bort all tomrum från början och slutet av raden\n            rad = rad.strip()\n\n            if kriterie(rad):\n                mal.write(rad + \"\\n\")\n\n# Exempel\nif __name__ == \"__main__\":\n    # Ifall tredje parametern inte är angiven, kopiera alla rader\n    kopiera_rader(\"första.txt\", \"andra.txt\")\n\n    # Kopiera alla icke-tomma rader\n    kopiera_rader(\"första.txt\", \"andra.txt\", lambda rad: len(rad) > 0)\n\n    # Kopierar alla rader som innehåller ordet \"Python\"\n    kopiera_rader(\"första.txt\", \"andra.txt\", lambda rad: \"Python\" in rad)\n\n    # Kopierar alla rader som inte slutar med en punkt\n    kopiera_rader(\"första.txt\", \"andra.txt\", lambda rad: rad[-1] != \".\")

Funktionsdefinitionen innehåller ett standardvärde för nyckelordsparametern kriterie: lambda x: True. Denna anonyma funktion returnerar alltid True oavsett indata. Standardbeteendet är alltså att kopiera alla rader. Som vanligt gäller att om ett värde anges för en parameter med ett standardvärde, ersätter det nya värdet standardvärdet.

Den här övningen hanterar produkter som förvaras som tupler. Exemplen antar en variabel med namnet produkter som förses med följande värde:

produkter = [(\"banan\", 5.95, 12), (\"äppel\", 3.95, 3), (\"apelsin\", 4.50, 2), (\"vattenmelon\", 4.95, 22), (\"kål\", 0.99, 1)]

Varje tupel innehåller tre föremål: namn, pris och antal.

Skapa funktionen sok(produkter: list, kriterie: callable). Det andra argumentet till funktionen är en funktion i sig, och den ska kunna bearbeta en tupel enligt definitionen ovan och returnera ett booleskt värde. Sökfunktionen ska returnera en ny lista som innehåller de tupler från originalet som uppfyller kriteriet.

Ett passande kriterie kunde exempelvis vara följande:

def pris_under_4(produkt):\n    return produkt[1] < 4

Funktionen returnerar alltså True ifall produktens pris, tupelns andra föremål, är mindre än 4.

Funktionen sok fungerar enligt följande:

for produkt in sok(produkter, pris_under_4):\n    print(produkt)

('äppel', 3.95, 3)\n('kål', 0.99, 1)

Kriteriefunktionen kan också vara en lambda-funktion. Om vi bara ville söka efter de produkter vars belopp är minst 11, skulle vi kunna skapa följande:

for produkt in sok(produkter, lambda t: t[2]>10):\n    print(produkt)

('banan', 5.95, 12)\n('vattenmelon', 4.95, 22)

","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/1-funktioner-som-argument.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-12/2-generatorer/page-data.json b/page-data/osa-12/2-generatorer/page-data.json index 6ce95b6de..1c9218fa7 100644 --- a/page-data/osa-12/2-generatorer/page-data.json +++ b/page-data/osa-12/2-generatorer/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-12/2-generatorer", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad en Python-generator är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att känna till nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva dina egna generator-funktioner"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan stött på situationer där vi har att göra med en serie föremål och vi behöver nästa föremål i serien, men vi vill inte nödvändigtvis formulera hela serien fram till den punkten varje gång ett nytt föremål krävs. Vissa rekursiva serier, till exempel Fibonacci-numret, är ett bra exempel på en sådan situation. Om varje funktionsanrop rekursivt genererar hela serien fram till önskad punkt, slutar det med att vi genererar början av serien många gånger om."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python-generatorer är ett sätt att bara producera nästa föremål i en serie när det behövs, vilket i princip innebär att genereringsprocessen för serien bara körs en gång (för en viss exekvering av ett program). De fungerar i stort sett som vanliga funktioner, eftersom de kan anropas och returnerar värden, men det värde som en generatorfunktion returnerar skiljer sig från en vanlig funktion. En normal funktion ska returnera samma värde varje gång, givet samma argument. En generatorfunktion, å andra sidan, ska komma ihåg sitt nuvarande tillstånd och returnera nästa föremål i serien, som kan skilja sig från föregående föremål."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Precis som det finns många sätt att lösa de flesta programmeringsproblem finns det många sätt att uppnå en funktionalitet som liknar generatorer, men generatorer kan bidra till att göra programmet lättare att förstå och kan i vissa situationer spara minne eller andra beräkningsresurser."}]},{"type":"element","tagName":"h2","properties":{"id":"nyckelordet-yield","style":"position:relative;"},"children":[{"type":"text","value":"Nyckelordet yield"},{"type":"element","tagName":"a","properties":{"href":"#nyckelordet-yield","ariaLabel":"nyckelordet yield permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En generatorfunktion måste innehålla nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":", som markerar det värde som funktionen returnerar. Låt oss titta på en funktion som genererar heltal, med början från noll och slut vid ett förutbestämt maxvärde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"raknare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"maximum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" maximum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" tal\n tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu kan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"raknare"}]},{"type":"text","value":"-funktionen skickas som argument till funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"next()"}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Första värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andra värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Första värde:\n0\nAndra värde:\n1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se i exemplet ovan liknar nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":": båda används för att definiera ett returvärde. Skillnaden är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" inte \"stänger\" funktionen på samma sätt som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":". En generatorfunktion med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" håller reda på sitt tillstånd och nästa gång den anropas kommer den att fortsätta från samma tillstånd."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här generatorn kräver också ett maxvärde, i exemplet ovan var det "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":". När generatorn får slut på värden kommer den att ge upphov till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StopIteration"}]},{"type":"text","value":"-undantag:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"0\n1\nTraceback (most recent call last):\nFile \"generatorexempel.py\", line 11, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nprint(next(talen))\nStopIteration"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Undantaget kan bli fångat med ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"- "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" StopIteration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talen tog slut\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n1\nTalen tog slut"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att gå igenom alla objekt i en generator görs enkelt med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n1\n2\n3\n4\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Generatorer behöver inte ha ett definierat maxvärde eller en slutpunkt. De kan generera värden i det oändliga (naturligtvis inom andra beräkningsmässiga och fysiska begränsningar)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tänk dock på att det bara fungerar att genomkorsa en generator med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop om generatorn avslutas vid någon punkt. Om generatorn är uppbyggd på en oändlig loop kommer en enkel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop att orsaka en oändlig exekvering, precis som en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loop utan slut- eller brytvillkor."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Jämna tal","tmcname":"osa12-08_jamna"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa generatorfunktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"jamna(borjan: int, maximum: int)"}]},{"type":"text","value":", som tar två heltal som argument. Funktionen ska producera jämna tal börjandes från "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"borjan"}]},{"type":"text","value":" och slutandes vid, senast, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"maximum"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Två exempel på användning av funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n4\n6\n8\n10"}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"21"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"12\n14\n16\n18\n20"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Primtal","tmcname":"osa12-09_primtal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett primtal är ett tal som är delbart endast med sig självt och talet 1. Enligt konvention definieras primtal som positiva heltal från talet 2 och uppåt. De sex första primtalen är 2, 3, 5, 7, 11 och 13."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en generatorfunktion "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"primtal()"}]},{"type":"text","value":" som skapar en ny generator. Generatorn ska returnera nya primtal, ett efter ett i sekvens, från 2 framåt. OBS: den här generatorn avslutas aldrig. Den kommer att generera tal så länge som de behövs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" primtal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n5\n7\n11\n13\n17\n19"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" du kan använda en loop för att granska ifall ett tal är ett primtal. Ifall vi granskar talet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" skulle loopen gå igenom talen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x-1"}]},{"type":"text","value":". Ifall "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" är dividerbart med någon av dessa, är det inte ett primtal."}]}]},{"type":"element","tagName":"h2","properties":{"id":"generator-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Generator comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#generator-comprehensions","ariaLabel":"generator comprehensions permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du behöver inte nödvändigtvis en funktionsdefinition för att skapa en generator. Vi kan använda en struktur som liknar en list comprehension istället. Den här gången använder vi runda parenteser för att beteckna en generator i stället för en lista eller en ordlista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Generatorn returnerar kvadraten av heltal"}]},{"type":"text","value":"\nkvadrat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"64"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":" at 0x000002B4224EBFC0>\n1\n4\n9\n16\n25"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel skriver vi ut delsträngar av det engelska alfabetet, var och en tre tecken lång. Detta skriver ut de första 10 objekten i generatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"delstrangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefghijklmnopqrstuvwxyz\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriver ut 10 första delsträngarna"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delstrangar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"abc\nbcd\ncde\ndef\nefg\nfgh\nghi\nhij\nijk\njkl"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Slumpmässiga ord","tmcname":"osa12-10_slumpmassiga_ord"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ordgenerator(bokstaver: str, langd: int, antal: int)"}]},{"type":"text","value":", som returnerar en ny generator som genererar nya, slumpmässiga ord baserat på de angiva parametrarna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett slumpmässigt ord är genererat genom att välja från en sträng av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bokstaver"}]},{"type":"text","value":" så många bokstäver som är indikerade av argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langd"}]},{"type":"text","value":". Samma bokstav kan förekomma flera gånger i ett slumpmässigt ord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Generatorn returnerar så många ord som specifierades i argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"antal"}]},{"type":"text","value":" innan dess terminering."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på användning av generatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ordgen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ordgenerator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" ordgen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"dbf\nbaf\nead\nfga\nccc"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Det är upp till dig hur du implementerar denna funktion. Du kan använda en \"traditionell\" generator eller en generator comprehension lika väl."}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad en Python-generator är
  • \n
  • Kommer du att känna till nyckelordet yield
  • \n
  • Kommer du att kunna skriva dina egna generator-funktioner
  • \n

Vi har redan stött på situationer där vi har att göra med en serie föremål och vi behöver nästa föremål i serien, men vi vill inte nödvändigtvis formulera hela serien fram till den punkten varje gång ett nytt föremål krävs. Vissa rekursiva serier, till exempel Fibonacci-numret, är ett bra exempel på en sådan situation. Om varje funktionsanrop rekursivt genererar hela serien fram till önskad punkt, slutar det med att vi genererar början av serien många gånger om.

Python-generatorer är ett sätt att bara producera nästa föremål i en serie när det behövs, vilket i princip innebär att genereringsprocessen för serien bara körs en gång (för en viss exekvering av ett program). De fungerar i stort sett som vanliga funktioner, eftersom de kan anropas och returnerar värden, men det värde som en generatorfunktion returnerar skiljer sig från en vanlig funktion. En normal funktion ska returnera samma värde varje gång, givet samma argument. En generatorfunktion, å andra sidan, ska komma ihåg sitt nuvarande tillstånd och returnera nästa föremål i serien, som kan skilja sig från föregående föremål.

Precis som det finns många sätt att lösa de flesta programmeringsproblem finns det många sätt att uppnå en funktionalitet som liknar generatorer, men generatorer kan bidra till att göra programmet lättare att förstå och kan i vissa situationer spara minne eller andra beräkningsresurser.

Nyckelordet yield

En generatorfunktion måste innehålla nyckelordet yield, som markerar det värde som funktionen returnerar. Låt oss titta på en funktion som genererar heltal, med början från noll och slut vid ett förutbestämt maxvärde:

\ndef raknare(maximum: int):\n    tal = 0\n    while tal <= maximum:\n        yield tal\n        tal += 1\n

Nu kan raknare-funktionen skickas som argument till funktionen next()

if __name__ == \"__main__\":\n    talen = raknare(10)\n    print(\"Första värde:\")\n    print(next(talen))\n    print(\"Andra värde:\")\n    print(next(talen))

Första värde:\n0\nAndra värde:\n1

Som du kan se i exemplet ovan liknar nyckelordet yield nyckelordet return: båda används för att definiera ett returvärde. Skillnaden är att yield inte \"stänger\" funktionen på samma sätt som return. En generatorfunktion med nyckelordet yield håller reda på sitt tillstånd och nästa gång den anropas kommer den att fortsätta från samma tillstånd.

Den här generatorn kräver också ett maxvärde, i exemplet ovan var det 10. När generatorn får slut på värden kommer den att ge upphov till ett StopIteration-undantag:

if __name__ == \"__main__\":\n    talen = raknare(1)\n    print(next(talen))\n    print(next(talen))\n    print(next(talen))
0\n1\nTraceback (most recent call last):\nFile \"generatorexempel.py\", line 11, in \nprint(next(talen))\nStopIteration

Undantaget kan bli fångat med ett try- except block:

if __name__ == \"__main__\":\n    talen = raknare(1)\n    try:\n        print(next(talen))\n        print(next(talen))\n        print(next(talen))\n    except StopIteration:\n        print(\"Talen tog slut\")

0\n1\nTalen tog slut

Att gå igenom alla objekt i en generator görs enkelt med en for-loop:

if __name__ == \"__main__\":\n    talen = raknare(5)\n    for tal in talen:\n        print(tal)

0\n1\n2\n3\n4\n5

Generatorer behöver inte ha ett definierat maxvärde eller en slutpunkt. De kan generera värden i det oändliga (naturligtvis inom andra beräkningsmässiga och fysiska begränsningar).

Tänk dock på att det bara fungerar att genomkorsa en generator med en for-loop om generatorn avslutas vid någon punkt. Om generatorn är uppbyggd på en oändlig loop kommer en enkel for-loop att orsaka en oändlig exekvering, precis som en while-loop utan slut- eller brytvillkor.

Skapa generatorfunktionen jamna(borjan: int, maximum: int), som tar två heltal som argument. Funktionen ska producera jämna tal börjandes från borjan och slutandes vid, senast, maximum.

Två exempel på användning av funktionen:

talen = jamna(2, 10)\nfor tal in talen:\n    print(tal)

2\n4\n6\n8\n10

talen = jamna(11, 21)\nfor tal in talen:\n    print(tal)

12\n14\n16\n18\n20

Ett primtal är ett tal som är delbart endast med sig självt och talet 1. Enligt konvention definieras primtal som positiva heltal från talet 2 och uppåt. De sex första primtalen är 2, 3, 5, 7, 11 och 13.

Skapa en generatorfunktion primtal() som skapar en ny generator. Generatorn ska returnera nya primtal, ett efter ett i sekvens, från 2 framåt. OBS: den här generatorn avslutas aldrig. Den kommer att generera tal så länge som de behövs.

Till exempel

talen = primtal()\nfor i in range(8):\n    print(next(talen))

2\n3\n5\n7\n11\n13\n17\n19

Tips: du kan använda en loop för att granska ifall ett tal är ett primtal. Ifall vi granskar talet x skulle loopen gå igenom talen 2 till x-1. Ifall x är dividerbart med någon av dessa, är det inte ett primtal.

Generator comprehensions

Du behöver inte nödvändigtvis en funktionsdefinition för att skapa en generator. Vi kan använda en struktur som liknar en list comprehension istället. Den här gången använder vi runda parenteser för att beteckna en generator i stället för en lista eller en ordlista:

# Generatorn returnerar kvadraten av heltal\nkvadrat = (x ** 2 for x in range(1, 64))\n\nprint(kvadrat)\n\nfor i in range(5):\n    print(next(kvadrat))

<generator object <genexpr> at 0x000002B4224EBFC0>\n1\n4\n9\n16\n25

I följande exempel skriver vi ut delsträngar av det engelska alfabetet, var och en tre tecken lång. Detta skriver ut de första 10 objekten i generatorn:

delstrangar = (\"abcdefghijklmnopqrstuvwxyz\"[i : i + 3] for i in range(24))\n\n# skriver ut 10 första delsträngarna\nfor i in range(10):\n    print(next(delstrangar))

abc\nbcd\ncde\ndef\nefg\nfgh\nghi\nhij\nijk\njkl

Skapa funktionen ordgenerator(bokstaver: str, langd: int, antal: int), som returnerar en ny generator som genererar nya, slumpmässiga ord baserat på de angiva parametrarna.

Ett slumpmässigt ord är genererat genom att välja från en sträng av bokstaver så många bokstäver som är indikerade av argumentet langd. Samma bokstav kan förekomma flera gånger i ett slumpmässigt ord.

Generatorn returnerar så många ord som specifierades i argumentet antal innan dess terminering.

Exempel på användning av generatorn:

ordgen = ordgenerator(\"abcdefg\", 3, 5)\nfor ord in ordgen:\n    print(ord)

dbf\nbaf\nead\nfga\nccc

OBS: Det är upp till dig hur du implementerar denna funktion. Du kan använda en \"traditionell\" generator eller en generator comprehension lika väl.

","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/2-generatorer.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad en Python-generator är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att känna till nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva dina egna generator-funktioner"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan stött på situationer där vi har att göra med en serie föremål och vi behöver nästa föremål i serien, men vi vill inte nödvändigtvis formulera hela serien fram till den punkten varje gång ett nytt föremål krävs. Vissa rekursiva serier, till exempel Fibonacci-numret, är ett bra exempel på en sådan situation. Om varje funktionsanrop rekursivt genererar hela serien fram till önskad punkt, slutar det med att vi genererar början av serien många gånger om."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python-generatorer är ett sätt att bara producera nästa föremål i en serie när det behövs, vilket i princip innebär att genereringsprocessen för serien bara körs en gång (för en viss exekvering av ett program). De fungerar i stort sett som vanliga funktioner, eftersom de kan anropas och returnerar värden, men det värde som en generatorfunktion returnerar skiljer sig från en vanlig funktion. En normal funktion ska returnera samma värde varje gång, givet samma argument. En generatorfunktion, å andra sidan, ska komma ihåg sitt nuvarande tillstånd och returnera nästa föremål i serien, som kan skilja sig från föregående föremål."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Precis som det finns många sätt att lösa de flesta programmeringsproblem finns det många sätt att uppnå en funktionalitet som liknar generatorer, men generatorer kan bidra till att göra programmet lättare att förstå och kan i vissa situationer spara minne eller andra beräkningsresurser."}]},{"type":"element","tagName":"h2","properties":{"id":"nyckelordet-yield","style":"position:relative;"},"children":[{"type":"text","value":"Nyckelordet yield"},{"type":"element","tagName":"a","properties":{"href":"#nyckelordet-yield","ariaLabel":"nyckelordet yield permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En generatorfunktion måste innehålla nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":", som markerar det värde som funktionen returnerar. Låt oss titta på en funktion som genererar heltal, med början från noll och slut vid ett förutbestämt maxvärde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"raknare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"maximum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" maximum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" tal\n tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu kan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"raknare"}]},{"type":"text","value":"-funktionen skickas som argument till funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"next()"}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Första värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andra värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Första värde:\n0\nAndra värde:\n1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se i exemplet ovan liknar nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":": båda används för att definiera ett returvärde. Skillnaden är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" inte \"stänger\" funktionen på samma sätt som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":". En generatorfunktion med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" håller reda på sitt tillstånd och nästa gång den anropas kommer den att fortsätta från samma tillstånd."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här generatorn kräver också ett maxvärde, i exemplet ovan var det "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":". När generatorn får slut på värden kommer den att ge upphov till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StopIteration"}]},{"type":"text","value":"-undantag:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"0\n1\nTraceback (most recent call last):\nFile \"generatorexempel.py\", line 11, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nprint(next(talen))\nStopIteration"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Undantaget kan bli fångat med ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"- "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" StopIteration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talen tog slut\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n1\nTalen tog slut"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att gå igenom alla objekt i en generator görs enkelt med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n1\n2\n3\n4\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Generatorer behöver inte ha ett definierat maxvärde eller en slutpunkt. De kan generera värden i det oändliga (naturligtvis inom andra beräkningsmässiga och fysiska begränsningar)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tänk dock på att det bara fungerar att genomkorsa en generator med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop om generatorn avslutas vid någon punkt. Om generatorn är uppbyggd på en oändlig loop kommer en enkel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop att orsaka en oändlig exekvering, precis som en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":"-loop utan slut- eller brytvillkor."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Jämna tal","tmcname":"osa12-08_jamna"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa generatorfunktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"jamna(borjan: int, maximum: int)"}]},{"type":"text","value":", som tar två heltal som argument. Funktionen ska producera jämna tal börjandes från "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"borjan"}]},{"type":"text","value":" och slutandes vid, senast, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"maximum"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Två exempel på användning av funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n4\n6\n8\n10"}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"21"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"12\n14\n16\n18\n20"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Primtal","tmcname":"osa12-09_primtal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett primtal är ett tal som är delbart endast med sig självt och talet 1. Enligt konvention definieras primtal som positiva heltal från talet 2 och uppåt. De sex första primtalen är 2, 3, 5, 7, 11 och 13."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en generatorfunktion "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"primtal()"}]},{"type":"text","value":" som skapar en ny generator. Generatorn ska returnera nya primtal, ett efter ett i sekvens, från 2 framåt. OBS: den här generatorn avslutas aldrig. Den kommer att generera tal så länge som de behövs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" primtal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n5\n7\n11\n13\n17\n19"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" du kan använda en loop för att granska ifall ett tal är ett primtal. Ifall vi granskar talet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" skulle loopen gå igenom talen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x-1"}]},{"type":"text","value":". Ifall "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" är dividerbart med någon av dessa, är det inte ett primtal."}]}]},{"type":"element","tagName":"h2","properties":{"id":"generator-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Generator comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#generator-comprehensions","ariaLabel":"generator comprehensions permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du behöver inte nödvändigtvis en funktionsdefinition för att skapa en generator. Vi kan använda en struktur som liknar en list comprehension istället. Den här gången använder vi runda parenteser för att beteckna en generator i stället för en lista eller en ordlista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Generatorn returnerar kvadraten av heltal"}]},{"type":"text","value":"\nkvadrat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"64"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":" at 0x000002B4224EBFC0>\n1\n4\n9\n16\n25"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel skriver vi ut delsträngar av det engelska alfabetet, var och en tre tecken lång. Detta skriver ut de första 10 objekten i generatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"delstrangar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefghijklmnopqrstuvwxyz\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriver ut 10 första delsträngarna"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delstrangar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"abc\nbcd\ncde\ndef\nefg\nfgh\nghi\nhij\nijk\njkl"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Slumpmässiga ord","tmcname":"osa12-10_slumpmassiga_ord"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ordgenerator(bokstaver: str, langd: int, antal: int)"}]},{"type":"text","value":", som returnerar en ny generator som genererar nya, slumpmässiga ord baserat på de angiva parametrarna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett slumpmässigt ord är genererat genom att välja från en sträng av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bokstaver"}]},{"type":"text","value":" så många bokstäver som är indikerade av argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langd"}]},{"type":"text","value":". Samma bokstav kan förekomma flera gånger i ett slumpmässigt ord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Generatorn returnerar så många ord som specifierades i argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"antal"}]},{"type":"text","value":" innan dess terminering."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på användning av generatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ordgen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ordgenerator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" ordgen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"dbf\nbaf\nead\nfga\nccc"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Det är upp till dig hur du implementerar denna funktion. Du kan använda en \"traditionell\" generator eller en generator comprehension lika väl."}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad en Python-generator är
  • \n
  • Kommer du att känna till nyckelordet yield
  • \n
  • Kommer du att kunna skriva dina egna generator-funktioner
  • \n

Vi har redan stött på situationer där vi har att göra med en serie föremål och vi behöver nästa föremål i serien, men vi vill inte nödvändigtvis formulera hela serien fram till den punkten varje gång ett nytt föremål krävs. Vissa rekursiva serier, till exempel Fibonacci-numret, är ett bra exempel på en sådan situation. Om varje funktionsanrop rekursivt genererar hela serien fram till önskad punkt, slutar det med att vi genererar början av serien många gånger om.

Python-generatorer är ett sätt att bara producera nästa föremål i en serie när det behövs, vilket i princip innebär att genereringsprocessen för serien bara körs en gång (för en viss exekvering av ett program). De fungerar i stort sett som vanliga funktioner, eftersom de kan anropas och returnerar värden, men det värde som en generatorfunktion returnerar skiljer sig från en vanlig funktion. En normal funktion ska returnera samma värde varje gång, givet samma argument. En generatorfunktion, å andra sidan, ska komma ihåg sitt nuvarande tillstånd och returnera nästa föremål i serien, som kan skilja sig från föregående föremål.

Precis som det finns många sätt att lösa de flesta programmeringsproblem finns det många sätt att uppnå en funktionalitet som liknar generatorer, men generatorer kan bidra till att göra programmet lättare att förstå och kan i vissa situationer spara minne eller andra beräkningsresurser.

Nyckelordet yield

En generatorfunktion måste innehålla nyckelordet yield, som markerar det värde som funktionen returnerar. Låt oss titta på en funktion som genererar heltal, med början från noll och slut vid ett förutbestämt maxvärde:

\ndef raknare(maximum: int):\n    tal = 0\n    while tal <= maximum:\n        yield tal\n        tal += 1\n

Nu kan raknare-funktionen skickas som argument till funktionen next()

if __name__ == \"__main__\":\n    talen = raknare(10)\n    print(\"Första värde:\")\n    print(next(talen))\n    print(\"Andra värde:\")\n    print(next(talen))

Första värde:\n0\nAndra värde:\n1

Som du kan se i exemplet ovan liknar nyckelordet yield nyckelordet return: båda används för att definiera ett returvärde. Skillnaden är att yield inte \"stänger\" funktionen på samma sätt som return. En generatorfunktion med nyckelordet yield håller reda på sitt tillstånd och nästa gång den anropas kommer den att fortsätta från samma tillstånd.

Den här generatorn kräver också ett maxvärde, i exemplet ovan var det 10. När generatorn får slut på värden kommer den att ge upphov till ett StopIteration-undantag:

if __name__ == \"__main__\":\n    talen = raknare(1)\n    print(next(talen))\n    print(next(talen))\n    print(next(talen))
0\n1\nTraceback (most recent call last):\nFile \"generatorexempel.py\", line 11, in \nprint(next(talen))\nStopIteration

Undantaget kan bli fångat med ett try- except block:

if __name__ == \"__main__\":\n    talen = raknare(1)\n    try:\n        print(next(talen))\n        print(next(talen))\n        print(next(talen))\n    except StopIteration:\n        print(\"Talen tog slut\")

0\n1\nTalen tog slut

Att gå igenom alla objekt i en generator görs enkelt med en for-loop:

if __name__ == \"__main__\":\n    talen = raknare(5)\n    for tal in talen:\n        print(tal)

0\n1\n2\n3\n4\n5

Generatorer behöver inte ha ett definierat maxvärde eller en slutpunkt. De kan generera värden i det oändliga (naturligtvis inom andra beräkningsmässiga och fysiska begränsningar).

Tänk dock på att det bara fungerar att genomkorsa en generator med en for-loop om generatorn avslutas vid någon punkt. Om generatorn är uppbyggd på en oändlig loop kommer en enkel for-loop att orsaka en oändlig exekvering, precis som en while-loop utan slut- eller brytvillkor.

Skapa generatorfunktionen jamna(borjan: int, maximum: int), som tar två heltal som argument. Funktionen ska producera jämna tal börjandes från borjan och slutandes vid, senast, maximum.

Två exempel på användning av funktionen:

talen = jamna(2, 10)\nfor tal in talen:\n    print(tal)

2\n4\n6\n8\n10

talen = jamna(11, 21)\nfor tal in talen:\n    print(tal)

12\n14\n16\n18\n20

Ett primtal är ett tal som är delbart endast med sig självt och talet 1. Enligt konvention definieras primtal som positiva heltal från talet 2 och uppåt. De sex första primtalen är 2, 3, 5, 7, 11 och 13.

Skapa en generatorfunktion primtal() som skapar en ny generator. Generatorn ska returnera nya primtal, ett efter ett i sekvens, från 2 framåt. OBS: den här generatorn avslutas aldrig. Den kommer att generera tal så länge som de behövs.

Till exempel

talen = primtal()\nfor i in range(8):\n    print(next(talen))

2\n3\n5\n7\n11\n13\n17\n19

Tips: du kan använda en loop för att granska ifall ett tal är ett primtal. Ifall vi granskar talet x skulle loopen gå igenom talen 2 till x-1. Ifall x är dividerbart med någon av dessa, är det inte ett primtal.

Generator comprehensions

Du behöver inte nödvändigtvis en funktionsdefinition för att skapa en generator. Vi kan använda en struktur som liknar en list comprehension istället. Den här gången använder vi runda parenteser för att beteckna en generator i stället för en lista eller en ordlista:

# Generatorn returnerar kvadraten av heltal\nkvadrat = (x ** 2 for x in range(1, 64))\n\nprint(kvadrat)\n\nfor i in range(5):\n    print(next(kvadrat))

<generator object <genexpr> at 0x000002B4224EBFC0>\n1\n4\n9\n16\n25

I följande exempel skriver vi ut delsträngar av det engelska alfabetet, var och en tre tecken lång. Detta skriver ut de första 10 objekten i generatorn:

delstrangar = (\"abcdefghijklmnopqrstuvwxyz\"[i : i + 3] for i in range(24))\n\n# skriver ut 10 första delsträngarna\nfor i in range(10):\n    print(next(delstrangar))

abc\nbcd\ncde\ndef\nefg\nfgh\nghi\nhij\nijk\njkl

Skapa funktionen ordgenerator(bokstaver: str, langd: int, antal: int), som returnerar en ny generator som genererar nya, slumpmässiga ord baserat på de angiva parametrarna.

Ett slumpmässigt ord är genererat genom att välja från en sträng av bokstaver så många bokstäver som är indikerade av argumentet langd. Samma bokstav kan förekomma flera gånger i ett slumpmässigt ord.

Generatorn returnerar så många ord som specifierades i argumentet antal innan dess terminering.

Exempel på användning av generatorn:

ordgen = ordgenerator(\"abcdefg\", 3, 5)\nfor ord in ordgen:\n    print(ord)

dbf\nbaf\nead\nfga\nccc

OBS: Det är upp till dig hur du implementerar denna funktion. Du kan använda en \"traditionell\" generator eller en generator comprehension lika väl.

","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/2-generatorer.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-12/3-funktionell-programmering/page-data.json b/page-data/osa-12/3-funktionell-programmering/page-data.json index 62e68fba2..54289f119 100644 --- a/page-data/osa-12/3-funktionell-programmering/page-data.json +++ b/page-data/osa-12/3-funktionell-programmering/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-12/3-funktionell-programmering", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad funktionell programmering innebär"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" i dina egna program"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionell programmering avser ett programmeringsparadigm som undviker förändringar i programtillståndet så mycket som möjligt. Variabler undviks i allmänhet. Istället är det kedjor av funktionsanrop som utgör ryggraden i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lambda-uttryck och olika typer av förståelser är vanliga tekniker i den funktionella programmeringsstilen, eftersom de låter dig bearbeta data utan att lagra dem i variabler, så att programmets tillstånd inte ändras. Ett lambdauttryck är till exempel i alla avseenden en funktion, men vi behöver inte lagra en namngiven referens till den någonstans."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som nämnts ovan är funktionell programmering ett programmeringsparadigm, eller en programmeringsstil. Det finns många olika programmeringsparadigm, och vi har redan stött på några av dem:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"imperativ programmering, där programmet består av en sekvens av instruktioner som utförs i tur och ordning"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"procedurprogrammering, där programmet är uppdelat i procedurer eller underprogram"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"objektorienterad programmering, där programmet och dess tillstånd lagras i objekt som definieras i klasser."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns olika uppfattningar om gränsdragningen mellan de olika paradigmen, t.ex. hävdar vissa att imperativ och procedurell programmering betyder samma sak, medan andra placerar imperativ programmering som ett paraplybegrepp som täcker både procedurell och objektorienterad programmering. Terminologin och uppdelningen är inte så viktig, och det är inte heller viktigt att strikt hålla sig till det ena eller andra paradigmet, men det är viktigt att förstå att det finns sådana olika synsätt eftersom de påverkar de val som programmerare gör."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Många programmeringsspråk är utformade med det ena eller det andra programmeringsparadigmet i åtanke, men Python är ett ganska mångsidigt programmeringsspråk och gör det möjligt att följa flera olika programmeringsparadigm, även inom ett enda program. Detta gör att vi kan välja den mest effektiva och tydliga metoden för att lösa varje problem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på några funktionella programmeringsverktyg som tillhandahålls av Python."}]},{"type":"element","tagName":"h2","properties":{"id":"map","style":"position:relative;"},"children":[{"type":"text","value":"map"},{"type":"element","tagName":"a","properties":{"href":"#map","ariaLabel":"map permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" utför någon operation på varje objekt i en iterabel serie. Det här låter ungefär som den effekt en comprehension har, men syntaxen är annorlunda."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi har en lista med strängar som vi vill konvertera till en lista med heltal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"stranglista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"23\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"98\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-110\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntalen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stranglista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\n123\n-10\n23\n98\n0\n-110"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den allmänna syntaxen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen är"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map(, )"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"funktion"}]},{"type":"text","value":" är den operation vi vill utföra på varje föremål i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"serie"}]},{"type":"text","value":"n."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen returnerar ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", som är itererbart och kan konverteras till en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"versalisera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntestlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nfardiga "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"versalisera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfardiga_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fardiga_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['Första', 'Andra', 'Tredje', 'Fjärde']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se i exemplen ovan accepterar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen både en anonym lambda-funktion och en namngiven funktion som definieras med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skulle kunna uppnå samma resultat med en list comprehension:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"versalisera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntestlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n\nfardiga_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"versalisera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fardiga_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...eller så kan vi gå igenom den ursprungliga listan med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop och spara de bearbetade objekten i en ny lista med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":"-metoden. I programmering finns det vanligtvis många olika lösningar på varje problem. Det finns sällan några absolut rätta eller felaktiga svar. Att känna till många olika tillvägagångssätt hjälper dig att välja den mest lämpliga för varje situation, eller den som bäst passar din egen smak."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är värt att påpeka att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen inte returnerar en lista, utan ett iteratorobjekt av typen map. En iterator beter sig på många sätt som en lista, men det finns undantag, vilket kan ses i följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"versalisera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntestlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lagrar map-funktionens returvärde"}]},{"type":"text","value":"\nfardiga "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"versalisera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"samma igen:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta skulle skriva ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Första\nAndra\nTredje\nFjärde\nsamma igen:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovan försökte vi skriva ut innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-iteratorn två gånger, men det andra försöket gav ingen utskrift. Anledningen är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" är en iterator; när man går igenom den med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop \"töms\" den, ungefär som en generator töms när dess maximala värde har uppnåtts. När objekten i iteratorn har genomgåtts med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop finns det inget kvar att gå igenom."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du behöver gå igenom innehållet i en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-iterator mer än en gång kan du t.ex. konvertera map till en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"testlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# konvertera returvärdet från map-funktionen till en lista"}]},{"type":"text","value":"\nfardiga "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"versalisera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"samma igen:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Första\nAndra\nTredje\nFjärde\nsamma igen:\nFörsta\nAndra\nTredje\nFjärde"}]}]},{"type":"element","tagName":"h2","properties":{"id":"map-funktionen-och-dina-egna-klasser","style":"position:relative;"},"children":[{"type":"text","value":"Map-funktionen och dina egna klasser"},{"type":"element","tagName":"a","properties":{"href":"#map-funktionen-och-dina-egna-klasser","ariaLabel":"map funktionen och dina egna klasser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan naturligtvis också bearbeta instanser av dina egna klasser med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen. Det krävs inga speciella knep, som du kan se i exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo\n\nk1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nk2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pank\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nk3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"223344\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja Miljonär \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkonton "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"k1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" k2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" k3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nkunder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kunder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsaldon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" saldo "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" saldon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Robert Rik\nPeter Pank\nMaja Miljonär\n5000\n1\n1000000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här samlar vi först in namnen på kontoinnehavarna med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen. En anonym lambda-funktion används för att hämta värdet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":"-attributet från varje Bankkonto-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kunder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt samlas saldot för varje Bankkonto in. Lambda-funktionen ser lite annorlunda ut, eftersom saldot hämtas med ett metodanrop, inte direkt från attributet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"saldon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Prestationer","tmcname":"osa12-11_prestationer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Prestation"}]},{"type":"text","value":", som fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"prestation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandens_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python\nIntroduktion till programmering\n5\nPeter Python, vitsord från kursen Introduktion till programmering 5"}]}]},{"type":"element","tagName":"h2","properties":{"id":"namnet-pa-studeranden","style":"position:relative;"},"children":[{"type":"text","value":"Namnet på studeranden"},{"type":"element","tagName":"a","properties":{"href":"#namnet-pa-studeranden","ariaLabel":"namnet pa studeranden permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"presterarnas_namn(prestationer: list)"}]},{"type":"text","value":" som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista med namnen på studeranden som genomför kursen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" presterarnas_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python\nPatrik Programmerare\nPeter Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen!"}]},{"type":"element","tagName":"h2","properties":{"id":"kurser","style":"position:relative;"},"children":[{"type":"text","value":"Kurser"},{"type":"element","tagName":"a","properties":{"href":"#kurser","ariaLabel":"kurser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kursnamn(prestationer: list)"}]},{"type":"text","value":" som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista som innehåller namnet på kurserna i den ursprungliga listan i alfabetisk ordning. Varje kursnamn ska förekomma endast en gång på listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kursnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fortsättningskurs i programmering\nIntroduktion till programmering"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen. Du behöver nånting annat också, för att se till att kursnamnen är unika."}]}]},{"type":"element","tagName":"h2","properties":{"id":"filter","style":"position:relative;"},"children":[{"type":"text","value":"filter"},{"type":"element","tagName":"a","properties":{"href":"#filter","ariaLabel":"filter permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den inbyggda Python-funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" liknar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen, men som namnet antyder tar den inte alla föremål från källan. Istället filtrerar den dem med en kriteriefunktion, som skickas som ett argument. Om kriteriefunktionen returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" väljs föremålet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett exempel med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\njamna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n6\n4\n10\n14"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det kunde göra ovanstående exemplet en aning tydligare ifall vi använde en namngiven funktion istället:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ar_det_jamnt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\ntalen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\njamna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar_det_jamnt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa två program är funktionellt helt identiska. Det är mest en fråga om åsikt vilket du anser vara det bättre tillvägagångssättet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ett annat filtreringsexempel. Det här programmet modellerar fiskar och väljer bara ut dem som väger minst 1000 gram:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Fisk"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en fisk av en specifik art och vikt \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" art"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"art "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" art\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vikt\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"art"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" g.)\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n f1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gädda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1870"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Abborre\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"763"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gädda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3410"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Torsk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2449"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mört\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fiskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n over_kilot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fiskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_kilot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gädda (1870 g.)\nGädda (3410 g.)\nTorsk (2449 g.)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kunde lika väl använda oss av en list comprehension för att uppnå samma resultat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"over_kilot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fiskar "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"returvardet-for-filter-ar-en-iterator","style":"position:relative;"},"children":[{"type":"text","value":"Returvärdet för filter är en iterator"},{"type":"element","tagName":"a","properties":{"href":"#returvardet-for-filter-ar-en-iterator","ariaLabel":"returvardet for filter ar en iterator permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" liknar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" även i det avseendet att den returnerar en iterator. Det finns situationer där du bör vara särskilt försiktig med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" eftersom iteratorer bara kan genomlöpas en gång. Så att försöka skriva ut samlingen av stora fiskar två gånger kommer inte att fungera så enkelt som du kanske tror:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"f1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gädda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1870"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Abborre\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"763"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gädda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3410"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Torsk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2449"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mört\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfiskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nover_kilot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fiskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_kilot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"samma igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_kilot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta skulle skriva ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gädda (1870 g.)\nGädda (3410 g.)\nTorsk (2449 g.)\nsamma igen"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du behöver gå igenom innehållet i en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" iterator mer än en gång kan du konvertera resultatet till en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fiskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# konvertera returvärdet från filter-funktionen till en lista"}]},{"type":"text","value":"\nover_kilot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fiskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Begränsade prestationer","tmcname":"osa12-12_begransade_prestationer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna övning fortsätter vi med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Prestation"}]},{"type":"text","value":"-klassen."}]},{"type":"element","tagName":"h2","properties":{"id":"godkanda-forsok","style":"position:relative;"},"children":[{"type":"text","value":"Godkända försök"},{"type":"element","tagName":"a","properties":{"href":"#godkanda-forsok","ariaLabel":"godkanda forsok permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"godkanda(prestationer: list)"}]},{"type":"text","value":", som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista av Prestation-objekt, inkluderandes endast de föremål från den ursprungliga listan vars vitsord är åtminstone 1."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" godkanda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python, vitsord från kursen Introduktion till programmering 3\nPatrik Programmerare vitsord från kursen Introduktion till programmering 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"-funktionen!"}]},{"type":"element","tagName":"h2","properties":{"id":"prestationer-med-vitsord","style":"position:relative;"},"children":[{"type":"text","value":"Prestationer med vitsord"},{"type":"element","tagName":"a","properties":{"href":"#prestationer-med-vitsord","ariaLabel":"prestationer med vitsord permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prestationer_med_vitsord(prestationer: list, vitsord: int)"}]},{"type":"text","value":", som tar en lista av Prestation-objekt och ett heltal som argument. Funktionen ska returnera en ny lista som innehåller endast de Prestation-objekt från den ursprungliga listan vars vitsord matchar det andra argumentet."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till universitetsmatematik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" prestationer_med_vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python, vitsord från kursen Introduktion till programmering 3\nPeter Python, vitsord från kursen Introduktion till datakommunikation 3\nPatrik Programmerare, vitsord från kursen Introduktion till universitetsmatematik 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"-funktionen!"}]},{"type":"element","tagName":"h2","properties":{"id":"studeranden-som-klarat-kursen","style":"position:relative;"},"children":[{"type":"text","value":"Studeranden som klarat kursen"},{"type":"element","tagName":"a","properties":{"href":"#studeranden-som-klarat-kursen","ariaLabel":"studeranden som klarat kursen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kursens_deltagare(prestationer: list, kurs: str)"}]},{"type":"text","value":", som tar en lista av Prestation-objekt och ett kursnamn som argument. Funktionen ska returnera en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"alfabetiskt sorterad"}]},{"type":"text","value":" lista av namn på de studeranden som klarat av kursen, d.v.s. deras vitsord för den givna kursen är högre än 0."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Niko Nörd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kursens_deltagare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Niko Nörd\nPatrik Programmerare"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"- och map-funktionerna!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"reduce","style":"position:relative;"},"children":[{"type":"text","value":"reduce"},{"type":"element","tagName":"a","properties":{"href":"#reduce","ariaLabel":"reduce permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En tredje hörnstensfunktion i denna introduktion till funktionella programmeringsprinciper är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":", från modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"functools"}]},{"type":"text","value":". Som namnet antyder är dess syfte att reducera objekten i en serie till ett enda värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen börjar med en operation och ett startvärde. Den utför den givna operationen på varje objekt i serien i tur och ordning, så att värdet ändras i varje steg. När alla objekt har bearbetats returneras det resulterande värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har gjort summering av listor med heltal på olika sätt tidigare, men här har vi ett exempel med hjälp av funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":". Notera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen; i Python version 3 och senare är den nödvändig för att komma åt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen. I äldre Python-versioner behövdes inte "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen, så du kan stöta på exempel utan den på nätet."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntalens_summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en närmare titt på vad som händer här. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen tar emot tre argument: en funktion, en serie av föremål och ett startvärde. I det här fallet är serien en lista med heltal, och eftersom vi beräknar en summa är ett lämpligt startvärde noll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det första argumentet är en funktion, som representerar den operation vi vill utföra på varje objekt. Här är funktionen en anonym lambda-funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" foremal"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna funktion tar två argument: det aktuella reducerade värdet och det föremål vars tur det är att bearbetas. Dessa används för att beräkna ett nytt värde för det reducerade värdet. I detta fall är det nya värdet summan av det gamla värdet och det aktuella objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det kan vara lättare att förstå vad funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" faktiskt gör om vi använder en vanlig namngiven funktion i stället för en lambda-funktion. På så sätt kan vi också inkludera användbara utskrifter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# en hjälparfunktion för reduce, som lägger till ett värde till den för tillfället reducerade summan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"summa nu "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", nästa föremål "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# den nya summan är gamla summan + nästa föremål"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" foremal\n\ntalens_summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta program skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"summa nu 0, nästa föremål 2\nsumma nu 2, nästa föremål 3\nsumma nu 5, nästa föremål 1\nsumma nu 6, nästa föremål 5\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först tar funktionen hand om objektet med värdet 2. Till att börja med är den reducerade summan 0, vilket är det ursprungliga värdet som skickas till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen. Funktionen beräknar och returnerar summan av dessa två: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0 + 2 = 2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta är det värde som lagras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reducerad_summa"}]},{"type":"text","value":" när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen bearbetar nästa objekt i listan, med värdet 3. Funktionen beräknar och returnerar summan av dessa två: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 3 = 5"}]},{"type":"text","value":". Detta resultat används sedan när nästa objekt bearbetas, och så vidare, och så vidare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu är det enkelt att summera, eftersom det till och med finns en inbyggd "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":"-funktion för detta ändamål. Men hur är det med multiplikation? Det krävs bara små förändringar för att skapa en reducerad produkt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nprodukt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"480"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom vi har att göra med multiplikation är startvärdet inte noll. Istället använder vi 1. Vad skulle hända om vi använde 0 som startvärde?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovan har vi till stor del behandlat heltal, men "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" kan alla hantera en samling objekt av alla typer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss som ett exempel generera en totalsumma av saldona för alla konton i en bank med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo\n\nk1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nk2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pank\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nk3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"223344\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja Miljonär\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkonton "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"k1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" k2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" k3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tot_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" tot_saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsaldon_totalt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldo_summa_hjalpare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bankens totala saldo:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldon_totalt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta program skulle skriva ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bankens totala saldo:\n1005001"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"text","value":" tar saldot på varje bankkonto, med den metod som är avsedd för ändamålet i klassdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tot_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" tot_saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Alkuarvoton reduce"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du måste inte alltid ange ett tredje argument till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen. Till exempel skulle summering fungera lika väl "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"utan"}]},{"type":"text","value":" ursprungsvärdet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntalens_summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall ett ursprungligt värde lämnas bort tar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" det första föremålet i listan som sitt ursprungsvärde och börjar reducera från det andra föremålet framåt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Om föremålen i serien är av en annan typ än det avsedda reducerade resultatet, är det tredje argumentet obligatoriskt. Exemplet med bankkontona skulle inte fungera utan det ursprungliga värdet. Det vill säga att prova detta"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"saldon_totalt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldo_summa_hjalpare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skulle producera ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"TypeError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" unsupported operand "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Bankkonto'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'int'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I ovanstående fall, när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" försöker utföra funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"text","value":" för första gången, är de argument som används de två första föremålen i listan, som båda är av typen Bankkonto. Specifikt är det värde som tilldelats parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo_summa"}]},{"type":"text","value":" det första föremålet i listan. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"text","value":" försöker lägga till ett heltalsvärde till den, men att lägga till ett heltal direkt till ett Bankkonto-objekt är inte en åtgärd som stöds."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Studiepoäng","tmcname":"osa12-13_studiepoang"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna övning jobbar vi med en aning modifierad version av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Prestation"}]},{"type":"text","value":"-klassen. Namnet på studeranden är utelämnat, men antalet studiepoäng är inkluderat. Klassen fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"prestation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Datastrukturer och algoritmer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datastrukturer och algoritmer (10 sp) vitsord 3\nDatastrukturer och algoritmer\n10\n3"}]}]},{"type":"element","tagName":"h2","properties":{"id":"totala-antalet-studiepoang","style":"position:relative;"},"children":[{"type":"text","value":"Totala antalet studiepoäng"},{"type":"element","tagName":"a","properties":{"href":"#totala-antalet-studiepoang","ariaLabel":"totala antalet studiepoang permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studiepoangens_summa"}]},{"type":"text","value":", som tar en lista av prestationer som argument. Funktionen adderar ihop det totala antalet studiepoäng som fås av kurserna. Den ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Datastrukturer och algoritmer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoangens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen!"}]},{"type":"element","tagName":"h2","properties":{"id":"summan-av-avklarade-studiepoang","style":"position:relative;"},"children":[{"type":"text","value":"Summan av avklarade studiepoäng"},{"type":"element","tagName":"a","properties":{"href":"#summan-av-avklarade-studiepoang","ariaLabel":"summan av avklarade studiepoang permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"godkanda_studiepoangens_summa"}]},{"type":"text","value":", som tar en lista av prestationer som argument. Funktionen adderar ihop studiepoängen för prestationerna med vitsordet 1 eller högre. Den ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Datastrukturer och algoritmer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" godkanda_studiepoangens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"15"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"-funktionerna!"}]},{"type":"element","tagName":"h2","properties":{"id":"avklarade-kursers-medelvitsord","style":"position:relative;"},"children":[{"type":"text","value":"Avklarade kursers medelvitsord"},{"type":"element","tagName":"a","properties":{"href":"#avklarade-kursers-medelvitsord","ariaLabel":"avklarade kursers medelvitsord permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"medeltal"}]},{"type":"text","value":", som tar en lista av suorits som argument. Funktionen räknar ut medelvitsordet för prestationerna med vitsord 1 eller högre. Den ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Datastrukturer och algoritmer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"-funktionerna!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du jobbar på den här övningen kan det vara värt att komma ihåg att returvärdet av filter är en iterator."}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad funktionell programmering innebär
  • \n
  • Kommer du att kunna använda funktionerna map, reduce och filter i dina egna program
  • \n

Funktionell programmering avser ett programmeringsparadigm som undviker förändringar i programtillståndet så mycket som möjligt. Variabler undviks i allmänhet. Istället är det kedjor av funktionsanrop som utgör ryggraden i programmet.

Lambda-uttryck och olika typer av förståelser är vanliga tekniker i den funktionella programmeringsstilen, eftersom de låter dig bearbeta data utan att lagra dem i variabler, så att programmets tillstånd inte ändras. Ett lambdauttryck är till exempel i alla avseenden en funktion, men vi behöver inte lagra en namngiven referens till den någonstans.

Som nämnts ovan är funktionell programmering ett programmeringsparadigm, eller en programmeringsstil. Det finns många olika programmeringsparadigm, och vi har redan stött på några av dem:

    \n
  • imperativ programmering, där programmet består av en sekvens av instruktioner som utförs i tur och ordning
  • \n
  • procedurprogrammering, där programmet är uppdelat i procedurer eller underprogram
  • \n
  • objektorienterad programmering, där programmet och dess tillstånd lagras i objekt som definieras i klasser.
  • \n

Det finns olika uppfattningar om gränsdragningen mellan de olika paradigmen, t.ex. hävdar vissa att imperativ och procedurell programmering betyder samma sak, medan andra placerar imperativ programmering som ett paraplybegrepp som täcker både procedurell och objektorienterad programmering. Terminologin och uppdelningen är inte så viktig, och det är inte heller viktigt att strikt hålla sig till det ena eller andra paradigmet, men det är viktigt att förstå att det finns sådana olika synsätt eftersom de påverkar de val som programmerare gör.

Många programmeringsspråk är utformade med det ena eller det andra programmeringsparadigmet i åtanke, men Python är ett ganska mångsidigt programmeringsspråk och gör det möjligt att följa flera olika programmeringsparadigm, även inom ett enda program. Detta gör att vi kan välja den mest effektiva och tydliga metoden för att lösa varje problem.

Låt oss ta en titt på några funktionella programmeringsverktyg som tillhandahålls av Python.

map

Funktionen map utför någon operation på varje objekt i en iterabel serie. Det här låter ungefär som den effekt en comprehension har, men syntaxen är annorlunda.

Låt oss anta att vi har en lista med strängar som vi vill konvertera till en lista med heltal:

stranglista = [\"123\",\"-10\", \"23\", \"98\", \"0\", \"-110\"]\n\ntalen = map(lambda x : int(x), stranglista)\n\nprint(talen)\n\nfor tal in talen:\n    print(tal)

<map object at 0x0000021A4BFA9A90>\n123\n-10\n23\n98\n0\n-110

Den allmänna syntaxen för map-funktionen är

map(<funktion>, <serie>)

där funktion är den operation vi vill utföra på varje föremål i serien.

map-funktionen returnerar ett objekt av typen map, som är itererbart och kan konverteras till en lista:

def versalisera(strang: str):\n    borjan = strang[0]\n    borjan = borjan.upper()\n    return borjan + strang[1:]\n\ntestlista = [\"första\", \"andra\", \"tredje\", \"fjärde\"]\n\nfardiga = map(versalisera, testlista)\n\nfardiga_lista = list(fardiga)\nprint(fardiga_lista)

['Första', 'Andra', 'Tredje', 'Fjärde']

Som du kan se i exemplen ovan accepterar map-funktionen både en anonym lambda-funktion och en namngiven funktion som definieras med nyckelordet def.

Vi skulle kunna uppnå samma resultat med en list comprehension:

def versalisera(strang: str):\n    borjan = strang[0]\n    borjan = borjan.upper()\n    return borjan + strang[1:]\n\ntestlista = [\"första\", \"andra\", \"tredje\", \"fjärde\"]\n\n\nfardiga_lista = [versalisera(foremal) for foremal in testlista]\nprint(fardiga_lista)

...eller så kan vi gå igenom den ursprungliga listan med en for-loop och spara de bearbetade objekten i en ny lista med append-metoden. I programmering finns det vanligtvis många olika lösningar på varje problem. Det finns sällan några absolut rätta eller felaktiga svar. Att känna till många olika tillvägagångssätt hjälper dig att välja den mest lämpliga för varje situation, eller den som bäst passar din egen smak.

Det är värt att påpeka att map-funktionen inte returnerar en lista, utan ett iteratorobjekt av typen map. En iterator beter sig på många sätt som en lista, men det finns undantag, vilket kan ses i följande exempel:

def versalisera(strang: str):\n    borjan = strang[0]\n    borjan = borjan.upper()\n    return borjan + strang[1:]\n\ntestlista = [\"första\", \"andra\", \"tredje\", \"fjärde\"]\n\n# lagrar map-funktionens returvärde\nfardiga = map(versalisera, testlista)\n\nfor ord in fardiga:\n  print(ord)\n\nprint(\"samma igen:\")\nfor ord in fardiga:\n  print(ord)

Detta skulle skriva ut följande:

Första\nAndra\nTredje\nFjärde\nsamma igen:

Ovan försökte vi skriva ut innehållet i map-iteratorn två gånger, men det andra försöket gav ingen utskrift. Anledningen är att map är en iterator; när man går igenom den med en for-loop \"töms\" den, ungefär som en generator töms när dess maximala värde har uppnåtts. När objekten i iteratorn har genomgåtts med en for-loop finns det inget kvar att gå igenom.

Om du behöver gå igenom innehållet i en map-iterator mer än en gång kan du t.ex. konvertera map till en lista:

testlista = [\"första\", \"andra\", \"tredje\", \"fjärde\"]\n\n# konvertera returvärdet från map-funktionen till en lista\nfardiga = list(map(versalisera, testlista))\n\nfor ord in fardiga:\n  print(ord)\n\nprint(\"samma igen:\")\nfor ord in fardiga:\n  print(ord)

Första\nAndra\nTredje\nFjärde\nsamma igen:\nFörsta\nAndra\nTredje\nFjärde

Map-funktionen och dina egna klasser

Du kan naturligtvis också bearbeta instanser av dina egna klasser med map-funktionen. Det krävs inga speciella knep, som du kan se i exemplet nedan:

class Bankkonto:\n    def __init__(self, nummer: str, namn: str, saldo: float):\n        self.__nummer = nummer\n        self.namn = namn\n        self.__saldo = saldo\n\n    def tillsatt_pengar(self, mangd: float):\n        if mangd > 0:\n            self.__saldo += mangd\n\n    def hamta_saldo(self):\n        return self.__saldo\n\nk1 = Bankkonto(\"123456\", \"Robert Rik\", 5000)\nk2 = Bankkonto(\"12321\", \"Peter Pank\", 1)\nk3 = Bankkonto(\"223344\", \"Maja Miljonär \", 1000000)\n\nkonton = [k1, k2, k3]\n\nkunder = map(lambda t: t.namn, konton)\nfor namn in kunder:\n  print(namn)\n\nsaldon = map(lambda t: t.hamta_saldo(), konton)\nfor saldo in saldon:\n  print(saldo)

Robert Rik\nPeter Pank\nMaja Miljonär\n5000\n1\n1000000

Här samlar vi först in namnen på kontoinnehavarna med map-funktionen. En anonym lambda-funktion används för att hämta värdet på namn-attributet från varje Bankkonto-objekt:

kunder = map(lambda t: t.namn, konton)

På samma sätt samlas saldot för varje Bankkonto in. Lambda-funktionen ser lite annorlunda ut, eftersom saldot hämtas med ett metodanrop, inte direkt från attributet:

saldon = map(lambda t: t.hamta_saldo(), konton)

I uppgiftsbotten finns klassdefinitionen för Prestation, som fungerar enligt följande:

prestation = Prestation(\"Peter Python\", \"Introduktion till programmering\", 5)\nprint(prestation.studerandens_namn)\nprint(prestation.kurs)\nprint(prestation.vitsord)\nprint(prestation)

Peter Python\nIntroduktion till programmering\n5\nPeter Python, vitsord från kursen Introduktion till programmering 5

Namnet på studeranden

Skapa funktionen presterarnas_namn(prestationer: list) som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista med namnen på studeranden som genomför kursen.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till programmering\", 5)\np3 = Prestation(\"Peter Python\", \"Fortsättningskurs i programmering\", 2)\n\nfor namn in presterarnas_namn([p1, p2, p3]):\n    print(namn)

Peter Python\nPatrik Programmerare\nPeter Python

Implementera funktionen användandes map-funktionen!

Kurser

Skapa funktionen kursnamn(prestationer: list) som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista som innehåller namnet på kurserna i den ursprungliga listan i alfabetisk ordning. Varje kursnamn ska förekomma endast en gång på listan.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till programmering\", 5)\np3 = Prestation(\"Peter Python\", \"Fortsättningskurs i programmering\", 2)\n\nfor namn in kursnamn([p1, p2, p3]):\n    print(namn)

Fortsättningskurs i programmering\nIntroduktion till programmering

Implementera funktionen användandes map-funktionen. Du behöver nånting annat också, för att se till att kursnamnen är unika.

filter

Den inbyggda Python-funktionen filter liknar map-funktionen, men som namnet antyder tar den inte alla föremål från källan. Istället filtrerar den dem med en kriteriefunktion, som skickas som ett argument. Om kriteriefunktionen returnerar True väljs föremålet.

Låt oss titta på ett exempel med filter:

talen = [1, 2, 3, 5, 6, 4, 9, 10, 14, 15]\n\njamna = filter(lambda tal: tal % 2 == 0, talen)\n\nfor tal in jamna:\n    print(tal)

2\n6\n4\n10\n14

Det kunde göra ovanstående exemplet en aning tydligare ifall vi använde en namngiven funktion istället:

def ar_det_jamnt(tal: int):\n    if tal % 2 == 0:\n        return True\n    return False\n\ntalen = [1, 2, 3, 5, 6, 4, 9, 10, 14, 15]\n\njamna = filter(ar_det_jamnt, talen)\n\nfor tal in jamna:\n    print(tal)

Dessa två program är funktionellt helt identiska. Det är mest en fråga om åsikt vilket du anser vara det bättre tillvägagångssättet.

Låt oss ta en titt på ett annat filtreringsexempel. Det här programmet modellerar fiskar och väljer bara ut dem som väger minst 1000 gram:

class Fisk:\n    \"\"\" Klassen modellerar en fisk av en specifik art och vikt \"\"\"\n    def __init__(self, art: str, vikt: int):\n        self.art = art\n        self.vikt = vikt\n\n    def __repr__(self):\n        return f\"{self.art} ({self.vikt} g.)\"\n\nif __name__ == \"__main__\":\n    f1 = Fisk(\"Gädda\", 1870)\n    f2 = Fisk(\"Abborre\", 763)\n    f3 = Fisk(\"Gädda\", 3410)\n    f4 = Fisk(\"Torsk\", 2449)\n    f5 = Fisk(\"Mört\", 210)\n\n    fiskar = [f1, f2, f3, f4, f5]\n\n    over_kilot = filter(lambda fisk : fisk.vikt >= 1000, fiskar)\n\n    for fisk in over_kilot:\n        print(fisk)

Gädda (1870 g.)\nGädda (3410 g.)\nTorsk (2449 g.)

Vi kunde lika väl använda oss av en list comprehension för att uppnå samma resultat:

over_kilot = [fisk for fisk in fiskar if fisk.vikt >= 1000]

Returvärdet för filter är en iterator

Funktionen filter liknar funktionen map även i det avseendet att den returnerar en iterator. Det finns situationer där du bör vara särskilt försiktig med filter eftersom iteratorer bara kan genomlöpas en gång. Så att försöka skriva ut samlingen av stora fiskar två gånger kommer inte att fungera så enkelt som du kanske tror:

f1 = Fisk(\"Gädda\", 1870)\nf2 = Fisk(\"Abborre\", 763)\nf3 = Fisk(\"Gädda\", 3410)\nf4 = Fisk(\"Torsk\", 2449)\nf5 = Fisk(\"Mört\", 210)\n\nfiskar = [f1, f2, f3, f4, f5]\n\nover_kilot = filter(lambda fisk : fisk.vikt >= 1000, fiskar)\n\nfor fisk in over_kilot:\n    print(fisk)\n\nprint(\"samma igen\")\n\nfor fisk in over_kilot:\n    print(fisk)

Detta skulle skriva ut följande:

Gädda (1870 g.)\nGädda (3410 g.)\nTorsk (2449 g.)\nsamma igen

Om du behöver gå igenom innehållet i en filter iterator mer än en gång kan du konvertera resultatet till en lista:

fiskar = [f1, f2, f3, f4, f5]\n\n# konvertera returvärdet från filter-funktionen till en lista\nover_kilot = list(filter(lambda fisk : fisk.vikt >= 1000, fiskar))

I denna övning fortsätter vi med Prestation-klassen.

Godkända försök

Skapa funktionen godkanda(prestationer: list), som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista av Prestation-objekt, inkluderandes endast de föremål från den ursprungliga listan vars vitsord är åtminstone 1.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till programmering\", 5)\np3 = Prestation(\"Peter Python\", \"Fortsättningskurs i programmering\", 0)\n\nfor prestation in godkanda([p1, p2, p3]):\n    print(prestation)

Peter Python, vitsord från kursen Introduktion till programmering 3\nPatrik Programmerare vitsord från kursen Introduktion till programmering 5

Implementera funktionen användandes filter-funktionen!

Prestationer med vitsord

Skapa funktionen prestationer_med_vitsord(prestationer: list, vitsord: int), som tar en lista av Prestation-objekt och ett heltal som argument. Funktionen ska returnera en ny lista som innehåller endast de Prestation-objekt från den ursprungliga listan vars vitsord matchar det andra argumentet.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till programmering\", 5)\np3 = Prestation(\"Peter Python\", \"Introduktion till datakommunikation\", 3)\np4 = Prestation(\"Patrik Programmerare\", \"Introduktion till universitetsmatematik\", 3)\n\nfor prestation in prestationer_med_vitsord([p1, p2, p3, p4], 3):\n    print(prestation)

Peter Python, vitsord från kursen Introduktion till programmering 3\nPeter Python, vitsord från kursen Introduktion till datakommunikation 3\nPatrik Programmerare, vitsord från kursen Introduktion till universitetsmatematik 3

Implementera funktionen användandes filter-funktionen!

Studeranden som klarat kursen

Skapa funktionen kursens_deltagare(prestationer: list, kurs: str), som tar en lista av Prestation-objekt och ett kursnamn som argument. Funktionen ska returnera en alfabetiskt sorterad lista av namn på de studeranden som klarat av kursen, d.v.s. deras vitsord för den givna kursen är högre än 0.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till datakommunikation\", 5)\np3 = Prestation(\"Peter Python\", \"Introduktion till datakommunikation\", 0)\np4 = Prestation(\"Niko Nörd\", \"Introduktion till datakommunikation\", 3)\n\nfor prestation in kursens_deltagare([p1, p2, p3, p4], \"Introduktion till datakommunikation\"):\n    print(prestation)

Niko Nörd\nPatrik Programmerare

Implementera funktionen användandes filter- och map-funktionerna!

reduce

En tredje hörnstensfunktion i denna introduktion till funktionella programmeringsprinciper är reduce, från modulen functools. Som namnet antyder är dess syfte att reducera objekten i en serie till ett enda värde.

reduce-funktionen börjar med en operation och ett startvärde. Den utför den givna operationen på varje objekt i serien i tur och ordning, så att värdet ändras i varje steg. När alla objekt har bearbetats returneras det resulterande värdet.

Vi har gjort summering av listor med heltal på olika sätt tidigare, men här har vi ett exempel med hjälp av funktionen reduce. Notera import-satsen; i Python version 3 och senare är den nödvändig för att komma åt reduce-funktionen. I äldre Python-versioner behövdes inte import-satsen, så du kan stöta på exempel utan den på nätet.

from functools import reduce\n\nlista = [2, 3, 1, 5]\n\ntalens_summa = reduce(lambda summa, foremal: summa + foremal, lista, 0)\n\nprint(talens_summa)

11

Låt oss ta en närmare titt på vad som händer här. reduce-funktionen tar emot tre argument: en funktion, en serie av föremål och ett startvärde. I det här fallet är serien en lista med heltal, och eftersom vi beräknar en summa är ett lämpligt startvärde noll.

Det första argumentet är en funktion, som representerar den operation vi vill utföra på varje objekt. Här är funktionen en anonym lambda-funktion:

lambda summa, foremal: summa + foremal

Denna funktion tar två argument: det aktuella reducerade värdet och det föremål vars tur det är att bearbetas. Dessa används för att beräkna ett nytt värde för det reducerade värdet. I detta fall är det nya värdet summan av det gamla värdet och det aktuella objektet.

Det kan vara lättare att förstå vad funktionen reduce faktiskt gör om vi använder en vanlig namngiven funktion i stället för en lambda-funktion. På så sätt kan vi också inkludera användbara utskrifter:

from functools import reduce\n\nlista = [2, 3, 1, 5]\n\n# en hjälparfunktion för reduce, som lägger till ett värde till den för tillfället reducerade summan\ndef summare(summa, foremal):\n  print(f\"summa nu {summa}, nästa föremål {foremal}\")\n  # den nya summan är gamla summan + nästa föremål\n  return summa + foremal\n\ntalens_summa = reduce(summare, lista, 0)\n\nprint(talens_summa)

Detta program skriver ut:

summa nu 0, nästa föremål 2\nsumma nu 2, nästa föremål 3\nsumma nu 5, nästa föremål 1\nsumma nu 6, nästa föremål 5\n11

Först tar funktionen hand om objektet med värdet 2. Till att börja med är den reducerade summan 0, vilket är det ursprungliga värdet som skickas till reduce-funktionen. Funktionen beräknar och returnerar summan av dessa två: 0 + 2 = 2.

Detta är det värde som lagras i reducerad_summa när reduce-funktionen bearbetar nästa objekt i listan, med värdet 3. Funktionen beräknar och returnerar summan av dessa två: 2 + 3 = 5. Detta resultat används sedan när nästa objekt bearbetas, och så vidare, och så vidare.

Nu är det enkelt att summera, eftersom det till och med finns en inbyggd sum-funktion för detta ändamål. Men hur är det med multiplikation? Det krävs bara små förändringar för att skapa en reducerad produkt:

from functools import reduce\n\nlista = [2, 2, 4, 3, 5, 2]\n\nprodukt = reduce(lambda produkt, foremal: produkt * foremal, lista, 1)\n\nprint(produkt)

480

Eftersom vi har att göra med multiplikation är startvärdet inte noll. Istället använder vi 1. Vad skulle hända om vi använde 0 som startvärde?

Ovan har vi till stor del behandlat heltal, men map, filter och reduce kan alla hantera en samling objekt av alla typer.

Låt oss som ett exempel generera en totalsumma av saldona för alla konton i en bank med hjälp av reduce:

class Bankkonto:\n    def __init__(self, nummer: str, namn: str, saldo: float):\n        self.__nummer = nummer\n        self.namn = namn\n        self.__saldo = saldo\n\n    def tillsatt_pengar(self, mangd: float):\n        if mangd > 0:\n            self.__saldo += mangd\n\n    def hamta_saldo(self):\n        return self.__saldo\n\nk1 = Bankkonto(\"123456\", \"Robert Rik\", 5000)\nk2 = Bankkonto(\"12321\", \"Peter Pank\", 1)\nk3 = Bankkonto(\"223344\", \"Maja Miljonär\", 1000000)\n\nkonton = [k1, k2, k3]\n\nfrom functools import reduce\n\ndef saldo_summa_hjalpare(tot_saldo, konto):\n  return tot_saldo + konto.hamta_saldo()\n\nsaldon_totalt = reduce(saldo_summa_hjalpare, konton, 0)\n\nprint(\"Bankens totala saldo:\")\nprint(saldon_totalt)

Detta program skulle skriva ut:

Bankens totala saldo:\n1005001

Funktionen saldo_summa_hjalpare tar saldot på varje bankkonto, med den metod som är avsedd för ändamålet i klassdefinitionen Bankkonto:

def saldo_summa_hjalpare(tot_saldo, konto):\n  return tot_saldo + konto.hamta_saldo()

Du måste inte alltid ange ett tredje argument till reduce-funktionen. Till exempel skulle summering fungera lika väl utan ursprungsvärdet:

lista = [2, 3, 1, 5]\n\ntalens_summa = reduce(lambda summa, foremal: summa + foremal, lista)\n\nprint(talens_summa)

Ifall ett ursprungligt värde lämnas bort tar reduce det första föremålet i listan som sitt ursprungsvärde och börjar reducera från det andra föremålet framåt.

OBS: Om föremålen i serien är av en annan typ än det avsedda reducerade resultatet, är det tredje argumentet obligatoriskt. Exemplet med bankkontona skulle inte fungera utan det ursprungliga värdet. Det vill säga att prova detta

saldon_totalt = reduce(saldo_summa_hjalpare, konton)

Skulle producera ett fel:

TypeError: unsupported operand type(s) for +: 'Bankkonto' and 'int'

I ovanstående fall, när reduce försöker utföra funktionen saldo_summa_hjalpare för första gången, är de argument som används de två första föremålen i listan, som båda är av typen Bankkonto. Specifikt är det värde som tilldelats parametern saldo_summa det första föremålet i listan. Funktionen saldo_summa_hjalpare försöker lägga till ett heltalsvärde till den, men att lägga till ett heltal direkt till ett Bankkonto-objekt är inte en åtgärd som stöds.

I denna övning jobbar vi med en aning modifierad version av Prestation-klassen. Namnet på studeranden är utelämnat, men antalet studiepoäng är inkluderat. Klassen fungerar enligt följande:

prestation = Prestation(\"Datastrukturer och algoritmer\", 3, 10)\nprint(prestation)\nprint(prestation.kurs)\nprint(prestation.studiepoang)\nprint(prestation.vitsord)

Datastrukturer och algoritmer (10 sp) vitsord 3\nDatastrukturer och algoritmer\n10\n3

Totala antalet studiepoäng

Skapa funktionen studiepoangens_summa, som tar en lista av prestationer som argument. Funktionen adderar ihop det totala antalet studiepoäng som fås av kurserna. Den ska fungera enligt följande:

p1 = Prestation(\"Introduktion till programmering\", 5, 5)\np2 = Prestation(\"Fortsättningskurs i programmering\", 4, 5)\np3 = Prestation(\"Datastrukturer och algoritmer\", 3, 10)\nsumma = studiepoangens_summa([p1, p2, p3])\nprint(summa)

20

Implementera funktionen användandes reduce-funktionen!

Summan av avklarade studiepoäng

Skapa funktionen godkanda_studiepoangens_summa, som tar en lista av prestationer som argument. Funktionen adderar ihop studiepoängen för prestationerna med vitsordet 1 eller högre. Den ska fungera enligt följande:

p1 = Prestation(\"Introduktion till programmering\", 5, 5)\np2 = Prestation(\"Fortsättningskurs i programmering\", 0, 4)\np3 = Prestation(\"Datastrukturer och algoritmer\", 3, 10)\nsumma = godkanda_studiepoangens_summa([p1, p2, p3])\nprint(summa)

15

Implementera funktionen användandes reduce- och filter-funktionerna!

Avklarade kursers medelvitsord

Skapa funktionen medeltal, som tar en lista av suorits som argument. Funktionen räknar ut medelvitsordet för prestationerna med vitsord 1 eller högre. Den ska fungera enligt följande:

p1 = Prestation(\"Introduktion till programmering\", 5, 5)\np2 = Prestation(\"Fortsättningskurs i programmering\", 0, 4)\np3 = Prestation(\"Datastrukturer och algoritmer\", 3, 10)\nsumma = medeltal([p1, p2, p3])\nprint(summa)

4.0

Implementera funktionen användandes reduce- och filter-funktionerna!

När du jobbar på den här övningen kan det vara värt att komma ihåg att returvärdet av filter är en iterator.

","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/3-funktionell-programmering.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad funktionell programmering innebär"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" i dina egna program"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionell programmering avser ett programmeringsparadigm som undviker förändringar i programtillståndet så mycket som möjligt. Variabler undviks i allmänhet. Istället är det kedjor av funktionsanrop som utgör ryggraden i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lambda-uttryck och olika typer av förståelser är vanliga tekniker i den funktionella programmeringsstilen, eftersom de låter dig bearbeta data utan att lagra dem i variabler, så att programmets tillstånd inte ändras. Ett lambdauttryck är till exempel i alla avseenden en funktion, men vi behöver inte lagra en namngiven referens till den någonstans."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som nämnts ovan är funktionell programmering ett programmeringsparadigm, eller en programmeringsstil. Det finns många olika programmeringsparadigm, och vi har redan stött på några av dem:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"imperativ programmering, där programmet består av en sekvens av instruktioner som utförs i tur och ordning"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"procedurprogrammering, där programmet är uppdelat i procedurer eller underprogram"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"objektorienterad programmering, där programmet och dess tillstånd lagras i objekt som definieras i klasser."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns olika uppfattningar om gränsdragningen mellan de olika paradigmen, t.ex. hävdar vissa att imperativ och procedurell programmering betyder samma sak, medan andra placerar imperativ programmering som ett paraplybegrepp som täcker både procedurell och objektorienterad programmering. Terminologin och uppdelningen är inte så viktig, och det är inte heller viktigt att strikt hålla sig till det ena eller andra paradigmet, men det är viktigt att förstå att det finns sådana olika synsätt eftersom de påverkar de val som programmerare gör."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Många programmeringsspråk är utformade med det ena eller det andra programmeringsparadigmet i åtanke, men Python är ett ganska mångsidigt programmeringsspråk och gör det möjligt att följa flera olika programmeringsparadigm, även inom ett enda program. Detta gör att vi kan välja den mest effektiva och tydliga metoden för att lösa varje problem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på några funktionella programmeringsverktyg som tillhandahålls av Python."}]},{"type":"element","tagName":"h2","properties":{"id":"map","style":"position:relative;"},"children":[{"type":"text","value":"map"},{"type":"element","tagName":"a","properties":{"href":"#map","ariaLabel":"map permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" utför någon operation på varje objekt i en iterabel serie. Det här låter ungefär som den effekt en comprehension har, men syntaxen är annorlunda."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi har en lista med strängar som vi vill konvertera till en lista med heltal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"stranglista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"23\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"98\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-110\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntalen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stranglista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\n123\n-10\n23\n98\n0\n-110"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den allmänna syntaxen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen är"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map(, )"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"funktion"}]},{"type":"text","value":" är den operation vi vill utföra på varje föremål i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"serie"}]},{"type":"text","value":"n."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen returnerar ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", som är itererbart och kan konverteras till en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"versalisera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntestlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nfardiga "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"versalisera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfardiga_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fardiga_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['Första', 'Andra', 'Tredje', 'Fjärde']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se i exemplen ovan accepterar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen både en anonym lambda-funktion och en namngiven funktion som definieras med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skulle kunna uppnå samma resultat med en list comprehension:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"versalisera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntestlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n\nfardiga_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"versalisera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" foremal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fardiga_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...eller så kan vi gå igenom den ursprungliga listan med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop och spara de bearbetade objekten i en ny lista med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":"-metoden. I programmering finns det vanligtvis många olika lösningar på varje problem. Det finns sällan några absolut rätta eller felaktiga svar. Att känna till många olika tillvägagångssätt hjälper dig att välja den mest lämpliga för varje situation, eller den som bäst passar din egen smak."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är värt att påpeka att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen inte returnerar en lista, utan ett iteratorobjekt av typen map. En iterator beter sig på många sätt som en lista, men det finns undantag, vilket kan ses i följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"versalisera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntestlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lagrar map-funktionens returvärde"}]},{"type":"text","value":"\nfardiga "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"versalisera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"samma igen:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta skulle skriva ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Första\nAndra\nTredje\nFjärde\nsamma igen:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovan försökte vi skriva ut innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-iteratorn två gånger, men det andra försöket gav ingen utskrift. Anledningen är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" är en iterator; när man går igenom den med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop \"töms\" den, ungefär som en generator töms när dess maximala värde har uppnåtts. När objekten i iteratorn har genomgåtts med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop finns det inget kvar att gå igenom."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du behöver gå igenom innehållet i en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-iterator mer än en gång kan du t.ex. konvertera map till en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"testlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# konvertera returvärdet från map-funktionen till en lista"}]},{"type":"text","value":"\nfardiga "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"versalisera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" testlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"samma igen:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fardiga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Första\nAndra\nTredje\nFjärde\nsamma igen:\nFörsta\nAndra\nTredje\nFjärde"}]}]},{"type":"element","tagName":"h2","properties":{"id":"map-funktionen-och-dina-egna-klasser","style":"position:relative;"},"children":[{"type":"text","value":"Map-funktionen och dina egna klasser"},{"type":"element","tagName":"a","properties":{"href":"#map-funktionen-och-dina-egna-klasser","ariaLabel":"map funktionen och dina egna klasser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan naturligtvis också bearbeta instanser av dina egna klasser med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen. Det krävs inga speciella knep, som du kan se i exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo\n\nk1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nk2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pank\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nk3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"223344\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja Miljonär \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkonton "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"k1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" k2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" k3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nkunder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kunder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsaldon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" saldo "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" saldon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Robert Rik\nPeter Pank\nMaja Miljonär\n5000\n1\n1000000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här samlar vi först in namnen på kontoinnehavarna med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen. En anonym lambda-funktion används för att hämta värdet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":"-attributet från varje Bankkonto-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kunder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt samlas saldot för varje Bankkonto in. Lambda-funktionen ser lite annorlunda ut, eftersom saldot hämtas med ett metodanrop, inte direkt från attributet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"saldon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Prestationer","tmcname":"osa12-11_prestationer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Prestation"}]},{"type":"text","value":", som fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"prestation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandens_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python\nIntroduktion till programmering\n5\nPeter Python, vitsord från kursen Introduktion till programmering 5"}]}]},{"type":"element","tagName":"h2","properties":{"id":"namnet-pa-studeranden","style":"position:relative;"},"children":[{"type":"text","value":"Namnet på studeranden"},{"type":"element","tagName":"a","properties":{"href":"#namnet-pa-studeranden","ariaLabel":"namnet pa studeranden permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"presterarnas_namn(prestationer: list)"}]},{"type":"text","value":" som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista med namnen på studeranden som genomför kursen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" presterarnas_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python\nPatrik Programmerare\nPeter Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen!"}]},{"type":"element","tagName":"h2","properties":{"id":"kurser","style":"position:relative;"},"children":[{"type":"text","value":"Kurser"},{"type":"element","tagName":"a","properties":{"href":"#kurser","ariaLabel":"kurser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kursnamn(prestationer: list)"}]},{"type":"text","value":" som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista som innehåller namnet på kurserna i den ursprungliga listan i alfabetisk ordning. Varje kursnamn ska förekomma endast en gång på listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kursnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fortsättningskurs i programmering\nIntroduktion till programmering"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen. Du behöver nånting annat också, för att se till att kursnamnen är unika."}]}]},{"type":"element","tagName":"h2","properties":{"id":"filter","style":"position:relative;"},"children":[{"type":"text","value":"filter"},{"type":"element","tagName":"a","properties":{"href":"#filter","ariaLabel":"filter permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den inbyggda Python-funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" liknar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":"-funktionen, men som namnet antyder tar den inte alla föremål från källan. Istället filtrerar den dem med en kriteriefunktion, som skickas som ett argument. Om kriteriefunktionen returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" väljs föremålet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett exempel med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"talen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\njamna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n6\n4\n10\n14"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det kunde göra ovanstående exemplet en aning tydligare ifall vi använde en namngiven funktion istället:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ar_det_jamnt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\ntalen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\njamna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar_det_jamnt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" talen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tal "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa två program är funktionellt helt identiska. Det är mest en fråga om åsikt vilket du anser vara det bättre tillvägagångssättet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ett annat filtreringsexempel. Det här programmet modellerar fiskar och väljer bara ut dem som väger minst 1000 gram:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Fisk"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en fisk av en specifik art och vikt \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" art"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"art "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" art\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vikt\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"art"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" g.)\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n f1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gädda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1870"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Abborre\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"763"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gädda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3410"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Torsk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2449"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mört\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fiskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n over_kilot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fiskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_kilot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gädda (1870 g.)\nGädda (3410 g.)\nTorsk (2449 g.)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kunde lika väl använda oss av en list comprehension för att uppnå samma resultat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"over_kilot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fiskar "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"returvardet-for-filter-ar-en-iterator","style":"position:relative;"},"children":[{"type":"text","value":"Returvärdet för filter är en iterator"},{"type":"element","tagName":"a","properties":{"href":"#returvardet-for-filter-ar-en-iterator","ariaLabel":"returvardet for filter ar en iterator permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" liknar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" även i det avseendet att den returnerar en iterator. Det finns situationer där du bör vara särskilt försiktig med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" eftersom iteratorer bara kan genomlöpas en gång. Så att försöka skriva ut samlingen av stora fiskar två gånger kommer inte att fungera så enkelt som du kanske tror:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"f1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gädda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1870"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Abborre\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"763"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gädda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3410"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Torsk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2449"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mört\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfiskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nover_kilot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fiskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_kilot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"samma igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_kilot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta skulle skriva ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gädda (1870 g.)\nGädda (3410 g.)\nTorsk (2449 g.)\nsamma igen"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du behöver gå igenom innehållet i en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" iterator mer än en gång kan du konvertera resultatet till en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fiskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# konvertera returvärdet från filter-funktionen till en lista"}]},{"type":"text","value":"\nover_kilot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fisk "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fisk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fiskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Begränsade prestationer","tmcname":"osa12-12_begransade_prestationer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna övning fortsätter vi med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Prestation"}]},{"type":"text","value":"-klassen."}]},{"type":"element","tagName":"h2","properties":{"id":"godkanda-forsok","style":"position:relative;"},"children":[{"type":"text","value":"Godkända försök"},{"type":"element","tagName":"a","properties":{"href":"#godkanda-forsok","ariaLabel":"godkanda forsok permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"godkanda(prestationer: list)"}]},{"type":"text","value":", som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista av Prestation-objekt, inkluderandes endast de föremål från den ursprungliga listan vars vitsord är åtminstone 1."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" godkanda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python, vitsord från kursen Introduktion till programmering 3\nPatrik Programmerare vitsord från kursen Introduktion till programmering 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"-funktionen!"}]},{"type":"element","tagName":"h2","properties":{"id":"prestationer-med-vitsord","style":"position:relative;"},"children":[{"type":"text","value":"Prestationer med vitsord"},{"type":"element","tagName":"a","properties":{"href":"#prestationer-med-vitsord","ariaLabel":"prestationer med vitsord permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prestationer_med_vitsord(prestationer: list, vitsord: int)"}]},{"type":"text","value":", som tar en lista av Prestation-objekt och ett heltal som argument. Funktionen ska returnera en ny lista som innehåller endast de Prestation-objekt från den ursprungliga listan vars vitsord matchar det andra argumentet."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till universitetsmatematik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" prestationer_med_vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python, vitsord från kursen Introduktion till programmering 3\nPeter Python, vitsord från kursen Introduktion till datakommunikation 3\nPatrik Programmerare, vitsord från kursen Introduktion till universitetsmatematik 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"-funktionen!"}]},{"type":"element","tagName":"h2","properties":{"id":"studeranden-som-klarat-kursen","style":"position:relative;"},"children":[{"type":"text","value":"Studeranden som klarat kursen"},{"type":"element","tagName":"a","properties":{"href":"#studeranden-som-klarat-kursen","ariaLabel":"studeranden som klarat kursen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kursens_deltagare(prestationer: list, kurs: str)"}]},{"type":"text","value":", som tar en lista av Prestation-objekt och ett kursnamn som argument. Funktionen ska returnera en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"alfabetiskt sorterad"}]},{"type":"text","value":" lista av namn på de studeranden som klarat av kursen, d.v.s. deras vitsord för den givna kursen är högre än 0."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Programmerare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Niko Nörd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kursens_deltagare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till datakommunikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Niko Nörd\nPatrik Programmerare"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"- och map-funktionerna!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"reduce","style":"position:relative;"},"children":[{"type":"text","value":"reduce"},{"type":"element","tagName":"a","properties":{"href":"#reduce","ariaLabel":"reduce permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En tredje hörnstensfunktion i denna introduktion till funktionella programmeringsprinciper är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":", från modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"functools"}]},{"type":"text","value":". Som namnet antyder är dess syfte att reducera objekten i en serie till ett enda värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen börjar med en operation och ett startvärde. Den utför den givna operationen på varje objekt i serien i tur och ordning, så att värdet ändras i varje steg. När alla objekt har bearbetats returneras det resulterande värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har gjort summering av listor med heltal på olika sätt tidigare, men här har vi ett exempel med hjälp av funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":". Notera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen; i Python version 3 och senare är den nödvändig för att komma åt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen. I äldre Python-versioner behövdes inte "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen, så du kan stöta på exempel utan den på nätet."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntalens_summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en närmare titt på vad som händer här. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen tar emot tre argument: en funktion, en serie av föremål och ett startvärde. I det här fallet är serien en lista med heltal, och eftersom vi beräknar en summa är ett lämpligt startvärde noll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det första argumentet är en funktion, som representerar den operation vi vill utföra på varje objekt. Här är funktionen en anonym lambda-funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" foremal"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna funktion tar två argument: det aktuella reducerade värdet och det föremål vars tur det är att bearbetas. Dessa används för att beräkna ett nytt värde för det reducerade värdet. I detta fall är det nya värdet summan av det gamla värdet och det aktuella objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det kan vara lättare att förstå vad funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" faktiskt gör om vi använder en vanlig namngiven funktion i stället för en lambda-funktion. På så sätt kan vi också inkludera användbara utskrifter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# en hjälparfunktion för reduce, som lägger till ett värde till den för tillfället reducerade summan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"summa nu "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", nästa föremål "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# den nya summan är gamla summan + nästa föremål"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" foremal\n\ntalens_summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta program skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"summa nu 0, nästa föremål 2\nsumma nu 2, nästa föremål 3\nsumma nu 5, nästa föremål 1\nsumma nu 6, nästa föremål 5\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först tar funktionen hand om objektet med värdet 2. Till att börja med är den reducerade summan 0, vilket är det ursprungliga värdet som skickas till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen. Funktionen beräknar och returnerar summan av dessa två: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0 + 2 = 2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta är det värde som lagras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reducerad_summa"}]},{"type":"text","value":" när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen bearbetar nästa objekt i listan, med värdet 3. Funktionen beräknar och returnerar summan av dessa två: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 3 = 5"}]},{"type":"text","value":". Detta resultat används sedan när nästa objekt bearbetas, och så vidare, och så vidare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu är det enkelt att summera, eftersom det till och med finns en inbyggd "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":"-funktion för detta ändamål. Men hur är det med multiplikation? Det krävs bara små förändringar för att skapa en reducerad produkt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nprodukt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" produkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"480"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom vi har att göra med multiplikation är startvärdet inte noll. Istället använder vi 1. Vad skulle hända om vi använde 0 som startvärde?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovan har vi till stor del behandlat heltal, men "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" kan alla hantera en samling objekt av alla typer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss som ett exempel generera en totalsumma av saldona för alla konton i en bank med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo\n\nk1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nk2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pank\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nk3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"223344\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja Miljonär\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkonton "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"k1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" k2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" k3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tot_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" tot_saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsaldon_totalt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldo_summa_hjalpare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bankens totala saldo:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldon_totalt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta program skulle skriva ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bankens totala saldo:\n1005001"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"text","value":" tar saldot på varje bankkonto, med den metod som är avsedd för ändamålet i klassdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tot_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" tot_saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Alkuarvoton reduce"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du måste inte alltid ange ett tredje argument till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen. Till exempel skulle summering fungera lika väl "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"utan"}]},{"type":"text","value":" ursprungsvärdet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ntalens_summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"talens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall ett ursprungligt värde lämnas bort tar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" det första föremålet i listan som sitt ursprungsvärde och börjar reducera från det andra föremålet framåt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Om föremålen i serien är av en annan typ än det avsedda reducerade resultatet, är det tredje argumentet obligatoriskt. Exemplet med bankkontona skulle inte fungera utan det ursprungliga värdet. Det vill säga att prova detta"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"saldon_totalt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldo_summa_hjalpare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" konton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skulle producera ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"TypeError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" unsupported operand "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Bankkonto'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'int'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I ovanstående fall, när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" försöker utföra funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"text","value":" för första gången, är de argument som används de två första föremålen i listan, som båda är av typen Bankkonto. Specifikt är det värde som tilldelats parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo_summa"}]},{"type":"text","value":" det första föremålet i listan. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo_summa_hjalpare"}]},{"type":"text","value":" försöker lägga till ett heltalsvärde till den, men att lägga till ett heltal direkt till ett Bankkonto-objekt är inte en åtgärd som stöds."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Studiepoäng","tmcname":"osa12-13_studiepoang"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna övning jobbar vi med en aning modifierad version av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Prestation"}]},{"type":"text","value":"-klassen. Namnet på studeranden är utelämnat, men antalet studiepoäng är inkluderat. Klassen fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"prestation "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Datastrukturer och algoritmer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datastrukturer och algoritmer (10 sp) vitsord 3\nDatastrukturer och algoritmer\n10\n3"}]}]},{"type":"element","tagName":"h2","properties":{"id":"totala-antalet-studiepoang","style":"position:relative;"},"children":[{"type":"text","value":"Totala antalet studiepoäng"},{"type":"element","tagName":"a","properties":{"href":"#totala-antalet-studiepoang","ariaLabel":"totala antalet studiepoang permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studiepoangens_summa"}]},{"type":"text","value":", som tar en lista av prestationer som argument. Funktionen adderar ihop det totala antalet studiepoäng som fås av kurserna. Den ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Datastrukturer och algoritmer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoangens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"-funktionen!"}]},{"type":"element","tagName":"h2","properties":{"id":"summan-av-avklarade-studiepoang","style":"position:relative;"},"children":[{"type":"text","value":"Summan av avklarade studiepoäng"},{"type":"element","tagName":"a","properties":{"href":"#summan-av-avklarade-studiepoang","ariaLabel":"summan av avklarade studiepoang permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"godkanda_studiepoangens_summa"}]},{"type":"text","value":", som tar en lista av prestationer som argument. Funktionen adderar ihop studiepoängen för prestationerna med vitsordet 1 eller högre. Den ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Datastrukturer och algoritmer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" godkanda_studiepoangens_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"15"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"-funktionerna!"}]},{"type":"element","tagName":"h2","properties":{"id":"avklarade-kursers-medelvitsord","style":"position:relative;"},"children":[{"type":"text","value":"Avklarade kursers medelvitsord"},{"type":"element","tagName":"a","properties":{"href":"#avklarade-kursers-medelvitsord","ariaLabel":"avklarade kursers medelvitsord permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"medeltal"}]},{"type":"text","value":", som tar en lista av suorits som argument. Funktionen räknar ut medelvitsordet för prestationerna med vitsord 1 eller högre. Den ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Datastrukturer och algoritmer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera funktionen användandes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":"-funktionerna!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du jobbar på den här övningen kan det vara värt att komma ihåg att returvärdet av filter är en iterator."}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad funktionell programmering innebär
  • \n
  • Kommer du att kunna använda funktionerna map, reduce och filter i dina egna program
  • \n

Funktionell programmering avser ett programmeringsparadigm som undviker förändringar i programtillståndet så mycket som möjligt. Variabler undviks i allmänhet. Istället är det kedjor av funktionsanrop som utgör ryggraden i programmet.

Lambda-uttryck och olika typer av förståelser är vanliga tekniker i den funktionella programmeringsstilen, eftersom de låter dig bearbeta data utan att lagra dem i variabler, så att programmets tillstånd inte ändras. Ett lambdauttryck är till exempel i alla avseenden en funktion, men vi behöver inte lagra en namngiven referens till den någonstans.

Som nämnts ovan är funktionell programmering ett programmeringsparadigm, eller en programmeringsstil. Det finns många olika programmeringsparadigm, och vi har redan stött på några av dem:

    \n
  • imperativ programmering, där programmet består av en sekvens av instruktioner som utförs i tur och ordning
  • \n
  • procedurprogrammering, där programmet är uppdelat i procedurer eller underprogram
  • \n
  • objektorienterad programmering, där programmet och dess tillstånd lagras i objekt som definieras i klasser.
  • \n

Det finns olika uppfattningar om gränsdragningen mellan de olika paradigmen, t.ex. hävdar vissa att imperativ och procedurell programmering betyder samma sak, medan andra placerar imperativ programmering som ett paraplybegrepp som täcker både procedurell och objektorienterad programmering. Terminologin och uppdelningen är inte så viktig, och det är inte heller viktigt att strikt hålla sig till det ena eller andra paradigmet, men det är viktigt att förstå att det finns sådana olika synsätt eftersom de påverkar de val som programmerare gör.

Många programmeringsspråk är utformade med det ena eller det andra programmeringsparadigmet i åtanke, men Python är ett ganska mångsidigt programmeringsspråk och gör det möjligt att följa flera olika programmeringsparadigm, även inom ett enda program. Detta gör att vi kan välja den mest effektiva och tydliga metoden för att lösa varje problem.

Låt oss ta en titt på några funktionella programmeringsverktyg som tillhandahålls av Python.

map

Funktionen map utför någon operation på varje objekt i en iterabel serie. Det här låter ungefär som den effekt en comprehension har, men syntaxen är annorlunda.

Låt oss anta att vi har en lista med strängar som vi vill konvertera till en lista med heltal:

stranglista = [\"123\",\"-10\", \"23\", \"98\", \"0\", \"-110\"]\n\ntalen = map(lambda x : int(x), stranglista)\n\nprint(talen)\n\nfor tal in talen:\n    print(tal)

<map object at 0x0000021A4BFA9A90>\n123\n-10\n23\n98\n0\n-110

Den allmänna syntaxen för map-funktionen är

map(<funktion>, <serie>)

där funktion är den operation vi vill utföra på varje föremål i serien.

map-funktionen returnerar ett objekt av typen map, som är itererbart och kan konverteras till en lista:

def versalisera(strang: str):\n    borjan = strang[0]\n    borjan = borjan.upper()\n    return borjan + strang[1:]\n\ntestlista = [\"första\", \"andra\", \"tredje\", \"fjärde\"]\n\nfardiga = map(versalisera, testlista)\n\nfardiga_lista = list(fardiga)\nprint(fardiga_lista)

['Första', 'Andra', 'Tredje', 'Fjärde']

Som du kan se i exemplen ovan accepterar map-funktionen både en anonym lambda-funktion och en namngiven funktion som definieras med nyckelordet def.

Vi skulle kunna uppnå samma resultat med en list comprehension:

def versalisera(strang: str):\n    borjan = strang[0]\n    borjan = borjan.upper()\n    return borjan + strang[1:]\n\ntestlista = [\"första\", \"andra\", \"tredje\", \"fjärde\"]\n\n\nfardiga_lista = [versalisera(foremal) for foremal in testlista]\nprint(fardiga_lista)

...eller så kan vi gå igenom den ursprungliga listan med en for-loop och spara de bearbetade objekten i en ny lista med append-metoden. I programmering finns det vanligtvis många olika lösningar på varje problem. Det finns sällan några absolut rätta eller felaktiga svar. Att känna till många olika tillvägagångssätt hjälper dig att välja den mest lämpliga för varje situation, eller den som bäst passar din egen smak.

Det är värt att påpeka att map-funktionen inte returnerar en lista, utan ett iteratorobjekt av typen map. En iterator beter sig på många sätt som en lista, men det finns undantag, vilket kan ses i följande exempel:

def versalisera(strang: str):\n    borjan = strang[0]\n    borjan = borjan.upper()\n    return borjan + strang[1:]\n\ntestlista = [\"första\", \"andra\", \"tredje\", \"fjärde\"]\n\n# lagrar map-funktionens returvärde\nfardiga = map(versalisera, testlista)\n\nfor ord in fardiga:\n  print(ord)\n\nprint(\"samma igen:\")\nfor ord in fardiga:\n  print(ord)

Detta skulle skriva ut följande:

Första\nAndra\nTredje\nFjärde\nsamma igen:

Ovan försökte vi skriva ut innehållet i map-iteratorn två gånger, men det andra försöket gav ingen utskrift. Anledningen är att map är en iterator; när man går igenom den med en for-loop \"töms\" den, ungefär som en generator töms när dess maximala värde har uppnåtts. När objekten i iteratorn har genomgåtts med en for-loop finns det inget kvar att gå igenom.

Om du behöver gå igenom innehållet i en map-iterator mer än en gång kan du t.ex. konvertera map till en lista:

testlista = [\"första\", \"andra\", \"tredje\", \"fjärde\"]\n\n# konvertera returvärdet från map-funktionen till en lista\nfardiga = list(map(versalisera, testlista))\n\nfor ord in fardiga:\n  print(ord)\n\nprint(\"samma igen:\")\nfor ord in fardiga:\n  print(ord)

Första\nAndra\nTredje\nFjärde\nsamma igen:\nFörsta\nAndra\nTredje\nFjärde

Map-funktionen och dina egna klasser

Du kan naturligtvis också bearbeta instanser av dina egna klasser med map-funktionen. Det krävs inga speciella knep, som du kan se i exemplet nedan:

class Bankkonto:\n    def __init__(self, nummer: str, namn: str, saldo: float):\n        self.__nummer = nummer\n        self.namn = namn\n        self.__saldo = saldo\n\n    def tillsatt_pengar(self, mangd: float):\n        if mangd > 0:\n            self.__saldo += mangd\n\n    def hamta_saldo(self):\n        return self.__saldo\n\nk1 = Bankkonto(\"123456\", \"Robert Rik\", 5000)\nk2 = Bankkonto(\"12321\", \"Peter Pank\", 1)\nk3 = Bankkonto(\"223344\", \"Maja Miljonär \", 1000000)\n\nkonton = [k1, k2, k3]\n\nkunder = map(lambda t: t.namn, konton)\nfor namn in kunder:\n  print(namn)\n\nsaldon = map(lambda t: t.hamta_saldo(), konton)\nfor saldo in saldon:\n  print(saldo)

Robert Rik\nPeter Pank\nMaja Miljonär\n5000\n1\n1000000

Här samlar vi först in namnen på kontoinnehavarna med map-funktionen. En anonym lambda-funktion används för att hämta värdet på namn-attributet från varje Bankkonto-objekt:

kunder = map(lambda t: t.namn, konton)

På samma sätt samlas saldot för varje Bankkonto in. Lambda-funktionen ser lite annorlunda ut, eftersom saldot hämtas med ett metodanrop, inte direkt från attributet:

saldon = map(lambda t: t.hamta_saldo(), konton)

I uppgiftsbotten finns klassdefinitionen för Prestation, som fungerar enligt följande:

prestation = Prestation(\"Peter Python\", \"Introduktion till programmering\", 5)\nprint(prestation.studerandens_namn)\nprint(prestation.kurs)\nprint(prestation.vitsord)\nprint(prestation)

Peter Python\nIntroduktion till programmering\n5\nPeter Python, vitsord från kursen Introduktion till programmering 5

Namnet på studeranden

Skapa funktionen presterarnas_namn(prestationer: list) som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista med namnen på studeranden som genomför kursen.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till programmering\", 5)\np3 = Prestation(\"Peter Python\", \"Fortsättningskurs i programmering\", 2)\n\nfor namn in presterarnas_namn([p1, p2, p3]):\n    print(namn)

Peter Python\nPatrik Programmerare\nPeter Python

Implementera funktionen användandes map-funktionen!

Kurser

Skapa funktionen kursnamn(prestationer: list) som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista som innehåller namnet på kurserna i den ursprungliga listan i alfabetisk ordning. Varje kursnamn ska förekomma endast en gång på listan.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till programmering\", 5)\np3 = Prestation(\"Peter Python\", \"Fortsättningskurs i programmering\", 2)\n\nfor namn in kursnamn([p1, p2, p3]):\n    print(namn)

Fortsättningskurs i programmering\nIntroduktion till programmering

Implementera funktionen användandes map-funktionen. Du behöver nånting annat också, för att se till att kursnamnen är unika.

filter

Den inbyggda Python-funktionen filter liknar map-funktionen, men som namnet antyder tar den inte alla föremål från källan. Istället filtrerar den dem med en kriteriefunktion, som skickas som ett argument. Om kriteriefunktionen returnerar True väljs föremålet.

Låt oss titta på ett exempel med filter:

talen = [1, 2, 3, 5, 6, 4, 9, 10, 14, 15]\n\njamna = filter(lambda tal: tal % 2 == 0, talen)\n\nfor tal in jamna:\n    print(tal)

2\n6\n4\n10\n14

Det kunde göra ovanstående exemplet en aning tydligare ifall vi använde en namngiven funktion istället:

def ar_det_jamnt(tal: int):\n    if tal % 2 == 0:\n        return True\n    return False\n\ntalen = [1, 2, 3, 5, 6, 4, 9, 10, 14, 15]\n\njamna = filter(ar_det_jamnt, talen)\n\nfor tal in jamna:\n    print(tal)

Dessa två program är funktionellt helt identiska. Det är mest en fråga om åsikt vilket du anser vara det bättre tillvägagångssättet.

Låt oss ta en titt på ett annat filtreringsexempel. Det här programmet modellerar fiskar och väljer bara ut dem som väger minst 1000 gram:

class Fisk:\n    \"\"\" Klassen modellerar en fisk av en specifik art och vikt \"\"\"\n    def __init__(self, art: str, vikt: int):\n        self.art = art\n        self.vikt = vikt\n\n    def __repr__(self):\n        return f\"{self.art} ({self.vikt} g.)\"\n\nif __name__ == \"__main__\":\n    f1 = Fisk(\"Gädda\", 1870)\n    f2 = Fisk(\"Abborre\", 763)\n    f3 = Fisk(\"Gädda\", 3410)\n    f4 = Fisk(\"Torsk\", 2449)\n    f5 = Fisk(\"Mört\", 210)\n\n    fiskar = [f1, f2, f3, f4, f5]\n\n    over_kilot = filter(lambda fisk : fisk.vikt >= 1000, fiskar)\n\n    for fisk in over_kilot:\n        print(fisk)

Gädda (1870 g.)\nGädda (3410 g.)\nTorsk (2449 g.)

Vi kunde lika väl använda oss av en list comprehension för att uppnå samma resultat:

over_kilot = [fisk for fisk in fiskar if fisk.vikt >= 1000]

Returvärdet för filter är en iterator

Funktionen filter liknar funktionen map även i det avseendet att den returnerar en iterator. Det finns situationer där du bör vara särskilt försiktig med filter eftersom iteratorer bara kan genomlöpas en gång. Så att försöka skriva ut samlingen av stora fiskar två gånger kommer inte att fungera så enkelt som du kanske tror:

f1 = Fisk(\"Gädda\", 1870)\nf2 = Fisk(\"Abborre\", 763)\nf3 = Fisk(\"Gädda\", 3410)\nf4 = Fisk(\"Torsk\", 2449)\nf5 = Fisk(\"Mört\", 210)\n\nfiskar = [f1, f2, f3, f4, f5]\n\nover_kilot = filter(lambda fisk : fisk.vikt >= 1000, fiskar)\n\nfor fisk in over_kilot:\n    print(fisk)\n\nprint(\"samma igen\")\n\nfor fisk in over_kilot:\n    print(fisk)

Detta skulle skriva ut följande:

Gädda (1870 g.)\nGädda (3410 g.)\nTorsk (2449 g.)\nsamma igen

Om du behöver gå igenom innehållet i en filter iterator mer än en gång kan du konvertera resultatet till en lista:

fiskar = [f1, f2, f3, f4, f5]\n\n# konvertera returvärdet från filter-funktionen till en lista\nover_kilot = list(filter(lambda fisk : fisk.vikt >= 1000, fiskar))

I denna övning fortsätter vi med Prestation-klassen.

Godkända försök

Skapa funktionen godkanda(prestationer: list), som tar en lista av Prestation-objekt som argument. Funktionen ska returnera en ny lista av Prestation-objekt, inkluderandes endast de föremål från den ursprungliga listan vars vitsord är åtminstone 1.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till programmering\", 5)\np3 = Prestation(\"Peter Python\", \"Fortsättningskurs i programmering\", 0)\n\nfor prestation in godkanda([p1, p2, p3]):\n    print(prestation)

Peter Python, vitsord från kursen Introduktion till programmering 3\nPatrik Programmerare vitsord från kursen Introduktion till programmering 5

Implementera funktionen användandes filter-funktionen!

Prestationer med vitsord

Skapa funktionen prestationer_med_vitsord(prestationer: list, vitsord: int), som tar en lista av Prestation-objekt och ett heltal som argument. Funktionen ska returnera en ny lista som innehåller endast de Prestation-objekt från den ursprungliga listan vars vitsord matchar det andra argumentet.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till programmering\", 5)\np3 = Prestation(\"Peter Python\", \"Introduktion till datakommunikation\", 3)\np4 = Prestation(\"Patrik Programmerare\", \"Introduktion till universitetsmatematik\", 3)\n\nfor prestation in prestationer_med_vitsord([p1, p2, p3, p4], 3):\n    print(prestation)

Peter Python, vitsord från kursen Introduktion till programmering 3\nPeter Python, vitsord från kursen Introduktion till datakommunikation 3\nPatrik Programmerare, vitsord från kursen Introduktion till universitetsmatematik 3

Implementera funktionen användandes filter-funktionen!

Studeranden som klarat kursen

Skapa funktionen kursens_deltagare(prestationer: list, kurs: str), som tar en lista av Prestation-objekt och ett kursnamn som argument. Funktionen ska returnera en alfabetiskt sorterad lista av namn på de studeranden som klarat av kursen, d.v.s. deras vitsord för den givna kursen är högre än 0.

p1 = Prestation(\"Peter Python\", \"Introduktion till programmering\", 3)\np2 = Prestation(\"Patrik Programmerare\", \"Introduktion till datakommunikation\", 5)\np3 = Prestation(\"Peter Python\", \"Introduktion till datakommunikation\", 0)\np4 = Prestation(\"Niko Nörd\", \"Introduktion till datakommunikation\", 3)\n\nfor prestation in kursens_deltagare([p1, p2, p3, p4], \"Introduktion till datakommunikation\"):\n    print(prestation)

Niko Nörd\nPatrik Programmerare

Implementera funktionen användandes filter- och map-funktionerna!

reduce

En tredje hörnstensfunktion i denna introduktion till funktionella programmeringsprinciper är reduce, från modulen functools. Som namnet antyder är dess syfte att reducera objekten i en serie till ett enda värde.

reduce-funktionen börjar med en operation och ett startvärde. Den utför den givna operationen på varje objekt i serien i tur och ordning, så att värdet ändras i varje steg. När alla objekt har bearbetats returneras det resulterande värdet.

Vi har gjort summering av listor med heltal på olika sätt tidigare, men här har vi ett exempel med hjälp av funktionen reduce. Notera import-satsen; i Python version 3 och senare är den nödvändig för att komma åt reduce-funktionen. I äldre Python-versioner behövdes inte import-satsen, så du kan stöta på exempel utan den på nätet.

from functools import reduce\n\nlista = [2, 3, 1, 5]\n\ntalens_summa = reduce(lambda summa, foremal: summa + foremal, lista, 0)\n\nprint(talens_summa)

11

Låt oss ta en närmare titt på vad som händer här. reduce-funktionen tar emot tre argument: en funktion, en serie av föremål och ett startvärde. I det här fallet är serien en lista med heltal, och eftersom vi beräknar en summa är ett lämpligt startvärde noll.

Det första argumentet är en funktion, som representerar den operation vi vill utföra på varje objekt. Här är funktionen en anonym lambda-funktion:

lambda summa, foremal: summa + foremal

Denna funktion tar två argument: det aktuella reducerade värdet och det föremål vars tur det är att bearbetas. Dessa används för att beräkna ett nytt värde för det reducerade värdet. I detta fall är det nya värdet summan av det gamla värdet och det aktuella objektet.

Det kan vara lättare att förstå vad funktionen reduce faktiskt gör om vi använder en vanlig namngiven funktion i stället för en lambda-funktion. På så sätt kan vi också inkludera användbara utskrifter:

from functools import reduce\n\nlista = [2, 3, 1, 5]\n\n# en hjälparfunktion för reduce, som lägger till ett värde till den för tillfället reducerade summan\ndef summare(summa, foremal):\n  print(f\"summa nu {summa}, nästa föremål {foremal}\")\n  # den nya summan är gamla summan + nästa föremål\n  return summa + foremal\n\ntalens_summa = reduce(summare, lista, 0)\n\nprint(talens_summa)

Detta program skriver ut:

summa nu 0, nästa föremål 2\nsumma nu 2, nästa föremål 3\nsumma nu 5, nästa föremål 1\nsumma nu 6, nästa föremål 5\n11

Först tar funktionen hand om objektet med värdet 2. Till att börja med är den reducerade summan 0, vilket är det ursprungliga värdet som skickas till reduce-funktionen. Funktionen beräknar och returnerar summan av dessa två: 0 + 2 = 2.

Detta är det värde som lagras i reducerad_summa när reduce-funktionen bearbetar nästa objekt i listan, med värdet 3. Funktionen beräknar och returnerar summan av dessa två: 2 + 3 = 5. Detta resultat används sedan när nästa objekt bearbetas, och så vidare, och så vidare.

Nu är det enkelt att summera, eftersom det till och med finns en inbyggd sum-funktion för detta ändamål. Men hur är det med multiplikation? Det krävs bara små förändringar för att skapa en reducerad produkt:

from functools import reduce\n\nlista = [2, 2, 4, 3, 5, 2]\n\nprodukt = reduce(lambda produkt, foremal: produkt * foremal, lista, 1)\n\nprint(produkt)

480

Eftersom vi har att göra med multiplikation är startvärdet inte noll. Istället använder vi 1. Vad skulle hända om vi använde 0 som startvärde?

Ovan har vi till stor del behandlat heltal, men map, filter och reduce kan alla hantera en samling objekt av alla typer.

Låt oss som ett exempel generera en totalsumma av saldona för alla konton i en bank med hjälp av reduce:

class Bankkonto:\n    def __init__(self, nummer: str, namn: str, saldo: float):\n        self.__nummer = nummer\n        self.namn = namn\n        self.__saldo = saldo\n\n    def tillsatt_pengar(self, mangd: float):\n        if mangd > 0:\n            self.__saldo += mangd\n\n    def hamta_saldo(self):\n        return self.__saldo\n\nk1 = Bankkonto(\"123456\", \"Robert Rik\", 5000)\nk2 = Bankkonto(\"12321\", \"Peter Pank\", 1)\nk3 = Bankkonto(\"223344\", \"Maja Miljonär\", 1000000)\n\nkonton = [k1, k2, k3]\n\nfrom functools import reduce\n\ndef saldo_summa_hjalpare(tot_saldo, konto):\n  return tot_saldo + konto.hamta_saldo()\n\nsaldon_totalt = reduce(saldo_summa_hjalpare, konton, 0)\n\nprint(\"Bankens totala saldo:\")\nprint(saldon_totalt)

Detta program skulle skriva ut:

Bankens totala saldo:\n1005001

Funktionen saldo_summa_hjalpare tar saldot på varje bankkonto, med den metod som är avsedd för ändamålet i klassdefinitionen Bankkonto:

def saldo_summa_hjalpare(tot_saldo, konto):\n  return tot_saldo + konto.hamta_saldo()

Du måste inte alltid ange ett tredje argument till reduce-funktionen. Till exempel skulle summering fungera lika väl utan ursprungsvärdet:

lista = [2, 3, 1, 5]\n\ntalens_summa = reduce(lambda summa, foremal: summa + foremal, lista)\n\nprint(talens_summa)

Ifall ett ursprungligt värde lämnas bort tar reduce det första föremålet i listan som sitt ursprungsvärde och börjar reducera från det andra föremålet framåt.

OBS: Om föremålen i serien är av en annan typ än det avsedda reducerade resultatet, är det tredje argumentet obligatoriskt. Exemplet med bankkontona skulle inte fungera utan det ursprungliga värdet. Det vill säga att prova detta

saldon_totalt = reduce(saldo_summa_hjalpare, konton)

Skulle producera ett fel:

TypeError: unsupported operand type(s) for +: 'Bankkonto' and 'int'

I ovanstående fall, när reduce försöker utföra funktionen saldo_summa_hjalpare för första gången, är de argument som används de två första föremålen i listan, som båda är av typen Bankkonto. Specifikt är det värde som tilldelats parametern saldo_summa det första föremålet i listan. Funktionen saldo_summa_hjalpare försöker lägga till ett heltalsvärde till den, men att lägga till ett heltal direkt till ett Bankkonto-objekt är inte en åtgärd som stöds.

I denna övning jobbar vi med en aning modifierad version av Prestation-klassen. Namnet på studeranden är utelämnat, men antalet studiepoäng är inkluderat. Klassen fungerar enligt följande:

prestation = Prestation(\"Datastrukturer och algoritmer\", 3, 10)\nprint(prestation)\nprint(prestation.kurs)\nprint(prestation.studiepoang)\nprint(prestation.vitsord)

Datastrukturer och algoritmer (10 sp) vitsord 3\nDatastrukturer och algoritmer\n10\n3

Totala antalet studiepoäng

Skapa funktionen studiepoangens_summa, som tar en lista av prestationer som argument. Funktionen adderar ihop det totala antalet studiepoäng som fås av kurserna. Den ska fungera enligt följande:

p1 = Prestation(\"Introduktion till programmering\", 5, 5)\np2 = Prestation(\"Fortsättningskurs i programmering\", 4, 5)\np3 = Prestation(\"Datastrukturer och algoritmer\", 3, 10)\nsumma = studiepoangens_summa([p1, p2, p3])\nprint(summa)

20

Implementera funktionen användandes reduce-funktionen!

Summan av avklarade studiepoäng

Skapa funktionen godkanda_studiepoangens_summa, som tar en lista av prestationer som argument. Funktionen adderar ihop studiepoängen för prestationerna med vitsordet 1 eller högre. Den ska fungera enligt följande:

p1 = Prestation(\"Introduktion till programmering\", 5, 5)\np2 = Prestation(\"Fortsättningskurs i programmering\", 0, 4)\np3 = Prestation(\"Datastrukturer och algoritmer\", 3, 10)\nsumma = godkanda_studiepoangens_summa([p1, p2, p3])\nprint(summa)

15

Implementera funktionen användandes reduce- och filter-funktionerna!

Avklarade kursers medelvitsord

Skapa funktionen medeltal, som tar en lista av suorits som argument. Funktionen räknar ut medelvitsordet för prestationerna med vitsord 1 eller högre. Den ska fungera enligt följande:

p1 = Prestation(\"Introduktion till programmering\", 5, 5)\np2 = Prestation(\"Fortsättningskurs i programmering\", 0, 4)\np3 = Prestation(\"Datastrukturer och algoritmer\", 3, 10)\nsumma = medeltal([p1, p2, p3])\nprint(summa)

4.0

Implementera funktionen användandes reduce- och filter-funktionerna!

När du jobbar på den här övningen kan det vara värt att komma ihåg att returvärdet av filter är en iterator.

","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/3-funktionell-programmering.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-12/4-reguljara-uttryck/page-data.json b/page-data/osa-12/4-reguljara-uttryck/page-data.json index 96b65cedc..c9a508740 100644 --- a/page-data/osa-12/4-reguljara-uttryck/page-data.json +++ b/page-data/osa-12/4-reguljara-uttryck/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-12/4-reguljara-uttryck", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad reguljära uttryck är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda reguljära uttryck i dina egna program"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan konstaterat att Python är en utmärkt miljö för att bearbeta text. Ett ytterligare kraftfullt verktyg för textbehandling är reguljära uttryck (eng. regular expressions), ofta förkortat som regex eller regexp. De är ett sätt att välja ut och söka efter strängar som följer ett visst mönster. I det här avsnittet får du en introduktion till grunderna i reguljära uttryck, men du hittar mycket mer information på nätet, bland annat i Pythons egna "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/howto/regex.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"handledning"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"vad-ar-reguljara-uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Vad är reguljära uttryck?"},{"type":"element","tagName":"a","properties":{"href":"#vad-ar-reguljara-uttryck","ariaLabel":"vad ar reguljara uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Reguljära uttryck är inte bara en Python-funktion. De representerar på sätt och vis ett programmeringsspråk inom ett programmeringsspråk. De är i viss utsträckning kompatibla med många olika programmeringsspråk. Reguljära uttryck har sin egen specifika syntax. Tanken är att definiera en samling strängar som följer vissa regler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja med ett enkelt exempel innan vi dyker djupare in i syntaxen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" re\n\norden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Population\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pantomi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Perfekt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Prestation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall strängen börjar med \"P\" och slutar med \"on\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"^P.*on$\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittad!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python hittad!\nPopulation hittad!\nPrestation hittad!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi behöver importera modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"re"}]},{"type":"text","value":" för att kunna använda reguljära uttryck i Python. Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"re"}]},{"type":"text","value":" innehåller många funktioner för att arbeta med reguljära uttryck. I exemplet ovan tar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search"}]},{"type":"text","value":"-funktionen två strängargument: mönstersträngen och den målsträng där mönstret ska sökas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här andra exemplet letar man efter alla siffror i en sträng. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"findall"}]},{"type":"text","value":" returnerar en lista över alla instanser som matchar mönstret:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" re\n\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Först, 2 !#tredje 44 fem 678xyz962\""}]},{"type":"text","value":"\n\nnumren "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"findall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\d+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n44\n678\n962"}]}]},{"type":"element","tagName":"h2","properties":{"id":"syntaxen-for-reguljara-uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Syntaxen för reguljära uttryck"},{"type":"element","tagName":"a","properties":{"href":"#syntaxen-for-reguljara-uttryck","ariaLabel":"syntaxen for reguljara uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss bekanta oss med den grundläggande syntaxen för reguljära uttryck. De flesta av följande exempel använder sig av detta testprogram:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" re\n\nuttryck "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange uttryck: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"uttryck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittad!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittades inte.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"alternativa-delstrangar","style":"position:relative;"},"children":[{"type":"text","value":"Alternativa delsträngar"},{"type":"element","tagName":"a","properties":{"href":"#alternativa-delstrangar","ariaLabel":"alternativa delstrangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lodstrecket (eng. vertical bar) "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"|"}]},{"type":"text","value":", gör att du kan matcha alternativa delsträngar. Dess betydelse är alltså eller. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"911|112"}]},{"type":"text","value":" matchar t.ex. strängar som innehåller antingen delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"911"}]},{"type":"text","value":" eller delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"112"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel med testprogrammet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aa|ee|ii"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kassaaparat"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"beteende"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"friidrott"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kooperation"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bastuugn"}]},{"type":"text","value":"\nHittades inte."}]}]},{"type":"element","tagName":"h3","properties":{"id":"grupper-av-tecken","style":"position:relative;"},"children":[{"type":"text","value":"Grupper av tecken"},{"type":"element","tagName":"a","properties":{"href":"#grupper-av-tecken","ariaLabel":"grupper av tecken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hakparenteser används för att beteckna grupper av accepterade tecken. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[aeio]"}]},{"type":"text","value":" skulle t.ex. matcha alla strängar som innehåller något av tecknen a, e, i eller o."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett bindestreck är också tillåtet för att matcha intervall av tecken. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[0-68a-d]"}]},{"type":"text","value":" skulle till exempel matcha alla strängar som innehåller en siffra mellan 0 och 6, eller en åtta, eller ett tecken mellan a och d. I den här notationen är alla intervall inkluderande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du kombinerar två uppsättningar parenteser kan du matcha två tecken i följd. Till exempel skulle uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1-3][0-9]"}]},{"type":"text","value":" matcha alla tvåsiffriga tal mellan 10 och 39, inklusive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel med testprogrammet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"[C-FRSÖ]"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"C"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"E"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"G"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"R"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ö"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"T"}]},{"type":"text","value":"\nHittades inte."}]}]},{"type":"element","tagName":"h3","properties":{"id":"upprepade-matchningar","style":"position:relative;"},"children":[{"type":"text","value":"Upprepade matchningar"},{"type":"element","tagName":"a","properties":{"href":"#upprepade-matchningar","ariaLabel":"upprepade matchningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje del av ett uttryck kan upprepas med följande operatorer:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" upprepas hur många gånger som helst, inklusive noll"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" upprepas hur många gånger som helst, men minst en gång"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{m}"}]},{"type":"text","value":" upprepas exakt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"m"}]},{"type":"text","value":" gånger"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa operatorer fungerar på den del av uttrycket som kommer omedelbart före operatorn. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ba+b"}]},{"type":"text","value":" skulle t.ex. matcha delsträngarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bab"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"baab"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"baaaaaaaaaaab"}]},{"type":"text","value":", bland andra. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A[BCDE]*Z"}]},{"type":"text","value":" skulle matcha delsträngarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"AZ"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADZ"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ABCDEBCDEBCDEZ"}]},{"type":"text","value":", bland andra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel med testprogrammet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1[234]*5"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"125"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"145"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12342345"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"126"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"165"}]},{"type":"text","value":"\nHittades inte."}]}]},{"type":"element","tagName":"h3","properties":{"id":"andra-specialtecken","style":"position:relative;"},"children":[{"type":"text","value":"Andra specialtecken"},{"type":"element","tagName":"a","properties":{"href":"#andra-specialtecken","ariaLabel":"andra specialtecken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En punkt är ett jokertecken som kan matcha vilket enskilt tecken som helst. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c...o"}]},{"type":"text","value":" skulle till exempel matcha alla delsträngar med fem tecken som börjar med ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":" och slutar med ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"o"}]},{"type":"text","value":", till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c-3po"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cello"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"^"}]},{"type":"text","value":" anger att matchningen måste ske i början av strängen och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"$"}]},{"type":"text","value":" anger att matchningen måste ske i slutet av strängen. Dessa tecken kan också användas för att utesluta andra tecken än de angivna från matchningen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^[123]*$"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1221"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"333333333"}]},{"type":"text","value":"\nHittad!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland behöver du matcha för specialtecken som är reserverade för syntaxen för reguljära uttryck. Omvänt snedstreck (eng. backslash) "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":" kan användas för att undkomma specialtecken. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1+"}]},{"type":"text","value":" matchar alltså ett eller flera tal "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", men uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1\\+"}]},{"type":"text","value":" matchar strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1+"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^\\*"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hej*"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"h*e*j"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"*hej"}]},{"type":"text","value":"\nHittad!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Runda parenteser kan användas för att gruppera ihop olika delar av uttrycket. Till exempel skulle uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(ab)+c"}]},{"type":"text","value":" matcha delsträngarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ababc"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"abababababababc"}]},{"type":"text","value":", men inte strängarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ac"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bc"}]},{"type":"text","value":", eftersom hela delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ab"}]},{"type":"text","value":" måste förekomma minst en gång."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^(jabba).*(hut)$"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba the hut"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba a hut"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jarmo the hut"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba the smut"}]},{"type":"text","value":"\nHittades inte."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Reguljära uttryck","tmcname":"osa12-14_reguljara_uttryck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här följer några övningar för att bekanta dig med syntaxen för reguljära uttryck."}]},{"type":"element","tagName":"h2","properties":{"id":"veckodagar","style":"position:relative;"},"children":[{"type":"text","value":"Veckodagar"},{"type":"element","tagName":"a","properties":{"href":"#veckodagar","ariaLabel":"veckodagar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd ett reguljärt uttryck för att skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar_veckodag(strang: str)"}]},{"type":"text","value":". Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om strängen som skickas som argument innehåller en förkortning av en veckodag (mån, tis, ons, tors, fre, lör, sön)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen ska fungera"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar_veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mån\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar_veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fre\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar_veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"turs\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nFalse"}]}]},{"type":"element","tagName":"h2","properties":{"id":"vokalcheck","style":"position:relative;"},"children":[{"type":"text","value":"Vokalcheck"},{"type":"element","tagName":"a","properties":{"href":"#vokalcheck","ariaLabel":"vokalcheck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alla_vokaler(strang: str)"}]},{"type":"text","value":" som använder ett reguljärt uttryck för att kontrollera om alla tecken i den givna strängen är vokaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alla_vokaler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eioueioieoieouyyyy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alla_vokaler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"biiiiil\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse"}]}]},{"type":"element","tagName":"h2","properties":{"id":"klockans-tid","style":"position:relative;"},"children":[{"type":"text","value":"Klockans tid"},{"type":"element","tagName":"a","properties":{"href":"#klockans-tid","ariaLabel":"klockans tid permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"klocktid(strang: str)"}]},{"type":"text","value":", som använder ett reguljärt uttryck för att granska ifall en sträng av formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tt:mm:ss"}]},{"type":"text","value":" är en giltig tid i ett 24-timmars format, med två siffror var för timmar, minuter och sekunder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocktid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12:43:01\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocktid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"AB:01:CD\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocktid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"17:59:59\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocktid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"33:66:77\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse\nTrue\nFalse"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"den-stora-finalen","style":"position:relative;"},"children":[{"type":"text","value":"Den stora finalen"},{"type":"element","tagName":"a","properties":{"href":"#den-stora-finalen","ariaLabel":"den stora finalen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som avslutning på denna del av materialet ska vi arbeta lite mer med objekt och klasser genom att bygga ett lite mer omfattande program. Denna övning innefattar inte nödvändigtvis reguljära uttryck, men avsnitten om "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-12/1-funktioner-som-argument","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Funktioner som argument "}]},{"type":"text","value":" och "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-11/1-list-comprehension","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"list comprehension"}]},{"type":"text","value":" kommer sannolikt att vara användbara."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också ha nytta av de exempel som finns i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-10/4-storre-applikation","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 10"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Statistik i ordning","tmcname":"osa12-15_statistik_i_ordning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen kommer du att bygga en applikation för att undersöka hockeyligastatistik från NHL på ett par olika sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller två JSON-filer: del.json och alla.json. Den första av dessa är mest avsedd för testning. Den senare innehåller en hel del data, eftersom all NHL-spelarstatistik för säsongen 2019-20 ingår i filen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inlägget för en enskild spelare är i följande format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"name\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Laine\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"nationality\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"FIN\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"assists\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"goals\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"penalties\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"team\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"WPG\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"games\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"68"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Båda filerna innehållar en lista av inlägg enligt ovanstående format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall du behöver en uppfriskare när det gäller hantering av JOSN-filer, kan du ta en titt på "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-7/4-behandla-data","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 7 i kursmaterialet"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"sok-och-lista","style":"position:relative;"},"children":[{"type":"text","value":"Sök och lista"},{"type":"element","tagName":"a","properties":{"href":"#sok-och-lista","ariaLabel":"sok och lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en interaktiv applikation som först frågar efter namnet på filen och sedan erbjuder följande funktioner:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"sök enligt namn för en enskild spelares statistik"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista alla förkortningar för lagnamn i alfabetisk ordning"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista alla förkortningar för länder i alfabetisk ordning"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa funktioner ger dig totalt ett övningspoäng. Din applikation ska nu fungera enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"fil: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"del.json"}]},{"type":"text","value":"\nläste 14 spelares data"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Travis Zajac"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Travis Zajac NJD  9 + 16 = 25\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nBUF\nCGY\nDAL\nNJD\nNYI\nOTT\nPIT\nWPG\nWSH"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nCAN\nCHE\nCZE\nSWE\nUSA"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs: Utskriftens format för en spelare måste vara exakt enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Leon Draisaitl EDM 43 + 67 = 110\nConnor McDavid EDM 34 + 63 = 97\nTravis Zajac NJD  9 + 16 = 25\nMike Green EDM 3 + 8 = 11\nMarkus Granlund EDM 3 + 1 = 4\n123456789012345678901234567890123456789\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den sista raden i exemplet ovan är till för att hjälpa dig att beräkna bredden på de olika fälten i utskriften; du ska inte skriva ut nummerraden själv i din slutliga inlämning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förkortningen för teamet skrivs ut från det 22:a tecknet och framåt. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-tecknet är det 30:e tecknet och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"-tecknet är det 35:e tecknet. Alla fält ska vara justifierade till högerkanten. Alla blanksteg är mellanslagstecken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"F-strängar är förmodligen det enklaste sättet att uppnå den önskade utskriften. Processen är liknande som övningen Kursresultat del 3 från "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/1-lasa-filer","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"lista-spelare-enligt-poang","style":"position:relative;"},"children":[{"type":"text","value":"Lista spelare enligt poäng"},{"type":"element","tagName":"a","properties":{"href":"#lista-spelare-enligt-poang","ariaLabel":"lista spelare enligt poang permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande funktionalitet ger dig ditt andra övningspoäng:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista spelare i ett specifikt lag enligt ordningen flest poäng, från högst till lägst. Poäng är mängden "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"mål"}]},{"type":"text","value":" + "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"assistanser"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista spelare från ett specifikt land enligt ordningen flest poäng, från högst till lägst"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din applikation ska nu fungera enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"fil: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"del.json"}]},{"type":"text","value":"\nläste 14 spelares data"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nlag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OTT"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Drake Batherson OTT 3 + 7 = 10\nJonathan Davidsson OTT 0 + 1 = 1\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nland: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"CAN"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jared McCann PIT 14 + 21 = 35\nTravis Zajac NJD 9 + 16 = 25\nTaylor Fedun DAL 2 + 7 = 9\nMark Jankowski CGY 5 + 2 = 7\nLogan Shaw WPG 3 + 2 = 5\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"mest-framgangsrika-spelare","style":"position:relative;"},"children":[{"type":"text","value":"Mest framgångsrika spelare"},{"type":"element","tagName":"a","properties":{"href":"#mest-framgangsrika-spelare","ariaLabel":"mest framgangsrika spelare permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det tredje övningspoänget får du från följande två funktionaliteter:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" mängd spelare som fått flest poäng\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ifall två spelare har samma mängd poäng, ska den som har flera mål komma först"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" mängd spelare som har gjort flest mål\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ifall två spelare har samma mängd mål, ska den som spelat färre spel komma först"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Applikationen ska nu fungera enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"fil: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"del.json"}]},{"type":"text","value":"\nläste 14 spelares data"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nhur många: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nhur många: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\nJohn Klingberg DAL 6 + 26 = 32\nTravis Zajac NJD 9 + 16 = 25\nConor Sheary BUF 10 + 13 = 23\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nhur många: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\nConor Sheary BUF 10 + 13 = 23\nTravis Zajac NJD 9 + 16 = 25\nJohn Klingberg DAL 6 + 26 = 32\nMark Jankowski CGY 5 + 2 = 7\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara till sist på en snabb enkät:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad reguljära uttryck är
  • \n
  • Kommer du att kunna använda reguljära uttryck i dina egna program
  • \n

Vi har redan konstaterat att Python är en utmärkt miljö för att bearbeta text. Ett ytterligare kraftfullt verktyg för textbehandling är reguljära uttryck (eng. regular expressions), ofta förkortat som regex eller regexp. De är ett sätt att välja ut och söka efter strängar som följer ett visst mönster. I det här avsnittet får du en introduktion till grunderna i reguljära uttryck, men du hittar mycket mer information på nätet, bland annat i Pythons egna handledning.

Vad är reguljära uttryck?

Reguljära uttryck är inte bara en Python-funktion. De representerar på sätt och vis ett programmeringsspråk inom ett programmeringsspråk. De är i viss utsträckning kompatibla med många olika programmeringsspråk. Reguljära uttryck har sin egen specifika syntax. Tanken är att definiera en samling strängar som följer vissa regler.

Låt oss börja med ett enkelt exempel innan vi dyker djupare in i syntaxen:

import re\n\norden = [\"Python\", \"Population\", \"Pantomi\", \"Perfekt\", \"Prestation\"]\n\nfor ord in orden:\n    # Ifall strängen börjar med \"P\" och slutar med \"on\"\n    if re.search(\"^P.*on$\", ord):\n        print(ord, \"hittad!\")

Python hittad!\nPopulation hittad!\nPrestation hittad!

Vi behöver importera modulen re för att kunna använda reguljära uttryck i Python. Modulen re innehåller många funktioner för att arbeta med reguljära uttryck. I exemplet ovan tar search-funktionen två strängargument: mönstersträngen och den målsträng där mönstret ska sökas.

I det här andra exemplet letar man efter alla siffror i en sträng. Funktionen findall returnerar en lista över alla instanser som matchar mönstret:

import re\n\nmening = \"Först, 2 !#tredje 44 fem 678xyz962\"\n\nnumren = re.findall(\"\\d+\", mening)\n\nfor nummer in numren:\n    print(nummer)

2\n44\n678\n962

Syntaxen för reguljära uttryck

Låt oss bekanta oss med den grundläggande syntaxen för reguljära uttryck. De flesta av följande exempel använder sig av detta testprogram:

import re\n\nuttryck = input(\"Ange uttryck: \")\n\nwhile True:\n    strang = input(\"Ange sträng: \")\n    if strang == \"\":\n        break\n    if re.search(uttryck, strang):\n        print(\"Hittad!\")\n    else:\n        print(\"Hittades inte.\")

Alternativa delsträngar

Lodstrecket (eng. vertical bar) |, gör att du kan matcha alternativa delsträngar. Dess betydelse är alltså eller. Uttrycket 911|112 matchar t.ex. strängar som innehåller antingen delsträngen 911 eller delsträngen 112.

Ett exempel med testprogrammet:

Ange uttryck: aa|ee|ii\nAnge sträng: kassaaparat\nHittad!\nAnge sträng: beteende\nHittad!\nAnge sträng: friidrott\nHittad!\nAnge sträng: kooperation\nHittades inte.\nAnge sträng: bastuugn\nHittades inte.

Grupper av tecken

Hakparenteser används för att beteckna grupper av accepterade tecken. Uttrycket [aeio] skulle t.ex. matcha alla strängar som innehåller något av tecknen a, e, i eller o.

Ett bindestreck är också tillåtet för att matcha intervall av tecken. Uttrycket [0-68a-d] skulle till exempel matcha alla strängar som innehåller en siffra mellan 0 och 6, eller en åtta, eller ett tecken mellan a och d. I den här notationen är alla intervall inkluderande.

Om du kombinerar två uppsättningar parenteser kan du matcha två tecken i följd. Till exempel skulle uttrycket [1-3][0-9] matcha alla tvåsiffriga tal mellan 10 och 39, inklusive.

Ett exempel med testprogrammet:

Ange uttryck: [C-FRSÖ]\nAnge sträng: C\nHittad!\nAnge sträng: E\nHittad!\nAnge sträng: G\nHittades inte.\nAnge sträng: R\nHittad!\nAnge sträng: Ö\nHittad!\nAnge sträng: T\nHittades inte.

Upprepade matchningar

Varje del av ett uttryck kan upprepas med följande operatorer:

    \n
  • * upprepas hur många gånger som helst, inklusive noll
  • \n
  • + upprepas hur många gånger som helst, men minst en gång
  • \n
  • {m} upprepas exakt m gånger
  • \n

Dessa operatorer fungerar på den del av uttrycket som kommer omedelbart före operatorn. Uttrycket ba+b skulle t.ex. matcha delsträngarna bab, baab och baaaaaaaaaaab, bland andra. Uttrycket A[BCDE]*Z skulle matcha delsträngarna AZ, ADZ eller ABCDEBCDEBCDEZ, bland andra.

Ett exempel med testprogrammet:

Ange uttryck: 1[234]*5\nAnge sträng: 15\nHittad!\nAnge sträng: 125\nHittad!\nAnge sträng: 145\nHittad!\nAnge sträng: 12342345\nHittad!\nAnge sträng: 126\nHittades inte.\nAnge sträng: 165\nHittades inte.

Andra specialtecken

En punkt är ett jokertecken som kan matcha vilket enskilt tecken som helst. Uttrycket c...o skulle till exempel matcha alla delsträngar med fem tecken som börjar med ett c och slutar med ett o, till exempel c-3po eller cello.

Tecknet ^ anger att matchningen måste ske i början av strängen och $ anger att matchningen måste ske i slutet av strängen. Dessa tecken kan också användas för att utesluta andra tecken än de angivna från matchningen:

Ange uttryck: ^[123]*$\nAnge sträng: 4\nHittades inte.\nAnge sträng: 1221\nHittad!\nAnge sträng: 333333333\nHittad!

Ibland behöver du matcha för specialtecken som är reserverade för syntaxen för reguljära uttryck. Omvänt snedstreck (eng. backslash) \\ kan användas för att undkomma specialtecken. Uttrycket 1+ matchar alltså ett eller flera tal 1, men uttrycket 1\\+ matchar strängen 1+.

Ange uttryck: ^\\*\nAnge sträng: hej*\nHittades inte.\nAnge sträng: h*e*j\nHittades inte.\nAnge sträng: *hej\nHittad!

Runda parenteser kan användas för att gruppera ihop olika delar av uttrycket. Till exempel skulle uttrycket (ab)+c matcha delsträngarna abc, ababc och abababababababc, men inte strängarna ac eller bc, eftersom hela delsträngen ab måste förekomma minst en gång.

Ange uttryck: ^(jabba).*(hut)$\nAnge sträng: jabba the hut\nHittad!\nAnge sträng: jabba a hut\nHittad!\nAnge sträng: jarmo the hut\nHittades inte.\nAnge sträng: jabba the smut\nHittades inte.

Här följer några övningar för att bekanta dig med syntaxen för reguljära uttryck.

Veckodagar

Använd ett reguljärt uttryck för att skapa en funktion med namnet ar_veckodag(strang: str). Funktionen ska returnera True om strängen som skickas som argument innehåller en förkortning av en veckodag (mån, tis, ons, tors, fre, lör, sön).

Exempel på hur funktionen ska fungera

print(ar_veckodag(\"mån\"))\nprint(ar_veckodag(\"fre\"))\nprint(ar_veckodag(\"turs\"))

True\nTrue\nFalse

Vokalcheck

Skapa en funktion med namnet alla_vokaler(strang: str) som använder ett reguljärt uttryck för att kontrollera om alla tecken i den givna strängen är vokaler.

Exempel på hur funktionen ska fungera:

print(alla_vokaler(\"eioueioieoieouyyyy\"))\nprint(alla_vokaler(\"biiiiil\"))

True\nFalse

Klockans tid

Skapa funktionen klocktid(strang: str), som använder ett reguljärt uttryck för att granska ifall en sträng av formatet tt:mm:ss är en giltig tid i ett 24-timmars format, med två siffror var för timmar, minuter och sekunder.

Exempel på hur funktionen ska fungera:

print(klocktid(\"12:43:01\"))\nprint(klocktid(\"AB:01:CD\"))\nprint(klocktid(\"17:59:59\"))\nprint(klocktid(\"33:66:77\"))

True\nFalse\nTrue\nFalse

Den stora finalen

Som avslutning på denna del av materialet ska vi arbeta lite mer med objekt och klasser genom att bygga ett lite mer omfattande program. Denna övning innefattar inte nödvändigtvis reguljära uttryck, men avsnitten om Funktioner som argument och list comprehension kommer sannolikt att vara användbara.

Du kan också ha nytta av de exempel som finns i modul 10.

I den här övningen kommer du att bygga en applikation för att undersöka hockeyligastatistik från NHL på ett par olika sätt.

Övningsmallen innehåller två JSON-filer: del.json och alla.json. Den första av dessa är mest avsedd för testning. Den senare innehåller en hel del data, eftersom all NHL-spelarstatistik för säsongen 2019-20 ingår i filen.

Inlägget för en enskild spelare är i följande format:

{\n    \"name\": \"Patrik Laine\",\n    \"nationality\": \"FIN\",\n    \"assists\": 35,\n    \"goals\": 28,\n    \"penalties\": 22,\n    \"team\": \"WPG\",\n    \"games\": 68\n},

Båda filerna innehållar en lista av inlägg enligt ovanstående format.

Ifall du behöver en uppfriskare när det gäller hantering av JOSN-filer, kan du ta en titt på modul 7 i kursmaterialet.

Sök och lista

Skapa en interaktiv applikation som först frågar efter namnet på filen och sedan erbjuder följande funktioner:

    \n
  • sök enligt namn för en enskild spelares statistik
  • \n
  • lista alla förkortningar för lagnamn i alfabetisk ordning
  • \n
  • lista alla förkortningar för länder i alfabetisk ordning
  • \n

Dessa funktioner ger dig totalt ett övningspoäng. Din applikation ska nu fungera enligt följande:

fil: del.json\nläste 14 spelares data

instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål

instruktion: 1\nnamn: Travis Zajac

\nTravis Zajac         NJD   9 + 16 =  25\n

instruktion: 2\nBUF\nCGY\nDAL\nNJD\nNYI\nOTT\nPIT\nWPG\nWSH

instruktion: 3\nCAN\nCHE\nCZE\nSWE\nUSA

instruktion: 0

Obs: Utskriftens format för en spelare måste vara exakt enligt följande:

\nLeon Draisaitl       EDM  43 + 67 = 110\nConnor McDavid       EDM  34 + 63 =  97\nTravis Zajac         NJD   9 + 16 =  25\nMike Green           EDM   3 +  8 =  11\nMarkus Granlund      EDM   3 +  1 =   4\n123456789012345678901234567890123456789\n

Den sista raden i exemplet ovan är till för att hjälpa dig att beräkna bredden på de olika fälten i utskriften; du ska inte skriva ut nummerraden själv i din slutliga inlämning.

Förkortningen för teamet skrivs ut från det 22:a tecknet och framåt. +-tecknet är det 30:e tecknet och =-tecknet är det 35:e tecknet. Alla fält ska vara justifierade till högerkanten. Alla blanksteg är mellanslagstecken.

F-strängar är förmodligen det enklaste sättet att uppnå den önskade utskriften. Processen är liknande som övningen Kursresultat del 3 från modul 6.

Lista spelare enligt poäng

Följande funktionalitet ger dig ditt andra övningspoäng:

    \n
  • lista spelare i ett specifikt lag enligt ordningen flest poäng, från högst till lägst. Poäng är mängden mål + assistanser
  • \n
  • lista spelare från ett specifikt land enligt ordningen flest poäng, från högst till lägst
  • \n

Din applikation ska nu fungera enligt följande:

fil: del.json\nläste 14 spelares data

instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål

instruktion: 4\nlag: OTT

\nDrake Batherson      OTT   3 +  7 =  10\nJonathan Davidsson   OTT   0 +  1 =   1\n

instruktion: 5\nland: CAN

\nJared McCann         PIT  14 + 21 =  35\nTravis Zajac         NJD   9 + 16 =  25\nTaylor Fedun         DAL   2 +  7 =   9\nMark Jankowski       CGY   5 +  2 =   7\nLogan Shaw           WPG   3 +  2 =   5\n

instruktion: 0

Mest framgångsrika spelare

Det tredje övningspoänget får du från följande två funktionaliteter:

    \n
  • lista av n mängd spelare som fått flest poäng\n
      \n
    • ifall två spelare har samma mängd poäng, ska den som har flera mål komma först
    • \n
    \n
  • \n
  • lista av n mängd spelare som har gjort flest mål\n
      \n
    • ifall två spelare har samma mängd mål, ska den som spelat färre spel komma först
    • \n
    \n
  • \n

Applikationen ska nu fungera enligt följande:

fil: del.json\nläste 14 spelares data

instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål

instruktion: 6\nhur många: 2

\nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\n

instruktion: 6\nhur många: 5

\nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\nJohn Klingberg       DAL   6 + 26 =  32\nTravis Zajac         NJD   9 + 16 =  25\nConor Sheary         BUF  10 + 13 =  23\n

instruktion: 7\nhur många: 6

\nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\nConor Sheary         BUF  10 + 13 =  23\nTravis Zajac         NJD   9 + 16 =  25\nJohn Klingberg       DAL   6 + 26 =  32\nMark Jankowski       CGY   5 +  2 =   7\n

instruktion: 0

Svara till sist på en snabb enkät:

","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/4-reguljara-uttryck.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad reguljära uttryck är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda reguljära uttryck i dina egna program"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan konstaterat att Python är en utmärkt miljö för att bearbeta text. Ett ytterligare kraftfullt verktyg för textbehandling är reguljära uttryck (eng. regular expressions), ofta förkortat som regex eller regexp. De är ett sätt att välja ut och söka efter strängar som följer ett visst mönster. I det här avsnittet får du en introduktion till grunderna i reguljära uttryck, men du hittar mycket mer information på nätet, bland annat i Pythons egna "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/howto/regex.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"handledning"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"vad-ar-reguljara-uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Vad är reguljära uttryck?"},{"type":"element","tagName":"a","properties":{"href":"#vad-ar-reguljara-uttryck","ariaLabel":"vad ar reguljara uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Reguljära uttryck är inte bara en Python-funktion. De representerar på sätt och vis ett programmeringsspråk inom ett programmeringsspråk. De är i viss utsträckning kompatibla med många olika programmeringsspråk. Reguljära uttryck har sin egen specifika syntax. Tanken är att definiera en samling strängar som följer vissa regler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja med ett enkelt exempel innan vi dyker djupare in i syntaxen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" re\n\norden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Population\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pantomi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Perfekt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Prestation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall strängen börjar med \"P\" och slutar med \"on\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"^P.*on$\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittad!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python hittad!\nPopulation hittad!\nPrestation hittad!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi behöver importera modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"re"}]},{"type":"text","value":" för att kunna använda reguljära uttryck i Python. Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"re"}]},{"type":"text","value":" innehåller många funktioner för att arbeta med reguljära uttryck. I exemplet ovan tar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search"}]},{"type":"text","value":"-funktionen två strängargument: mönstersträngen och den målsträng där mönstret ska sökas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här andra exemplet letar man efter alla siffror i en sträng. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"findall"}]},{"type":"text","value":" returnerar en lista över alla instanser som matchar mönstret:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" re\n\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Först, 2 !#tredje 44 fem 678xyz962\""}]},{"type":"text","value":"\n\nnumren "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"findall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\d+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n44\n678\n962"}]}]},{"type":"element","tagName":"h2","properties":{"id":"syntaxen-for-reguljara-uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Syntaxen för reguljära uttryck"},{"type":"element","tagName":"a","properties":{"href":"#syntaxen-for-reguljara-uttryck","ariaLabel":"syntaxen for reguljara uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss bekanta oss med den grundläggande syntaxen för reguljära uttryck. De flesta av följande exempel använder sig av detta testprogram:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" re\n\nuttryck "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange uttryck: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"uttryck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittad!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittades inte.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"alternativa-delstrangar","style":"position:relative;"},"children":[{"type":"text","value":"Alternativa delsträngar"},{"type":"element","tagName":"a","properties":{"href":"#alternativa-delstrangar","ariaLabel":"alternativa delstrangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lodstrecket (eng. vertical bar) "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"|"}]},{"type":"text","value":", gör att du kan matcha alternativa delsträngar. Dess betydelse är alltså eller. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"911|112"}]},{"type":"text","value":" matchar t.ex. strängar som innehåller antingen delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"911"}]},{"type":"text","value":" eller delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"112"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel med testprogrammet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aa|ee|ii"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kassaaparat"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"beteende"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"friidrott"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kooperation"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bastuugn"}]},{"type":"text","value":"\nHittades inte."}]}]},{"type":"element","tagName":"h3","properties":{"id":"grupper-av-tecken","style":"position:relative;"},"children":[{"type":"text","value":"Grupper av tecken"},{"type":"element","tagName":"a","properties":{"href":"#grupper-av-tecken","ariaLabel":"grupper av tecken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hakparenteser används för att beteckna grupper av accepterade tecken. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[aeio]"}]},{"type":"text","value":" skulle t.ex. matcha alla strängar som innehåller något av tecknen a, e, i eller o."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett bindestreck är också tillåtet för att matcha intervall av tecken. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[0-68a-d]"}]},{"type":"text","value":" skulle till exempel matcha alla strängar som innehåller en siffra mellan 0 och 6, eller en åtta, eller ett tecken mellan a och d. I den här notationen är alla intervall inkluderande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du kombinerar två uppsättningar parenteser kan du matcha två tecken i följd. Till exempel skulle uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1-3][0-9]"}]},{"type":"text","value":" matcha alla tvåsiffriga tal mellan 10 och 39, inklusive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel med testprogrammet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"[C-FRSÖ]"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"C"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"E"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"G"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"R"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ö"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"T"}]},{"type":"text","value":"\nHittades inte."}]}]},{"type":"element","tagName":"h3","properties":{"id":"upprepade-matchningar","style":"position:relative;"},"children":[{"type":"text","value":"Upprepade matchningar"},{"type":"element","tagName":"a","properties":{"href":"#upprepade-matchningar","ariaLabel":"upprepade matchningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje del av ett uttryck kan upprepas med följande operatorer:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" upprepas hur många gånger som helst, inklusive noll"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" upprepas hur många gånger som helst, men minst en gång"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{m}"}]},{"type":"text","value":" upprepas exakt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"m"}]},{"type":"text","value":" gånger"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa operatorer fungerar på den del av uttrycket som kommer omedelbart före operatorn. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ba+b"}]},{"type":"text","value":" skulle t.ex. matcha delsträngarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bab"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"baab"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"baaaaaaaaaaab"}]},{"type":"text","value":", bland andra. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A[BCDE]*Z"}]},{"type":"text","value":" skulle matcha delsträngarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"AZ"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADZ"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ABCDEBCDEBCDEZ"}]},{"type":"text","value":", bland andra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel med testprogrammet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1[234]*5"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"125"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"145"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12342345"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"126"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"165"}]},{"type":"text","value":"\nHittades inte."}]}]},{"type":"element","tagName":"h3","properties":{"id":"andra-specialtecken","style":"position:relative;"},"children":[{"type":"text","value":"Andra specialtecken"},{"type":"element","tagName":"a","properties":{"href":"#andra-specialtecken","ariaLabel":"andra specialtecken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En punkt är ett jokertecken som kan matcha vilket enskilt tecken som helst. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c...o"}]},{"type":"text","value":" skulle till exempel matcha alla delsträngar med fem tecken som börjar med ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":" och slutar med ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"o"}]},{"type":"text","value":", till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c-3po"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cello"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"^"}]},{"type":"text","value":" anger att matchningen måste ske i början av strängen och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"$"}]},{"type":"text","value":" anger att matchningen måste ske i slutet av strängen. Dessa tecken kan också användas för att utesluta andra tecken än de angivna från matchningen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^[123]*$"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1221"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"333333333"}]},{"type":"text","value":"\nHittad!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland behöver du matcha för specialtecken som är reserverade för syntaxen för reguljära uttryck. Omvänt snedstreck (eng. backslash) "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":" kan användas för att undkomma specialtecken. Uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1+"}]},{"type":"text","value":" matchar alltså ett eller flera tal "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", men uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1\\+"}]},{"type":"text","value":" matchar strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1+"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^\\*"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hej*"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"h*e*j"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"*hej"}]},{"type":"text","value":"\nHittad!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Runda parenteser kan användas för att gruppera ihop olika delar av uttrycket. Till exempel skulle uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(ab)+c"}]},{"type":"text","value":" matcha delsträngarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ababc"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"abababababababc"}]},{"type":"text","value":", men inte strängarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ac"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bc"}]},{"type":"text","value":", eftersom hela delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ab"}]},{"type":"text","value":" måste förekomma minst en gång."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange uttryck: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^(jabba).*(hut)$"}]},{"type":"text","value":"\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba the hut"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba a hut"}]},{"type":"text","value":"\nHittad!\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jarmo the hut"}]},{"type":"text","value":"\nHittades inte.\nAnge sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba the smut"}]},{"type":"text","value":"\nHittades inte."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Reguljära uttryck","tmcname":"osa12-14_reguljara_uttryck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här följer några övningar för att bekanta dig med syntaxen för reguljära uttryck."}]},{"type":"element","tagName":"h2","properties":{"id":"veckodagar","style":"position:relative;"},"children":[{"type":"text","value":"Veckodagar"},{"type":"element","tagName":"a","properties":{"href":"#veckodagar","ariaLabel":"veckodagar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd ett reguljärt uttryck för att skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar_veckodag(strang: str)"}]},{"type":"text","value":". Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om strängen som skickas som argument innehåller en förkortning av en veckodag (mån, tis, ons, tors, fre, lör, sön)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen ska fungera"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar_veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mån\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar_veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fre\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar_veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"turs\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nFalse"}]}]},{"type":"element","tagName":"h2","properties":{"id":"vokalcheck","style":"position:relative;"},"children":[{"type":"text","value":"Vokalcheck"},{"type":"element","tagName":"a","properties":{"href":"#vokalcheck","ariaLabel":"vokalcheck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alla_vokaler(strang: str)"}]},{"type":"text","value":" som använder ett reguljärt uttryck för att kontrollera om alla tecken i den givna strängen är vokaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alla_vokaler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eioueioieoieouyyyy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alla_vokaler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"biiiiil\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse"}]}]},{"type":"element","tagName":"h2","properties":{"id":"klockans-tid","style":"position:relative;"},"children":[{"type":"text","value":"Klockans tid"},{"type":"element","tagName":"a","properties":{"href":"#klockans-tid","ariaLabel":"klockans tid permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"klocktid(strang: str)"}]},{"type":"text","value":", som använder ett reguljärt uttryck för att granska ifall en sträng av formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tt:mm:ss"}]},{"type":"text","value":" är en giltig tid i ett 24-timmars format, med två siffror var för timmar, minuter och sekunder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocktid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12:43:01\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocktid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"AB:01:CD\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocktid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"17:59:59\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocktid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"33:66:77\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse\nTrue\nFalse"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"den-stora-finalen","style":"position:relative;"},"children":[{"type":"text","value":"Den stora finalen"},{"type":"element","tagName":"a","properties":{"href":"#den-stora-finalen","ariaLabel":"den stora finalen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som avslutning på denna del av materialet ska vi arbeta lite mer med objekt och klasser genom att bygga ett lite mer omfattande program. Denna övning innefattar inte nödvändigtvis reguljära uttryck, men avsnitten om "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-12/1-funktioner-som-argument","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Funktioner som argument "}]},{"type":"text","value":" och "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-11/1-list-comprehension","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"list comprehension"}]},{"type":"text","value":" kommer sannolikt att vara användbara."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också ha nytta av de exempel som finns i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-10/4-storre-applikation","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 10"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Statistik i ordning","tmcname":"osa12-15_statistik_i_ordning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen kommer du att bygga en applikation för att undersöka hockeyligastatistik från NHL på ett par olika sätt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller två JSON-filer: del.json och alla.json. Den första av dessa är mest avsedd för testning. Den senare innehåller en hel del data, eftersom all NHL-spelarstatistik för säsongen 2019-20 ingår i filen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inlägget för en enskild spelare är i följande format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"name\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Patrik Laine\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"nationality\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"FIN\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"assists\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"goals\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"penalties\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"team\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"WPG\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"games\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"68"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Båda filerna innehållar en lista av inlägg enligt ovanstående format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall du behöver en uppfriskare när det gäller hantering av JOSN-filer, kan du ta en titt på "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-7/4-behandla-data","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 7 i kursmaterialet"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"sok-och-lista","style":"position:relative;"},"children":[{"type":"text","value":"Sök och lista"},{"type":"element","tagName":"a","properties":{"href":"#sok-och-lista","ariaLabel":"sok och lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en interaktiv applikation som först frågar efter namnet på filen och sedan erbjuder följande funktioner:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"sök enligt namn för en enskild spelares statistik"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista alla förkortningar för lagnamn i alfabetisk ordning"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista alla förkortningar för länder i alfabetisk ordning"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessa funktioner ger dig totalt ett övningspoäng. Din applikation ska nu fungera enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"fil: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"del.json"}]},{"type":"text","value":"\nläste 14 spelares data"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Travis Zajac"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Travis Zajac NJD  9 + 16 = 25\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nBUF\nCGY\nDAL\nNJD\nNYI\nOTT\nPIT\nWPG\nWSH"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nCAN\nCHE\nCZE\nSWE\nUSA"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs: Utskriftens format för en spelare måste vara exakt enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Leon Draisaitl EDM 43 + 67 = 110\nConnor McDavid EDM 34 + 63 = 97\nTravis Zajac NJD  9 + 16 = 25\nMike Green EDM 3 + 8 = 11\nMarkus Granlund EDM 3 + 1 = 4\n123456789012345678901234567890123456789\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den sista raden i exemplet ovan är till för att hjälpa dig att beräkna bredden på de olika fälten i utskriften; du ska inte skriva ut nummerraden själv i din slutliga inlämning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förkortningen för teamet skrivs ut från det 22:a tecknet och framåt. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-tecknet är det 30:e tecknet och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"-tecknet är det 35:e tecknet. Alla fält ska vara justifierade till högerkanten. Alla blanksteg är mellanslagstecken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"F-strängar är förmodligen det enklaste sättet att uppnå den önskade utskriften. Processen är liknande som övningen Kursresultat del 3 från "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/1-lasa-filer","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"lista-spelare-enligt-poang","style":"position:relative;"},"children":[{"type":"text","value":"Lista spelare enligt poäng"},{"type":"element","tagName":"a","properties":{"href":"#lista-spelare-enligt-poang","ariaLabel":"lista spelare enligt poang permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande funktionalitet ger dig ditt andra övningspoäng:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista spelare i ett specifikt lag enligt ordningen flest poäng, från högst till lägst. Poäng är mängden "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"mål"}]},{"type":"text","value":" + "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"assistanser"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista spelare från ett specifikt land enligt ordningen flest poäng, från högst till lägst"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din applikation ska nu fungera enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"fil: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"del.json"}]},{"type":"text","value":"\nläste 14 spelares data"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nlag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OTT"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Drake Batherson OTT 3 + 7 = 10\nJonathan Davidsson OTT 0 + 1 = 1\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nland: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"CAN"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jared McCann PIT 14 + 21 = 35\nTravis Zajac NJD 9 + 16 = 25\nTaylor Fedun DAL 2 + 7 = 9\nMark Jankowski CGY 5 + 2 = 7\nLogan Shaw WPG 3 + 2 = 5\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"mest-framgangsrika-spelare","style":"position:relative;"},"children":[{"type":"text","value":"Mest framgångsrika spelare"},{"type":"element","tagName":"a","properties":{"href":"#mest-framgangsrika-spelare","ariaLabel":"mest framgangsrika spelare permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det tredje övningspoänget får du från följande två funktionaliteter:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" mängd spelare som fått flest poäng\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ifall två spelare har samma mängd poäng, ska den som har flera mål komma först"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"lista av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" mängd spelare som har gjort flest mål\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ifall två spelare har samma mängd mål, ska den som spelat färre spel komma först"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Applikationen ska nu fungera enligt följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"fil: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"del.json"}]},{"type":"text","value":"\nläste 14 spelares data"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nhur många: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nhur många: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\nJohn Klingberg DAL 6 + 26 = 32\nTravis Zajac NJD 9 + 16 = 25\nConor Sheary BUF 10 + 13 = 23\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nhur många: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\nConor Sheary BUF 10 + 13 = 23\nTravis Zajac NJD 9 + 16 = 25\nJohn Klingberg DAL 6 + 26 = 32\nMark Jankowski CGY 5 + 2 = 7\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"instruktion: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara till sist på en snabb enkät:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad reguljära uttryck är
  • \n
  • Kommer du att kunna använda reguljära uttryck i dina egna program
  • \n

Vi har redan konstaterat att Python är en utmärkt miljö för att bearbeta text. Ett ytterligare kraftfullt verktyg för textbehandling är reguljära uttryck (eng. regular expressions), ofta förkortat som regex eller regexp. De är ett sätt att välja ut och söka efter strängar som följer ett visst mönster. I det här avsnittet får du en introduktion till grunderna i reguljära uttryck, men du hittar mycket mer information på nätet, bland annat i Pythons egna handledning.

Vad är reguljära uttryck?

Reguljära uttryck är inte bara en Python-funktion. De representerar på sätt och vis ett programmeringsspråk inom ett programmeringsspråk. De är i viss utsträckning kompatibla med många olika programmeringsspråk. Reguljära uttryck har sin egen specifika syntax. Tanken är att definiera en samling strängar som följer vissa regler.

Låt oss börja med ett enkelt exempel innan vi dyker djupare in i syntaxen:

import re\n\norden = [\"Python\", \"Population\", \"Pantomi\", \"Perfekt\", \"Prestation\"]\n\nfor ord in orden:\n    # Ifall strängen börjar med \"P\" och slutar med \"on\"\n    if re.search(\"^P.*on$\", ord):\n        print(ord, \"hittad!\")

Python hittad!\nPopulation hittad!\nPrestation hittad!

Vi behöver importera modulen re för att kunna använda reguljära uttryck i Python. Modulen re innehåller många funktioner för att arbeta med reguljära uttryck. I exemplet ovan tar search-funktionen två strängargument: mönstersträngen och den målsträng där mönstret ska sökas.

I det här andra exemplet letar man efter alla siffror i en sträng. Funktionen findall returnerar en lista över alla instanser som matchar mönstret:

import re\n\nmening = \"Först, 2 !#tredje 44 fem 678xyz962\"\n\nnumren = re.findall(\"\\d+\", mening)\n\nfor nummer in numren:\n    print(nummer)

2\n44\n678\n962

Syntaxen för reguljära uttryck

Låt oss bekanta oss med den grundläggande syntaxen för reguljära uttryck. De flesta av följande exempel använder sig av detta testprogram:

import re\n\nuttryck = input(\"Ange uttryck: \")\n\nwhile True:\n    strang = input(\"Ange sträng: \")\n    if strang == \"\":\n        break\n    if re.search(uttryck, strang):\n        print(\"Hittad!\")\n    else:\n        print(\"Hittades inte.\")

Alternativa delsträngar

Lodstrecket (eng. vertical bar) |, gör att du kan matcha alternativa delsträngar. Dess betydelse är alltså eller. Uttrycket 911|112 matchar t.ex. strängar som innehåller antingen delsträngen 911 eller delsträngen 112.

Ett exempel med testprogrammet:

Ange uttryck: aa|ee|ii\nAnge sträng: kassaaparat\nHittad!\nAnge sträng: beteende\nHittad!\nAnge sträng: friidrott\nHittad!\nAnge sträng: kooperation\nHittades inte.\nAnge sträng: bastuugn\nHittades inte.

Grupper av tecken

Hakparenteser används för att beteckna grupper av accepterade tecken. Uttrycket [aeio] skulle t.ex. matcha alla strängar som innehåller något av tecknen a, e, i eller o.

Ett bindestreck är också tillåtet för att matcha intervall av tecken. Uttrycket [0-68a-d] skulle till exempel matcha alla strängar som innehåller en siffra mellan 0 och 6, eller en åtta, eller ett tecken mellan a och d. I den här notationen är alla intervall inkluderande.

Om du kombinerar två uppsättningar parenteser kan du matcha två tecken i följd. Till exempel skulle uttrycket [1-3][0-9] matcha alla tvåsiffriga tal mellan 10 och 39, inklusive.

Ett exempel med testprogrammet:

Ange uttryck: [C-FRSÖ]\nAnge sträng: C\nHittad!\nAnge sträng: E\nHittad!\nAnge sträng: G\nHittades inte.\nAnge sträng: R\nHittad!\nAnge sträng: Ö\nHittad!\nAnge sträng: T\nHittades inte.

Upprepade matchningar

Varje del av ett uttryck kan upprepas med följande operatorer:

    \n
  • * upprepas hur många gånger som helst, inklusive noll
  • \n
  • + upprepas hur många gånger som helst, men minst en gång
  • \n
  • {m} upprepas exakt m gånger
  • \n

Dessa operatorer fungerar på den del av uttrycket som kommer omedelbart före operatorn. Uttrycket ba+b skulle t.ex. matcha delsträngarna bab, baab och baaaaaaaaaaab, bland andra. Uttrycket A[BCDE]*Z skulle matcha delsträngarna AZ, ADZ eller ABCDEBCDEBCDEZ, bland andra.

Ett exempel med testprogrammet:

Ange uttryck: 1[234]*5\nAnge sträng: 15\nHittad!\nAnge sträng: 125\nHittad!\nAnge sträng: 145\nHittad!\nAnge sträng: 12342345\nHittad!\nAnge sträng: 126\nHittades inte.\nAnge sträng: 165\nHittades inte.

Andra specialtecken

En punkt är ett jokertecken som kan matcha vilket enskilt tecken som helst. Uttrycket c...o skulle till exempel matcha alla delsträngar med fem tecken som börjar med ett c och slutar med ett o, till exempel c-3po eller cello.

Tecknet ^ anger att matchningen måste ske i början av strängen och $ anger att matchningen måste ske i slutet av strängen. Dessa tecken kan också användas för att utesluta andra tecken än de angivna från matchningen:

Ange uttryck: ^[123]*$\nAnge sträng: 4\nHittades inte.\nAnge sträng: 1221\nHittad!\nAnge sträng: 333333333\nHittad!

Ibland behöver du matcha för specialtecken som är reserverade för syntaxen för reguljära uttryck. Omvänt snedstreck (eng. backslash) \\ kan användas för att undkomma specialtecken. Uttrycket 1+ matchar alltså ett eller flera tal 1, men uttrycket 1\\+ matchar strängen 1+.

Ange uttryck: ^\\*\nAnge sträng: hej*\nHittades inte.\nAnge sträng: h*e*j\nHittades inte.\nAnge sträng: *hej\nHittad!

Runda parenteser kan användas för att gruppera ihop olika delar av uttrycket. Till exempel skulle uttrycket (ab)+c matcha delsträngarna abc, ababc och abababababababc, men inte strängarna ac eller bc, eftersom hela delsträngen ab måste förekomma minst en gång.

Ange uttryck: ^(jabba).*(hut)$\nAnge sträng: jabba the hut\nHittad!\nAnge sträng: jabba a hut\nHittad!\nAnge sträng: jarmo the hut\nHittades inte.\nAnge sträng: jabba the smut\nHittades inte.

Här följer några övningar för att bekanta dig med syntaxen för reguljära uttryck.

Veckodagar

Använd ett reguljärt uttryck för att skapa en funktion med namnet ar_veckodag(strang: str). Funktionen ska returnera True om strängen som skickas som argument innehåller en förkortning av en veckodag (mån, tis, ons, tors, fre, lör, sön).

Exempel på hur funktionen ska fungera

print(ar_veckodag(\"mån\"))\nprint(ar_veckodag(\"fre\"))\nprint(ar_veckodag(\"turs\"))

True\nTrue\nFalse

Vokalcheck

Skapa en funktion med namnet alla_vokaler(strang: str) som använder ett reguljärt uttryck för att kontrollera om alla tecken i den givna strängen är vokaler.

Exempel på hur funktionen ska fungera:

print(alla_vokaler(\"eioueioieoieouyyyy\"))\nprint(alla_vokaler(\"biiiiil\"))

True\nFalse

Klockans tid

Skapa funktionen klocktid(strang: str), som använder ett reguljärt uttryck för att granska ifall en sträng av formatet tt:mm:ss är en giltig tid i ett 24-timmars format, med två siffror var för timmar, minuter och sekunder.

Exempel på hur funktionen ska fungera:

print(klocktid(\"12:43:01\"))\nprint(klocktid(\"AB:01:CD\"))\nprint(klocktid(\"17:59:59\"))\nprint(klocktid(\"33:66:77\"))

True\nFalse\nTrue\nFalse

Den stora finalen

Som avslutning på denna del av materialet ska vi arbeta lite mer med objekt och klasser genom att bygga ett lite mer omfattande program. Denna övning innefattar inte nödvändigtvis reguljära uttryck, men avsnitten om Funktioner som argument och list comprehension kommer sannolikt att vara användbara.

Du kan också ha nytta av de exempel som finns i modul 10.

I den här övningen kommer du att bygga en applikation för att undersöka hockeyligastatistik från NHL på ett par olika sätt.

Övningsmallen innehåller två JSON-filer: del.json och alla.json. Den första av dessa är mest avsedd för testning. Den senare innehåller en hel del data, eftersom all NHL-spelarstatistik för säsongen 2019-20 ingår i filen.

Inlägget för en enskild spelare är i följande format:

{\n    \"name\": \"Patrik Laine\",\n    \"nationality\": \"FIN\",\n    \"assists\": 35,\n    \"goals\": 28,\n    \"penalties\": 22,\n    \"team\": \"WPG\",\n    \"games\": 68\n},

Båda filerna innehållar en lista av inlägg enligt ovanstående format.

Ifall du behöver en uppfriskare när det gäller hantering av JOSN-filer, kan du ta en titt på modul 7 i kursmaterialet.

Sök och lista

Skapa en interaktiv applikation som först frågar efter namnet på filen och sedan erbjuder följande funktioner:

    \n
  • sök enligt namn för en enskild spelares statistik
  • \n
  • lista alla förkortningar för lagnamn i alfabetisk ordning
  • \n
  • lista alla förkortningar för länder i alfabetisk ordning
  • \n

Dessa funktioner ger dig totalt ett övningspoäng. Din applikation ska nu fungera enligt följande:

fil: del.json\nläste 14 spelares data

instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål

instruktion: 1\nnamn: Travis Zajac

\nTravis Zajac         NJD   9 + 16 =  25\n

instruktion: 2\nBUF\nCGY\nDAL\nNJD\nNYI\nOTT\nPIT\nWPG\nWSH

instruktion: 3\nCAN\nCHE\nCZE\nSWE\nUSA

instruktion: 0

Obs: Utskriftens format för en spelare måste vara exakt enligt följande:

\nLeon Draisaitl       EDM  43 + 67 = 110\nConnor McDavid       EDM  34 + 63 =  97\nTravis Zajac         NJD   9 + 16 =  25\nMike Green           EDM   3 +  8 =  11\nMarkus Granlund      EDM   3 +  1 =   4\n123456789012345678901234567890123456789\n

Den sista raden i exemplet ovan är till för att hjälpa dig att beräkna bredden på de olika fälten i utskriften; du ska inte skriva ut nummerraden själv i din slutliga inlämning.

Förkortningen för teamet skrivs ut från det 22:a tecknet och framåt. +-tecknet är det 30:e tecknet och =-tecknet är det 35:e tecknet. Alla fält ska vara justifierade till högerkanten. Alla blanksteg är mellanslagstecken.

F-strängar är förmodligen det enklaste sättet att uppnå den önskade utskriften. Processen är liknande som övningen Kursresultat del 3 från modul 6.

Lista spelare enligt poäng

Följande funktionalitet ger dig ditt andra övningspoäng:

    \n
  • lista spelare i ett specifikt lag enligt ordningen flest poäng, från högst till lägst. Poäng är mängden mål + assistanser
  • \n
  • lista spelare från ett specifikt land enligt ordningen flest poäng, från högst till lägst
  • \n

Din applikation ska nu fungera enligt följande:

fil: del.json\nläste 14 spelares data

instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål

instruktion: 4\nlag: OTT

\nDrake Batherson      OTT   3 +  7 =  10\nJonathan Davidsson   OTT   0 +  1 =   1\n

instruktion: 5\nland: CAN

\nJared McCann         PIT  14 + 21 =  35\nTravis Zajac         NJD   9 + 16 =  25\nTaylor Fedun         DAL   2 +  7 =   9\nMark Jankowski       CGY   5 +  2 =   7\nLogan Shaw           WPG   3 +  2 =   5\n

instruktion: 0

Mest framgångsrika spelare

Det tredje övningspoänget får du från följande två funktionaliteter:

    \n
  • lista av n mängd spelare som fått flest poäng\n
      \n
    • ifall två spelare har samma mängd poäng, ska den som har flera mål komma först
    • \n
    \n
  • \n
  • lista av n mängd spelare som har gjort flest mål\n
      \n
    • ifall två spelare har samma mängd mål, ska den som spelat färre spel komma först
    • \n
    \n
  • \n

Applikationen ska nu fungera enligt följande:

fil: del.json\nläste 14 spelares data

instruktioner:\n0 avsluta\n1 sök spelare\n2 lag\n3 länder\n4 lagets spelare\n5 landets spelare\n6 flest poäng\n7 flest mål

instruktion: 6\nhur många: 2

\nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\n

instruktion: 6\nhur många: 5

\nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\nJohn Klingberg       DAL   6 + 26 =  32\nTravis Zajac         NJD   9 + 16 =  25\nConor Sheary         BUF  10 + 13 =  23\n

instruktion: 7\nhur många: 6

\nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\nConor Sheary         BUF  10 + 13 =  23\nTravis Zajac         NJD   9 + 16 =  25\nJohn Klingberg       DAL   6 + 26 =  32\nMark Jankowski       CGY   5 +  2 =   7\n

instruktion: 0

Svara till sist på en snabb enkät:

","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/4-reguljara-uttryck.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-12/page-data.json b/page-data/osa-12/page-data.json index 42438b207..20874f765 100644 --- a/page-data/osa-12/page-data.json +++ b/page-data/osa-12/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-12", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-12","title":"Modul 12"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-12","title":"Modul 12"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-12/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-13/1-pygame/page-data.json b/page-data/osa-13/1-pygame/page-data.json index f831ace23..59f9df441 100644 --- a/page-data/osa-13/1-pygame/page-data.json +++ b/page-data/osa-13/1-pygame/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-13/1-pygame", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Har du installerat pygame-biblioteket på din dator"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur man skapar ett pygame-fönster och hur man avslutar ett program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda en bild som lagras i en fil i ett pygame-fönster"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I de här två sista delarna av kursmaterialet kommer vi att bekanta oss med pygame-biblioteket. Det är ett Python-bibliotek för programmering av spel. Det hjälper dig att skapa grafiska element, hantera händelser från tangentbordet och musen samt implementera andra funktioner som är nödvändiga i spel."}]},{"type":"element","tagName":"h2","properties":{"id":"att-installera-pygame","style":"position:relative;"},"children":[{"type":"text","value":"Att installera pygame"},{"type":"element","tagName":"a","properties":{"href":"#att-installera-pygame","ariaLabel":"att installera pygame permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"linux","style":"position:relative;"},"children":[{"type":"text","value":"Linux"},{"type":"element","tagName":"a","properties":{"href":"#linux","ariaLabel":"linux permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Öppna en instruktionsrad, skriv in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]},{"type":"text","value":" och tryck på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 399px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 60.434782608695656%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAyElEQVQoz+2Oy26CYBSE/5XXxssTKAWqUQSixmorKhEEfgQv7/8un8FqorELFm5MXHyZyZnJ5IhEBgSeyy4Oz5rGIZG/JokC9lvJerVgn0gOaYwMPOLQ57jbMp994/w8IrzuL5G5YtNzWKoTpOkSGkuc1oh07HOcRrj6FLOmYje/sBr6n9a1fxFWVWNY62B/6FgVDbuqk93Mssqo3j1nme8X2hhF5cKtv0cYJYV+VigpD2T3azYof+ZC5C3mHc09+PQP34MvPHgC7r1OntPJWSAAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a92634cb53af0aa1734e9ca12ca79562/a0b58/pygame_linux.webp 230w","/static/a92634cb53af0aa1734e9ca12ca79562/66086/pygame_linux.webp 399w"],"sizes":"(max-width: 399px) 100vw, 399px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a92634cb53af0aa1734e9ca12ca79562/81c8e/pygame_linux.png 230w","/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png 399w"],"sizes":"(max-width: 399px) 100vw, 399px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png","alt":"pygame linux","title":"pygame linux","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde installera pygame-biblioteket på din dator."}]},{"type":"element","tagName":"h3","properties":{"id":"windows","style":"position:relative;"},"children":[{"type":"text","value":"Windows"},{"type":"element","tagName":"a","properties":{"href":"#windows","ariaLabel":"windows permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Öppna Windows-terminalen genom att öppna menyn, skriva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cmd"}]},{"type":"text","value":" och trycka på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 392px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 173.47826086956525%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAjCAIAAAAblL1PAAAACXBIWXMAAA7DAAAOwwHHb6hkAAADp0lEQVRIx+2VT0gjVxzHp0g1kwlqNEmJqUTblB6EPewaEKGlINKlhW5Peyhb2GNPwkIPhUJvPRVakP6hQr0UlvoHm6yaSfaSVTdqEpdQt8ZsTGidxJ3JG2cmk0ky+fPevDKZVUrQrnruly+PuXze7/d+v/d7Q9hsNovFEgwGYUu1Wq3RaGiaJsuypmn4P0W43e6BgYHl5eVMJoMQKhQKsixDCBOJBITwPKxeryOEiJ6ens7OziBNY4xVVW02GkjTjJjay0QMDg66XK7FhYVcLndwcPB0d1dRFIPELxPhdDrtNtt3vz7YFfBGVt7IlsJpMZyWwmnxUVoKPxPXs0rk7+qZ1tOmKOqLb2fffW/i3tc/LCT4n0N/zDx8OvPwz9lw6qdA4n40v5yq+PZK/qTSZsJqtVos1Jff//bVjO/HQOJ+7PncE/AgVfUly/79si9Z9iWV3/dKZ1qHzaRpdiHAIfzsuL73vLLHVfdB7dQpUD/PLdhspldX/soe8IXCBUv1omBWq5UkyUBAb5VcKgHAHzJMVVUxxlBDSNNOrWm4zTpsMpGhUBBjLAgCy7KpVKpcLl8kBaNglvn5+XQ6I4kSAEBRlGKxCAAoFAo8zx8fH7Msq7ZyadtOhymKWlxcPDo6KhaLHMcBAGRZFgQBtCSKIsdxjUbj3Mhzc3PJ5L4sy7lcDgDAMAzLshzHGVvwPM+yLMMw+Xz+3xf+ReSlpSUAQLlcBgBIkiQIgrEqiiJJUrFYNHIRBAEh1F7t1dVVURQzmUw+n89mszzPX7RgJEnSNN1sNhVFqVQqqqrWarXmidomycAghPpUnfQ5YAwzwzDValU8kSAIZ8avVCr6PLf6bAqFQsZ++DL6H74s3NfXR5LkFWHj6b0i7Ha7nU4n3Xq3Lw17PB632x0IBK4C9/b2Xv3MY2NjXq/X7/efXvdLwA6Hg6KolZUVjHGtVoMQ1i8svVUGXK/XeZ4/PDxUVRUhpGGMEEIQYg1BCJGm6StCuPUBNQQRJCiK6ujomJ6ejsVi4XCYpulIJPJkJ76x9mh7OxqJxdY2t3biO4/XH29H41ubW+vrkXg8Hl3bjG1FCa/XOzk5SRD677Krq2t4+I3B113Eq+bR21P9Dvv4a92fjnsIgrj74biru+v66Jt3PnnnFYKw3r1mfqufGBoaGhkZMZlMDofDbDbb7HZ7v9Uy4Jn65pebH30wcX3is1t3eoff/vjzqWs3bozevHV76l5vn936vqd7yP4P6JyrnQ25/+oAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/76954afbb36ac5953d205b7cdab98ff6/a0b58/13_1_1.webp 230w","/static/76954afbb36ac5953d205b7cdab98ff6/86099/13_1_1.webp 392w"],"sizes":"(max-width: 392px) 100vw, 392px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/76954afbb36ac5953d205b7cdab98ff6/81c8e/13_1_1.png 230w","/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png 392w"],"sizes":"(max-width: 392px) 100vw, 392px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png","alt":"13 1 1","title":"13 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fönstret för kommandoradstolken borde öppnas. Skriv in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]},{"type":"text","value":" och tryck på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde installera pygame-biblioteket på din dator."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Installationen kan kräva systemadministratörsbehörighet. Om ovanstående inte fungerar kan du prova på att köra terminalprogrammet som administratör: öppna Windows-menyn, leta reda på CMD-programmet, högerklicka på det och välj \"Kör som administratör\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att installera och få åtkomst till pygame krävs att din Python-installation läggs till i sökvägen, enligt "},{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/fi/installation/vscode#python3","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"instruktionerna"}]},{"type":"text","value":" här."}]},{"type":"element","tagName":"h3","properties":{"id":"mac","style":"position:relative;"},"children":[{"type":"text","value":"Mac"},{"type":"element","tagName":"a","properties":{"href":"#mac","ariaLabel":"mac permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Öppna Terminalen, t.ex. genom förstoringsglas-symbolen i det övre högra hörnet:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 746px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 24.347826086956523%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAABGUlEQVQY02OYs2Rje9/sSTOWzlmycdKsZVPmrJg6Z+WC5Zt7py7s6J/dO2nehDnLW6bOb+mftWD55oUrN/VPXVNRPW3SzEVzlqxn0Lf207Hw1rPyhTF89Kx99W38tM08VE3dZE2c7S18z7gX99gnqll7G9n6alt6q5u561l56lv7MLCIaLGIaLGKaLMIQ0kWYU1GIQ1GYU1JMf1QKbs6OY9OeW8DcVMGQTUWIQ1mYQ1WEU0mIQ1mQXUGPnkzEJIzhSBeOVMeWRNZOQtHRYdAZRdzBTt+OVMGaQMOWWOQrKwJCMlASGMGfgVzfjlTXhkjXlljXhlDHil9PmkDNRlTKRkTNlljVjkTPnkzIXlzPnlzXpDRUJvAbFMAjAFKS35RLdoAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/77ceacb61dfaf80c7c442a4225b65e87/a0b58/13-1-2.webp 230w","/static/77ceacb61dfaf80c7c442a4225b65e87/bc10c/13-1-2.webp 460w","/static/77ceacb61dfaf80c7c442a4225b65e87/f7ebd/13-1-2.webp 746w"],"sizes":"(max-width: 746px) 100vw, 746px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/77ceacb61dfaf80c7c442a4225b65e87/81c8e/13-1-2.png 230w","/static/77ceacb61dfaf80c7c442a4225b65e87/08a84/13-1-2.png 460w","/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png 746w"],"sizes":"(max-width: 746px) 100vw, 746px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png","alt":"13 1 2","title":"13 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sökverktyget borde öppna. Skriv in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"terminal"}]},{"type":"text","value":" och tryck "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a310ec5ccf9438989514ad8da107dcc3/47218/13-1-3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 66.95652173913044%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAABYlAAAWJQFJUiTwAAACa0lEQVQozy2STXLTQBCFdYFwgzh3IlmFDeQGkAQ2LKGo4hBAQhWp7FglQMohln8iO7Yly7JkzWime370M5LsACeg5KTq23W97ur3njX3poNup2t3Bv0+53z9+aL63ilGcx1zlRZK50pnSmdCapQKUQHKvCi/nZ0PnKFlDyZt22nbo+vu3cQnvhP4HvEXHEArnUn1qASUHAQHAahUmg+c0cwPrIOP3dcn+PbcvDw1h6fF8Vn55swcfjVOkJs8lypXOi9MvVr/Xf/5V5Q1CiVUWlYrqVLr99XNz+vBzdB3PDKeM2/BZyF4IeeodZoJmZqy/vTl5PmLg929vXfvPxSm0UuVAUirb/fa1+3e7W0UkygmhFCaJJRSFFKoFIQq6/XR8fHOTuvJ1tbT3T1TrQGl0hmgsJxhMBxPnYnrBVFMgaFuAM1AMZCUYW7ql6+Otre3W63W/v6zLDeMI0rNAK1Ob2L3h+2b3tW17frhkkAYs3DJ4gQJEyRBDspx7i4vf11c/Oh0eknCCWEJw0UYW+4s6N+OnOHE88NFRBdLuohoEBHKxcNxAIkgXW8+Hk8TyilhyyWlCUw93/LcmTube37QpJLmchOPUBnKdIMWQgMIShkhjDHgXHCOHCRNuEVRzfxgNJ66ng/YxCBUo0GhUahHUAmppUql1KIhlSprfg4TGS1JGMVhFLPGx2bQ3NyIYQMKHUZkPPFcz3+ojc7yxu04pjFJHiCUMY4gFEeVbKymDBgXwWLpDO96fcfu9jmI5i+ZMY6WTvMGnWmdZXluTFlWa1OtTFkXpipMWZgKmmKiaJaKsqrr1X29ui/L+j/LRoPjTcXpPgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a310ec5ccf9438989514ad8da107dcc3/a0b58/13-1-3.webp 230w","/static/a310ec5ccf9438989514ad8da107dcc3/bc10c/13-1-3.webp 460w","/static/a310ec5ccf9438989514ad8da107dcc3/966d8/13-1-3.webp 920w","/static/a310ec5ccf9438989514ad8da107dcc3/3dddb/13-1-3.webp 1344w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a310ec5ccf9438989514ad8da107dcc3/81c8e/13-1-3.png 230w","/static/a310ec5ccf9438989514ad8da107dcc3/08a84/13-1-3.png 460w","/static/a310ec5ccf9438989514ad8da107dcc3/c0255/13-1-3.png 920w","/static/a310ec5ccf9438989514ad8da107dcc3/47218/13-1-3.png 1344w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a310ec5ccf9438989514ad8da107dcc3/c0255/13-1-3.png","alt":"13 1 3","title":"13 1 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv in följande och tryck "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cc8d7c319bc7baf0232b2d92350f9631/d56e1/13-1-4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAiElEQVQY06XNwQoCIRSFYd//ZXoJ4dpUIpqEYRZii1y4cRai4xgMGEEboW954PAjcd6BYXd1OR0PhBDGGABgjAkhADBNe8651lpKKYSglHLOHx2yz9ftqp1z1lpjjFIqhDDPc4zRe19Kaa2tm9atHVpqXTa11pRSzrkNQ7/TJ/JdGz3/VR4/vwF1E1iIv4njxgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cc8d7c319bc7baf0232b2d92350f9631/a0b58/13-1-4.webp 230w","/static/cc8d7c319bc7baf0232b2d92350f9631/bc10c/13-1-4.webp 460w","/static/cc8d7c319bc7baf0232b2d92350f9631/966d8/13-1-4.webp 920w","/static/cc8d7c319bc7baf0232b2d92350f9631/bbe5b/13-1-4.webp 1130w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cc8d7c319bc7baf0232b2d92350f9631/81c8e/13-1-4.png 230w","/static/cc8d7c319bc7baf0232b2d92350f9631/08a84/13-1-4.png 460w","/static/cc8d7c319bc7baf0232b2d92350f9631/c0255/13-1-4.png 920w","/static/cc8d7c319bc7baf0232b2d92350f9631/d56e1/13-1-4.png 1130w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cc8d7c319bc7baf0232b2d92350f9631/c0255/13-1-4.png","alt":"13 1 4","title":"13 1 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde installera pygame-biblioteket på din dator."}]},{"type":"element","tagName":"h2","properties":{"id":"ditt-forsta-program","style":"position:relative;"},"children":[{"type":"text","value":"Ditt första program"},{"type":"element","tagName":"a","properties":{"href":"#ditt-forsta-program","ariaLabel":"ditt forsta program permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett enkelt program för att kontrollera att din pygame-installation fungerar korrekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När detta program körs borde det visa ett fönster:"}]},{"type":"element","tagName":"img","properties":{"src":"/d11704399cba1d2075b1135cef373b65/pygame_eka.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet visar endast ett fönster, och det körs tills användaren stänger fönstret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en närmare titt på de steg som krävs för att uppnå detta. Den första raden tar pygame-biblioteket i bruk: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import pygame"}]},{"type":"text","value":". Nästa instruktion, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.init"}]},{"type":"text","value":", initierar pygame-modulerna, och nästa skapar ett fönster med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.set_mode"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set_mode"}]},{"type":"text","value":" tar fönstrets dimensioner som ett argument. Tupeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(640, 480)"}]},{"type":"text","value":" anger att fönstret är 640 pixlar brett och 480 pixlar högt. Variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fonster"}]},{"type":"text","value":" kan senare användas för att komma åt fönstret, t.ex. för att rita något i det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De följande två instruktionerna gör just detta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fill"}]},{"type":"text","value":" fyller fönstret med den färg som anges som argument. I det här fallet är färgen svart, som skickas som ett RGB-värde i tupeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0, 0)"}]},{"type":"text","value":". Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":" uppdaterar innehållet i fönstret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter dessa initialiseringsinstruktioner börjar programmets huvudloop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Huvudloopen hanterar alla händelser som operativsystemet skickar till programmet. Vid varje iteration returnerar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.event.get"}]},{"type":"text","value":" en lista över alla händelser som har samlats in sedan föregående iteration."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan hanterar programmet endast händelser av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":". Denna händelse uppstår t.ex. genom att man klickar på exit-knappen i fönstrets hörn. Om händelsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":" utlöses avslutas programmet genom funktionen exit."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan prova och se vad som händer om ditt program inte hanterar händelsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":". Detta borde innebära att det inte händer någonting om man klickar på exit-knappen, vilket skulle vara förvirrande för användaren. Eftersom programmet körs från kommandoraden kan du fortfarande stoppa det från kommandoraden med Control+C."}]},{"type":"element","tagName":"h2","properties":{"id":"lagg-till-en-bild","style":"position:relative;"},"children":[{"type":"text","value":"Lägg till en bild"},{"type":"element","tagName":"a","properties":{"href":"#lagg-till-en-bild","ariaLabel":"lagg till en bild permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss lägga till en bild i fönstret:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet använder denna bild av en robot, som finns lagrad i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"img","properties":{"src":"robot.png"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":" måste finnas i samma katalog som källkoden för ditt program, annars kan programmet inte hitta den. I övningsmallarna för denna del väntar bilderna i övningskatalogen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fönstret borde nu se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/0f7db0536914ccd471841bcee4201632/pygame_kuva.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.image.load"}]},{"type":"text","value":" laddar in bilden i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":" och lagrar en referens till den i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot"}]},{"type":"text","value":". Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":" ritar bilden på platsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(100, 50)"}]},{"type":"text","value":", och funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":" uppdaterar fönstrets innehåll, som tidigare. Platsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(100, 50)"}]},{"type":"text","value":" innebär att bildens övre vänstra hörn befinner sig på den platsen i fönstret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I pygame ligger origo-punkten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0)"}]},{"type":"text","value":" i fönstrets övre vänstra hörn. X-koordinaterna ökar åt höger och y-koordinaterna ökar nedåt, så att det nedre högra hörnet har koordinaterna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(640, 480)"}]},{"type":"text","value":". Detta är tvärtemot hur koordinater brukar hanteras inom t.ex. matematiken, men det är ganska vanligt i programmeringssammanhang och värt att vänja sig vid."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du har laddat en bild kan du använda den många gånger i samma fönster. I följande kod ritas bilden av roboten på tre olika platser:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"300"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet borde vara att fönstret ser ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/a7807afa43ad1cc7d9686968e6838712/pygame_kuva2.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här sätter vi lokationen av bilden så, att den ligger i mitten av fönstret:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fönstret borde nu se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/498f521486feca567f74dd51c44e371b/pygame_kuva3.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_width"}]},{"type":"text","value":" ger bildens bredd och metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_height"}]},{"type":"text","value":" ger dess höjd, båda i pixlar. Fönstrets mittpunkt ligger på halva bredden och höjden, alltså på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(320, 240)"}]},{"type":"text","value":", vilket vi kan använda för att beräkna en lämplig plats för bildens övre vänstra hörn, så att det ligger exakt i mitten."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Pygame-övningar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningarna i den här modulen av kursen har inga automatiserade tester, eftersom resultaten verifieras visuellt. Testerna ger poäng automatiskt när du skickar in din lösning till servern, oavsett hur du har implementerat den. Skicka bara in din lösning när du är redo och din lösning matchar övningsbeskrivningen. Övningarna kanske inte har automatiska tester, men kurspersonalen kommer ändå att se din lösning. Att skicka in en ofullständig lösning till TMC Paste ger också poäng automatiskt, så det bör inte användas när du ber om hjälp med övningarna i den här modulen. Du kan använda "},{"type":"element","tagName":"a","properties":{"href":"https://pastebin.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pastebin.com"}]},{"type":"text","value":" eller någon annan pastebin-tjänst på internet när du ber om hjälp i kursens stödkanaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om din lösning helt klart inte stämmer överens med övningsbeskrivningen kan du förlora de poäng som du har fått för övningarna i den här modulen."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fyra robotar","tmcname":"osa13-01_fyra_robotar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som ritar en robot i vart och ett av de fyra hörnen av fönstret. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/8ba6d635c4ceb93c2b86711f83589b3a/pygame_nelja.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robotar i rad","tmcname":"osa13-02_robotar_i_rad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som ritar tio robotar i rad. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/8dc589e5970721e2bf8bfe2bd22fa6cf/pygame_rivi.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hundra robotar","tmcname":"osa13-03_hundra_robotar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Var snäll och skriv ett program som ritar hundra robotar: tio rader med tio robotar i varje rad. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/7012bfc1e6ade044e03c53f07f35e7a6/pygame_sata.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Slumpmässiga robotar","tmcname":"osa13-04_slumpmassiga_robotar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som ritar "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tusen"}]},{"type":"text","value":" robotar på slumpmässiga platser. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/61a30808255873a3e24df2fc559d9367/pygame_tuhat.gif"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Har du installerat pygame-biblioteket på din dator
  • \n
  • Vet du hur man skapar ett pygame-fönster och hur man avslutar ett program
  • \n
  • Kommer du att kunna använda en bild som lagras i en fil i ett pygame-fönster
  • \n

I de här två sista delarna av kursmaterialet kommer vi att bekanta oss med pygame-biblioteket. Det är ett Python-bibliotek för programmering av spel. Det hjälper dig att skapa grafiska element, hantera händelser från tangentbordet och musen samt implementera andra funktioner som är nödvändiga i spel.

Att installera pygame

Linux

Öppna en instruktionsrad, skriv in pip3 install pygame och tryck på enter.

\n \n \n \n \n \n \"13\n \n \n

Skriv in följande och tryck enter:

pip3 install pygame

\n \n \n \n \n \n \"13\n \n \n

Detta borde installera pygame-biblioteket på din dator.

Ditt första program

Här är ett enkelt program för att kontrollera att din pygame-installation fungerar korrekt:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nfonster.fill((0,0,0))\npygame.display.flip()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

När detta program körs borde det visa ett fönster:

Programmet visar endast ett fönster, och det körs tills användaren stänger fönstret.

Låt oss ta en närmare titt på de steg som krävs för att uppnå detta. Den första raden tar pygame-biblioteket i bruk: import pygame. Nästa instruktion, pygame.init, initierar pygame-modulerna, och nästa skapar ett fönster med funktionen pygame.display.set_mode.

pygame.init()\nfonster = pygame.display.set_mode((640, 480))

Funktionen set_mode tar fönstrets dimensioner som ett argument. Tupeln (640, 480) anger att fönstret är 640 pixlar brett och 480 pixlar högt. Variabelnamnet fonster kan senare användas för att komma åt fönstret, t.ex. för att rita något i det.

De följande två instruktionerna gör just detta:

fonster.fill((0, 0, 0))\npygame.display.flip()

Metoden fill fyller fönstret med den färg som anges som argument. I det här fallet är färgen svart, som skickas som ett RGB-värde i tupeln (0, 0, 0). Metoden pygame.display.flip uppdaterar innehållet i fönstret.

Efter dessa initialiseringsinstruktioner börjar programmets huvudloop:

while True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

Huvudloopen hanterar alla händelser som operativsystemet skickar till programmet. Vid varje iteration returnerar funktionen pygame.event.get en lista över alla händelser som har samlats in sedan föregående iteration.

I exemplet ovan hanterar programmet endast händelser av typen pygame.QUIT. Denna händelse uppstår t.ex. genom att man klickar på exit-knappen i fönstrets hörn. Om händelsen pygame.QUIT utlöses avslutas programmet genom funktionen exit.

Du kan prova och se vad som händer om ditt program inte hanterar händelsen pygame.QUIT. Detta borde innebära att det inte händer någonting om man klickar på exit-knappen, vilket skulle vara förvirrande för användaren. Eftersom programmet körs från kommandoraden kan du fortfarande stoppa det från kommandoraden med Control+C.

Lägg till en bild

Låt oss lägga till en bild i fönstret:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nfonster.fill((0, 0, 0))\nfonster.blit(robot, (100, 50))\npygame.display.flip()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

Programmet använder denna bild av en robot, som finns lagrad i filen robot.png:

Filen robot.png måste finnas i samma katalog som källkoden för ditt program, annars kan programmet inte hitta den. I övningsmallarna för denna del väntar bilderna i övningskatalogen.

Fönstret borde nu se ut så här:

Funktionen pygame.image.load laddar in bilden i filen robot.png och lagrar en referens till den i variabeln robot. Metoden blit ritar bilden på platsen (100, 50), och funktionen pygame.display.flip uppdaterar fönstrets innehåll, som tidigare. Platsen (100, 50) innebär att bildens övre vänstra hörn befinner sig på den platsen i fönstret.

I pygame ligger origo-punkten (0, 0) i fönstrets övre vänstra hörn. X-koordinaterna ökar åt höger och y-koordinaterna ökar nedåt, så att det nedre högra hörnet har koordinaterna (640, 480). Detta är tvärtemot hur koordinater brukar hanteras inom t.ex. matematiken, men det är ganska vanligt i programmeringssammanhang och värt att vänja sig vid.

När du har laddat en bild kan du använda den många gånger i samma fönster. I följande kod ritas bilden av roboten på tre olika platser:

fonster.blit(robot, (0, 0))\nfonster.blit(robot, (300, 0))\nfonster.blit(robot, (100, 200))

Resultatet borde vara att fönstret ser ut så här:

Här sätter vi lokationen av bilden så, att den ligger i mitten av fönstret:

bredd = robot.get_width()\nhojd = robot.get_height()\nfonster.blit(robot, (320-bredd/2, 240-hojd/2))

Fönstret borde nu se ut så här:

Metoden get_width ger bildens bredd och metoden get_height ger dess höjd, båda i pixlar. Fönstrets mittpunkt ligger på halva bredden och höjden, alltså på (320, 240), vilket vi kan använda för att beräkna en lämplig plats för bildens övre vänstra hörn, så att det ligger exakt i mitten.

Övningarna i den här modulen av kursen har inga automatiserade tester, eftersom resultaten verifieras visuellt. Testerna ger poäng automatiskt när du skickar in din lösning till servern, oavsett hur du har implementerat den. Skicka bara in din lösning när du är redo och din lösning matchar övningsbeskrivningen. Övningarna kanske inte har automatiska tester, men kurspersonalen kommer ändå att se din lösning. Att skicka in en ofullständig lösning till TMC Paste ger också poäng automatiskt, så det bör inte användas när du ber om hjälp med övningarna i den här modulen. Du kan använda Pastebin.com eller någon annan pastebin-tjänst på internet när du ber om hjälp i kursens stödkanaler.

Om din lösning helt klart inte stämmer överens med övningsbeskrivningen kan du förlora de poäng som du har fått för övningarna i den här modulen.

Skriv ett program som ritar en robot i vart och ett av de fyra hörnen av fönstret. Slutresultatet ska se ut så här:

Skriv ett program som ritar tio robotar i rad. Slutresultatet ska se ut så här:

Var snäll och skriv ett program som ritar hundra robotar: tio rader med tio robotar i varje rad. Slutresultatet ska se ut så här:

Skriv ett program som ritar tusen robotar på slumpmässiga platser. Slutresultatet ska se ut så här:

","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/1-pygame.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Har du installerat pygame-biblioteket på din dator"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur man skapar ett pygame-fönster och hur man avslutar ett program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda en bild som lagras i en fil i ett pygame-fönster"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I de här två sista delarna av kursmaterialet kommer vi att bekanta oss med pygame-biblioteket. Det är ett Python-bibliotek för programmering av spel. Det hjälper dig att skapa grafiska element, hantera händelser från tangentbordet och musen samt implementera andra funktioner som är nödvändiga i spel."}]},{"type":"element","tagName":"h2","properties":{"id":"att-installera-pygame","style":"position:relative;"},"children":[{"type":"text","value":"Att installera pygame"},{"type":"element","tagName":"a","properties":{"href":"#att-installera-pygame","ariaLabel":"att installera pygame permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"linux","style":"position:relative;"},"children":[{"type":"text","value":"Linux"},{"type":"element","tagName":"a","properties":{"href":"#linux","ariaLabel":"linux permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Öppna en instruktionsrad, skriv in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]},{"type":"text","value":" och tryck på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 399px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 60.434782608695656%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAyElEQVQoz+2Oy26CYBSE/5XXxssTKAWqUQSixmorKhEEfgQv7/8un8FqorELFm5MXHyZyZnJ5IhEBgSeyy4Oz5rGIZG/JokC9lvJerVgn0gOaYwMPOLQ57jbMp994/w8IrzuL5G5YtNzWKoTpOkSGkuc1oh07HOcRrj6FLOmYje/sBr6n9a1fxFWVWNY62B/6FgVDbuqk93Mssqo3j1nme8X2hhF5cKtv0cYJYV+VigpD2T3azYof+ZC5C3mHc09+PQP34MvPHgC7r1OntPJWSAAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a92634cb53af0aa1734e9ca12ca79562/a0b58/pygame_linux.webp 230w","/static/a92634cb53af0aa1734e9ca12ca79562/66086/pygame_linux.webp 399w"],"sizes":"(max-width: 399px) 100vw, 399px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a92634cb53af0aa1734e9ca12ca79562/81c8e/pygame_linux.png 230w","/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png 399w"],"sizes":"(max-width: 399px) 100vw, 399px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png","alt":"pygame linux","title":"pygame linux","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde installera pygame-biblioteket på din dator."}]},{"type":"element","tagName":"h3","properties":{"id":"windows","style":"position:relative;"},"children":[{"type":"text","value":"Windows"},{"type":"element","tagName":"a","properties":{"href":"#windows","ariaLabel":"windows permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Öppna Windows-terminalen genom att öppna menyn, skriva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cmd"}]},{"type":"text","value":" och trycka på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 392px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 173.47826086956525%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAjCAIAAAAblL1PAAAACXBIWXMAAA7DAAAOwwHHb6hkAAADp0lEQVRIx+2VT0gjVxzHp0g1kwlqNEmJqUTblB6EPewaEKGlINKlhW5Peyhb2GNPwkIPhUJvPRVakP6hQr0UlvoHm6yaSfaSVTdqEpdQt8ZsTGidxJ3JG2cmk0ky+fPevDKZVUrQrnruly+PuXze7/d+v/d7Q9hsNovFEgwGYUu1Wq3RaGiaJsuypmn4P0W43e6BgYHl5eVMJoMQKhQKsixDCBOJBITwPKxeryOEiJ6ens7OziBNY4xVVW02GkjTjJjay0QMDg66XK7FhYVcLndwcPB0d1dRFIPELxPhdDrtNtt3vz7YFfBGVt7IlsJpMZyWwmnxUVoKPxPXs0rk7+qZ1tOmKOqLb2fffW/i3tc/LCT4n0N/zDx8OvPwz9lw6qdA4n40v5yq+PZK/qTSZsJqtVos1Jff//bVjO/HQOJ+7PncE/AgVfUly/79si9Z9iWV3/dKZ1qHzaRpdiHAIfzsuL73vLLHVfdB7dQpUD/PLdhspldX/soe8IXCBUv1omBWq5UkyUBAb5VcKgHAHzJMVVUxxlBDSNNOrWm4zTpsMpGhUBBjLAgCy7KpVKpcLl8kBaNglvn5+XQ6I4kSAEBRlGKxCAAoFAo8zx8fH7Msq7ZyadtOhymKWlxcPDo6KhaLHMcBAGRZFgQBtCSKIsdxjUbj3Mhzc3PJ5L4sy7lcDgDAMAzLshzHGVvwPM+yLMMw+Xz+3xf+ReSlpSUAQLlcBgBIkiQIgrEqiiJJUrFYNHIRBAEh1F7t1dVVURQzmUw+n89mszzPX7RgJEnSNN1sNhVFqVQqqqrWarXmidomycAghPpUnfQ5YAwzwzDValU8kSAIZ8avVCr6PLf6bAqFQsZ++DL6H74s3NfXR5LkFWHj6b0i7Ha7nU4n3Xq3Lw17PB632x0IBK4C9/b2Xv3MY2NjXq/X7/efXvdLwA6Hg6KolZUVjHGtVoMQ1i8svVUGXK/XeZ4/PDxUVRUhpGGMEEIQYg1BCJGm6StCuPUBNQQRJCiK6ujomJ6ejsVi4XCYpulIJPJkJ76x9mh7OxqJxdY2t3biO4/XH29H41ubW+vrkXg8Hl3bjG1FCa/XOzk5SRD677Krq2t4+I3B113Eq+bR21P9Dvv4a92fjnsIgrj74biru+v66Jt3PnnnFYKw3r1mfqufGBoaGhkZMZlMDofDbDbb7HZ7v9Uy4Jn65pebH30wcX3is1t3eoff/vjzqWs3bozevHV76l5vn936vqd7yP4P6JyrnQ25/+oAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/76954afbb36ac5953d205b7cdab98ff6/a0b58/13_1_1.webp 230w","/static/76954afbb36ac5953d205b7cdab98ff6/86099/13_1_1.webp 392w"],"sizes":"(max-width: 392px) 100vw, 392px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/76954afbb36ac5953d205b7cdab98ff6/81c8e/13_1_1.png 230w","/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png 392w"],"sizes":"(max-width: 392px) 100vw, 392px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png","alt":"13 1 1","title":"13 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fönstret för kommandoradstolken borde öppnas. Skriv in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]},{"type":"text","value":" och tryck på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde installera pygame-biblioteket på din dator."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Installationen kan kräva systemadministratörsbehörighet. Om ovanstående inte fungerar kan du prova på att köra terminalprogrammet som administratör: öppna Windows-menyn, leta reda på CMD-programmet, högerklicka på det och välj \"Kör som administratör\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att installera och få åtkomst till pygame krävs att din Python-installation läggs till i sökvägen, enligt "},{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/fi/installation/vscode#python3","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"instruktionerna"}]},{"type":"text","value":" här."}]},{"type":"element","tagName":"h3","properties":{"id":"mac","style":"position:relative;"},"children":[{"type":"text","value":"Mac"},{"type":"element","tagName":"a","properties":{"href":"#mac","ariaLabel":"mac permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Öppna Terminalen, t.ex. genom förstoringsglas-symbolen i det övre högra hörnet:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 746px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 24.347826086956523%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAABGUlEQVQY02OYs2Rje9/sSTOWzlmycdKsZVPmrJg6Z+WC5Zt7py7s6J/dO2nehDnLW6bOb+mftWD55oUrN/VPXVNRPW3SzEVzlqxn0Lf207Hw1rPyhTF89Kx99W38tM08VE3dZE2c7S18z7gX99gnqll7G9n6alt6q5u561l56lv7MLCIaLGIaLGKaLMIQ0kWYU1GIQ1GYU1JMf1QKbs6OY9OeW8DcVMGQTUWIQ1mYQ1WEU0mIQ1mQXUGPnkzEJIzhSBeOVMeWRNZOQtHRYdAZRdzBTt+OVMGaQMOWWOQrKwJCMlASGMGfgVzfjlTXhkjXlljXhlDHil9PmkDNRlTKRkTNlljVjkTPnkzIXlzPnlzXpDRUJvAbFMAjAFKS35RLdoAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/77ceacb61dfaf80c7c442a4225b65e87/a0b58/13-1-2.webp 230w","/static/77ceacb61dfaf80c7c442a4225b65e87/bc10c/13-1-2.webp 460w","/static/77ceacb61dfaf80c7c442a4225b65e87/f7ebd/13-1-2.webp 746w"],"sizes":"(max-width: 746px) 100vw, 746px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/77ceacb61dfaf80c7c442a4225b65e87/81c8e/13-1-2.png 230w","/static/77ceacb61dfaf80c7c442a4225b65e87/08a84/13-1-2.png 460w","/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png 746w"],"sizes":"(max-width: 746px) 100vw, 746px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png","alt":"13 1 2","title":"13 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sökverktyget borde öppna. Skriv in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"terminal"}]},{"type":"text","value":" och tryck "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a310ec5ccf9438989514ad8da107dcc3/47218/13-1-3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 66.95652173913044%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAABYlAAAWJQFJUiTwAAACa0lEQVQozy2STXLTQBCFdYFwgzh3IlmFDeQGkAQ2LKGo4hBAQhWp7FglQMohln8iO7Yly7JkzWime370M5LsACeg5KTq23W97ur3njX3poNup2t3Bv0+53z9+aL63ilGcx1zlRZK50pnSmdCapQKUQHKvCi/nZ0PnKFlDyZt22nbo+vu3cQnvhP4HvEXHEArnUn1qASUHAQHAahUmg+c0cwPrIOP3dcn+PbcvDw1h6fF8Vn55swcfjVOkJs8lypXOi9MvVr/Xf/5V5Q1CiVUWlYrqVLr99XNz+vBzdB3PDKeM2/BZyF4IeeodZoJmZqy/vTl5PmLg929vXfvPxSm0UuVAUirb/fa1+3e7W0UkygmhFCaJJRSFFKoFIQq6/XR8fHOTuvJ1tbT3T1TrQGl0hmgsJxhMBxPnYnrBVFMgaFuAM1AMZCUYW7ql6+Otre3W63W/v6zLDeMI0rNAK1Ob2L3h+2b3tW17frhkkAYs3DJ4gQJEyRBDspx7i4vf11c/Oh0eknCCWEJw0UYW+4s6N+OnOHE88NFRBdLuohoEBHKxcNxAIkgXW8+Hk8TyilhyyWlCUw93/LcmTube37QpJLmchOPUBnKdIMWQgMIShkhjDHgXHCOHCRNuEVRzfxgNJ66ng/YxCBUo0GhUahHUAmppUql1KIhlSprfg4TGS1JGMVhFLPGx2bQ3NyIYQMKHUZkPPFcz3+ojc7yxu04pjFJHiCUMY4gFEeVbKymDBgXwWLpDO96fcfu9jmI5i+ZMY6WTvMGnWmdZXluTFlWa1OtTFkXpipMWZgKmmKiaJaKsqrr1X29ui/L+j/LRoPjTcXpPgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a310ec5ccf9438989514ad8da107dcc3/a0b58/13-1-3.webp 230w","/static/a310ec5ccf9438989514ad8da107dcc3/bc10c/13-1-3.webp 460w","/static/a310ec5ccf9438989514ad8da107dcc3/966d8/13-1-3.webp 920w","/static/a310ec5ccf9438989514ad8da107dcc3/3dddb/13-1-3.webp 1344w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a310ec5ccf9438989514ad8da107dcc3/81c8e/13-1-3.png 230w","/static/a310ec5ccf9438989514ad8da107dcc3/08a84/13-1-3.png 460w","/static/a310ec5ccf9438989514ad8da107dcc3/c0255/13-1-3.png 920w","/static/a310ec5ccf9438989514ad8da107dcc3/47218/13-1-3.png 1344w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a310ec5ccf9438989514ad8da107dcc3/c0255/13-1-3.png","alt":"13 1 3","title":"13 1 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv in följande och tryck "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cc8d7c319bc7baf0232b2d92350f9631/d56e1/13-1-4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAiElEQVQY06XNwQoCIRSFYd//ZXoJ4dpUIpqEYRZii1y4cRai4xgMGEEboW954PAjcd6BYXd1OR0PhBDGGABgjAkhADBNe8651lpKKYSglHLOHx2yz9ftqp1z1lpjjFIqhDDPc4zRe19Kaa2tm9atHVpqXTa11pRSzrkNQ7/TJ/JdGz3/VR4/vwF1E1iIv4njxgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cc8d7c319bc7baf0232b2d92350f9631/a0b58/13-1-4.webp 230w","/static/cc8d7c319bc7baf0232b2d92350f9631/bc10c/13-1-4.webp 460w","/static/cc8d7c319bc7baf0232b2d92350f9631/966d8/13-1-4.webp 920w","/static/cc8d7c319bc7baf0232b2d92350f9631/bbe5b/13-1-4.webp 1130w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cc8d7c319bc7baf0232b2d92350f9631/81c8e/13-1-4.png 230w","/static/cc8d7c319bc7baf0232b2d92350f9631/08a84/13-1-4.png 460w","/static/cc8d7c319bc7baf0232b2d92350f9631/c0255/13-1-4.png 920w","/static/cc8d7c319bc7baf0232b2d92350f9631/d56e1/13-1-4.png 1130w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cc8d7c319bc7baf0232b2d92350f9631/c0255/13-1-4.png","alt":"13 1 4","title":"13 1 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde installera pygame-biblioteket på din dator."}]},{"type":"element","tagName":"h2","properties":{"id":"ditt-forsta-program","style":"position:relative;"},"children":[{"type":"text","value":"Ditt första program"},{"type":"element","tagName":"a","properties":{"href":"#ditt-forsta-program","ariaLabel":"ditt forsta program permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett enkelt program för att kontrollera att din pygame-installation fungerar korrekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När detta program körs borde det visa ett fönster:"}]},{"type":"element","tagName":"img","properties":{"src":"/d11704399cba1d2075b1135cef373b65/pygame_eka.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet visar endast ett fönster, och det körs tills användaren stänger fönstret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en närmare titt på de steg som krävs för att uppnå detta. Den första raden tar pygame-biblioteket i bruk: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import pygame"}]},{"type":"text","value":". Nästa instruktion, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.init"}]},{"type":"text","value":", initierar pygame-modulerna, och nästa skapar ett fönster med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.set_mode"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set_mode"}]},{"type":"text","value":" tar fönstrets dimensioner som ett argument. Tupeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(640, 480)"}]},{"type":"text","value":" anger att fönstret är 640 pixlar brett och 480 pixlar högt. Variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fonster"}]},{"type":"text","value":" kan senare användas för att komma åt fönstret, t.ex. för att rita något i det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De följande två instruktionerna gör just detta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fill"}]},{"type":"text","value":" fyller fönstret med den färg som anges som argument. I det här fallet är färgen svart, som skickas som ett RGB-värde i tupeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0, 0)"}]},{"type":"text","value":". Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":" uppdaterar innehållet i fönstret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter dessa initialiseringsinstruktioner börjar programmets huvudloop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Huvudloopen hanterar alla händelser som operativsystemet skickar till programmet. Vid varje iteration returnerar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.event.get"}]},{"type":"text","value":" en lista över alla händelser som har samlats in sedan föregående iteration."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan hanterar programmet endast händelser av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":". Denna händelse uppstår t.ex. genom att man klickar på exit-knappen i fönstrets hörn. Om händelsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":" utlöses avslutas programmet genom funktionen exit."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan prova och se vad som händer om ditt program inte hanterar händelsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":". Detta borde innebära att det inte händer någonting om man klickar på exit-knappen, vilket skulle vara förvirrande för användaren. Eftersom programmet körs från kommandoraden kan du fortfarande stoppa det från kommandoraden med Control+C."}]},{"type":"element","tagName":"h2","properties":{"id":"lagg-till-en-bild","style":"position:relative;"},"children":[{"type":"text","value":"Lägg till en bild"},{"type":"element","tagName":"a","properties":{"href":"#lagg-till-en-bild","ariaLabel":"lagg till en bild permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss lägga till en bild i fönstret:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet använder denna bild av en robot, som finns lagrad i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"img","properties":{"src":"robot.png"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":" måste finnas i samma katalog som källkoden för ditt program, annars kan programmet inte hitta den. I övningsmallarna för denna del väntar bilderna i övningskatalogen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fönstret borde nu se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/0f7db0536914ccd471841bcee4201632/pygame_kuva.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.image.load"}]},{"type":"text","value":" laddar in bilden i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":" och lagrar en referens till den i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot"}]},{"type":"text","value":". Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":" ritar bilden på platsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(100, 50)"}]},{"type":"text","value":", och funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":" uppdaterar fönstrets innehåll, som tidigare. Platsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(100, 50)"}]},{"type":"text","value":" innebär att bildens övre vänstra hörn befinner sig på den platsen i fönstret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I pygame ligger origo-punkten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0)"}]},{"type":"text","value":" i fönstrets övre vänstra hörn. X-koordinaterna ökar åt höger och y-koordinaterna ökar nedåt, så att det nedre högra hörnet har koordinaterna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(640, 480)"}]},{"type":"text","value":". Detta är tvärtemot hur koordinater brukar hanteras inom t.ex. matematiken, men det är ganska vanligt i programmeringssammanhang och värt att vänja sig vid."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du har laddat en bild kan du använda den många gånger i samma fönster. I följande kod ritas bilden av roboten på tre olika platser:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"300"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Resultatet borde vara att fönstret ser ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/a7807afa43ad1cc7d9686968e6838712/pygame_kuva2.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här sätter vi lokationen av bilden så, att den ligger i mitten av fönstret:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fönstret borde nu se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/498f521486feca567f74dd51c44e371b/pygame_kuva3.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_width"}]},{"type":"text","value":" ger bildens bredd och metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_height"}]},{"type":"text","value":" ger dess höjd, båda i pixlar. Fönstrets mittpunkt ligger på halva bredden och höjden, alltså på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(320, 240)"}]},{"type":"text","value":", vilket vi kan använda för att beräkna en lämplig plats för bildens övre vänstra hörn, så att det ligger exakt i mitten."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Pygame-övningar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningarna i den här modulen av kursen har inga automatiserade tester, eftersom resultaten verifieras visuellt. Testerna ger poäng automatiskt när du skickar in din lösning till servern, oavsett hur du har implementerat den. Skicka bara in din lösning när du är redo och din lösning matchar övningsbeskrivningen. Övningarna kanske inte har automatiska tester, men kurspersonalen kommer ändå att se din lösning. Att skicka in en ofullständig lösning till TMC Paste ger också poäng automatiskt, så det bör inte användas när du ber om hjälp med övningarna i den här modulen. Du kan använda "},{"type":"element","tagName":"a","properties":{"href":"https://pastebin.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pastebin.com"}]},{"type":"text","value":" eller någon annan pastebin-tjänst på internet när du ber om hjälp i kursens stödkanaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om din lösning helt klart inte stämmer överens med övningsbeskrivningen kan du förlora de poäng som du har fått för övningarna i den här modulen."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fyra robotar","tmcname":"osa13-01_fyra_robotar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som ritar en robot i vart och ett av de fyra hörnen av fönstret. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/8ba6d635c4ceb93c2b86711f83589b3a/pygame_nelja.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robotar i rad","tmcname":"osa13-02_robotar_i_rad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som ritar tio robotar i rad. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/8dc589e5970721e2bf8bfe2bd22fa6cf/pygame_rivi.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hundra robotar","tmcname":"osa13-03_hundra_robotar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Var snäll och skriv ett program som ritar hundra robotar: tio rader med tio robotar i varje rad. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/7012bfc1e6ade044e03c53f07f35e7a6/pygame_sata.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Slumpmässiga robotar","tmcname":"osa13-04_slumpmassiga_robotar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som ritar "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tusen"}]},{"type":"text","value":" robotar på slumpmässiga platser. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/61a30808255873a3e24df2fc559d9367/pygame_tuhat.gif"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Har du installerat pygame-biblioteket på din dator
  • \n
  • Vet du hur man skapar ett pygame-fönster och hur man avslutar ett program
  • \n
  • Kommer du att kunna använda en bild som lagras i en fil i ett pygame-fönster
  • \n

I de här två sista delarna av kursmaterialet kommer vi att bekanta oss med pygame-biblioteket. Det är ett Python-bibliotek för programmering av spel. Det hjälper dig att skapa grafiska element, hantera händelser från tangentbordet och musen samt implementera andra funktioner som är nödvändiga i spel.

Att installera pygame

Linux

Öppna en instruktionsrad, skriv in pip3 install pygame och tryck på enter.

\n \n \n \n \n \n \"13\n \n \n

Skriv in följande och tryck enter:

pip3 install pygame

\n \n \n \n \n \n \"13\n \n \n

Detta borde installera pygame-biblioteket på din dator.

Ditt första program

Här är ett enkelt program för att kontrollera att din pygame-installation fungerar korrekt:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nfonster.fill((0,0,0))\npygame.display.flip()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

När detta program körs borde det visa ett fönster:

Programmet visar endast ett fönster, och det körs tills användaren stänger fönstret.

Låt oss ta en närmare titt på de steg som krävs för att uppnå detta. Den första raden tar pygame-biblioteket i bruk: import pygame. Nästa instruktion, pygame.init, initierar pygame-modulerna, och nästa skapar ett fönster med funktionen pygame.display.set_mode.

pygame.init()\nfonster = pygame.display.set_mode((640, 480))

Funktionen set_mode tar fönstrets dimensioner som ett argument. Tupeln (640, 480) anger att fönstret är 640 pixlar brett och 480 pixlar högt. Variabelnamnet fonster kan senare användas för att komma åt fönstret, t.ex. för att rita något i det.

De följande två instruktionerna gör just detta:

fonster.fill((0, 0, 0))\npygame.display.flip()

Metoden fill fyller fönstret med den färg som anges som argument. I det här fallet är färgen svart, som skickas som ett RGB-värde i tupeln (0, 0, 0). Metoden pygame.display.flip uppdaterar innehållet i fönstret.

Efter dessa initialiseringsinstruktioner börjar programmets huvudloop:

while True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

Huvudloopen hanterar alla händelser som operativsystemet skickar till programmet. Vid varje iteration returnerar funktionen pygame.event.get en lista över alla händelser som har samlats in sedan föregående iteration.

I exemplet ovan hanterar programmet endast händelser av typen pygame.QUIT. Denna händelse uppstår t.ex. genom att man klickar på exit-knappen i fönstrets hörn. Om händelsen pygame.QUIT utlöses avslutas programmet genom funktionen exit.

Du kan prova och se vad som händer om ditt program inte hanterar händelsen pygame.QUIT. Detta borde innebära att det inte händer någonting om man klickar på exit-knappen, vilket skulle vara förvirrande för användaren. Eftersom programmet körs från kommandoraden kan du fortfarande stoppa det från kommandoraden med Control+C.

Lägg till en bild

Låt oss lägga till en bild i fönstret:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nfonster.fill((0, 0, 0))\nfonster.blit(robot, (100, 50))\npygame.display.flip()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

Programmet använder denna bild av en robot, som finns lagrad i filen robot.png:

Filen robot.png måste finnas i samma katalog som källkoden för ditt program, annars kan programmet inte hitta den. I övningsmallarna för denna del väntar bilderna i övningskatalogen.

Fönstret borde nu se ut så här:

Funktionen pygame.image.load laddar in bilden i filen robot.png och lagrar en referens till den i variabeln robot. Metoden blit ritar bilden på platsen (100, 50), och funktionen pygame.display.flip uppdaterar fönstrets innehåll, som tidigare. Platsen (100, 50) innebär att bildens övre vänstra hörn befinner sig på den platsen i fönstret.

I pygame ligger origo-punkten (0, 0) i fönstrets övre vänstra hörn. X-koordinaterna ökar åt höger och y-koordinaterna ökar nedåt, så att det nedre högra hörnet har koordinaterna (640, 480). Detta är tvärtemot hur koordinater brukar hanteras inom t.ex. matematiken, men det är ganska vanligt i programmeringssammanhang och värt att vänja sig vid.

När du har laddat en bild kan du använda den många gånger i samma fönster. I följande kod ritas bilden av roboten på tre olika platser:

fonster.blit(robot, (0, 0))\nfonster.blit(robot, (300, 0))\nfonster.blit(robot, (100, 200))

Resultatet borde vara att fönstret ser ut så här:

Här sätter vi lokationen av bilden så, att den ligger i mitten av fönstret:

bredd = robot.get_width()\nhojd = robot.get_height()\nfonster.blit(robot, (320-bredd/2, 240-hojd/2))

Fönstret borde nu se ut så här:

Metoden get_width ger bildens bredd och metoden get_height ger dess höjd, båda i pixlar. Fönstrets mittpunkt ligger på halva bredden och höjden, alltså på (320, 240), vilket vi kan använda för att beräkna en lämplig plats för bildens övre vänstra hörn, så att det ligger exakt i mitten.

Övningarna i den här modulen av kursen har inga automatiserade tester, eftersom resultaten verifieras visuellt. Testerna ger poäng automatiskt när du skickar in din lösning till servern, oavsett hur du har implementerat den. Skicka bara in din lösning när du är redo och din lösning matchar övningsbeskrivningen. Övningarna kanske inte har automatiska tester, men kurspersonalen kommer ändå att se din lösning. Att skicka in en ofullständig lösning till TMC Paste ger också poäng automatiskt, så det bör inte användas när du ber om hjälp med övningarna i den här modulen. Du kan använda Pastebin.com eller någon annan pastebin-tjänst på internet när du ber om hjälp i kursens stödkanaler.

Om din lösning helt klart inte stämmer överens med övningsbeskrivningen kan du förlora de poäng som du har fått för övningarna i den här modulen.

Skriv ett program som ritar en robot i vart och ett av de fyra hörnen av fönstret. Slutresultatet ska se ut så här:

Skriv ett program som ritar tio robotar i rad. Slutresultatet ska se ut så här:

Var snäll och skriv ett program som ritar hundra robotar: tio rader med tio robotar i varje rad. Slutresultatet ska se ut så här:

Skriv ett program som ritar tusen robotar på slumpmässiga platser. Slutresultatet ska se ut så här:

","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/1-pygame.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-13/2-animation/page-data.json b/page-data/osa-13/2-animation/page-data.json index 57d028860..0c4a2ae5f 100644 --- a/page-data/osa-13/2-animation/page-data.json +++ b/page-data/osa-13/2-animation/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-13/2-animation", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur man skapar en animation med pygame"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda en klocka för att ställa in hastigheten på ditt program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda grundläggande trigonometriska funktioner i dina animationer"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Många spel har rörliga karaktärer, så ett logiskt nästa steg är att skapa animationer. Vi kan skapa en illusion av rörelse genom att rita samma bild på olika ställen på skärmen och tajma ändringarna på rätt sätt."}]},{"type":"element","tagName":"h2","properties":{"id":"skapa-en-animation","style":"position:relative;"},"children":[{"type":"text","value":"Skapa en animation"},{"type":"element","tagName":"a","properties":{"href":"#skapa-en-animation","ariaLabel":"skapa en animation permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande kod skapar en animation där en robot rör sig från vänster till höger i ett pygame-fönster:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När detta exekveras, borde resultatet se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/e0ac2065e7c54214a7440186eb4b9a5b/pygame_animaatio.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en närmare titt på de instruktioner som är inblandade. Om vi vill spåra bildens rörelse på skärmen måste vi veta var den befinner sig. Därför har vi två variabler för koordinaterna för bildens övre vänstra hörn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har även en klocka, som vi använder för att se till att animationens hastighet är korrekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"klocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Huvudloopen ritar bilden på sin aktuella plats vid varje iteration:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först fyller metoden fill fönstret med svart, precis som tidigare. Färgen skickas som en tupel som innehåller RGB-värdena för färgen. I det här fallet är argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0, 0)"}]},{"type":"text","value":", vilket innebär att alla tre komponenterna - röd, grön och blå - har värdet 0. Varje komponent kan ha ett värde mellan 0 och 255. Om vi skickar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(255, 255, 255)"}]},{"type":"text","value":" som argument får vi alltså ett vitt fönster, och med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(255, 0, 0)"}]},{"type":"text","value":" får vi ett rött fönster. RGB-färgkoder utgör ryggraden i digital färgläggning, och det finns många verktyg online för att arbeta med dem, till exempel "},{"type":"element","tagName":"a","properties":{"href":"https://www.rapidtables.com/web/color/RGB_Color.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"RGB Color Codes Chart"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När fönstret har fyllts med färg ritas bilden på den angivna platsen med blit-metoden. Sedan uppdateras innehållet i fönstret med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Slutligen ökas värdet som lagras i x, vilket gör att bilden flyttas en pixel åt höger för varje iteration:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klock-metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" kallas i slutet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" tar hand om hastigheten på animationen. Argumentet 60 anger att loopen ska exekveras "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"60"}]},{"type":"text","value":" gånger per sekund, vilket innebär att bilden förflyttas 60 pixlar åt höger varje sekund. Detta motsvarar ungefär det värde för FPS eller bilder per sekund som används i spel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I princip ser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":"-metoden till att animationen körs med samma hastighet på alla datorer. Om det inte fanns någon sådan timing skulle animationens hastighet bero på datorns hastighet."}]},{"type":"element","tagName":"h2","properties":{"id":"studsa-av-en-vagg","style":"position:relative;"},"children":[{"type":"text","value":"Studsa av en vägg"},{"type":"element","tagName":"a","properties":{"href":"#studsa-av-en-vagg","ariaLabel":"studsa av en vagg permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den föregående animationen var annars utmärkt, men när roboten nådde en vägg fortsatte den bara att försvinna ur syn. Låt oss få roboten att studsa mot väggen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nhastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" hastighet\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hastighet\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hastighet\n\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekvering av koden ovan borde se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/5dac9b07ba4879c217b0b35a817a727f/pygame_animaatio2.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns en ny variabel, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hastighet"}]},{"type":"text","value":", som bestämmer rörelseriktningen. Om värdet är över noll sker förflyttningen åt höger och om det är under noll sker förflyttningen åt vänster. I det här fallet rör sig roboten åt höger om värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", åt vänster om värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande rader gör att roboten studsar mot sidoväggarna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hastighet\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hastighet"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om hastigheten är över noll så att roboten rör sig åt höger, och högerkanten på bilden går utanför fönstrets högra kant, vänds riktningen och roboten börjar röra sig åt vänster. På samma sätt, om hastigheten är under noll så att roboten rör sig åt vänster, och bildens vänstra kant når fönstrets vänstra kant, vänds riktningen igen och roboten börjar röra sig åt höger igen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta gör att roboten rör sig på en bana från fönstrets vänstra kant till den högra kanten, och tillbaka till vänster, och sedan till höger igen, upprepat i all oändlighet."}]},{"type":"element","tagName":"h2","properties":{"id":"rotation","style":"position:relative;"},"children":[{"type":"text","value":"Rotation"},{"type":"element","tagName":"a","properties":{"href":"#rotation","ariaLabel":"rotation permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss skapa ytterligare en animation. Den här gången ska roboten rotera i en cirkel runt fönstrets mitt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvinkel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vinkel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vinkel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n vinkel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.01"}]},{"type":"text","value":"\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekvering av koden ovan borde se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/ce164a1f214ed31343271340f9157733/pygame_pyorinta.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rotation i en relativt exakt cirkel uppnås med hjälp av några grundläggande trigonometriska funktioner. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vinkel"}]},{"type":"text","value":" innehåller vinkeln för robotens position i förhållande till fönstrets mittpunkt och den horisontella linjen som går genom fönstret. Sinus- och cosinusfunktionerna från Pythons matematikbibliotek används här för att beräkna koordinaterna för robotens position:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vinkel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vinkel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Roboten roterar runt en cirkel med radien 100 runt fönstrets mittpunkt. Hypotenusan i detta scenario är cirkelns radie. Cosinusfunktionen anger längden på den angränsande sidan i en rätvinklig triangel i förhållande till hypotenusan, vilket innebär att den ger oss platsens "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"-koordinat. Sinusfunktionen ger längden på den motsatta sidan, dvs. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":"-koordinaten. Platsen justeras sedan för bildens storlek, så att cirkelns mittpunkt ligger i fönstrets mittpunkt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje iteration ökar storleken på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vinkel"}]},{"type":"text","value":" med 0,01. Eftersom vi använder radianer är en hel cirkel 2π, vilket motsvarar ca 6,28. Det tar cirka 628 iterationer för roboten att gå en hel cirkel, och med 60 iterationer per sekund tar detta drygt 10 sekunder."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vertikal rörelse","tmcname":"osa13-05_vertikal_rorelse"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där roboten rör sig upp och ner i en ändlös loop. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/7dfa8e1aefab9c5f0586d8148b819543/pygame_pysty.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Längs med kanten","tmcname":"osa13-06_langs_med_kanten"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där roboten följer fönstrets omkrets. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/b313e2cd6e747c9b47375655616b0e2d/pygame_kierto.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Två robotar","tmcname":"osa13-07_tva_robotar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där två robotar rör sig fram och tillbaka till vänster och höger. Den nedre roboten ska röra sig med dubbelt så hög hastighet som den övre. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/5b2b134a4afcdbaa9c11796d3cadb5e2/pygame_liike2.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ringlek","tmcname":"osa13-08_ringlek"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där tio robotar går runt i en cirkel. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/c8832b391e97f55d98358c8f0e5bfd57/pygame_piiri.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Studsande boll","tmcname":"osa13-09_studsande_boll"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där en boll studsar från fönstrets kanter. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/e9503906920a8a19bc3a33143b0dde81/pygame_pallo.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller bilden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"boll.png"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robot invasion","tmcname":"osa13-10_robot_invasion"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där robotar faller från himlen slumpmässigt. När en robot når marken börjar den röra sig åt vänster eller höger och försvinner till slut från skärmen. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/23bd928da4c554324efdf7de4806cc60/pygame_invaasio.gif"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Vet du hur man skapar en animation med pygame
  • \n
  • Kommer du att kunna använda en klocka för att ställa in hastigheten på ditt program
  • \n
  • Kommer du att kunna använda grundläggande trigonometriska funktioner i dina animationer
  • \n

Många spel har rörliga karaktärer, så ett logiskt nästa steg är att skapa animationer. Vi kan skapa en illusion av rörelse genom att rita samma bild på olika ställen på skärmen och tajma ändringarna på rätt sätt.

Skapa en animation

Följande kod skapar en animation där en robot rör sig från vänster till höger i ett pygame-fönster:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nx = 0\ny = 0\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()\n\n    x += 1\n    klocka.tick(60)

När detta exekveras, borde resultatet se ut så här:

Låt oss ta en närmare titt på de instruktioner som är inblandade. Om vi vill spåra bildens rörelse på skärmen måste vi veta var den befinner sig. Därför har vi två variabler för koordinaterna för bildens övre vänstra hörn:

x = 0\ny = 0

Vi har även en klocka, som vi använder för att se till att animationens hastighet är korrekt:

klocka = pygame.time.Clock()

Huvudloopen ritar bilden på sin aktuella plats vid varje iteration:

    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()

Först fyller metoden fill fönstret med svart, precis som tidigare. Färgen skickas som en tupel som innehåller RGB-värdena för färgen. I det här fallet är argumentet (0, 0, 0), vilket innebär att alla tre komponenterna - röd, grön och blå - har värdet 0. Varje komponent kan ha ett värde mellan 0 och 255. Om vi skickar (255, 255, 255) som argument får vi alltså ett vitt fönster, och med (255, 0, 0) får vi ett rött fönster. RGB-färgkoder utgör ryggraden i digital färgläggning, och det finns många verktyg online för att arbeta med dem, till exempel RGB Color Codes Chart.

När fönstret har fyllts med färg ritas bilden på den angivna platsen med blit-metoden. Sedan uppdateras innehållet i fönstret med funktionen pygame.display.flip.

Slutligen ökas värdet som lagras i x, vilket gör att bilden flyttas en pixel åt höger för varje iteration:

    x += 1

Klock-metoden tick kallas i slutet:

    klocka.tick(60)

Metoden tick tar hand om hastigheten på animationen. Argumentet 60 anger att loopen ska exekveras 60 gånger per sekund, vilket innebär att bilden förflyttas 60 pixlar åt höger varje sekund. Detta motsvarar ungefär det värde för FPS eller bilder per sekund som används i spel.

I princip ser tick-metoden till att animationen körs med samma hastighet på alla datorer. Om det inte fanns någon sådan timing skulle animationens hastighet bero på datorns hastighet.

Studsa av en vägg

Den föregående animationen var annars utmärkt, men när roboten nådde en vägg fortsatte den bara att försvinna ur syn. Låt oss få roboten att studsa mot väggen.

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nx = 0\ny = 0\nhastighet = 1\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()\n\n    x += hastighet\n    if hastighet > 0 and x+robot.get_width() >= 640:\n        hastighet = -hastighet\n    if hastighet < 0 and x <= 0:\n        hastighet = -hastighet\n\n    klocka.tick(60)

Exekvering av koden ovan borde se ut så här:

Det finns en ny variabel, hastighet, som bestämmer rörelseriktningen. Om värdet är över noll sker förflyttningen åt höger och om det är under noll sker förflyttningen åt vänster. I det här fallet rör sig roboten åt höger om värdet är 1, åt vänster om värdet är -1.

Följande rader gör att roboten studsar mot sidoväggarna:

    if hastighet > 0 and x+robot.get_width() >= 640:\n        hastighet = -hastighet\n    if hastighet < 0 and x <= 0:\n        hastighet = -hastighet

Om hastigheten är över noll så att roboten rör sig åt höger, och högerkanten på bilden går utanför fönstrets högra kant, vänds riktningen och roboten börjar röra sig åt vänster. På samma sätt, om hastigheten är under noll så att roboten rör sig åt vänster, och bildens vänstra kant når fönstrets vänstra kant, vänds riktningen igen och roboten börjar röra sig åt höger igen.

Detta gör att roboten rör sig på en bana från fönstrets vänstra kant till den högra kanten, och tillbaka till vänster, och sedan till höger igen, upprepat i all oändlighet.

Rotation

Låt oss skapa ytterligare en animation. Den här gången ska roboten rotera i en cirkel runt fönstrets mitt:

import pygame\nimport math\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nvinkel = 0\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    x = 320+math.cos(vinkel)*100-robot.get_width()/2\n    y = 240+math.sin(vinkel)*100-robot.get_height()/2\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()\n\n    vinkel += 0.01\n    klocka.tick(60)

Exekvering av koden ovan borde se ut så här:

Rotation i en relativt exakt cirkel uppnås med hjälp av några grundläggande trigonometriska funktioner. Variabeln vinkel innehåller vinkeln för robotens position i förhållande till fönstrets mittpunkt och den horisontella linjen som går genom fönstret. Sinus- och cosinusfunktionerna från Pythons matematikbibliotek används här för att beräkna koordinaterna för robotens position:

        x = 320+math.cos(vinkel)*100-robot.get_width()/2\n        y = 240+math.sin(vinkel)*100-robot.get_height()/2

Roboten roterar runt en cirkel med radien 100 runt fönstrets mittpunkt. Hypotenusan i detta scenario är cirkelns radie. Cosinusfunktionen anger längden på den angränsande sidan i en rätvinklig triangel i förhållande till hypotenusan, vilket innebär att den ger oss platsens x-koordinat. Sinusfunktionen ger längden på den motsatta sidan, dvs. y-koordinaten. Platsen justeras sedan för bildens storlek, så att cirkelns mittpunkt ligger i fönstrets mittpunkt.

För varje iteration ökar storleken på vinkel med 0,01. Eftersom vi använder radianer är en hel cirkel 2π, vilket motsvarar ca 6,28. Det tar cirka 628 iterationer för roboten att gå en hel cirkel, och med 60 iterationer per sekund tar detta drygt 10 sekunder.

Skapa en animation där roboten rör sig upp och ner i en ändlös loop. Slutresultatet ska se ut så här:

Skapa en animation där roboten följer fönstrets omkrets. Slutresultatet ska se ut så här:

Skapa en animation där två robotar rör sig fram och tillbaka till vänster och höger. Den nedre roboten ska röra sig med dubbelt så hög hastighet som den övre. Slutresultatet ska se ut så här:

Skapa en animation där tio robotar går runt i en cirkel. Slutresultatet ska se ut så här:

Skapa en animation där en boll studsar från fönstrets kanter. Slutresultatet ska se ut så här:

Övningsmallen innehåller bilden boll.png.

Skapa en animation där robotar faller från himlen slumpmässigt. När en robot når marken börjar den röra sig åt vänster eller höger och försvinner till slut från skärmen. Slutresultatet ska se ut så här:

","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/2-animation.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur man skapar en animation med pygame"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda en klocka för att ställa in hastigheten på ditt program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda grundläggande trigonometriska funktioner i dina animationer"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Många spel har rörliga karaktärer, så ett logiskt nästa steg är att skapa animationer. Vi kan skapa en illusion av rörelse genom att rita samma bild på olika ställen på skärmen och tajma ändringarna på rätt sätt."}]},{"type":"element","tagName":"h2","properties":{"id":"skapa-en-animation","style":"position:relative;"},"children":[{"type":"text","value":"Skapa en animation"},{"type":"element","tagName":"a","properties":{"href":"#skapa-en-animation","ariaLabel":"skapa en animation permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande kod skapar en animation där en robot rör sig från vänster till höger i ett pygame-fönster:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När detta exekveras, borde resultatet se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/e0ac2065e7c54214a7440186eb4b9a5b/pygame_animaatio.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en närmare titt på de instruktioner som är inblandade. Om vi vill spåra bildens rörelse på skärmen måste vi veta var den befinner sig. Därför har vi två variabler för koordinaterna för bildens övre vänstra hörn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har även en klocka, som vi använder för att se till att animationens hastighet är korrekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"klocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Huvudloopen ritar bilden på sin aktuella plats vid varje iteration:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först fyller metoden fill fönstret med svart, precis som tidigare. Färgen skickas som en tupel som innehåller RGB-värdena för färgen. I det här fallet är argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0, 0)"}]},{"type":"text","value":", vilket innebär att alla tre komponenterna - röd, grön och blå - har värdet 0. Varje komponent kan ha ett värde mellan 0 och 255. Om vi skickar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(255, 255, 255)"}]},{"type":"text","value":" som argument får vi alltså ett vitt fönster, och med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(255, 0, 0)"}]},{"type":"text","value":" får vi ett rött fönster. RGB-färgkoder utgör ryggraden i digital färgläggning, och det finns många verktyg online för att arbeta med dem, till exempel "},{"type":"element","tagName":"a","properties":{"href":"https://www.rapidtables.com/web/color/RGB_Color.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"RGB Color Codes Chart"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När fönstret har fyllts med färg ritas bilden på den angivna platsen med blit-metoden. Sedan uppdateras innehållet i fönstret med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Slutligen ökas värdet som lagras i x, vilket gör att bilden flyttas en pixel åt höger för varje iteration:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klock-metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" kallas i slutet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" tar hand om hastigheten på animationen. Argumentet 60 anger att loopen ska exekveras "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"60"}]},{"type":"text","value":" gånger per sekund, vilket innebär att bilden förflyttas 60 pixlar åt höger varje sekund. Detta motsvarar ungefär det värde för FPS eller bilder per sekund som används i spel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I princip ser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":"-metoden till att animationen körs med samma hastighet på alla datorer. Om det inte fanns någon sådan timing skulle animationens hastighet bero på datorns hastighet."}]},{"type":"element","tagName":"h2","properties":{"id":"studsa-av-en-vagg","style":"position:relative;"},"children":[{"type":"text","value":"Studsa av en vägg"},{"type":"element","tagName":"a","properties":{"href":"#studsa-av-en-vagg","ariaLabel":"studsa av en vagg permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den föregående animationen var annars utmärkt, men när roboten nådde en vägg fortsatte den bara att försvinna ur syn. Låt oss få roboten att studsa mot väggen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nhastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" hastighet\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hastighet\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hastighet\n\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekvering av koden ovan borde se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/5dac9b07ba4879c217b0b35a817a727f/pygame_animaatio2.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns en ny variabel, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hastighet"}]},{"type":"text","value":", som bestämmer rörelseriktningen. Om värdet är över noll sker förflyttningen åt höger och om det är under noll sker förflyttningen åt vänster. I det här fallet rör sig roboten åt höger om värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", åt vänster om värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande rader gör att roboten studsar mot sidoväggarna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hastighet\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hastighet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"hastighet"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om hastigheten är över noll så att roboten rör sig åt höger, och högerkanten på bilden går utanför fönstrets högra kant, vänds riktningen och roboten börjar röra sig åt vänster. På samma sätt, om hastigheten är under noll så att roboten rör sig åt vänster, och bildens vänstra kant når fönstrets vänstra kant, vänds riktningen igen och roboten börjar röra sig åt höger igen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta gör att roboten rör sig på en bana från fönstrets vänstra kant till den högra kanten, och tillbaka till vänster, och sedan till höger igen, upprepat i all oändlighet."}]},{"type":"element","tagName":"h2","properties":{"id":"rotation","style":"position:relative;"},"children":[{"type":"text","value":"Rotation"},{"type":"element","tagName":"a","properties":{"href":"#rotation","ariaLabel":"rotation permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss skapa ytterligare en animation. Den här gången ska roboten rotera i en cirkel runt fönstrets mitt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvinkel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vinkel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vinkel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n vinkel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.01"}]},{"type":"text","value":"\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekvering av koden ovan borde se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/ce164a1f214ed31343271340f9157733/pygame_pyorinta.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rotation i en relativt exakt cirkel uppnås med hjälp av några grundläggande trigonometriska funktioner. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vinkel"}]},{"type":"text","value":" innehåller vinkeln för robotens position i förhållande till fönstrets mittpunkt och den horisontella linjen som går genom fönstret. Sinus- och cosinusfunktionerna från Pythons matematikbibliotek används här för att beräkna koordinaterna för robotens position:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vinkel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vinkel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Roboten roterar runt en cirkel med radien 100 runt fönstrets mittpunkt. Hypotenusan i detta scenario är cirkelns radie. Cosinusfunktionen anger längden på den angränsande sidan i en rätvinklig triangel i förhållande till hypotenusan, vilket innebär att den ger oss platsens "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"-koordinat. Sinusfunktionen ger längden på den motsatta sidan, dvs. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":"-koordinaten. Platsen justeras sedan för bildens storlek, så att cirkelns mittpunkt ligger i fönstrets mittpunkt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje iteration ökar storleken på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vinkel"}]},{"type":"text","value":" med 0,01. Eftersom vi använder radianer är en hel cirkel 2π, vilket motsvarar ca 6,28. Det tar cirka 628 iterationer för roboten att gå en hel cirkel, och med 60 iterationer per sekund tar detta drygt 10 sekunder."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vertikal rörelse","tmcname":"osa13-05_vertikal_rorelse"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där roboten rör sig upp och ner i en ändlös loop. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/7dfa8e1aefab9c5f0586d8148b819543/pygame_pysty.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Längs med kanten","tmcname":"osa13-06_langs_med_kanten"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där roboten följer fönstrets omkrets. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/b313e2cd6e747c9b47375655616b0e2d/pygame_kierto.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Två robotar","tmcname":"osa13-07_tva_robotar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där två robotar rör sig fram och tillbaka till vänster och höger. Den nedre roboten ska röra sig med dubbelt så hög hastighet som den övre. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/5b2b134a4afcdbaa9c11796d3cadb5e2/pygame_liike2.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ringlek","tmcname":"osa13-08_ringlek"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där tio robotar går runt i en cirkel. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/c8832b391e97f55d98358c8f0e5bfd57/pygame_piiri.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Studsande boll","tmcname":"osa13-09_studsande_boll"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där en boll studsar från fönstrets kanter. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/e9503906920a8a19bc3a33143b0dde81/pygame_pallo.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller bilden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"boll.png"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robot invasion","tmcname":"osa13-10_robot_invasion"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en animation där robotar faller från himlen slumpmässigt. När en robot når marken börjar den röra sig åt vänster eller höger och försvinner till slut från skärmen. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/23bd928da4c554324efdf7de4806cc60/pygame_invaasio.gif"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Vet du hur man skapar en animation med pygame
  • \n
  • Kommer du att kunna använda en klocka för att ställa in hastigheten på ditt program
  • \n
  • Kommer du att kunna använda grundläggande trigonometriska funktioner i dina animationer
  • \n

Många spel har rörliga karaktärer, så ett logiskt nästa steg är att skapa animationer. Vi kan skapa en illusion av rörelse genom att rita samma bild på olika ställen på skärmen och tajma ändringarna på rätt sätt.

Skapa en animation

Följande kod skapar en animation där en robot rör sig från vänster till höger i ett pygame-fönster:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nx = 0\ny = 0\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()\n\n    x += 1\n    klocka.tick(60)

När detta exekveras, borde resultatet se ut så här:

Låt oss ta en närmare titt på de instruktioner som är inblandade. Om vi vill spåra bildens rörelse på skärmen måste vi veta var den befinner sig. Därför har vi två variabler för koordinaterna för bildens övre vänstra hörn:

x = 0\ny = 0

Vi har även en klocka, som vi använder för att se till att animationens hastighet är korrekt:

klocka = pygame.time.Clock()

Huvudloopen ritar bilden på sin aktuella plats vid varje iteration:

    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()

Först fyller metoden fill fönstret med svart, precis som tidigare. Färgen skickas som en tupel som innehåller RGB-värdena för färgen. I det här fallet är argumentet (0, 0, 0), vilket innebär att alla tre komponenterna - röd, grön och blå - har värdet 0. Varje komponent kan ha ett värde mellan 0 och 255. Om vi skickar (255, 255, 255) som argument får vi alltså ett vitt fönster, och med (255, 0, 0) får vi ett rött fönster. RGB-färgkoder utgör ryggraden i digital färgläggning, och det finns många verktyg online för att arbeta med dem, till exempel RGB Color Codes Chart.

När fönstret har fyllts med färg ritas bilden på den angivna platsen med blit-metoden. Sedan uppdateras innehållet i fönstret med funktionen pygame.display.flip.

Slutligen ökas värdet som lagras i x, vilket gör att bilden flyttas en pixel åt höger för varje iteration:

    x += 1

Klock-metoden tick kallas i slutet:

    klocka.tick(60)

Metoden tick tar hand om hastigheten på animationen. Argumentet 60 anger att loopen ska exekveras 60 gånger per sekund, vilket innebär att bilden förflyttas 60 pixlar åt höger varje sekund. Detta motsvarar ungefär det värde för FPS eller bilder per sekund som används i spel.

I princip ser tick-metoden till att animationen körs med samma hastighet på alla datorer. Om det inte fanns någon sådan timing skulle animationens hastighet bero på datorns hastighet.

Studsa av en vägg

Den föregående animationen var annars utmärkt, men när roboten nådde en vägg fortsatte den bara att försvinna ur syn. Låt oss få roboten att studsa mot väggen.

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nx = 0\ny = 0\nhastighet = 1\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()\n\n    x += hastighet\n    if hastighet > 0 and x+robot.get_width() >= 640:\n        hastighet = -hastighet\n    if hastighet < 0 and x <= 0:\n        hastighet = -hastighet\n\n    klocka.tick(60)

Exekvering av koden ovan borde se ut så här:

Det finns en ny variabel, hastighet, som bestämmer rörelseriktningen. Om värdet är över noll sker förflyttningen åt höger och om det är under noll sker förflyttningen åt vänster. I det här fallet rör sig roboten åt höger om värdet är 1, åt vänster om värdet är -1.

Följande rader gör att roboten studsar mot sidoväggarna:

    if hastighet > 0 and x+robot.get_width() >= 640:\n        hastighet = -hastighet\n    if hastighet < 0 and x <= 0:\n        hastighet = -hastighet

Om hastigheten är över noll så att roboten rör sig åt höger, och högerkanten på bilden går utanför fönstrets högra kant, vänds riktningen och roboten börjar röra sig åt vänster. På samma sätt, om hastigheten är under noll så att roboten rör sig åt vänster, och bildens vänstra kant når fönstrets vänstra kant, vänds riktningen igen och roboten börjar röra sig åt höger igen.

Detta gör att roboten rör sig på en bana från fönstrets vänstra kant till den högra kanten, och tillbaka till vänster, och sedan till höger igen, upprepat i all oändlighet.

Rotation

Låt oss skapa ytterligare en animation. Den här gången ska roboten rotera i en cirkel runt fönstrets mitt:

import pygame\nimport math\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nvinkel = 0\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    x = 320+math.cos(vinkel)*100-robot.get_width()/2\n    y = 240+math.sin(vinkel)*100-robot.get_height()/2\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()\n\n    vinkel += 0.01\n    klocka.tick(60)

Exekvering av koden ovan borde se ut så här:

Rotation i en relativt exakt cirkel uppnås med hjälp av några grundläggande trigonometriska funktioner. Variabeln vinkel innehåller vinkeln för robotens position i förhållande till fönstrets mittpunkt och den horisontella linjen som går genom fönstret. Sinus- och cosinusfunktionerna från Pythons matematikbibliotek används här för att beräkna koordinaterna för robotens position:

        x = 320+math.cos(vinkel)*100-robot.get_width()/2\n        y = 240+math.sin(vinkel)*100-robot.get_height()/2

Roboten roterar runt en cirkel med radien 100 runt fönstrets mittpunkt. Hypotenusan i detta scenario är cirkelns radie. Cosinusfunktionen anger längden på den angränsande sidan i en rätvinklig triangel i förhållande till hypotenusan, vilket innebär att den ger oss platsens x-koordinat. Sinusfunktionen ger längden på den motsatta sidan, dvs. y-koordinaten. Platsen justeras sedan för bildens storlek, så att cirkelns mittpunkt ligger i fönstrets mittpunkt.

För varje iteration ökar storleken på vinkel med 0,01. Eftersom vi använder radianer är en hel cirkel 2π, vilket motsvarar ca 6,28. Det tar cirka 628 iterationer för roboten att gå en hel cirkel, och med 60 iterationer per sekund tar detta drygt 10 sekunder.

Skapa en animation där roboten rör sig upp och ner i en ändlös loop. Slutresultatet ska se ut så här:

Skapa en animation där roboten följer fönstrets omkrets. Slutresultatet ska se ut så här:

Skapa en animation där två robotar rör sig fram och tillbaka till vänster och höger. Den nedre roboten ska röra sig med dubbelt så hög hastighet som den övre. Slutresultatet ska se ut så här:

Skapa en animation där tio robotar går runt i en cirkel. Slutresultatet ska se ut så här:

Skapa en animation där en boll studsar från fönstrets kanter. Slutresultatet ska se ut så här:

Övningsmallen innehåller bilden boll.png.

Skapa en animation där robotar faller från himlen slumpmässigt. När en robot når marken börjar den röra sig åt vänster eller höger och försvinner till slut från skärmen. Slutresultatet ska se ut så här:

","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/2-animation.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-13/3-handelser/page-data.json b/page-data/osa-13/3-handelser/page-data.json index cf2c4cdb1..33df86515 100644 --- a/page-data/osa-13/3-handelser/page-data.json +++ b/page-data/osa-13/3-handelser/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-13/3-handelser", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att vara bekant med pygame händelser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva ett program som reagerar på tangenttryckningar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva ett program som reagerar på mushändelser"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har våra huvudloopar bara kört förutbestämda animationer och reagerat på händelser av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":", trots att loopen får en lista över alla händelser från operativsystemet. Låt oss nu ta itu med några andra typer av händelser."}]},{"type":"element","tagName":"h2","properties":{"id":"hantering-av-handelser","style":"position:relative;"},"children":[{"type":"text","value":"Hantering av händelser"},{"type":"element","tagName":"a","properties":{"href":"#hantering-av-handelser","ariaLabel":"hantering av handelser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet skriver ut information om alla händelser som skickas från operativsystemet till programmet pygame, medan det körs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att programmet kördes ett tag och att man sedan klickade på avslutningsknappen. Programmet skriver ut följande information:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De första händelserna gäller musanvändningen, därefter kommer några händelser från tangentbordet och slutligen stänger den sista händelsen programmet. Varje händelse har åtminstone en typ, men de kan också innehålla annan identifierande information, till exempel var muspekaren befinner sig eller vilken tangent som trycktes in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan leta efter händelsebeskrivningar i pygame-dokumentationen, men det kan ibland vara enklare att skriva ut händelser med koden ovan och leta efter den händelse som inträffar när något du vill reagera på händer."}]},{"type":"element","tagName":"h2","properties":{"id":"tangentbordshandelser","style":"position:relative;"},"children":[{"type":"text","value":"Tangentbordshändelser"},{"type":"element","tagName":"a","properties":{"href":"#tangentbordshandelser","ariaLabel":"tangentbordshandelser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta program kan behandla händelser där användaren trycker på piltangenten antingen till höger eller till vänster på sitt tangentbord. Programmet skriver ut vilken tangent som trycktes in."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vänster\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"höger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstanterna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.K_LEFT"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.K_RIGHT"}]},{"type":"text","value":" avser piltangenterna till vänster och höger. Konstanterna för pygame-tangenterna för de olika tangenterna på ett tangentbord anges i "},{"type":"element","tagName":"a","properties":{"href":"https://www.pygame.org/docs/ref/key.html#key-constants-label","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pygame dokumentationen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren t.ex. trycker på piltangenten till höger två gånger, sedan den vänstra en gång och sedan den högra en gång till, skriver programmet ut"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"höger\nhöger\nvänster\nhöger"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har nu alla verktyg som behövs för att flytta en karaktär, eller sprite, på skärmen till höger och vänster med piltangenterna. Följande kod kommer att uppnå detta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Beroende på hur du använder piltangenterna kunde programmet köra på följande sätt:"}]},{"type":"element","tagName":"img","properties":{"src":"/aac8387e39d3bb4adbfc1c36cae57345/pygame_liikutus.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I koden ovan har vi variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" som innehåller sprite-koordinaterna. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" är inställd så att spriten visas längst ned i fönstret. Värdet för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" ändras inte under hela körningen av programmet. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"-värdet ökar däremot med 10 när användaren trycker på piltangenten till höger och minskar med 10 när användaren trycker på piltangenten till vänster."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet fungerar i övrigt ganska bra, men tangenten måste tryckas in igen varje gång vi vill förflytta oss igen. Det skulle vara bättre om rörelsen var kontinuerlig när tangenten hölls nedtryckt. Följande program erbjuder denna funktionalitet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhoger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\nvanster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n vanster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hoger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYUP"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n vanster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hoger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hoger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" vanster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden innehåller nu variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hoger"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vanster"}]},{"type":"text","value":". Dessa innehåller vetskap om huruvida spriten ska röra sig åt höger eller vänster vid ett givet tillfälle. När användaren trycker ner en piltangent blir värdet som lagras i den relevanta variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". När tangenten släpps ändras värdet till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klockan används för att tidsbestämma spritens rörelser, så att de potentiellt sker 60 gånger per sekund. Om en piltangent trycks ned förflyttas spriten två pixlar åt höger eller vänster. Detta innebär att spriten rör sig 120 pixlar per sekund om tangenten hålls nedtryckt."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fyra riktningar","tmcname":"osa13-11_fyra_riktningar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program där spelaren kan flytta en robot i fyra riktningar med piltangenterna på tangentbordet. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/0461dfa5cb7291b279185dba96dfd819/pygame_nelja_suuntaa.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fyra väggar","tmcname":"osa13-12_fyra_vaggar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förbättra programmet i den föregående övningen så att roboten inte kan passera utanför fönstret i någon av de fyra riktningarna. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/bddcb9af6c636563eea121e3360b7b37/pygame_nelja_seinaa.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Två spelare","tmcname":"osa13-13_tva_spelare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program där två spelare styr var sin robot. En av spelarna ska använda piltangenterna medan den andra kan använda t.ex. w-a-s-d-tangenterna. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/57101a815ad1b5363307bc90dc112da6/pygame_kaksi_pelaajaa.gif"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"handelser-med-musen","style":"position:relative;"},"children":[{"type":"text","value":"Händelser med musen"},{"type":"element","tagName":"a","properties":{"href":"#handelser-med-musen","ariaLabel":"handelser med musen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande kod reagerar på händelser där en musknapp trycks ned medan markören befinner sig inom fönsterområdet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEBUTTONDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"du tryckte knappen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"på lokationen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekveringen av detta program borde mer eller mindre se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"du tryckte knappen 1 på lokationen (82, 135)\ndu tryckte knappen 1 på lokationen (369, 135)\ndu tryckte knappen 1 på lokationen (269, 297)\ndu tryckte knappen 3 på lokationen (515, 324)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Knapp nummer 1 avser vänster musknapp och knapp nummer 3 avser höger musknapp."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nästa program kombinerar hantering av mushändelser och ritning av en bild på skärmen. När användaren trycker på en musknapp medan muspekaren befinner sig inom fönstrets gränser ritas en bild av en robot på den platsen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEBUTTONDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekveringen av programmet borde se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/a41511bd13655f68553cc7e34de61edf/pygame_hiiri.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program innehåller en animation där robotspriten följer muspekaren. Spritens position lagras i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_y"}]},{"type":"text","value":". När musen rör sig lagras dess position i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mal_x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mal_y"}]},{"type":"text","value":". Om roboten inte befinner sig på denna plats förflyttar den sig i lämplig riktning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nrobot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nmal_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nmal_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEMOTION"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mal_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n mal_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" mal_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" mal_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" mal_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" mal_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekveringen av programmet borde se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/cd4d53df2a1564c181f38b7a2d45e7c9/pygame_hiiri2.gif"},"children":[]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Roboten och musen","tmcname":"osa13-14_roboten_och_musen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program där roboten följer muspekaren så att robotens mittpunkt alltid är direkt vid muspekaren. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/58d3b2e994913c8089688bcde5e4ed86/pygame_robotti_hiiri.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robotens plats","tmcname":"osa13-15_robotens_plats"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program där roboten dyker upp på en slumpmässig plats i fönstret. När spelaren klickar på roboten med musen förflyttar sig roboten till en ny plats. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/93991031970283be78df3ba4341a7362/pygame_robotti_paikka.gif"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att vara bekant med pygame händelser
  • \n
  • Kommer du att kunna skriva ett program som reagerar på tangenttryckningar
  • \n
  • Kommer du att kunna skriva ett program som reagerar på mushändelser
  • \n

Hittills har våra huvudloopar bara kört förutbestämda animationer och reagerat på händelser av typen pygame.QUIT, trots att loopen får en lista över alla händelser från operativsystemet. Låt oss nu ta itu med några andra typer av händelser.

Hantering av händelser

Det här programmet skriver ut information om alla händelser som skickas från operativsystemet till programmet pygame, medan det körs:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for handelse in pygame.event.get():\n        print(handelse)\n        if handelse.type == pygame.QUIT:\n            exit()

Låt oss anta att programmet kördes ett tag och att man sedan klickade på avslutningsknappen. Programmet skriver ut följande information:

<Event(4-MouseMotion {'pos': (495, 274), 'rel': (495, 274), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (494, 274), 'rel': (-1, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (492, 274), 'rel': (-2, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (491, 274), 'rel': (-1, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(5-MouseButtonDown {'pos': (491, 274), 'button': 1, 'window': None})>\n<Event(6-MouseButtonUp {'pos': (491, 274), 'button': 1, 'window': None})>\n<Event(2-KeyDown {'unicode': 'a', 'key': 97, 'mod': 0, 'scancode': 38, 'window': None})>\n<Event(3-KeyUp {'key': 97, 'mod': 0, 'scancode': 38, 'window': None})>\n<Event(2-KeyDown {'unicode': 'b', 'key': 98, 'mod': 0, 'scancode': 56, 'window': None})>\n<Event(3-KeyUp {'key': 98, 'mod': 0, 'scancode': 56, 'window': None})>\n<Event(2-KeyDown {'unicode': 'c', 'key': 99, 'mod': 0, 'scancode': 54, 'window': None})>\n<Event(3-KeyUp {'key': 99, 'mod': 0, 'scancode': 54, 'window': None})>\n<Event(12-Quit {})>

De första händelserna gäller musanvändningen, därefter kommer några händelser från tangentbordet och slutligen stänger den sista händelsen programmet. Varje händelse har åtminstone en typ, men de kan också innehålla annan identifierande information, till exempel var muspekaren befinner sig eller vilken tangent som trycktes in.

Du kan leta efter händelsebeskrivningar i pygame-dokumentationen, men det kan ibland vara enklare att skriva ut händelser med koden ovan och leta efter den händelse som inträffar när något du vill reagera på händer.

Tangentbordshändelser

Detta program kan behandla händelser där användaren trycker på piltangenten antingen till höger eller till vänster på sitt tangentbord. Programmet skriver ut vilken tangent som trycktes in.

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.KEYDOWN:\n            if handelse.key == pygame.K_LEFT:\n                print(\"vänster\")\n            if handelse.key == pygame.K_RIGHT:\n                print(\"höger\")\n\n        if handelse.type == pygame.QUIT:\n            exit()

Konstanterna pygame.K_LEFT och pygame.K_RIGHT avser piltangenterna till vänster och höger. Konstanterna för pygame-tangenterna för de olika tangenterna på ett tangentbord anges i Pygame dokumentationen.

Om användaren t.ex. trycker på piltangenten till höger två gånger, sedan den vänstra en gång och sedan den högra en gång till, skriver programmet ut

höger\nhöger\nvänster\nhöger

Vi har nu alla verktyg som behövs för att flytta en karaktär, eller sprite, på skärmen till höger och vänster med piltangenterna. Följande kod kommer att uppnå detta:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\nx = 0\ny = 480-robot.get_height()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.KEYDOWN:\n            if handelse.key == pygame.K_LEFT:\n                x -= 10\n            if handelse.key == pygame.K_RIGHT:\n                x += 10\n\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()

Beroende på hur du använder piltangenterna kunde programmet köra på följande sätt:

I koden ovan har vi variablerna x och y som innehåller sprite-koordinaterna. Variabeln y är inställd så att spriten visas längst ned i fönstret. Värdet för y ändras inte under hela körningen av programmet. x-värdet ökar däremot med 10 när användaren trycker på piltangenten till höger och minskar med 10 när användaren trycker på piltangenten till vänster.

Programmet fungerar i övrigt ganska bra, men tangenten måste tryckas in igen varje gång vi vill förflytta oss igen. Det skulle vara bättre om rörelsen var kontinuerlig när tangenten hölls nedtryckt. Följande program erbjuder denna funktionalitet:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\nx = 0\ny = 480-robot.get_height()\n\nhoger = False\nvanster = False\n\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.KEYDOWN:\n            if handelse.key == pygame.K_LEFT:\n                vanster = True\n            if handelse.key == pygame.K_RIGHT:\n                hoger = True\n\n        if handelse.type == pygame.KEYUP:\n            if handelse.key == pygame.K_LEFT:\n                vanster = False\n            if handelse.key == pygame.K_RIGHT:\n                hoger = False\n\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    if hoger:\n        x += 2\n    if vanster:\n        x -= 2\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()\n\n    klocka.tick(60)

Koden innehåller nu variablerna hoger och vanster. Dessa innehåller vetskap om huruvida spriten ska röra sig åt höger eller vänster vid ett givet tillfälle. När användaren trycker ner en piltangent blir värdet som lagras i den relevanta variabeln True. När tangenten släpps ändras värdet till False.

Klockan används för att tidsbestämma spritens rörelser, så att de potentiellt sker 60 gånger per sekund. Om en piltangent trycks ned förflyttas spriten två pixlar åt höger eller vänster. Detta innebär att spriten rör sig 120 pixlar per sekund om tangenten hålls nedtryckt.

Skriv ett program där spelaren kan flytta en robot i fyra riktningar med piltangenterna på tangentbordet. Slutresultatet ska se ut så här:

Förbättra programmet i den föregående övningen så att roboten inte kan passera utanför fönstret i någon av de fyra riktningarna. Slutresultatet ska se ut så här:

Skriv ett program där två spelare styr var sin robot. En av spelarna ska använda piltangenterna medan den andra kan använda t.ex. w-a-s-d-tangenterna. Slutresultatet ska se ut så här:

Händelser med musen

Följande kod reagerar på händelser där en musknapp trycks ned medan markören befinner sig inom fönsterområdet:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.MOUSEBUTTONDOWN:\n            print(\"du tryckte knappen\", handelse.button, \"på lokationen\", handelse.pos)\n\n        if handelse.type == pygame.QUIT:\n            exit()

Exekveringen av detta program borde mer eller mindre se ut så här:

du tryckte knappen 1 på lokationen (82, 135)\ndu tryckte knappen 1 på lokationen (369, 135)\ndu tryckte knappen 1 på lokationen (269, 297)\ndu tryckte knappen 3 på lokationen (515, 324)

Knapp nummer 1 avser vänster musknapp och knapp nummer 3 avser höger musknapp.

Nästa program kombinerar hantering av mushändelser och ritning av en bild på skärmen. När användaren trycker på en musknapp medan muspekaren befinner sig inom fönstrets gränser ritas en bild av en robot på den platsen.

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.MOUSEBUTTONDOWN:\n            x = handelse.pos[0]-robot.get_width()/2\n            y = handelse.pos[1]-robot.get_height()/2\n\n            fonster.fill((0, 0, 0))\n            fonster.blit(robot, (x, y))\n            pygame.display.flip()\n\n        if handelse.type == pygame.QUIT:\n            exit()

Exekveringen av programmet borde se ut så här:

Följande program innehåller en animation där robotspriten följer muspekaren. Spritens position lagras i variablerna robot_x och robot_y. När musen rör sig lagras dess position i variablerna mal_x och mal_y. Om roboten inte befinner sig på denna plats förflyttar den sig i lämplig riktning.

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nrobot_x = 0\nrobot_y = 0\nmal_x = 0\nmal_y = 0\n\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.MOUSEMOTION:\n            mal_x = handelse.pos[0]-robot.get_width()/2\n            mal_y = handelse.pos[1]-robot.get_height()/2\n\n        if handelse.type == pygame.QUIT:\n            exit(0)\n\n    if robot_x > mal_x:\n        robot_x -= 1\n    if robot_x < mal_x:\n        robot_x += 1\n    if robot_y > mal_y:\n        robot_y -= 1\n    if robot_y < mal_y:\n        robot_y += 1\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (robot_x, robot_y))\n    pygame.display.flip()\n\n    klocka.tick(60)

Exekveringen av programmet borde se ut så här:

Skriv ett program där roboten följer muspekaren så att robotens mittpunkt alltid är direkt vid muspekaren. Slutresultatet ska se ut så här:

Skriv ett program där roboten dyker upp på en slumpmässig plats i fönstret. När spelaren klickar på roboten med musen förflyttar sig roboten till en ny plats. Slutresultatet ska se ut så här:

","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/3-handelser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att vara bekant med pygame händelser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva ett program som reagerar på tangenttryckningar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva ett program som reagerar på mushändelser"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har våra huvudloopar bara kört förutbestämda animationer och reagerat på händelser av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":", trots att loopen får en lista över alla händelser från operativsystemet. Låt oss nu ta itu med några andra typer av händelser."}]},{"type":"element","tagName":"h2","properties":{"id":"hantering-av-handelser","style":"position:relative;"},"children":[{"type":"text","value":"Hantering av händelser"},{"type":"element","tagName":"a","properties":{"href":"#hantering-av-handelser","ariaLabel":"hantering av handelser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet skriver ut information om alla händelser som skickas från operativsystemet till programmet pygame, medan det körs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att programmet kördes ett tag och att man sedan klickade på avslutningsknappen. Programmet skriver ut följande information:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De första händelserna gäller musanvändningen, därefter kommer några händelser från tangentbordet och slutligen stänger den sista händelsen programmet. Varje händelse har åtminstone en typ, men de kan också innehålla annan identifierande information, till exempel var muspekaren befinner sig eller vilken tangent som trycktes in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan leta efter händelsebeskrivningar i pygame-dokumentationen, men det kan ibland vara enklare att skriva ut händelser med koden ovan och leta efter den händelse som inträffar när något du vill reagera på händer."}]},{"type":"element","tagName":"h2","properties":{"id":"tangentbordshandelser","style":"position:relative;"},"children":[{"type":"text","value":"Tangentbordshändelser"},{"type":"element","tagName":"a","properties":{"href":"#tangentbordshandelser","ariaLabel":"tangentbordshandelser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta program kan behandla händelser där användaren trycker på piltangenten antingen till höger eller till vänster på sitt tangentbord. Programmet skriver ut vilken tangent som trycktes in."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vänster\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"höger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstanterna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.K_LEFT"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.K_RIGHT"}]},{"type":"text","value":" avser piltangenterna till vänster och höger. Konstanterna för pygame-tangenterna för de olika tangenterna på ett tangentbord anges i "},{"type":"element","tagName":"a","properties":{"href":"https://www.pygame.org/docs/ref/key.html#key-constants-label","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pygame dokumentationen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren t.ex. trycker på piltangenten till höger två gånger, sedan den vänstra en gång och sedan den högra en gång till, skriver programmet ut"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"höger\nhöger\nvänster\nhöger"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har nu alla verktyg som behövs för att flytta en karaktär, eller sprite, på skärmen till höger och vänster med piltangenterna. Följande kod kommer att uppnå detta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Beroende på hur du använder piltangenterna kunde programmet köra på följande sätt:"}]},{"type":"element","tagName":"img","properties":{"src":"/aac8387e39d3bb4adbfc1c36cae57345/pygame_liikutus.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I koden ovan har vi variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" som innehåller sprite-koordinaterna. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" är inställd så att spriten visas längst ned i fönstret. Värdet för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" ändras inte under hela körningen av programmet. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"-värdet ökar däremot med 10 när användaren trycker på piltangenten till höger och minskar med 10 när användaren trycker på piltangenten till vänster."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet fungerar i övrigt ganska bra, men tangenten måste tryckas in igen varje gång vi vill förflytta oss igen. Det skulle vara bättre om rörelsen var kontinuerlig när tangenten hölls nedtryckt. Följande program erbjuder denna funktionalitet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhoger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\nvanster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n vanster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hoger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYUP"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n vanster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hoger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" hoger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" vanster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden innehåller nu variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hoger"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vanster"}]},{"type":"text","value":". Dessa innehåller vetskap om huruvida spriten ska röra sig åt höger eller vänster vid ett givet tillfälle. När användaren trycker ner en piltangent blir värdet som lagras i den relevanta variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". När tangenten släpps ändras värdet till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klockan används för att tidsbestämma spritens rörelser, så att de potentiellt sker 60 gånger per sekund. Om en piltangent trycks ned förflyttas spriten två pixlar åt höger eller vänster. Detta innebär att spriten rör sig 120 pixlar per sekund om tangenten hålls nedtryckt."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fyra riktningar","tmcname":"osa13-11_fyra_riktningar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program där spelaren kan flytta en robot i fyra riktningar med piltangenterna på tangentbordet. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/0461dfa5cb7291b279185dba96dfd819/pygame_nelja_suuntaa.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fyra väggar","tmcname":"osa13-12_fyra_vaggar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förbättra programmet i den föregående övningen så att roboten inte kan passera utanför fönstret i någon av de fyra riktningarna. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/bddcb9af6c636563eea121e3360b7b37/pygame_nelja_seinaa.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Två spelare","tmcname":"osa13-13_tva_spelare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program där två spelare styr var sin robot. En av spelarna ska använda piltangenterna medan den andra kan använda t.ex. w-a-s-d-tangenterna. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/57101a815ad1b5363307bc90dc112da6/pygame_kaksi_pelaajaa.gif"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"handelser-med-musen","style":"position:relative;"},"children":[{"type":"text","value":"Händelser med musen"},{"type":"element","tagName":"a","properties":{"href":"#handelser-med-musen","ariaLabel":"handelser med musen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande kod reagerar på händelser där en musknapp trycks ned medan markören befinner sig inom fönsterområdet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEBUTTONDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"du tryckte knappen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"button"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"på lokationen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekveringen av detta program borde mer eller mindre se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"du tryckte knappen 1 på lokationen (82, 135)\ndu tryckte knappen 1 på lokationen (369, 135)\ndu tryckte knappen 1 på lokationen (269, 297)\ndu tryckte knappen 3 på lokationen (515, 324)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Knapp nummer 1 avser vänster musknapp och knapp nummer 3 avser höger musknapp."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nästa program kombinerar hantering av mushändelser och ritning av en bild på skärmen. När användaren trycker på en musknapp medan muspekaren befinner sig inom fönstrets gränser ritas en bild av en robot på den platsen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEBUTTONDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekveringen av programmet borde se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/a41511bd13655f68553cc7e34de61edf/pygame_hiiri.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program innehåller en animation där robotspriten följer muspekaren. Spritens position lagras i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_y"}]},{"type":"text","value":". När musen rör sig lagras dess position i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mal_x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mal_y"}]},{"type":"text","value":". Om roboten inte befinner sig på denna plats förflyttar den sig i lämplig riktning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nrobot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nmal_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nmal_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\nklocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEMOTION"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mal_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n mal_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" mal_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" mal_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" mal_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" mal_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekveringen av programmet borde se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/cd4d53df2a1564c181f38b7a2d45e7c9/pygame_hiiri2.gif"},"children":[]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Roboten och musen","tmcname":"osa13-14_roboten_och_musen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program där roboten följer muspekaren så att robotens mittpunkt alltid är direkt vid muspekaren. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/58d3b2e994913c8089688bcde5e4ed86/pygame_robotti_hiiri.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robotens plats","tmcname":"osa13-15_robotens_plats"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program där roboten dyker upp på en slumpmässig plats i fönstret. När spelaren klickar på roboten med musen förflyttar sig roboten till en ny plats. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/93991031970283be78df3ba4341a7362/pygame_robotti_paikka.gif"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att vara bekant med pygame händelser
  • \n
  • Kommer du att kunna skriva ett program som reagerar på tangenttryckningar
  • \n
  • Kommer du att kunna skriva ett program som reagerar på mushändelser
  • \n

Hittills har våra huvudloopar bara kört förutbestämda animationer och reagerat på händelser av typen pygame.QUIT, trots att loopen får en lista över alla händelser från operativsystemet. Låt oss nu ta itu med några andra typer av händelser.

Hantering av händelser

Det här programmet skriver ut information om alla händelser som skickas från operativsystemet till programmet pygame, medan det körs:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for handelse in pygame.event.get():\n        print(handelse)\n        if handelse.type == pygame.QUIT:\n            exit()

Låt oss anta att programmet kördes ett tag och att man sedan klickade på avslutningsknappen. Programmet skriver ut följande information:

<Event(4-MouseMotion {'pos': (495, 274), 'rel': (495, 274), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (494, 274), 'rel': (-1, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (492, 274), 'rel': (-2, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (491, 274), 'rel': (-1, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(5-MouseButtonDown {'pos': (491, 274), 'button': 1, 'window': None})>\n<Event(6-MouseButtonUp {'pos': (491, 274), 'button': 1, 'window': None})>\n<Event(2-KeyDown {'unicode': 'a', 'key': 97, 'mod': 0, 'scancode': 38, 'window': None})>\n<Event(3-KeyUp {'key': 97, 'mod': 0, 'scancode': 38, 'window': None})>\n<Event(2-KeyDown {'unicode': 'b', 'key': 98, 'mod': 0, 'scancode': 56, 'window': None})>\n<Event(3-KeyUp {'key': 98, 'mod': 0, 'scancode': 56, 'window': None})>\n<Event(2-KeyDown {'unicode': 'c', 'key': 99, 'mod': 0, 'scancode': 54, 'window': None})>\n<Event(3-KeyUp {'key': 99, 'mod': 0, 'scancode': 54, 'window': None})>\n<Event(12-Quit {})>

De första händelserna gäller musanvändningen, därefter kommer några händelser från tangentbordet och slutligen stänger den sista händelsen programmet. Varje händelse har åtminstone en typ, men de kan också innehålla annan identifierande information, till exempel var muspekaren befinner sig eller vilken tangent som trycktes in.

Du kan leta efter händelsebeskrivningar i pygame-dokumentationen, men det kan ibland vara enklare att skriva ut händelser med koden ovan och leta efter den händelse som inträffar när något du vill reagera på händer.

Tangentbordshändelser

Detta program kan behandla händelser där användaren trycker på piltangenten antingen till höger eller till vänster på sitt tangentbord. Programmet skriver ut vilken tangent som trycktes in.

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.KEYDOWN:\n            if handelse.key == pygame.K_LEFT:\n                print(\"vänster\")\n            if handelse.key == pygame.K_RIGHT:\n                print(\"höger\")\n\n        if handelse.type == pygame.QUIT:\n            exit()

Konstanterna pygame.K_LEFT och pygame.K_RIGHT avser piltangenterna till vänster och höger. Konstanterna för pygame-tangenterna för de olika tangenterna på ett tangentbord anges i Pygame dokumentationen.

Om användaren t.ex. trycker på piltangenten till höger två gånger, sedan den vänstra en gång och sedan den högra en gång till, skriver programmet ut

höger\nhöger\nvänster\nhöger

Vi har nu alla verktyg som behövs för att flytta en karaktär, eller sprite, på skärmen till höger och vänster med piltangenterna. Följande kod kommer att uppnå detta:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\nx = 0\ny = 480-robot.get_height()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.KEYDOWN:\n            if handelse.key == pygame.K_LEFT:\n                x -= 10\n            if handelse.key == pygame.K_RIGHT:\n                x += 10\n\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()

Beroende på hur du använder piltangenterna kunde programmet köra på följande sätt:

I koden ovan har vi variablerna x och y som innehåller sprite-koordinaterna. Variabeln y är inställd så att spriten visas längst ned i fönstret. Värdet för y ändras inte under hela körningen av programmet. x-värdet ökar däremot med 10 när användaren trycker på piltangenten till höger och minskar med 10 när användaren trycker på piltangenten till vänster.

Programmet fungerar i övrigt ganska bra, men tangenten måste tryckas in igen varje gång vi vill förflytta oss igen. Det skulle vara bättre om rörelsen var kontinuerlig när tangenten hölls nedtryckt. Följande program erbjuder denna funktionalitet:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\nx = 0\ny = 480-robot.get_height()\n\nhoger = False\nvanster = False\n\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.KEYDOWN:\n            if handelse.key == pygame.K_LEFT:\n                vanster = True\n            if handelse.key == pygame.K_RIGHT:\n                hoger = True\n\n        if handelse.type == pygame.KEYUP:\n            if handelse.key == pygame.K_LEFT:\n                vanster = False\n            if handelse.key == pygame.K_RIGHT:\n                hoger = False\n\n        if handelse.type == pygame.QUIT:\n            exit()\n\n    if hoger:\n        x += 2\n    if vanster:\n        x -= 2\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (x, y))\n    pygame.display.flip()\n\n    klocka.tick(60)

Koden innehåller nu variablerna hoger och vanster. Dessa innehåller vetskap om huruvida spriten ska röra sig åt höger eller vänster vid ett givet tillfälle. När användaren trycker ner en piltangent blir värdet som lagras i den relevanta variabeln True. När tangenten släpps ändras värdet till False.

Klockan används för att tidsbestämma spritens rörelser, så att de potentiellt sker 60 gånger per sekund. Om en piltangent trycks ned förflyttas spriten två pixlar åt höger eller vänster. Detta innebär att spriten rör sig 120 pixlar per sekund om tangenten hålls nedtryckt.

Skriv ett program där spelaren kan flytta en robot i fyra riktningar med piltangenterna på tangentbordet. Slutresultatet ska se ut så här:

Förbättra programmet i den föregående övningen så att roboten inte kan passera utanför fönstret i någon av de fyra riktningarna. Slutresultatet ska se ut så här:

Skriv ett program där två spelare styr var sin robot. En av spelarna ska använda piltangenterna medan den andra kan använda t.ex. w-a-s-d-tangenterna. Slutresultatet ska se ut så här:

Händelser med musen

Följande kod reagerar på händelser där en musknapp trycks ned medan markören befinner sig inom fönsterområdet:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.MOUSEBUTTONDOWN:\n            print(\"du tryckte knappen\", handelse.button, \"på lokationen\", handelse.pos)\n\n        if handelse.type == pygame.QUIT:\n            exit()

Exekveringen av detta program borde mer eller mindre se ut så här:

du tryckte knappen 1 på lokationen (82, 135)\ndu tryckte knappen 1 på lokationen (369, 135)\ndu tryckte knappen 1 på lokationen (269, 297)\ndu tryckte knappen 3 på lokationen (515, 324)

Knapp nummer 1 avser vänster musknapp och knapp nummer 3 avser höger musknapp.

Nästa program kombinerar hantering av mushändelser och ritning av en bild på skärmen. När användaren trycker på en musknapp medan muspekaren befinner sig inom fönstrets gränser ritas en bild av en robot på den platsen.

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.MOUSEBUTTONDOWN:\n            x = handelse.pos[0]-robot.get_width()/2\n            y = handelse.pos[1]-robot.get_height()/2\n\n            fonster.fill((0, 0, 0))\n            fonster.blit(robot, (x, y))\n            pygame.display.flip()\n\n        if handelse.type == pygame.QUIT:\n            exit()

Exekveringen av programmet borde se ut så här:

Följande program innehåller en animation där robotspriten följer muspekaren. Spritens position lagras i variablerna robot_x och robot_y. När musen rör sig lagras dess position i variablerna mal_x och mal_y. Om roboten inte befinner sig på denna plats förflyttar den sig i lämplig riktning.

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nrobot_x = 0\nrobot_y = 0\nmal_x = 0\nmal_y = 0\n\nklocka = pygame.time.Clock()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.MOUSEMOTION:\n            mal_x = handelse.pos[0]-robot.get_width()/2\n            mal_y = handelse.pos[1]-robot.get_height()/2\n\n        if handelse.type == pygame.QUIT:\n            exit(0)\n\n    if robot_x > mal_x:\n        robot_x -= 1\n    if robot_x < mal_x:\n        robot_x += 1\n    if robot_y > mal_y:\n        robot_y -= 1\n    if robot_y < mal_y:\n        robot_y += 1\n\n    fonster.fill((0, 0, 0))\n    fonster.blit(robot, (robot_x, robot_y))\n    pygame.display.flip()\n\n    klocka.tick(60)

Exekveringen av programmet borde se ut så här:

Skriv ett program där roboten följer muspekaren så att robotens mittpunkt alltid är direkt vid muspekaren. Slutresultatet ska se ut så här:

Skriv ett program där roboten dyker upp på en slumpmässig plats i fönstret. När spelaren klickar på roboten med musen förflyttar sig roboten till en ny plats. Slutresultatet ska se ut så här:

","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/3-handelser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-13/4-fler-pygame-tekniker/page-data.json b/page-data/osa-13/4-fler-pygame-tekniker/page-data.json index 479d0018b..d2f91dbe4 100644 --- a/page-data/osa-13/4-fler-pygame-tekniker/page-data.json +++ b/page-data/osa-13/4-fler-pygame-tekniker/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-13/4-fler-pygame-tekniker", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att veta hur man titlar pygame-fönstret"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna rita former med pygame"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att veta hur du visar text i ditt fönster"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"fonstrets-titel","style":"position:relative;"},"children":[{"type":"text","value":"Fönstrets titel"},{"type":"element","tagName":"a","properties":{"href":"#fonstrets-titel","ariaLabel":"fonstrets titel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dina program kommer att se mer professionella ut om fönstertiteln istället för \"pygame window\" innehåller det faktiska namnet på programmet. Titeln ställs in med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.set_caption"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det stora äventyret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-rita-former","style":"position:relative;"},"children":[{"type":"text","value":"Att rita former"},{"type":"element","tagName":"a","properties":{"href":"#att-rita-former","ariaLabel":"att rita former permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program ritar en rektangel, en cirkel och en linje på skärmen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"250"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"circle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"150"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"80"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"120"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"300"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"160"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Körning av koden ovan borde se ut enligt följande:"}]},{"type":"element","tagName":"img","properties":{"src":"/9a7380e2e818c94a080d41344f073d5a/pygame_kuviot.gif"},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"att-rita-text","style":"position:relative;"},"children":[{"type":"text","value":"Att rita text"},{"type":"element","tagName":"a","properties":{"href":"#att-rita-text","ariaLabel":"att rita text permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Text i pygame ritas i två steg: först skapar vi en bild som innehåller den önskade texten, och sedan ritas denna bild på skärmen. Det fungerar på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfont "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"SysFont"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arial\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Körning av koden ovan borde se ut enligt följande:"}]},{"type":"element","tagName":"img","properties":{"src":"/ff536f08ddabdda8d522cc90c967ca8e/pygame_teksti.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här skapar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.font.SysFont"}]},{"type":"text","value":" ett typsnittsobjekt, som använder systemtypsnittet Arial i storlek 24. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"render"}]},{"type":"text","value":" skapar sedan en bild av den angivna texten i den angivna färgen. Denna bild ritas på fönstret med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":", precis som tidigare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: olika system kommer att ha olika teckensnitt tillgängliga. Om det system som det här programmet körs på inte har teckensnittet Arial, trots att Arial är ett mycket vanligt teckensnitt som finns på de flesta system, används istället systemets standardteckensnitt. Om du behöver ha ett specifikt teckensnitt tillgängligt för ditt spel kan du inkludera teckensnittsfilen i spelkatalogen och ange dess plats för metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.font.Font"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"ovningar","style":"position:relative;"},"children":[{"type":"text","value":"Övningar"},{"type":"element","tagName":"a","properties":{"href":"#ovningar","ariaLabel":"ovningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är några mer avancerade övningar för att öva på det du har lärt dig i denna del av kursmaterialet."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kello","tmcname":"osa13-16_kello"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som visar en urtavla som visar systemtiden. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/0e95a9664e02d0dd939be3ebfd4725d5/pygame_kello.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Asteroider","tmcname":"osa13-17_asteroider"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett spel där asteroider faller från himlen. Spelaren flyttar en robot till höger och vänster och försöker samla upp de fallande stenarna. Spelaren får ett poäng för varje asteroid som samlas in och den totala poängen visas högst upp i fönstret. Spelet tar slut när spelaren missar en asteroid. Slutresultatet bör se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/589bb5f7fafc7f948dfab758f0bd6273/pygame_asteroidit.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns bilden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sten.png"}]},{"type":"text","value":" för asteroiden."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara vänligen på en snabb enkät om denna del av kursen:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7ac40922-75c5-5306-96b6-029dec86aad9"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att veta hur man titlar pygame-fönstret
  • \n
  • Kommer du att kunna rita former med pygame
  • \n
  • Kommer du att veta hur du visar text i ditt fönster
  • \n

Fönstrets titel

Dina program kommer att se mer professionella ut om fönstertiteln istället för \"pygame window\" innehåller det faktiska namnet på programmet. Titeln ställs in med funktionen pygame.display.set_caption:

pygame.display.set_caption(\"Det stora äventyret\")

Att rita former

Följande program ritar en rektangel, en cirkel och en linje på skärmen:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\nfonster.fill((0, 0, 0))\n\npygame.draw.rect(fonster, (0, 255, 0), (50, 100, 200, 250))\npygame.draw.circle(fonster, (255, 0, 0), (200, 150), 40)\npygame.draw.line(fonster, (0, 0, 255), (80, 120), (300, 160), 2)\n\npygame.display.flip()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

Körning av koden ovan borde se ut enligt följande:

Att rita text

Text i pygame ritas i två steg: först skapar vi en bild som innehåller den önskade texten, och sedan ritas denna bild på skärmen. Det fungerar på följande sätt:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\nfonster.fill((0, 0, 0))\n\nfont = pygame.font.SysFont(\"Arial\", 24)\ntext = font.render(\"<Moikka!>\", True, (255, 0, 0))\nfonster.blit(text, (100, 50))\npygame.display.flip()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

Körning av koden ovan borde se ut enligt följande:

Här skapar metoden pygame.font.SysFont ett typsnittsobjekt, som använder systemtypsnittet Arial i storlek 24. Metoden render skapar sedan en bild av den angivna texten i den angivna färgen. Denna bild ritas på fönstret med metoden blit, precis som tidigare.

OBS: olika system kommer att ha olika teckensnitt tillgängliga. Om det system som det här programmet körs på inte har teckensnittet Arial, trots att Arial är ett mycket vanligt teckensnitt som finns på de flesta system, används istället systemets standardteckensnitt. Om du behöver ha ett specifikt teckensnitt tillgängligt för ditt spel kan du inkludera teckensnittsfilen i spelkatalogen och ange dess plats för metoden pygame.font.Font.

Övningar

Här är några mer avancerade övningar för att öva på det du har lärt dig i denna del av kursmaterialet.

Skriv ett program som visar en urtavla som visar systemtiden. Slutresultatet ska se ut så här:

Skapa ett spel där asteroider faller från himlen. Spelaren flyttar en robot till höger och vänster och försöker samla upp de fallande stenarna. Spelaren får ett poäng för varje asteroid som samlas in och den totala poängen visas högst upp i fönstret. Spelet tar slut när spelaren missar en asteroid. Slutresultatet bör se ut så här:

I uppgiftsbotten finns bilden sten.png för asteroiden.

Svara vänligen på en snabb enkät om denna del av kursen:

","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/4-fler-pygame-tekniker.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att veta hur man titlar pygame-fönstret"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna rita former med pygame"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att veta hur du visar text i ditt fönster"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"fonstrets-titel","style":"position:relative;"},"children":[{"type":"text","value":"Fönstrets titel"},{"type":"element","tagName":"a","properties":{"href":"#fonstrets-titel","ariaLabel":"fonstrets titel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dina program kommer att se mer professionella ut om fönstertiteln istället för \"pygame window\" innehåller det faktiska namnet på programmet. Titeln ställs in med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.set_caption"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det stora äventyret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-rita-former","style":"position:relative;"},"children":[{"type":"text","value":"Att rita former"},{"type":"element","tagName":"a","properties":{"href":"#att-rita-former","ariaLabel":"att rita former permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program ritar en rektangel, en cirkel och en linje på skärmen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"250"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"circle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"150"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"80"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"120"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"300"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"160"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Körning av koden ovan borde se ut enligt följande:"}]},{"type":"element","tagName":"img","properties":{"src":"/9a7380e2e818c94a080d41344f073d5a/pygame_kuviot.gif"},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"att-rita-text","style":"position:relative;"},"children":[{"type":"text","value":"Att rita text"},{"type":"element","tagName":"a","properties":{"href":"#att-rita-text","ariaLabel":"att rita text permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Text i pygame ritas i två steg: först skapar vi en bild som innehåller den önskade texten, och sedan ritas denna bild på skärmen. Det fungerar på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfont "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"SysFont"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arial\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Körning av koden ovan borde se ut enligt följande:"}]},{"type":"element","tagName":"img","properties":{"src":"/ff536f08ddabdda8d522cc90c967ca8e/pygame_teksti.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här skapar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.font.SysFont"}]},{"type":"text","value":" ett typsnittsobjekt, som använder systemtypsnittet Arial i storlek 24. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"render"}]},{"type":"text","value":" skapar sedan en bild av den angivna texten i den angivna färgen. Denna bild ritas på fönstret med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":", precis som tidigare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: olika system kommer att ha olika teckensnitt tillgängliga. Om det system som det här programmet körs på inte har teckensnittet Arial, trots att Arial är ett mycket vanligt teckensnitt som finns på de flesta system, används istället systemets standardteckensnitt. Om du behöver ha ett specifikt teckensnitt tillgängligt för ditt spel kan du inkludera teckensnittsfilen i spelkatalogen och ange dess plats för metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.font.Font"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"ovningar","style":"position:relative;"},"children":[{"type":"text","value":"Övningar"},{"type":"element","tagName":"a","properties":{"href":"#ovningar","ariaLabel":"ovningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är några mer avancerade övningar för att öva på det du har lärt dig i denna del av kursmaterialet."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kello","tmcname":"osa13-16_kello"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett program som visar en urtavla som visar systemtiden. Slutresultatet ska se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/0e95a9664e02d0dd939be3ebfd4725d5/pygame_kello.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Asteroider","tmcname":"osa13-17_asteroider"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett spel där asteroider faller från himlen. Spelaren flyttar en robot till höger och vänster och försöker samla upp de fallande stenarna. Spelaren får ett poäng för varje asteroid som samlas in och den totala poängen visas högst upp i fönstret. Spelet tar slut när spelaren missar en asteroid. Slutresultatet bör se ut så här:"}]},{"type":"element","tagName":"img","properties":{"src":"/589bb5f7fafc7f948dfab758f0bd6273/pygame_asteroidit.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns bilden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sten.png"}]},{"type":"text","value":" för asteroiden."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara vänligen på en snabb enkät om denna del av kursen:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7ac40922-75c5-5306-96b6-029dec86aad9"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att veta hur man titlar pygame-fönstret
  • \n
  • Kommer du att kunna rita former med pygame
  • \n
  • Kommer du att veta hur du visar text i ditt fönster
  • \n

Fönstrets titel

Dina program kommer att se mer professionella ut om fönstertiteln istället för \"pygame window\" innehåller det faktiska namnet på programmet. Titeln ställs in med funktionen pygame.display.set_caption:

pygame.display.set_caption(\"Det stora äventyret\")

Att rita former

Följande program ritar en rektangel, en cirkel och en linje på skärmen:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\nfonster.fill((0, 0, 0))\n\npygame.draw.rect(fonster, (0, 255, 0), (50, 100, 200, 250))\npygame.draw.circle(fonster, (255, 0, 0), (200, 150), 40)\npygame.draw.line(fonster, (0, 0, 255), (80, 120), (300, 160), 2)\n\npygame.display.flip()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

Körning av koden ovan borde se ut enligt följande:

Att rita text

Text i pygame ritas i två steg: först skapar vi en bild som innehåller den önskade texten, och sedan ritas denna bild på skärmen. Det fungerar på följande sätt:

import pygame\n\npygame.init()\nfonster = pygame.display.set_mode((640, 480))\nfonster.fill((0, 0, 0))\n\nfont = pygame.font.SysFont(\"Arial\", 24)\ntext = font.render(\"<Moikka!>\", True, (255, 0, 0))\nfonster.blit(text, (100, 50))\npygame.display.flip()\n\nwhile True:\n    for handelse in pygame.event.get():\n        if handelse.type == pygame.QUIT:\n            exit()

Körning av koden ovan borde se ut enligt följande:

Här skapar metoden pygame.font.SysFont ett typsnittsobjekt, som använder systemtypsnittet Arial i storlek 24. Metoden render skapar sedan en bild av den angivna texten i den angivna färgen. Denna bild ritas på fönstret med metoden blit, precis som tidigare.

OBS: olika system kommer att ha olika teckensnitt tillgängliga. Om det system som det här programmet körs på inte har teckensnittet Arial, trots att Arial är ett mycket vanligt teckensnitt som finns på de flesta system, används istället systemets standardteckensnitt. Om du behöver ha ett specifikt teckensnitt tillgängligt för ditt spel kan du inkludera teckensnittsfilen i spelkatalogen och ange dess plats för metoden pygame.font.Font.

Övningar

Här är några mer avancerade övningar för att öva på det du har lärt dig i denna del av kursmaterialet.

Skriv ett program som visar en urtavla som visar systemtiden. Slutresultatet ska se ut så här:

Skapa ett spel där asteroider faller från himlen. Spelaren flyttar en robot till höger och vänster och försöker samla upp de fallande stenarna. Spelaren får ett poäng för varje asteroid som samlas in och den totala poängen visas högst upp i fönstret. Spelet tar slut när spelaren missar en asteroid. Slutresultatet bör se ut så här:

I uppgiftsbotten finns bilden sten.png för asteroiden.

Svara vänligen på en snabb enkät om denna del av kursen:

","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/4-fler-pygame-tekniker.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-13/page-data.json b/page-data/osa-13/page-data.json index 44952dc4f..4eb68a366 100644 --- a/page-data/osa-13/page-data.json +++ b/page-data/osa-13/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-13", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-13","title":"Modul 13"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-13","title":"Modul 13"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-13/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-14/1-spelprojekt/page-data.json b/page-data/osa-14/1-spelprojekt/page-data.json index 3c623c55a..8e6aecb8a 100644 --- a/page-data/osa-14/1-spelprojekt/page-data.json +++ b/page-data/osa-14/1-spelprojekt/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-14/1-spelprojekt", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här modulen kommer vi att använda pygame för att skapa ett lite större spel. Det är en variant av det klassiska Sokoban spelet, där spelaren flyttar en robot på ett rutnät och skjuter lådor till rätt platser med så få drag som möjligt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Slutresultatet kommer att se ut så här:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 852px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/peli.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 44.34782608695652%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsTAAALEwEAmpwYAAACGUlEQVQozyWO0UsaAQCH73RHlgm10ztbJUEvkkIzzrsHxRPShx56LUYRkqVPC2rhqZ2iKGiEiFDsdAX2T0SshxhGk/J9hG+tWlIRBA41xm+4fY+/7+H7EbFYLBQKybKcSqWi0agkSfF4PJlMbm5urq9vJBIJeWtLkqRYLJZOpyVJikQi4XBYluVMJkOYzWaP12u323t6epxOp9frHR4eHhkZmZ6eFkVRq9VOcdzMzMz4+Pjg4KAoim632+PxGI1GjuOIvb09ALVajef5x8dHAH6/f2lpCUCj0TCZTOfn5wD29/dnZ2cBtNttAKurq1arlSgmk/fV6tfT09/N5sPzM4AVv39hcRHAz+vr0bGx7+Xyj8vL6sVFs9l8eXl5enoCEAgEbDYbkd/ZAfCtUnGL4kOlUj07q9Zqv66u7iuV45OTer3euLn58/paKpXm5+dbrdb/dz6fr1tWFAXA8dHRu6Gh57s7AJ8V5UuxCOD29vbTxka9XgeQz+c5jgPQ6XQABIPByclJ4sPCwu7u7sry8qjJlM5my4eHgiBwdnvp4CC7va3T6T6urZXL5bm5OYvFUigUcrmcoigul0sQBEJDUQRBUG+6qMku/RpNf19fd1Srdb29lEqlJkmKojQazT9PqlQqkiR5niem9HoLy7IGg4GmLQxjZVlerx+laRPLvmcYM00LDDPBsm+7nmZZlmEYo9E4MDDgcDj+AisL8KSDCe9vAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e334a0f0fea9438564e48d0bba7cbbc4/a0b58/peli.webp 230w","/static/e334a0f0fea9438564e48d0bba7cbbc4/bc10c/peli.webp 460w","/static/e334a0f0fea9438564e48d0bba7cbbc4/39392/peli.webp 852w"],"sizes":"(max-width: 852px) 100vw, 852px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e334a0f0fea9438564e48d0bba7cbbc4/81c8e/peli.png 230w","/static/e334a0f0fea9438564e48d0bba7cbbc4/08a84/peli.png 460w","/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/peli.png 852w"],"sizes":"(max-width: 852px) 100vw, 852px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/peli.png","alt":"peli","title":"peli","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"spelets-karta","style":"position:relative;"},"children":[{"type":"text","value":"Spelets karta"},{"type":"element","tagName":"a","properties":{"href":"#spelets-karta","ariaLabel":"spelets karta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja med att rita den karta som används i spelet. Spelet implementeras i klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sokoban"}]},{"type":"text","value":", som kommer att innehålla alla funktioner som krävs för att spela spelet. I detta första steg är innehållet i klassen följande:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Sokoban"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ladda_bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nytt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster_hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd\n fonster_bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster_bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fonster_hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sokoban\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"huvudloop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ladda_bilder"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"golv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mål\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"låda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"färdig\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"målrobot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"huvudloop"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kolla_handelser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rita_fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kolla_handelser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ruta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"ruta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n Sokoban"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du kör programmet visas ett fönster med spelets inledande tillstånd. Låt oss ta en närmare titt på koden som åstadkommer detta."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"konstruktorn","style":"position:relative;"},"children":[{"type":"text","value":"Konstruktorn"},{"type":"element","tagName":"a","properties":{"href":"#konstruktorn","ariaLabel":"konstruktorn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassens konstruktor initierar pygame-modulerna och de väsentliga variabler och datastrukturer som är involverade i spelet. Den anropar också spelets huvudloop-metod."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ladda_bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nytt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster_hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd\n fonster_bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster_bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fonster_hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sokoban\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"huvudloop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ladda_bilder"}]},{"type":"text","value":" laddar de bilder som används i spelet till en lista med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bilder"}]},{"type":"text","value":". Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"text","value":" skapar en tvådimensionell lista med namnet karta, som innehåller spelrutnätets tillstånd i början av spelet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hojd"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bredd"}]},{"type":"text","value":" sätts baserat på spelrutnätets dimensioner. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skala"}]},{"type":"text","value":" innehåller längden på sidan av en kvadrat i rutnätet. Eftersom varje bild är en kvadrat av exakt samma storlek täcks storleken på alla kvadrater av denna enda variabel, och bredden på den första bilden räcker gott och väl för värdet. Samma värde kan användas för att beräkna bredden och höjden på hela rutnätet, vilket gör att vi kan skapa ett fönster av lämplig storlek för att visa spelrutnätet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"att-ladda-bilder","style":"position:relative;"},"children":[{"type":"text","value":"Att ladda bilder"},{"type":"element","tagName":"a","properties":{"href":"#att-ladda-bilder","ariaLabel":"att ladda bilder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ladda_bilder"}]},{"type":"text","value":" laddar alla bilder som används i spelet:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ladda_bilder"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"golv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mål\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"låda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"färdig\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"målrobot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelet använder följande bilder:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"golvruta","style":"position:relative;"},"children":[{"type":"text","value":"Golvruta"},{"type":"element","tagName":"a","properties":{"href":"#golvruta","ariaLabel":"golvruta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/lattia.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAO0lEQVQ4y2M4dOjQ/7Nnz1IFg8xiADGoBUBmwQ389+8fRZh+BpIKRg0cNXDUwMFp4OAtbahWwFK7CgAAGH0Lv64YzdcAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9aefcdf4192b60fb3bf21288d49a0bd5/6ac13/lattia.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/lattia.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/lattia.png","alt":"lattia","title":"lattia","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"golv.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 0"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"vaggruta","style":"position:relative;"},"children":[{"type":"text","value":"Väggruta"},{"type":"element","tagName":"a","properties":{"href":"#vaggruta","ariaLabel":"vaggruta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/73ec5a44eba6d09dfe097ba346137a94/a1812/seina.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAASElEQVQ4y+3VMQoAMAgDwPhX/YSft0RwLrQZHQI6eGNEZpa7V0R8hQYtcAAgSVvUuZjZMzS3bQ2oyIILLrjgHZTVl7xg1S/gAHx86WTsjKFcAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73ec5a44eba6d09dfe097ba346137a94/6ac13/seina.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73ec5a44eba6d09dfe097ba346137a94/a1812/seina.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/73ec5a44eba6d09dfe097ba346137a94/a1812/seina.png","alt":"seina","title":"seina","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vägg.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 1"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"malruta","style":"position:relative;"},"children":[{"type":"text","value":"Målruta"},{"type":"element","tagName":"a","properties":{"href":"#malruta","ariaLabel":"malruta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/kohde.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABHElEQVQ4y7WVPQ6CUAzHWRw0yBG8gfEK3oCJlRNwB2/gBZzZPAU7I0zsbmiigQjav6nlgS+CUyVp0lfa3+vH4+EkSYI0TVWEWQ4rWg+zeiAR2fJ8gu53EL8z0ragpvn2JfoBZP3xGCBsK0vQ5WLbeMOPuHEgK00j+Z9OwG4HrNfAbAYsFoDvA3E81Gg2ngS2rThmGbBcAo4j4nmA6w7rIOiZ70xHgd1uyHPJhgPDUNa3G3C9AscjsNlY0OmSDXC7lYD9fnqcBhrHUnLXJhvIUhTiuFoBVSXB3AbOgKWuxcaZsp/v99O3gaa5h4M4RpEEmgEZYFfau3zu6XwOOp9BX+dQHahe8l+Gon5s1A/2Xz499ctB8/pSu2C1fwEvukCRvM6VtZAAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/6ac13/kohde.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/kohde.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/kohde.png","alt":"kohde","title":"kohde","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mål.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Roboten ska flytta en låda till den här rutan"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"lada","style":"position:relative;"},"children":[{"type":"text","value":"Låda"},{"type":"element","tagName":"a","properties":{"href":"#lada","ariaLabel":"lada permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/laatikko.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAzElEQVQ4y+2VPQqEMBCFvZxVjqAWAVN4CH8KtfEQQfAk9h5DC09go295Abcx7sIyWyzswIPATL6ZgckkGMcR0zSJiKyABykj6wnc9/0jyHEcThcgbds2zPOMZVneinHruvqBZ3XDMCAMQ2itEUUR4jj2ir4kSaCUctBLyyfQWossy9B1Heq6RtM0XtHHGEJZ6S2w73ukaeouFUVxqzzPHZSVsv2XQGMM2rZFWZaoqsorQpmU7f+BvwgUHRvxwRZ7euLL4WvrS2TBSn8BD7avDfrO6ztPAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e7afd4acd3f9b214a5c43273b23c834/6ac13/laatikko.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/laatikko.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/laatikko.png","alt":"laatikko","title":"laatikko","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"låda.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 3"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"robot","style":"position:relative;"},"children":[{"type":"text","value":"Robot"},{"type":"element","tagName":"a","properties":{"href":"#robot","ariaLabel":"robot permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robo.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB/klEQVQ4y5VV0WraYBjttbe+QR+iF32KXvdOWOeckVEGveqtF71qi6CusDFW6sSQJcwNklFHEcHZGfsAjdFCQCiFQew6qugZ58OUIqY1Hxw8nnP+w5+QP1mp1+uwbTsUlmWh1WoJyJ/KsmuFJGxG9/d4oyjo93oCcmphw66Hwul0KphMJvLf8zx8Oj5G/uQEFVUVkFOjx2E2WPdsYdd18S6Xw/v1dXwtlwXk1OhFLnT7fXw4OsLHjQ2Yug7TMIRToxe50HEcfC6XYZ2f47tpCsip0Ytc2Lu6QuHwEJl4HMlYDK9iMSjxuGj0ol8y72GhgNzuLrKKIiCn5i57D4MQZ+j70DQNVdNEMpMRkFOj9zj75A7l+RuNkM1mUSwW8a1axRdNE5BTo8fM/LrQQt/3kUgkUKlU5ITwBBDk1Oj5s10uVTgcDpFKpVAqldBoNFCr1QTk1OgxE6nwxdYWVFVFs9l82CE5NXqRCv/e3iKztob8/j5+np1B13UBef7gQDxmlioMzGvPww/LQjqdhmEYAnJq17OzPL8mdIfBL+d1MonBYCAgD+ZxdulCPhgvd3bkZUCQj6IWBua/8Ri/9vbwdnUV7uWlgJwavWcveX7uxmOcbm/jdHMTf25uBOTU7maFC1+wiz4B7XYbHdtGx3Hwu9tF5+JCINxxxGNm0SfgP7rpf7Qvj0ecAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5fb4f02fac6c4d361c9057099bc9f890/6ac13/robo.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robo.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robo.png","alt":"robo","title":"robo","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 4"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"lada-pa-malruta","style":"position:relative;"},"children":[{"type":"text","value":"Låda på målruta"},{"type":"element","tagName":"a","properties":{"href":"#lada-pa-malruta","ariaLabel":"lada pa malruta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/15225a12ccfe0e42a543bea73f4ff889/a1812/valmis.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABk0lEQVQ4y7WVPWsCQRCG7UTEX5Em/0IQbLS18gM80MLOIpgYb/fOu9mNcpD8g4hglzZtCNjbpkqrhV0IITb6hllivrzEDy4LL7dwew8zs+/MxcbjMSaTSSRiVow3US1mfQCXy+VBkNVqZbQB5LVYLDCdTjGbzbaKz83n83DgOrrRaIRsNgvLslAqlVAul0PF7yqVCvL5vIFupLwGDgYDNBoNBEEAIoJS6pu075unT2TOMJQj/RU4HA5Rq9XMR1LKTzkOHCFwphRs14UUwkA5Uk7/T2C9XofWGo7jwGWQ66IrJU57PdzmcrjLZNAmgiYy6e8FZJAnhIGNikU8J5O4ajbR8bzDgB0inCuFfquFp1QK15aFVr8PVwhTlt2BXCvPw6BahW638XB8jPt0GidBYCLmmu4FvFAKZ1rj8egIr/G4Adq+b2rK4ovaC9gjMsCbQgEviQQuuW5EpqZ8SXsBv9rGkRJd24bd7Zq9fI9OCGF8utU2YcYmraF+mJx2NXZo64W04NbWi3w4/Nv4imTARv0LeAO8hwm47qNppgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/15225a12ccfe0e42a543bea73f4ff889/6ac13/valmis.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/15225a12ccfe0e42a543bea73f4ff889/a1812/valmis.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/15225a12ccfe0e42a543bea73f4ff889/a1812/valmis.png","alt":"valmis","title":"valmis","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"färdig.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Lådan har flyttats till målrutan"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"malruta-och-robot","style":"position:relative;"},"children":[{"type":"text","value":"Målruta och robot"},{"type":"element","tagName":"a","properties":{"href":"#malruta-och-robot","ariaLabel":"malruta och robot permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/70f904da050def3ee01b9b01b0b50b16/a1812/kohderobo.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAC2klEQVQ4y41VXUsiURiemyANL7wRgm4q6D9U0B+IgrroIvrAqDVNWWKLrK66qD9gVEOrbF9axvSlSxq2hS1tVtoH1NWoSwlLdhEkFY3ps5wz4/bl2B54Zp55z/s+877nnHeG8fv9CAaDsvB6vQgEAhSE5/IlWgwhckN4fMRnoxG/o1EKwolNbhCtf4LpdJoilUrR59jlJaZnZjA2Nwfn4iIF4cQWi8WoD/HNxH0oGL64ADs6Cmt5Odbm5ymsFRVgLRaEef4/BYmTVFLk5AS28XF8q6uDZ3kZnpUVym0si0g8LgomkzRGXjCZBM3v+Bh8SQkcHAfv/j6+ezwU3oMDOBwO8DqdKEjipCzfCz49iRmenwN5eYgqFBi3WmFSq6FTKtGpVMKoVlNblGGA2lpRULbkjGBVFcAwiJhMYB0OWMxmDBuNFJaBAbBTU4gUFVGf1OysWLIgZBEkF55Hiry9tBSJeBzc6ipc6+vQmUwULrcbnM+HhNUqCtbUiLHJ5BtBKbv05CR1FDo7McyymLBY4Ha5sMRxFO61NUx8/YrhwUEI+flAQQHSNzdiMrkEb1tboe3thdNupx1COoAgsLcH59IStPX1uCWVqFQyglLJaXK+GAYJjQZ6rRb2hQX83NnB5uYmBeF2pxP65mYkiGB1tRj3ruQXm4LKSurc3tSERY7Dr91d+Le3KQgntvaWFlFwejrHpmQEz85wp1LBpNFgzG7Hj60tLHu9FIST9jMpFLhraHjVYbIHm06FQoiXlWGjvx9dej1WhoYougwGbPT1IW40Pi+T7MGWWg+CIN6vroCeHhg6OvCnsJDC0NYG2GzPxyyzmbk+Di/PFVmZT2YzwqenFIQL0o6SJfq45BeTD4KAvZERfCkuRiQSoSCc2B6kF76NeSX4dtwnk/B1d8PX2Iib62sKwontXhLM+oHN9gs4PDxEKBhEiOdxEA4jdHREQTnP0znik+0X8BeOmjrCN4yzpAAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/70f904da050def3ee01b9b01b0b50b16/6ac13/kohderobo.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/70f904da050def3ee01b9b01b0b50b16/a1812/kohderobo.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/70f904da050def3ee01b9b01b0b50b16/a1812/kohderobo.png","alt":"kohderobo","title":"kohderobo","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"målrobot.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Roboten kan också vara på en tom målruta"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"att-skapa-spelrutan","style":"position:relative;"},"children":[{"type":"text","value":"Att skapa spelrutan"},{"type":"element","tagName":"a","properties":{"href":"#att-skapa-spelrutan","ariaLabel":"att skapa spelrutan permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"text","value":" skapar den ursprungliga spelrutan:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden skapar en tvådimensionell lista med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"karta"}]},{"type":"text","value":" som använder de numrerade positionerna för bilderna i listan för att markera vilken bild som ska vara var. På så sätt innehåller spelet ett register över spelrutans tillstånd vid alla tidpunkter."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: I början innehåller alla rutor på spelplanen ett nummer mellan 0 och 4. Siffrorna 5 och 6 ingår inte, eftersom det i början inte finns någon låda eller robot på en målruta."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"huvudloopen","style":"position:relative;"},"children":[{"type":"text","value":"Huvudloopen"},{"type":"element","tagName":"a","properties":{"href":"#huvudloopen","ariaLabel":"huvudloopen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"huvudloop"}]},{"type":"text","value":" är ganska kort. Vid varje iteration anropar den två metoder: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kolla_handelser"}]},{"type":"text","value":" går igenom alla händelser som samlats in sedan föregående iteration, och metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"text","value":" uppdaterar innehållet i fönstret."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"huvudloop"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kolla_handelser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rita_fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kolla_handelser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ruta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"ruta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här skedet är den enda händelse som faktiskt hanteras av spelet att stänga spelfönstret, t.ex. med exit-knappen. Spelet avslutas sedan genom att anropa Pythons "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":"-funktion."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje gång metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"text","value":" anropas korsas hela spelrutnätet igenom och den bild som motsvarar varje ruta i rutnätet ritas på rätt plats."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: koordinaterna x och y används på två olika sätt i spelet. När man hanterar index i en tvådimensionell lista är det logiskt att ange y-koordinaten först, eftersom y hänvisar till numret på raden medan x är numret på kolumnen. Å andra sidan, när man använder pygame-metoder, skickas x vanligtvis först, vilket det ganska ofta gör när man arbetar med grafik och även i matematiska sammanhang."}]}],"data":{"quirksMode":false}},"html":"

I den här modulen kommer vi att använda pygame för att skapa ett lite större spel. Det är en variant av det klassiska Sokoban spelet, där spelaren flyttar en robot på ett rutnät och skjuter lådor till rätt platser med så få drag som möjligt.

\n

Slutresultatet kommer att se ut så här:

\n\n \n \n \n \n \n \"peli\"\n \n \n \n

Spelets karta

\n

Låt oss börja med att rita den karta som används i spelet. Spelet implementeras i klassen Sokoban, som kommer att innehålla alla funktioner som krävs för att spela spelet. I detta första steg är innehållet i klassen följande:

\n
import pygame\n\nclass Sokoban:\n    def __init__(self):\n        pygame.init()\n\n        self.ladda_bilder()\n        self.nytt_spel()\n\n        self.hojd = len(self.karta)\n        self.bredd = len(self.karta[0])\n        self.skala = self.bilder[0].get_width()\n\n        fonster_hojd = self.skala * self.hojd\n        fonster_bredd = self.skala * self.bredd\n        self.fonster = pygame.display.set_mode((fonster_bredd, fonster_hojd))\n\n        pygame.display.set_caption(\"Sokoban\")\n\n        self.huvudloop()\n\n    def ladda_bilder(self):\n        self.bilder = []\n        for namn in [\"golv\", \"vägg\", \"mål\", \"låda\", \"robot\", \"färdig\", \"målrobot\"]:\n            self.bilder.append(pygame.image.load(namn + \".png\"))\n\n    def nytt_spel(self):\n        self.karta = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                       [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],\n                       [1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]\n\n    def huvudloop(self):\n        while True:\n            self.kolla_handelser()\n            self.rita_fonster()\n\n    def kolla_handelser(self):\n        for handelse in pygame.event.get():\n            if handelse.type == pygame.QUIT:\n                exit()\n\n    def rita_fonster(self):\n        self.fonster.fill((0, 0, 0))\n\n        for y in range(self.hojd):\n            for x in range(self.bredd):\n                ruta = self.karta[y][x]\n                self.fonster.blit(self.bilder[ruta], (x * self.skala, y * self.skala))\n\n        pygame.display.flip()\n\nif __name__ == \"__main__\":\n    Sokoban()
\n

Om du kör programmet visas ett fönster med spelets inledande tillstånd. Låt oss ta en närmare titt på koden som åstadkommer detta.

\n

Konstruktorn

\n

Klassens konstruktor initierar pygame-modulerna och de väsentliga variabler och datastrukturer som är involverade i spelet. Den anropar också spelets huvudloop-metod.

\n
    def __init__(self):\n        pygame.init()\n\n        self.ladda_bilder()\n        self.nytt_spel()\n\n        self.hojd = len(self.karta)\n        self.bredd = len(self.karta[0])\n        self.skala = self.bilder[0].get_width()\n\n        fonster_hojd = self.skala * self.hojd\n        fonster_bredd = self.skala * self.bredd\n        self.fonster = pygame.display.set_mode((fonster_bredd, fonster_hojd))\n\n        pygame.display.set_caption(\"Sokoban\")\n\n        self.huvudloop()
\n

Metoden ladda_bilder laddar de bilder som används i spelet till en lista med namnet bilder. Metoden nytt_spel skapar en tvådimensionell lista med namnet karta, som innehåller spelrutnätets tillstånd i början av spelet.

\n

Variablerna hojd och bredd sätts baserat på spelrutnätets dimensioner. Variabeln skala innehåller längden på sidan av en kvadrat i rutnätet. Eftersom varje bild är en kvadrat av exakt samma storlek täcks storleken på alla kvadrater av denna enda variabel, och bredden på den första bilden räcker gott och väl för värdet. Samma värde kan användas för att beräkna bredden och höjden på hela rutnätet, vilket gör att vi kan skapa ett fönster av lämplig storlek för att visa spelrutnätet.

\n

Att ladda bilder

\n

Metoden ladda_bilder laddar alla bilder som används i spelet:

\n
    def ladda_bilder(self):\n        self.bilder = []\n        for namn in [\"golv\", \"vägg\", \"mål\", \"låda\", \"robot\", \"färdig\", \"målrobot\"]:\n            self.bilder.append(pygame.image.load(namn + \".png\"))
\n

Spelet använder följande bilder:

\n

Golvruta

\n\n \n \n \n \n \n \"seina\"\n \n \n \n
    \n
  • Filnamn: vägg.png
  • \n
  • Position i listan: 1
  • \n
\n

Målruta

\n\n \n \n \n \n \n \"laatikko\"\n \n \n \n
    \n
  • Filnamn: låda.png
  • \n
  • Position i listan: 3
  • \n
\n

Robot

\n\n \n \n \n \n \n \"robo\"\n \n \n \n
    \n
  • Filnamn robot.png
  • \n
  • Position i listan: 4
  • \n
\n

Låda på målruta

\n\n \n \n \n \n \n \"valmis\"\n \n \n \n
    \n
  • Filnamn: färdig.png
  • \n
  • Position i listan: 5
  • \n
  • Lådan har flyttats till målrutan
  • \n
\n

Målruta och robot

\n\n \n \n \n \n \n \"kohderobo\"\n \n \n \n
    \n
  • Filnamn: målrobot.png
  • \n
  • Position i listan: 6
  • \n
  • Roboten kan också vara på en tom målruta
  • \n
\n

Att skapa spelrutan

\n

Metoden nytt_spel skapar den ursprungliga spelrutan:

\n
    def nytt_spel(self):\n        self.karta = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                       [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],\n                       [1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
\n

Metoden skapar en tvådimensionell lista med namnet karta som använder de numrerade positionerna för bilderna i listan för att markera vilken bild som ska vara var. På så sätt innehåller spelet ett register över spelrutans tillstånd vid alla tidpunkter.

\n

OBS: I början innehåller alla rutor på spelplanen ett nummer mellan 0 och 4. Siffrorna 5 och 6 ingår inte, eftersom det i början inte finns någon låda eller robot på en målruta.

\n

Huvudloopen

\n

Metoden huvudloop är ganska kort. Vid varje iteration anropar den två metoder: kolla_handelser går igenom alla händelser som samlats in sedan föregående iteration, och metoden rita_fonster uppdaterar innehållet i fönstret.

\n
    def huvudloop(self):\n        while True:\n            self.kolla_handelser()\n            self.rita_fonster()\n\n    def kolla_handelser(self):\n        for handelse in pygame.event.get():\n            if handelse.type == pygame.QUIT:\n                exit()\n\n    def rita_fonster(self):\n        self.fonster.fill((0, 0, 0))\n\n        for y in range(self.hojd):\n            for x in range(self.bredd):\n                ruta = self.karta[y][x]\n                self.fonster.blit(self.bilder[ruta], (x * self.skala, y * self.skala))\n\n        pygame.display.flip()
\n

I det här skedet är den enda händelse som faktiskt hanteras av spelet att stänga spelfönstret, t.ex. med exit-knappen. Spelet avslutas sedan genom att anropa Pythons exit-funktion.

\n

Varje gång metoden rita_fonster anropas korsas hela spelrutnätet igenom och den bild som motsvarar varje ruta i rutnätet ritas på rätt plats.

\n

OBS: koordinaterna x och y används på två olika sätt i spelet. När man hanterar index i en tvådimensionell lista är det logiskt att ange y-koordinaten först, eftersom y hänvisar till numret på raden medan x är numret på kolumnen. Å andra sidan, när man använder pygame-metoder, skickas x vanligtvis först, vilket det ganska ofta gör när man arbetar med grafik och även i matematiska sammanhang.

","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/1-spelprojekt.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här modulen kommer vi att använda pygame för att skapa ett lite större spel. Det är en variant av det klassiska Sokoban spelet, där spelaren flyttar en robot på ett rutnät och skjuter lådor till rätt platser med så få drag som möjligt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Slutresultatet kommer att se ut så här:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 852px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/peli.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 44.34782608695652%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsTAAALEwEAmpwYAAACGUlEQVQozyWO0UsaAQCH73RHlgm10ztbJUEvkkIzzrsHxRPShx56LUYRkqVPC2rhqZ2iKGiEiFDsdAX2T0SshxhGk/J9hG+tWlIRBA41xm+4fY+/7+H7EbFYLBQKybKcSqWi0agkSfF4PJlMbm5urq9vJBIJeWtLkqRYLJZOpyVJikQi4XBYluVMJkOYzWaP12u323t6epxOp9frHR4eHhkZmZ6eFkVRq9VOcdzMzMz4+Pjg4KAoim632+PxGI1GjuOIvb09ALVajef5x8dHAH6/f2lpCUCj0TCZTOfn5wD29/dnZ2cBtNttAKurq1arlSgmk/fV6tfT09/N5sPzM4AVv39hcRHAz+vr0bGx7+Xyj8vL6sVFs9l8eXl5enoCEAgEbDYbkd/ZAfCtUnGL4kOlUj07q9Zqv66u7iuV45OTer3euLn58/paKpXm5+dbrdb/dz6fr1tWFAXA8dHRu6Gh57s7AJ8V5UuxCOD29vbTxka9XgeQz+c5jgPQ6XQABIPByclJ4sPCwu7u7sry8qjJlM5my4eHgiBwdnvp4CC7va3T6T6urZXL5bm5OYvFUigUcrmcoigul0sQBEJDUQRBUG+6qMku/RpNf19fd1Srdb29lEqlJkmKojQazT9PqlQqkiR5niem9HoLy7IGg4GmLQxjZVlerx+laRPLvmcYM00LDDPBsm+7nmZZlmEYo9E4MDDgcDj+AisL8KSDCe9vAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e334a0f0fea9438564e48d0bba7cbbc4/a0b58/peli.webp 230w","/static/e334a0f0fea9438564e48d0bba7cbbc4/bc10c/peli.webp 460w","/static/e334a0f0fea9438564e48d0bba7cbbc4/39392/peli.webp 852w"],"sizes":"(max-width: 852px) 100vw, 852px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e334a0f0fea9438564e48d0bba7cbbc4/81c8e/peli.png 230w","/static/e334a0f0fea9438564e48d0bba7cbbc4/08a84/peli.png 460w","/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/peli.png 852w"],"sizes":"(max-width: 852px) 100vw, 852px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/peli.png","alt":"peli","title":"peli","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"spelets-karta","style":"position:relative;"},"children":[{"type":"text","value":"Spelets karta"},{"type":"element","tagName":"a","properties":{"href":"#spelets-karta","ariaLabel":"spelets karta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss börja med att rita den karta som används i spelet. Spelet implementeras i klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sokoban"}]},{"type":"text","value":", som kommer att innehålla alla funktioner som krävs för att spela spelet. I detta första steg är innehållet i klassen följande:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Sokoban"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ladda_bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nytt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster_hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd\n fonster_bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster_bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fonster_hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sokoban\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"huvudloop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ladda_bilder"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"golv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mål\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"låda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"färdig\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"målrobot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"huvudloop"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kolla_handelser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rita_fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kolla_handelser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ruta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"ruta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n Sokoban"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du kör programmet visas ett fönster med spelets inledande tillstånd. Låt oss ta en närmare titt på koden som åstadkommer detta."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"konstruktorn","style":"position:relative;"},"children":[{"type":"text","value":"Konstruktorn"},{"type":"element","tagName":"a","properties":{"href":"#konstruktorn","ariaLabel":"konstruktorn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassens konstruktor initierar pygame-modulerna och de väsentliga variabler och datastrukturer som är involverade i spelet. Den anropar också spelets huvudloop-metod."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ladda_bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nytt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fonster_hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd\n fonster_bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster_bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fonster_hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sokoban\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"huvudloop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ladda_bilder"}]},{"type":"text","value":" laddar de bilder som används i spelet till en lista med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bilder"}]},{"type":"text","value":". Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"text","value":" skapar en tvådimensionell lista med namnet karta, som innehåller spelrutnätets tillstånd i början av spelet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hojd"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bredd"}]},{"type":"text","value":" sätts baserat på spelrutnätets dimensioner. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skala"}]},{"type":"text","value":" innehåller längden på sidan av en kvadrat i rutnätet. Eftersom varje bild är en kvadrat av exakt samma storlek täcks storleken på alla kvadrater av denna enda variabel, och bredden på den första bilden räcker gott och väl för värdet. Samma värde kan användas för att beräkna bredden och höjden på hela rutnätet, vilket gör att vi kan skapa ett fönster av lämplig storlek för att visa spelrutnätet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"att-ladda-bilder","style":"position:relative;"},"children":[{"type":"text","value":"Att ladda bilder"},{"type":"element","tagName":"a","properties":{"href":"#att-ladda-bilder","ariaLabel":"att ladda bilder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ladda_bilder"}]},{"type":"text","value":" laddar alla bilder som används i spelet:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ladda_bilder"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"golv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mål\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"låda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"färdig\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"målrobot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelet använder följande bilder:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"golvruta","style":"position:relative;"},"children":[{"type":"text","value":"Golvruta"},{"type":"element","tagName":"a","properties":{"href":"#golvruta","ariaLabel":"golvruta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/lattia.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAO0lEQVQ4y2M4dOjQ/7Nnz1IFg8xiADGoBUBmwQ389+8fRZh+BpIKRg0cNXDUwMFp4OAtbahWwFK7CgAAGH0Lv64YzdcAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9aefcdf4192b60fb3bf21288d49a0bd5/6ac13/lattia.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/lattia.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/lattia.png","alt":"lattia","title":"lattia","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"golv.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 0"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"vaggruta","style":"position:relative;"},"children":[{"type":"text","value":"Väggruta"},{"type":"element","tagName":"a","properties":{"href":"#vaggruta","ariaLabel":"vaggruta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/73ec5a44eba6d09dfe097ba346137a94/a1812/seina.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAASElEQVQ4y+3VMQoAMAgDwPhX/YSft0RwLrQZHQI6eGNEZpa7V0R8hQYtcAAgSVvUuZjZMzS3bQ2oyIILLrjgHZTVl7xg1S/gAHx86WTsjKFcAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73ec5a44eba6d09dfe097ba346137a94/6ac13/seina.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73ec5a44eba6d09dfe097ba346137a94/a1812/seina.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/73ec5a44eba6d09dfe097ba346137a94/a1812/seina.png","alt":"seina","title":"seina","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vägg.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 1"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"malruta","style":"position:relative;"},"children":[{"type":"text","value":"Målruta"},{"type":"element","tagName":"a","properties":{"href":"#malruta","ariaLabel":"malruta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/kohde.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABHElEQVQ4y7WVPQ6CUAzHWRw0yBG8gfEK3oCJlRNwB2/gBZzZPAU7I0zsbmiigQjav6nlgS+CUyVp0lfa3+vH4+EkSYI0TVWEWQ4rWg+zeiAR2fJ8gu53EL8z0ragpvn2JfoBZP3xGCBsK0vQ5WLbeMOPuHEgK00j+Z9OwG4HrNfAbAYsFoDvA3E81Gg2ngS2rThmGbBcAo4j4nmA6w7rIOiZ70xHgd1uyHPJhgPDUNa3G3C9AscjsNlY0OmSDXC7lYD9fnqcBhrHUnLXJhvIUhTiuFoBVSXB3AbOgKWuxcaZsp/v99O3gaa5h4M4RpEEmgEZYFfau3zu6XwOOp9BX+dQHahe8l+Gon5s1A/2Xz499ctB8/pSu2C1fwEvukCRvM6VtZAAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/6ac13/kohde.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/kohde.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/kohde.png","alt":"kohde","title":"kohde","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mål.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Roboten ska flytta en låda till den här rutan"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"lada","style":"position:relative;"},"children":[{"type":"text","value":"Låda"},{"type":"element","tagName":"a","properties":{"href":"#lada","ariaLabel":"lada permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/laatikko.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAzElEQVQ4y+2VPQqEMBCFvZxVjqAWAVN4CH8KtfEQQfAk9h5DC09go295Abcx7sIyWyzswIPATL6ZgckkGMcR0zSJiKyABykj6wnc9/0jyHEcThcgbds2zPOMZVneinHruvqBZ3XDMCAMQ2itEUUR4jj2ir4kSaCUctBLyyfQWossy9B1Heq6RtM0XtHHGEJZ6S2w73ukaeouFUVxqzzPHZSVsv2XQGMM2rZFWZaoqsorQpmU7f+BvwgUHRvxwRZ7euLL4WvrS2TBSn8BD7avDfrO6ztPAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e7afd4acd3f9b214a5c43273b23c834/6ac13/laatikko.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/laatikko.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/laatikko.png","alt":"laatikko","title":"laatikko","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"låda.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 3"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"robot","style":"position:relative;"},"children":[{"type":"text","value":"Robot"},{"type":"element","tagName":"a","properties":{"href":"#robot","ariaLabel":"robot permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robo.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB/klEQVQ4y5VV0WraYBjttbe+QR+iF32KXvdOWOeckVEGveqtF71qi6CusDFW6sSQJcwNklFHEcHZGfsAjdFCQCiFQew6qugZ58OUIqY1Hxw8nnP+w5+QP1mp1+uwbTsUlmWh1WoJyJ/KsmuFJGxG9/d4oyjo93oCcmphw66Hwul0KphMJvLf8zx8Oj5G/uQEFVUVkFOjx2E2WPdsYdd18S6Xw/v1dXwtlwXk1OhFLnT7fXw4OsLHjQ2Yug7TMIRToxe50HEcfC6XYZ2f47tpCsip0Ytc2Lu6QuHwEJl4HMlYDK9iMSjxuGj0ol8y72GhgNzuLrKKIiCn5i57D4MQZ+j70DQNVdNEMpMRkFOj9zj75A7l+RuNkM1mUSwW8a1axRdNE5BTo8fM/LrQQt/3kUgkUKlU5ITwBBDk1Oj5s10uVTgcDpFKpVAqldBoNFCr1QTk1OgxE6nwxdYWVFVFs9l82CE5NXqRCv/e3iKztob8/j5+np1B13UBef7gQDxmlioMzGvPww/LQjqdhmEYAnJq17OzPL8mdIfBL+d1MonBYCAgD+ZxdulCPhgvd3bkZUCQj6IWBua/8Ri/9vbwdnUV7uWlgJwavWcveX7uxmOcbm/jdHMTf25uBOTU7maFC1+wiz4B7XYbHdtGx3Hwu9tF5+JCINxxxGNm0SfgP7rpf7Qvj0ecAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5fb4f02fac6c4d361c9057099bc9f890/6ac13/robo.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robo.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robo.png","alt":"robo","title":"robo","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 4"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"lada-pa-malruta","style":"position:relative;"},"children":[{"type":"text","value":"Låda på målruta"},{"type":"element","tagName":"a","properties":{"href":"#lada-pa-malruta","ariaLabel":"lada pa malruta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/15225a12ccfe0e42a543bea73f4ff889/a1812/valmis.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABk0lEQVQ4y7WVPWsCQRCG7UTEX5Em/0IQbLS18gM80MLOIpgYb/fOu9mNcpD8g4hglzZtCNjbpkqrhV0IITb6hllivrzEDy4LL7dwew8zs+/MxcbjMSaTSSRiVow3US1mfQCXy+VBkNVqZbQB5LVYLDCdTjGbzbaKz83n83DgOrrRaIRsNgvLslAqlVAul0PF7yqVCvL5vIFupLwGDgYDNBoNBEEAIoJS6pu075unT2TOMJQj/RU4HA5Rq9XMR1LKTzkOHCFwphRs14UUwkA5Uk7/T2C9XofWGo7jwGWQ66IrJU57PdzmcrjLZNAmgiYy6e8FZJAnhIGNikU8J5O4ajbR8bzDgB0inCuFfquFp1QK15aFVr8PVwhTlt2BXCvPw6BahW638XB8jPt0GidBYCLmmu4FvFAKZ1rj8egIr/G4Adq+b2rK4ovaC9gjMsCbQgEviQQuuW5EpqZ8SXsBv9rGkRJd24bd7Zq9fI9OCGF8utU2YcYmraF+mJx2NXZo64W04NbWi3w4/Nv4imTARv0LeAO8hwm47qNppgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/15225a12ccfe0e42a543bea73f4ff889/6ac13/valmis.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/15225a12ccfe0e42a543bea73f4ff889/a1812/valmis.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/15225a12ccfe0e42a543bea73f4ff889/a1812/valmis.png","alt":"valmis","title":"valmis","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"färdig.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Lådan har flyttats till målrutan"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"malruta-och-robot","style":"position:relative;"},"children":[{"type":"text","value":"Målruta och robot"},{"type":"element","tagName":"a","properties":{"href":"#malruta-och-robot","ariaLabel":"malruta och robot permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/70f904da050def3ee01b9b01b0b50b16/a1812/kohderobo.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAC2klEQVQ4y41VXUsiURiemyANL7wRgm4q6D9U0B+IgrroIvrAqDVNWWKLrK66qD9gVEOrbF9axvSlSxq2hS1tVtoH1NWoSwlLdhEkFY3ps5wz4/bl2B54Zp55z/s+877nnHeG8fv9CAaDsvB6vQgEAhSE5/IlWgwhckN4fMRnoxG/o1EKwolNbhCtf4LpdJoilUrR59jlJaZnZjA2Nwfn4iIF4cQWi8WoD/HNxH0oGL64ADs6Cmt5Odbm5ymsFRVgLRaEef4/BYmTVFLk5AS28XF8q6uDZ3kZnpUVym0si0g8LgomkzRGXjCZBM3v+Bh8SQkcHAfv/j6+ezwU3oMDOBwO8DqdKEjipCzfCz49iRmenwN5eYgqFBi3WmFSq6FTKtGpVMKoVlNblGGA2lpRULbkjGBVFcAwiJhMYB0OWMxmDBuNFJaBAbBTU4gUFVGf1OysWLIgZBEkF55Hiry9tBSJeBzc6ipc6+vQmUwULrcbnM+HhNUqCtbUiLHJ5BtBKbv05CR1FDo7McyymLBY4Ha5sMRxFO61NUx8/YrhwUEI+flAQQHSNzdiMrkEb1tboe3thdNupx1COoAgsLcH59IStPX1uCWVqFQyglLJaXK+GAYJjQZ6rRb2hQX83NnB5uYmBeF2pxP65mYkiGB1tRj3ruQXm4LKSurc3tSERY7Dr91d+Le3KQgntvaWFlFwejrHpmQEz85wp1LBpNFgzG7Hj60tLHu9FIST9jMpFLhraHjVYbIHm06FQoiXlWGjvx9dej1WhoYougwGbPT1IW40Pi+T7MGWWg+CIN6vroCeHhg6OvCnsJDC0NYG2GzPxyyzmbk+Di/PFVmZT2YzwqenFIQL0o6SJfq45BeTD4KAvZERfCkuRiQSoSCc2B6kF76NeSX4dtwnk/B1d8PX2Iib62sKwontXhLM+oHN9gs4PDxEKBhEiOdxEA4jdHREQTnP0znik+0X8BeOmjrCN4yzpAAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/70f904da050def3ee01b9b01b0b50b16/6ac13/kohderobo.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/70f904da050def3ee01b9b01b0b50b16/a1812/kohderobo.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/70f904da050def3ee01b9b01b0b50b16/a1812/kohderobo.png","alt":"kohderobo","title":"kohderobo","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filnamn: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"målrobot.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position i listan: 6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Roboten kan också vara på en tom målruta"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"att-skapa-spelrutan","style":"position:relative;"},"children":[{"type":"text","value":"Att skapa spelrutan"},{"type":"element","tagName":"a","properties":{"href":"#att-skapa-spelrutan","ariaLabel":"att skapa spelrutan permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"text","value":" skapar den ursprungliga spelrutan:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden skapar en tvådimensionell lista med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"karta"}]},{"type":"text","value":" som använder de numrerade positionerna för bilderna i listan för att markera vilken bild som ska vara var. På så sätt innehåller spelet ett register över spelrutans tillstånd vid alla tidpunkter."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: I början innehåller alla rutor på spelplanen ett nummer mellan 0 och 4. Siffrorna 5 och 6 ingår inte, eftersom det i början inte finns någon låda eller robot på en målruta."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"huvudloopen","style":"position:relative;"},"children":[{"type":"text","value":"Huvudloopen"},{"type":"element","tagName":"a","properties":{"href":"#huvudloopen","ariaLabel":"huvudloopen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"huvudloop"}]},{"type":"text","value":" är ganska kort. Vid varje iteration anropar den två metoder: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kolla_handelser"}]},{"type":"text","value":" går igenom alla händelser som samlats in sedan föregående iteration, och metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"text","value":" uppdaterar innehållet i fönstret."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"huvudloop"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kolla_handelser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rita_fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kolla_handelser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ruta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bilder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"ruta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här skedet är den enda händelse som faktiskt hanteras av spelet att stänga spelfönstret, t.ex. med exit-knappen. Spelet avslutas sedan genom att anropa Pythons "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":"-funktion."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje gång metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"text","value":" anropas korsas hela spelrutnätet igenom och den bild som motsvarar varje ruta i rutnätet ritas på rätt plats."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: koordinaterna x och y används på två olika sätt i spelet. När man hanterar index i en tvådimensionell lista är det logiskt att ange y-koordinaten först, eftersom y hänvisar till numret på raden medan x är numret på kolumnen. Å andra sidan, när man använder pygame-metoder, skickas x vanligtvis först, vilket det ganska ofta gör när man arbetar med grafik och även i matematiska sammanhang."}]}],"data":{"quirksMode":false}},"html":"

I den här modulen kommer vi att använda pygame för att skapa ett lite större spel. Det är en variant av det klassiska Sokoban spelet, där spelaren flyttar en robot på ett rutnät och skjuter lådor till rätt platser med så få drag som möjligt.

\n

Slutresultatet kommer att se ut så här:

\n\n \n \n \n \n \n \"peli\"\n \n \n \n

Spelets karta

\n

Låt oss börja med att rita den karta som används i spelet. Spelet implementeras i klassen Sokoban, som kommer att innehålla alla funktioner som krävs för att spela spelet. I detta första steg är innehållet i klassen följande:

\n
import pygame\n\nclass Sokoban:\n    def __init__(self):\n        pygame.init()\n\n        self.ladda_bilder()\n        self.nytt_spel()\n\n        self.hojd = len(self.karta)\n        self.bredd = len(self.karta[0])\n        self.skala = self.bilder[0].get_width()\n\n        fonster_hojd = self.skala * self.hojd\n        fonster_bredd = self.skala * self.bredd\n        self.fonster = pygame.display.set_mode((fonster_bredd, fonster_hojd))\n\n        pygame.display.set_caption(\"Sokoban\")\n\n        self.huvudloop()\n\n    def ladda_bilder(self):\n        self.bilder = []\n        for namn in [\"golv\", \"vägg\", \"mål\", \"låda\", \"robot\", \"färdig\", \"målrobot\"]:\n            self.bilder.append(pygame.image.load(namn + \".png\"))\n\n    def nytt_spel(self):\n        self.karta = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                       [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],\n                       [1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]\n\n    def huvudloop(self):\n        while True:\n            self.kolla_handelser()\n            self.rita_fonster()\n\n    def kolla_handelser(self):\n        for handelse in pygame.event.get():\n            if handelse.type == pygame.QUIT:\n                exit()\n\n    def rita_fonster(self):\n        self.fonster.fill((0, 0, 0))\n\n        for y in range(self.hojd):\n            for x in range(self.bredd):\n                ruta = self.karta[y][x]\n                self.fonster.blit(self.bilder[ruta], (x * self.skala, y * self.skala))\n\n        pygame.display.flip()\n\nif __name__ == \"__main__\":\n    Sokoban()
\n

Om du kör programmet visas ett fönster med spelets inledande tillstånd. Låt oss ta en närmare titt på koden som åstadkommer detta.

\n

Konstruktorn

\n

Klassens konstruktor initierar pygame-modulerna och de väsentliga variabler och datastrukturer som är involverade i spelet. Den anropar också spelets huvudloop-metod.

\n
    def __init__(self):\n        pygame.init()\n\n        self.ladda_bilder()\n        self.nytt_spel()\n\n        self.hojd = len(self.karta)\n        self.bredd = len(self.karta[0])\n        self.skala = self.bilder[0].get_width()\n\n        fonster_hojd = self.skala * self.hojd\n        fonster_bredd = self.skala * self.bredd\n        self.fonster = pygame.display.set_mode((fonster_bredd, fonster_hojd))\n\n        pygame.display.set_caption(\"Sokoban\")\n\n        self.huvudloop()
\n

Metoden ladda_bilder laddar de bilder som används i spelet till en lista med namnet bilder. Metoden nytt_spel skapar en tvådimensionell lista med namnet karta, som innehåller spelrutnätets tillstånd i början av spelet.

\n

Variablerna hojd och bredd sätts baserat på spelrutnätets dimensioner. Variabeln skala innehåller längden på sidan av en kvadrat i rutnätet. Eftersom varje bild är en kvadrat av exakt samma storlek täcks storleken på alla kvadrater av denna enda variabel, och bredden på den första bilden räcker gott och väl för värdet. Samma värde kan användas för att beräkna bredden och höjden på hela rutnätet, vilket gör att vi kan skapa ett fönster av lämplig storlek för att visa spelrutnätet.

\n

Att ladda bilder

\n

Metoden ladda_bilder laddar alla bilder som används i spelet:

\n
    def ladda_bilder(self):\n        self.bilder = []\n        for namn in [\"golv\", \"vägg\", \"mål\", \"låda\", \"robot\", \"färdig\", \"målrobot\"]:\n            self.bilder.append(pygame.image.load(namn + \".png\"))
\n

Spelet använder följande bilder:

\n

Golvruta

\n\n \n \n \n \n \n \"seina\"\n \n \n \n
    \n
  • Filnamn: vägg.png
  • \n
  • Position i listan: 1
  • \n
\n

Målruta

\n\n \n \n \n \n \n \"laatikko\"\n \n \n \n
    \n
  • Filnamn: låda.png
  • \n
  • Position i listan: 3
  • \n
\n

Robot

\n\n \n \n \n \n \n \"robo\"\n \n \n \n
    \n
  • Filnamn robot.png
  • \n
  • Position i listan: 4
  • \n
\n

Låda på målruta

\n\n \n \n \n \n \n \"valmis\"\n \n \n \n
    \n
  • Filnamn: färdig.png
  • \n
  • Position i listan: 5
  • \n
  • Lådan har flyttats till målrutan
  • \n
\n

Målruta och robot

\n\n \n \n \n \n \n \"kohderobo\"\n \n \n \n
    \n
  • Filnamn: målrobot.png
  • \n
  • Position i listan: 6
  • \n
  • Roboten kan också vara på en tom målruta
  • \n
\n

Att skapa spelrutan

\n

Metoden nytt_spel skapar den ursprungliga spelrutan:

\n
    def nytt_spel(self):\n        self.karta = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                       [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],\n                       [1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
\n

Metoden skapar en tvådimensionell lista med namnet karta som använder de numrerade positionerna för bilderna i listan för att markera vilken bild som ska vara var. På så sätt innehåller spelet ett register över spelrutans tillstånd vid alla tidpunkter.

\n

OBS: I början innehåller alla rutor på spelplanen ett nummer mellan 0 och 4. Siffrorna 5 och 6 ingår inte, eftersom det i början inte finns någon låda eller robot på en målruta.

\n

Huvudloopen

\n

Metoden huvudloop är ganska kort. Vid varje iteration anropar den två metoder: kolla_handelser går igenom alla händelser som samlats in sedan föregående iteration, och metoden rita_fonster uppdaterar innehållet i fönstret.

\n
    def huvudloop(self):\n        while True:\n            self.kolla_handelser()\n            self.rita_fonster()\n\n    def kolla_handelser(self):\n        for handelse in pygame.event.get():\n            if handelse.type == pygame.QUIT:\n                exit()\n\n    def rita_fonster(self):\n        self.fonster.fill((0, 0, 0))\n\n        for y in range(self.hojd):\n            for x in range(self.bredd):\n                ruta = self.karta[y][x]\n                self.fonster.blit(self.bilder[ruta], (x * self.skala, y * self.skala))\n\n        pygame.display.flip()
\n

I det här skedet är den enda händelse som faktiskt hanteras av spelet att stänga spelfönstret, t.ex. med exit-knappen. Spelet avslutas sedan genom att anropa Pythons exit-funktion.

\n

Varje gång metoden rita_fonster anropas korsas hela spelrutnätet igenom och den bild som motsvarar varje ruta i rutnätet ritas på rätt plats.

\n

OBS: koordinaterna x och y används på två olika sätt i spelet. När man hanterar index i en tvådimensionell lista är det logiskt att ange y-koordinaten först, eftersom y hänvisar till numret på raden medan x är numret på kolumnen. Å andra sidan, när man använder pygame-metoder, skickas x vanligtvis först, vilket det ganska ofta gör när man arbetar med grafik och även i matematiska sammanhang.

","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/1-spelprojekt.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-14/2-robot-och-ladar/page-data.json b/page-data/osa-14/2-robot-och-ladar/page-data.json index dc0f63efe..01d36277b 100644 --- a/page-data/osa-14/2-robot-och-ladar/page-data.json +++ b/page-data/osa-14/2-robot-och-ladar/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-14/2-robot-och-ladar", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det svåraste att implementera i ett Sokoban-spel brukar vara att flytta roboten så att den kan skjuta lådor i önskad riktning. Spelet ska kunna avgöra när roboten kan röra sig i en angiven riktning och kunna hantera alla situationer där en låda också ska röra sig. Låt oss ta an den här utmaningen nu."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"att-hantera-viktiga-handelser","style":"position:relative;"},"children":[{"type":"text","value":"Att hantera viktiga händelser"},{"type":"element","tagName":"a","properties":{"href":"#att-hantera-viktiga-handelser","ariaLabel":"att hantera viktiga handelser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelaren styr roboten med de fyra piltangenterna, så vår händelsehanterare ska också kunna reagera på lämpliga tangenthändelser:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"granska_handelser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_UP"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_DOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När spelaren nu trycker på en piltangent anropas metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":" med ett lämpligt par av argument. Det första argumentet innehåller förflyttningen i vertikal riktning, medan det andra innehåller förflyttningen i horisontell riktning."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"sokning-av-roboten","style":"position:relative;"},"children":[{"type":"text","value":"Sökning av roboten"},{"type":"element","tagName":"a","properties":{"href":"#sokning-av-roboten","ariaLabel":"sokning av roboten permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelet måste veta var roboten befinner sig för att kunna förflytta den på rätt sätt. Låt oss lägga till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hitta_robot"}]},{"type":"text","value":" som räknar ut var roboten befinner sig:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hitta_robot"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden går igenom alla rutor i rutnätet och returnerar koordinaterna för den ruta som innehåller antingen siffran 4 (roboten på egen hand) eller siffran 6 (roboten på en målruta)."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tanken är att varje gång spelaren trycker på en piltangent ska robotens position först fastställas genom att gå igenom rutorna i rutnätet. Detta kan tyckas lite långsamt och överflödigt, eftersom vi lika gärna kan hålla robotens plats i en separat variabel eller två. Fördelen med denna sökmetod är att vi inte lagrar robotens position på två olika ställen (i spelrutan och i separata variabler), utan vi behöver bara bry oss om ett ställe (spelrutan), vilket innebär att spelets tillstånd i datorminnet blir enklare att hantera."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"forandringar-av-rutnatet","style":"position:relative;"},"children":[{"type":"text","value":"Förändringar av rutnätet"},{"type":"element","tagName":"a","properties":{"href":"#forandringar-av-rutnatet","ariaLabel":"forandringar av rutnatet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan anropat metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":" ovan, men vi har inte definierat den än. Låt oss göra det nu."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":" tar som argument den riktning som spelaren vill förflytta sig till. Den uppdaterar sedan rutnätet i enlighet med detta, eller fastställer att förflyttningen inte är tillåten och lämnar rutnätet oförändrad."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"flytta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_tidigare_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_tidigare_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hitta_robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n robot_nya_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_tidigare_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_y\n robot_nya_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_tidigare_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lada_ny_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_nya_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_y\n lada_ny_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_nya_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_tidigare_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_tidigare_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden har en hel del olika steg, så låt oss ta en titt på vart och ett i tur och ordning:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"robotens-gamla-och-nya-plats","style":"position:relative;"},"children":[{"type":"text","value":"Robotens gamla och nya plats"},{"type":"element","tagName":"a","properties":{"href":"#robotens-gamla-och-nya-plats","ariaLabel":"robotens gamla och nya plats permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" robot_tidigare_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_tidigare_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hitta_robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n robot_nya_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_tidigare_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_y\n robot_nya_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_tidigare_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_x"}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först anropas metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hitta_robot"}]},{"type":"text","value":" för att ta reda på robotens aktuella position innan förflyttningen. Detta lagras i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_tidigare_y"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_tidigare_x"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Därefter lagras robotens nya position efter den tänkta förflyttningen i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_nya_y"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_nya_x"}]},{"type":"text","value":". De nya koordinaterna kan enkelt beräknas genom att lägga till de värden som skickats som argument till robotens gamla position, eftersom både vertikala och horisontella värden ingår."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"gick-roboten-in-i-en-vagg","style":"position:relative;"},"children":[{"type":"text","value":"Gick roboten in i en vägg?"},{"type":"element","tagName":"a","properties":{"href":"#gick-roboten-in-i-en-vagg","ariaLabel":"gick roboten in i en vagg permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":"-satsen ovan tar hand om situationen där roboten skulle träffa en vägg som ett resultat av förflyttningen. Kom ihåg att 1 var positionen för en väggruta i listan med bilder. Detta är inte tillåtet, så metoden returnerar helt enkelt utan vidare."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"flyttning-av-lada","style":"position:relative;"},"children":[{"type":"text","value":"Flyttning av låda"},{"type":"element","tagName":"a","properties":{"href":"#flyttning-av-lada","ariaLabel":"flyttning av lada permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lada_ny_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_nya_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_y\n lada_ny_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_nya_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om robotens nytänkta position innehåller siffran 3 (en egen låda) eller siffran 5 (en låda i en målruta) försöker roboten flytta lådan till nästa ruta. För detta ändamål behöver vi två nya variabler: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lada_ny_y"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lada_ny_x"}]},{"type":"text","value":", som innehåller lådans placering efter flytten."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt som roboten kan lådan inte flyttas till en väggruta med identifieraren 1. Lådan kan inte heller flyttas till en annan låda eller till en målruta med en låda på. Om detta skulle hända till följd av förflyttningen, återgår metoden helt enkelt utan att göra några ändringar i rutnätet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I alla andra fall kan lådan röra sig. Värdet på lådans nuvarande rutnätsplats minskas med 3 och värdet på dess nya rutnätsplats ökas med 3. På grund av den smarta ordningen på objekten i listan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bilder"}]},{"type":"text","value":" fungerar detta korrekt både när det gäller golvrutor och målrutor."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"forflyttning-av-roboten","style":"position:relative;"},"children":[{"type":"text","value":"Förflyttning av roboten"},{"type":"element","tagName":"a","properties":{"href":"#forflyttning-av-roboten","ariaLabel":"forflyttning av roboten permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_tidigare_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_tidigare_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om metoden har nått denna punkt utan att återvända, är det dags att flytta roboten också. Proceduren är densamma som när lådan flyttas, men det värde som dras från och läggs till på de aktuella platserna i rutnätet är 4 den här gången. Detta säkerställer, återigen genom den smarta ordningen av objekten i bildlistan, att slutresultatet i rutnätet blir korrekt både när golv- och målrutor är inblandade i förflyttningen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"omfaktorisering","style":"position:relative;"},"children":[{"type":"text","value":"Omfaktorisering?"},{"type":"element","tagName":"a","properties":{"href":"#omfaktorisering","ariaLabel":"omfaktorisering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att endast använda rutnätet för att lagra spelets tillstånd hela tiden är mycket praktiskt i den meningen att endast en variabel är permanent inblandad i hela processen, och det är relativt enkelt att uppdatera rutnätets tillstånd genom enkla additioner och subtraktioner."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nackdelen är att det kan vara en aning svårt att förstå spelets programkod. Om någon som inte är bekant med den logik som används såg följande kodrad skulle de troligen bli lite förvirrade:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kodsnutten ovan använder magiska siffror för att representera rutorna i rutnätet. Den som läser koden måste veta att 1 betyder vägg, 3 betyder en låda och 5 betyder en låda i en målruta."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Raderna med de smarta subtraktionerna och adderingarna skulle se ännu mer förvirrande ut:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffran 3 betydde en ruta precis som tidigare, men nu subtraheras den från värdet på en ruta i rutnätet. Detta fungerar inom ramen för vårt numreringssystem, eftersom det ändrar en låda (3) till en normal golvruta (0) eller en målruta med en låda (5) till en tom målruta (2), men för att förstå detta krävs kännedom i det numreringssystem som används."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan göra det enklare för alla som läser koden genom att omfaktorisera vår implementation. Det innebär att vi förbättrar kodens struktur och läsbarhet. Ett sätt att uppnå detta skulle vara att använda namnen på rutorna i stället för siffrorna 0 till 6, även om detta fortfarande inte skulle förklara hur och varför siffror kan adderas och subtraheras samtidigt som rutnätets integritet bibehålls."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att göra programkoden verkligt tillgänglig skulle det sannolikt krävas mycket mer grundläggande transformativ omfaktorisering. Vi skulle till exempel kunna behålla spelkartans struktur på en plats och lagra robotens och lådornas platser i en separat datastruktur. Nackdelen med detta skulle vara att det sannolikt skulle resultera i mycket mer kod och att spelets interna struktur skulle bli mycket mer komplicerad."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Omfaktorisering och kodkvalitet är ett ämne för en del efterföljande kurser, t.ex. Software Development Methods och Software Engineering."}]}],"data":{"quirksMode":false}},"html":"

Det svåraste att implementera i ett Sokoban-spel brukar vara att flytta roboten så att den kan skjuta lådor i önskad riktning. Spelet ska kunna avgöra när roboten kan röra sig i en angiven riktning och kunna hantera alla situationer där en låda också ska röra sig. Låt oss ta an den här utmaningen nu.

\n

Att hantera viktiga händelser

\n

Spelaren styr roboten med de fyra piltangenterna, så vår händelsehanterare ska också kunna reagera på lämpliga tangenthändelser:

\n
    def granska_handelser(self):\n        for handelse in pygame.event.get():\n            if handelse.type == pygame.KEYDOWN:\n                if handelse.key == pygame.K_LEFT:\n                    self.flytta(0, -1)\n                if handelse.key == pygame.K_RIGHT:\n                    self.flytta(0, 1)\n                if handelse.key == pygame.K_UP:\n                    self.flytta(-1, 0)\n                if handelse.key == pygame.K_DOWN:\n                    self.flytta(1, 0)\n\n            if handelse.type == pygame.QUIT:\n                exit()
\n

När spelaren nu trycker på en piltangent anropas metoden flytta med ett lämpligt par av argument. Det första argumentet innehåller förflyttningen i vertikal riktning, medan det andra innehåller förflyttningen i horisontell riktning.

\n

Sökning av roboten

\n

Spelet måste veta var roboten befinner sig för att kunna förflytta den på rätt sätt. Låt oss lägga till metoden hitta_robot som räknar ut var roboten befinner sig:

\n
    def hitta_robot(self):\n        for y in range(self.hojd):\n            for x in range(self.bredd):\n                if self.karta[y][x] in [4, 6]:\n                    return (y, x)
\n

Metoden går igenom alla rutor i rutnätet och returnerar koordinaterna för den ruta som innehåller antingen siffran 4 (roboten på egen hand) eller siffran 6 (roboten på en målruta).

\n

Tanken är att varje gång spelaren trycker på en piltangent ska robotens position först fastställas genom att gå igenom rutorna i rutnätet. Detta kan tyckas lite långsamt och överflödigt, eftersom vi lika gärna kan hålla robotens plats i en separat variabel eller två. Fördelen med denna sökmetod är att vi inte lagrar robotens position på två olika ställen (i spelrutan och i separata variabler), utan vi behöver bara bry oss om ett ställe (spelrutan), vilket innebär att spelets tillstånd i datorminnet blir enklare att hantera.

\n

Förändringar av rutnätet

\n

Vi har redan anropat metoden flytta ovan, men vi har inte definierat den än. Låt oss göra det nu.

\n

Metoden flytta tar som argument den riktning som spelaren vill förflytta sig till. Den uppdaterar sedan rutnätet i enlighet med detta, eller fastställer att förflyttningen inte är tillåten och lämnar rutnätet oförändrad.

\n
    def flytta(self, flytta_y, flytta_x):\n        robot_tidigare_y, robot_tidigare_x = self.hitta_robot()\n        robot_nya_y = robot_tidigare_y + flytta_y\n        robot_nya_x = robot_tidigare_x + flytta_x\n\n        if self.karta[robot_nya_y][robot_nya_x] == 1:\n            return\n\n        if self.karta[robot_nya_y][robot_nya_x] in [3, 5]:\n            lada_ny_y = robot_nya_y + flytta_y\n            lada_ny_x = robot_nya_x + flytta_x\n\n            if self.karta[lada_ny_y][lada_ny_x] in [1, 3, 5]:\n                return\n\n            self.karta[robot_nya_y][robot_nya_x] -= 3\n            self.karta[lada_ny_y][lada_ny_x] += 3\n\n        self.karta[robot_tidigare_y][robot_tidigare_x] -= 4\n        self.karta[robot_nya_y][robot_nya_x] += 4
\n

Metoden har en hel del olika steg, så låt oss ta en titt på vart och ett i tur och ordning:

\n

Robotens gamla och nya plats

\n
        robot_tidigare_y, robot_tidigare_x = self.hitta_robot()\n        robot_nya_y = robot_tidigare_y + flytta_y\n        robot_nya_x = robot_tidigare_x + flytta_x
\n

Först anropas metoden hitta_robot för att ta reda på robotens aktuella position innan förflyttningen. Detta lagras i variablerna robot_tidigare_y och robot_tidigare_x.

\n

Därefter lagras robotens nya position efter den tänkta förflyttningen i variablerna robot_nya_y och robot_nya_x. De nya koordinaterna kan enkelt beräknas genom att lägga till de värden som skickats som argument till robotens gamla position, eftersom både vertikala och horisontella värden ingår.

\n

Gick roboten in i en vägg?

\n
        if self.karta[robot_nya_y][robot_nya_x] == 1:\n            return
\n

if-satsen ovan tar hand om situationen där roboten skulle träffa en vägg som ett resultat av förflyttningen. Kom ihåg att 1 var positionen för en väggruta i listan med bilder. Detta är inte tillåtet, så metoden returnerar helt enkelt utan vidare.

\n

Flyttning av låda

\n
        if self.karta[robot_nya_y][robot_nya_x] in [3, 5]:\n            lada_ny_y = robot_nya_y + flytta_y\n            lada_ny_x = robot_nya_x + flytta_x\n\n            if self.karta[lada_ny_y][lada_ny_x] in [1, 3, 5]:\n                return\n\n            self.karta[robot_nya_y][robot_nya_x] -= 3\n            self.karta[lada_ny_y][lada_ny_x] += 3
\n

Om robotens nytänkta position innehåller siffran 3 (en egen låda) eller siffran 5 (en låda i en målruta) försöker roboten flytta lådan till nästa ruta. För detta ändamål behöver vi två nya variabler: lada_ny_y och lada_ny_x, som innehåller lådans placering efter flytten.

\n

På samma sätt som roboten kan lådan inte flyttas till en väggruta med identifieraren 1. Lådan kan inte heller flyttas till en annan låda eller till en målruta med en låda på. Om detta skulle hända till följd av förflyttningen, återgår metoden helt enkelt utan att göra några ändringar i rutnätet.

\n

I alla andra fall kan lådan röra sig. Värdet på lådans nuvarande rutnätsplats minskas med 3 och värdet på dess nya rutnätsplats ökas med 3. På grund av den smarta ordningen på objekten i listan bilder fungerar detta korrekt både när det gäller golvrutor och målrutor.

\n

Förflyttning av roboten

\n
        self.karta[robot_tidigare_y][robot_tidigare_x] -= 4\n        self.karta[robot_nya_y][robot_nya_x] += 4
\n

Om metoden har nått denna punkt utan att återvända, är det dags att flytta roboten också. Proceduren är densamma som när lådan flyttas, men det värde som dras från och läggs till på de aktuella platserna i rutnätet är 4 den här gången. Detta säkerställer, återigen genom den smarta ordningen av objekten i bildlistan, att slutresultatet i rutnätet blir korrekt både när golv- och målrutor är inblandade i förflyttningen.

\n

Omfaktorisering?

\n

Att endast använda rutnätet för att lagra spelets tillstånd hela tiden är mycket praktiskt i den meningen att endast en variabel är permanent inblandad i hela processen, och det är relativt enkelt att uppdatera rutnätets tillstånd genom enkla additioner och subtraktioner.

\n

Nackdelen är att det kan vara en aning svårt att förstå spelets programkod. Om någon som inte är bekant med den logik som används såg följande kodrad skulle de troligen bli lite förvirrade:

\n
            if self.karta[lada_ny_y][lada_ny_x] in [1, 3, 5]:
\n

Kodsnutten ovan använder magiska siffror för att representera rutorna i rutnätet. Den som läser koden måste veta att 1 betyder vägg, 3 betyder en låda och 5 betyder en låda i en målruta.

\n

Raderna med de smarta subtraktionerna och adderingarna skulle se ännu mer förvirrande ut:

\n
            self.karta[robot_nya_y][robot_nya_x] -= 3
\n

Siffran 3 betydde en ruta precis som tidigare, men nu subtraheras den från värdet på en ruta i rutnätet. Detta fungerar inom ramen för vårt numreringssystem, eftersom det ändrar en låda (3) till en normal golvruta (0) eller en målruta med en låda (5) till en tom målruta (2), men för att förstå detta krävs kännedom i det numreringssystem som används.

\n

Vi kan göra det enklare för alla som läser koden genom att omfaktorisera vår implementation. Det innebär att vi förbättrar kodens struktur och läsbarhet. Ett sätt att uppnå detta skulle vara att använda namnen på rutorna i stället för siffrorna 0 till 6, även om detta fortfarande inte skulle förklara hur och varför siffror kan adderas och subtraheras samtidigt som rutnätets integritet bibehålls.

\n

För att göra programkoden verkligt tillgänglig skulle det sannolikt krävas mycket mer grundläggande transformativ omfaktorisering. Vi skulle till exempel kunna behålla spelkartans struktur på en plats och lagra robotens och lådornas platser i en separat datastruktur. Nackdelen med detta skulle vara att det sannolikt skulle resultera i mycket mer kod och att spelets interna struktur skulle bli mycket mer komplicerad.

\n

Omfaktorisering och kodkvalitet är ett ämne för en del efterföljande kurser, t.ex. Software Development Methods och Software Engineering.

","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/2-robot-och-ladar.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det svåraste att implementera i ett Sokoban-spel brukar vara att flytta roboten så att den kan skjuta lådor i önskad riktning. Spelet ska kunna avgöra när roboten kan röra sig i en angiven riktning och kunna hantera alla situationer där en låda också ska röra sig. Låt oss ta an den här utmaningen nu."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"att-hantera-viktiga-handelser","style":"position:relative;"},"children":[{"type":"text","value":"Att hantera viktiga händelser"},{"type":"element","tagName":"a","properties":{"href":"#att-hantera-viktiga-handelser","ariaLabel":"att hantera viktiga handelser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelaren styr roboten med de fyra piltangenterna, så vår händelsehanterare ska också kunna reagera på lämpliga tangenthändelser:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"granska_handelser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" handelse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_UP"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_DOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När spelaren nu trycker på en piltangent anropas metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":" med ett lämpligt par av argument. Det första argumentet innehåller förflyttningen i vertikal riktning, medan det andra innehåller förflyttningen i horisontell riktning."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"sokning-av-roboten","style":"position:relative;"},"children":[{"type":"text","value":"Sökning av roboten"},{"type":"element","tagName":"a","properties":{"href":"#sokning-av-roboten","ariaLabel":"sokning av roboten permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelet måste veta var roboten befinner sig för att kunna förflytta den på rätt sätt. Låt oss lägga till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hitta_robot"}]},{"type":"text","value":" som räknar ut var roboten befinner sig:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hitta_robot"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden går igenom alla rutor i rutnätet och returnerar koordinaterna för den ruta som innehåller antingen siffran 4 (roboten på egen hand) eller siffran 6 (roboten på en målruta)."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tanken är att varje gång spelaren trycker på en piltangent ska robotens position först fastställas genom att gå igenom rutorna i rutnätet. Detta kan tyckas lite långsamt och överflödigt, eftersom vi lika gärna kan hålla robotens plats i en separat variabel eller två. Fördelen med denna sökmetod är att vi inte lagrar robotens position på två olika ställen (i spelrutan och i separata variabler), utan vi behöver bara bry oss om ett ställe (spelrutan), vilket innebär att spelets tillstånd i datorminnet blir enklare att hantera."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"forandringar-av-rutnatet","style":"position:relative;"},"children":[{"type":"text","value":"Förändringar av rutnätet"},{"type":"element","tagName":"a","properties":{"href":"#forandringar-av-rutnatet","ariaLabel":"forandringar av rutnatet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan anropat metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":" ovan, men vi har inte definierat den än. Låt oss göra det nu."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":" tar som argument den riktning som spelaren vill förflytta sig till. Den uppdaterar sedan rutnätet i enlighet med detta, eller fastställer att förflyttningen inte är tillåten och lämnar rutnätet oförändrad."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"flytta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_tidigare_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_tidigare_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hitta_robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n robot_nya_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_tidigare_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_y\n robot_nya_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_tidigare_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lada_ny_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_nya_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_y\n lada_ny_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_nya_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_tidigare_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_tidigare_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden har en hel del olika steg, så låt oss ta en titt på vart och ett i tur och ordning:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"robotens-gamla-och-nya-plats","style":"position:relative;"},"children":[{"type":"text","value":"Robotens gamla och nya plats"},{"type":"element","tagName":"a","properties":{"href":"#robotens-gamla-och-nya-plats","ariaLabel":"robotens gamla och nya plats permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" robot_tidigare_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_tidigare_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hitta_robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n robot_nya_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_tidigare_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_y\n robot_nya_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_tidigare_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_x"}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först anropas metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hitta_robot"}]},{"type":"text","value":" för att ta reda på robotens aktuella position innan förflyttningen. Detta lagras i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_tidigare_y"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_tidigare_x"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Därefter lagras robotens nya position efter den tänkta förflyttningen i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_nya_y"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_nya_x"}]},{"type":"text","value":". De nya koordinaterna kan enkelt beräknas genom att lägga till de värden som skickats som argument till robotens gamla position, eftersom både vertikala och horisontella värden ingår."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"gick-roboten-in-i-en-vagg","style":"position:relative;"},"children":[{"type":"text","value":"Gick roboten in i en vägg?"},{"type":"element","tagName":"a","properties":{"href":"#gick-roboten-in-i-en-vagg","ariaLabel":"gick roboten in i en vagg permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":"-satsen ovan tar hand om situationen där roboten skulle träffa en vägg som ett resultat av förflyttningen. Kom ihåg att 1 var positionen för en väggruta i listan med bilder. Detta är inte tillåtet, så metoden returnerar helt enkelt utan vidare."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"flyttning-av-lada","style":"position:relative;"},"children":[{"type":"text","value":"Flyttning av låda"},{"type":"element","tagName":"a","properties":{"href":"#flyttning-av-lada","ariaLabel":"flyttning av lada permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lada_ny_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_nya_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_y\n lada_ny_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_nya_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" flytta_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om robotens nytänkta position innehåller siffran 3 (en egen låda) eller siffran 5 (en låda i en målruta) försöker roboten flytta lådan till nästa ruta. För detta ändamål behöver vi två nya variabler: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lada_ny_y"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lada_ny_x"}]},{"type":"text","value":", som innehåller lådans placering efter flytten."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt som roboten kan lådan inte flyttas till en väggruta med identifieraren 1. Lådan kan inte heller flyttas till en annan låda eller till en målruta med en låda på. Om detta skulle hända till följd av förflyttningen, återgår metoden helt enkelt utan att göra några ändringar i rutnätet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I alla andra fall kan lådan röra sig. Värdet på lådans nuvarande rutnätsplats minskas med 3 och värdet på dess nya rutnätsplats ökas med 3. På grund av den smarta ordningen på objekten i listan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bilder"}]},{"type":"text","value":" fungerar detta korrekt både när det gäller golvrutor och målrutor."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"forflyttning-av-roboten","style":"position:relative;"},"children":[{"type":"text","value":"Förflyttning av roboten"},{"type":"element","tagName":"a","properties":{"href":"#forflyttning-av-roboten","ariaLabel":"forflyttning av roboten permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_tidigare_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_tidigare_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om metoden har nått denna punkt utan att återvända, är det dags att flytta roboten också. Proceduren är densamma som när lådan flyttas, men det värde som dras från och läggs till på de aktuella platserna i rutnätet är 4 den här gången. Detta säkerställer, återigen genom den smarta ordningen av objekten i bildlistan, att slutresultatet i rutnätet blir korrekt både när golv- och målrutor är inblandade i förflyttningen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"omfaktorisering","style":"position:relative;"},"children":[{"type":"text","value":"Omfaktorisering?"},{"type":"element","tagName":"a","properties":{"href":"#omfaktorisering","ariaLabel":"omfaktorisering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att endast använda rutnätet för att lagra spelets tillstånd hela tiden är mycket praktiskt i den meningen att endast en variabel är permanent inblandad i hela processen, och det är relativt enkelt att uppdatera rutnätets tillstånd genom enkla additioner och subtraktioner."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nackdelen är att det kan vara en aning svårt att förstå spelets programkod. Om någon som inte är bekant med den logik som används såg följande kodrad skulle de troligen bli lite förvirrade:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"lada_ny_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kodsnutten ovan använder magiska siffror för att representera rutorna i rutnätet. Den som läser koden måste veta att 1 betyder vägg, 3 betyder en låda och 5 betyder en låda i en målruta."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Raderna med de smarta subtraktionerna och adderingarna skulle se ännu mer förvirrande ut:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_nya_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffran 3 betydde en ruta precis som tidigare, men nu subtraheras den från värdet på en ruta i rutnätet. Detta fungerar inom ramen för vårt numreringssystem, eftersom det ändrar en låda (3) till en normal golvruta (0) eller en målruta med en låda (5) till en tom målruta (2), men för att förstå detta krävs kännedom i det numreringssystem som används."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan göra det enklare för alla som läser koden genom att omfaktorisera vår implementation. Det innebär att vi förbättrar kodens struktur och läsbarhet. Ett sätt att uppnå detta skulle vara att använda namnen på rutorna i stället för siffrorna 0 till 6, även om detta fortfarande inte skulle förklara hur och varför siffror kan adderas och subtraheras samtidigt som rutnätets integritet bibehålls."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att göra programkoden verkligt tillgänglig skulle det sannolikt krävas mycket mer grundläggande transformativ omfaktorisering. Vi skulle till exempel kunna behålla spelkartans struktur på en plats och lagra robotens och lådornas platser i en separat datastruktur. Nackdelen med detta skulle vara att det sannolikt skulle resultera i mycket mer kod och att spelets interna struktur skulle bli mycket mer komplicerad."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Omfaktorisering och kodkvalitet är ett ämne för en del efterföljande kurser, t.ex. Software Development Methods och Software Engineering."}]}],"data":{"quirksMode":false}},"html":"

Det svåraste att implementera i ett Sokoban-spel brukar vara att flytta roboten så att den kan skjuta lådor i önskad riktning. Spelet ska kunna avgöra när roboten kan röra sig i en angiven riktning och kunna hantera alla situationer där en låda också ska röra sig. Låt oss ta an den här utmaningen nu.

\n

Att hantera viktiga händelser

\n

Spelaren styr roboten med de fyra piltangenterna, så vår händelsehanterare ska också kunna reagera på lämpliga tangenthändelser:

\n
    def granska_handelser(self):\n        for handelse in pygame.event.get():\n            if handelse.type == pygame.KEYDOWN:\n                if handelse.key == pygame.K_LEFT:\n                    self.flytta(0, -1)\n                if handelse.key == pygame.K_RIGHT:\n                    self.flytta(0, 1)\n                if handelse.key == pygame.K_UP:\n                    self.flytta(-1, 0)\n                if handelse.key == pygame.K_DOWN:\n                    self.flytta(1, 0)\n\n            if handelse.type == pygame.QUIT:\n                exit()
\n

När spelaren nu trycker på en piltangent anropas metoden flytta med ett lämpligt par av argument. Det första argumentet innehåller förflyttningen i vertikal riktning, medan det andra innehåller förflyttningen i horisontell riktning.

\n

Sökning av roboten

\n

Spelet måste veta var roboten befinner sig för att kunna förflytta den på rätt sätt. Låt oss lägga till metoden hitta_robot som räknar ut var roboten befinner sig:

\n
    def hitta_robot(self):\n        for y in range(self.hojd):\n            for x in range(self.bredd):\n                if self.karta[y][x] in [4, 6]:\n                    return (y, x)
\n

Metoden går igenom alla rutor i rutnätet och returnerar koordinaterna för den ruta som innehåller antingen siffran 4 (roboten på egen hand) eller siffran 6 (roboten på en målruta).

\n

Tanken är att varje gång spelaren trycker på en piltangent ska robotens position först fastställas genom att gå igenom rutorna i rutnätet. Detta kan tyckas lite långsamt och överflödigt, eftersom vi lika gärna kan hålla robotens plats i en separat variabel eller två. Fördelen med denna sökmetod är att vi inte lagrar robotens position på två olika ställen (i spelrutan och i separata variabler), utan vi behöver bara bry oss om ett ställe (spelrutan), vilket innebär att spelets tillstånd i datorminnet blir enklare att hantera.

\n

Förändringar av rutnätet

\n

Vi har redan anropat metoden flytta ovan, men vi har inte definierat den än. Låt oss göra det nu.

\n

Metoden flytta tar som argument den riktning som spelaren vill förflytta sig till. Den uppdaterar sedan rutnätet i enlighet med detta, eller fastställer att förflyttningen inte är tillåten och lämnar rutnätet oförändrad.

\n
    def flytta(self, flytta_y, flytta_x):\n        robot_tidigare_y, robot_tidigare_x = self.hitta_robot()\n        robot_nya_y = robot_tidigare_y + flytta_y\n        robot_nya_x = robot_tidigare_x + flytta_x\n\n        if self.karta[robot_nya_y][robot_nya_x] == 1:\n            return\n\n        if self.karta[robot_nya_y][robot_nya_x] in [3, 5]:\n            lada_ny_y = robot_nya_y + flytta_y\n            lada_ny_x = robot_nya_x + flytta_x\n\n            if self.karta[lada_ny_y][lada_ny_x] in [1, 3, 5]:\n                return\n\n            self.karta[robot_nya_y][robot_nya_x] -= 3\n            self.karta[lada_ny_y][lada_ny_x] += 3\n\n        self.karta[robot_tidigare_y][robot_tidigare_x] -= 4\n        self.karta[robot_nya_y][robot_nya_x] += 4
\n

Metoden har en hel del olika steg, så låt oss ta en titt på vart och ett i tur och ordning:

\n

Robotens gamla och nya plats

\n
        robot_tidigare_y, robot_tidigare_x = self.hitta_robot()\n        robot_nya_y = robot_tidigare_y + flytta_y\n        robot_nya_x = robot_tidigare_x + flytta_x
\n

Först anropas metoden hitta_robot för att ta reda på robotens aktuella position innan förflyttningen. Detta lagras i variablerna robot_tidigare_y och robot_tidigare_x.

\n

Därefter lagras robotens nya position efter den tänkta förflyttningen i variablerna robot_nya_y och robot_nya_x. De nya koordinaterna kan enkelt beräknas genom att lägga till de värden som skickats som argument till robotens gamla position, eftersom både vertikala och horisontella värden ingår.

\n

Gick roboten in i en vägg?

\n
        if self.karta[robot_nya_y][robot_nya_x] == 1:\n            return
\n

if-satsen ovan tar hand om situationen där roboten skulle träffa en vägg som ett resultat av förflyttningen. Kom ihåg att 1 var positionen för en väggruta i listan med bilder. Detta är inte tillåtet, så metoden returnerar helt enkelt utan vidare.

\n

Flyttning av låda

\n
        if self.karta[robot_nya_y][robot_nya_x] in [3, 5]:\n            lada_ny_y = robot_nya_y + flytta_y\n            lada_ny_x = robot_nya_x + flytta_x\n\n            if self.karta[lada_ny_y][lada_ny_x] in [1, 3, 5]:\n                return\n\n            self.karta[robot_nya_y][robot_nya_x] -= 3\n            self.karta[lada_ny_y][lada_ny_x] += 3
\n

Om robotens nytänkta position innehåller siffran 3 (en egen låda) eller siffran 5 (en låda i en målruta) försöker roboten flytta lådan till nästa ruta. För detta ändamål behöver vi två nya variabler: lada_ny_y och lada_ny_x, som innehåller lådans placering efter flytten.

\n

På samma sätt som roboten kan lådan inte flyttas till en väggruta med identifieraren 1. Lådan kan inte heller flyttas till en annan låda eller till en målruta med en låda på. Om detta skulle hända till följd av förflyttningen, återgår metoden helt enkelt utan att göra några ändringar i rutnätet.

\n

I alla andra fall kan lådan röra sig. Värdet på lådans nuvarande rutnätsplats minskas med 3 och värdet på dess nya rutnätsplats ökas med 3. På grund av den smarta ordningen på objekten i listan bilder fungerar detta korrekt både när det gäller golvrutor och målrutor.

\n

Förflyttning av roboten

\n
        self.karta[robot_tidigare_y][robot_tidigare_x] -= 4\n        self.karta[robot_nya_y][robot_nya_x] += 4
\n

Om metoden har nått denna punkt utan att återvända, är det dags att flytta roboten också. Proceduren är densamma som när lådan flyttas, men det värde som dras från och läggs till på de aktuella platserna i rutnätet är 4 den här gången. Detta säkerställer, återigen genom den smarta ordningen av objekten i bildlistan, att slutresultatet i rutnätet blir korrekt både när golv- och målrutor är inblandade i förflyttningen.

\n

Omfaktorisering?

\n

Att endast använda rutnätet för att lagra spelets tillstånd hela tiden är mycket praktiskt i den meningen att endast en variabel är permanent inblandad i hela processen, och det är relativt enkelt att uppdatera rutnätets tillstånd genom enkla additioner och subtraktioner.

\n

Nackdelen är att det kan vara en aning svårt att förstå spelets programkod. Om någon som inte är bekant med den logik som används såg följande kodrad skulle de troligen bli lite förvirrade:

\n
            if self.karta[lada_ny_y][lada_ny_x] in [1, 3, 5]:
\n

Kodsnutten ovan använder magiska siffror för att representera rutorna i rutnätet. Den som läser koden måste veta att 1 betyder vägg, 3 betyder en låda och 5 betyder en låda i en målruta.

\n

Raderna med de smarta subtraktionerna och adderingarna skulle se ännu mer förvirrande ut:

\n
            self.karta[robot_nya_y][robot_nya_x] -= 3
\n

Siffran 3 betydde en ruta precis som tidigare, men nu subtraheras den från värdet på en ruta i rutnätet. Detta fungerar inom ramen för vårt numreringssystem, eftersom det ändrar en låda (3) till en normal golvruta (0) eller en målruta med en låda (5) till en tom målruta (2), men för att förstå detta krävs kännedom i det numreringssystem som används.

\n

Vi kan göra det enklare för alla som läser koden genom att omfaktorisera vår implementation. Det innebär att vi förbättrar kodens struktur och läsbarhet. Ett sätt att uppnå detta skulle vara att använda namnen på rutorna i stället för siffrorna 0 till 6, även om detta fortfarande inte skulle förklara hur och varför siffror kan adderas och subtraheras samtidigt som rutnätets integritet bibehålls.

\n

För att göra programkoden verkligt tillgänglig skulle det sannolikt krävas mycket mer grundläggande transformativ omfaktorisering. Vi skulle till exempel kunna behålla spelkartans struktur på en plats och lagra robotens och lådornas platser i en separat datastruktur. Nackdelen med detta skulle vara att det sannolikt skulle resultera i mycket mer kod och att spelets interna struktur skulle bli mycket mer komplicerad.

\n

Omfaktorisering och kodkvalitet är ett ämne för en del efterföljande kurser, t.ex. Software Development Methods och Software Engineering.

","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/2-robot-och-ladar.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-14/3-fardigstallande-av-spelet/page-data.json b/page-data/osa-14/3-fardigstallande-av-spelet/page-data.json index ce7156572..2ab6fef2c 100644 --- a/page-data/osa-14/3-fardigstallande-av-spelet/page-data.json +++ b/page-data/osa-14/3-fardigstallande-av-spelet/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-14/3-fardigstallande-av-spelet", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vårt spel är redan ganska fungerande, så det är dags att lägga till några sista detaljer. Vi lägger till en räknare för att visa antalet drag, en möjlighet att starta ett nytt spel och stänga spelet med tangentbordsinmatning samt ett meddelande när spelaren lyckas vinna spelet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"raknare-for-mangden-drag","style":"position:relative;"},"children":[{"type":"text","value":"Räknare för mängden drag"},{"type":"element","tagName":"a","properties":{"href":"#raknare-for-mangden-drag","ariaLabel":"raknare for mangden drag permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dragräknaren i nedre kanten av spelfönstret visar antalet drag som spelaren har gjort hittills. Detta kan användas för att hitta den lösning som kräver minst antal drag."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Räknaren kräver några ändringar i koden. Först ändrar vi konstruktorn så att det finns tillräckligt med utrymme för räknaren och att vi har ett lämpligt teckensnitt till vårt förfogande för att rita texten:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster_bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fonster_hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"SysFont"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arial\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dragräknaren ställs in till noll i början av spelet. Varje drag ökar den med ett:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"flytta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje gång innehållet i fönstret uppdateras, bör även antalet drag som visas på skärmen uppdateras:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n speltext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drag: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"nytt-spel-och-att-avsluta-spelet","style":"position:relative;"},"children":[{"type":"text","value":"Nytt spel och att avsluta spelet"},{"type":"element","tagName":"a","properties":{"href":"#nytt-spel-och-att-avsluta-spelet","ariaLabel":"nytt spel och att avsluta spelet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu ska vi lägga till tangentbordsinstruktioner för att starta ett nytt spel med F2 och avsluta spelet med Esc. Båda är ganska enkla att implementera:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kolla_handelser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_F2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nytt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_ESCAPE"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi bör också lägga till information om den här funktionen så att spelaren kan se den:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n speltext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"F2 = nytt spel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n speltext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Esc = stäng spel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"400"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"vinna-spelet","style":"position:relative;"},"children":[{"type":"text","value":"Vinna spelet"},{"type":"element","tagName":"a","properties":{"href":"#vinna-spelet","ariaLabel":"vinna spelet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelaren har vunnit spelet när varje låda befinner sig i en av målrutorna. Följande metod tar hand om att kontrollera detta:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spelet_lost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden går igenom alla rutor i rutnätet. Om någon av rutorna är en 2:a (en tom målruta) eller en 6:a (en robot i en målruta) är spelet ännu inte löst och metoden returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":". Om det inte finns någon sådan ruta i rutnätet måste alla målrutor vara upptagna av lådor, spelet är löst och metoden returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om spelaren löser spelet bör vi visa ett lämpligt meddelande med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelet_lost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n speltext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gratulerar, du löste spelet!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n speltext_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n speltext_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" speltext_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" speltext_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För fullständighetens skull ändrar vi också "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":"-metoden så att spelaren inte längre kan flytta när han eller hon har löst spelet:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"flytta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelet_lost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelaren kan dock fortfarande se rutnätet och det slutliga läget i spelet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"ett-tips-for-testning","style":"position:relative;"},"children":[{"type":"text","value":"Ett tips för testning"},{"type":"element","tagName":"a","properties":{"href":"#ett-tips-for-testning","ariaLabel":"ett tips for testning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man utvecklar spel händer det ofta att man vill kontrollera vad som händer i någon senare situation i spelet. I det här spelet är till exempel det ögonblick då spelet vinns en sådan situation."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det kan vara svårt att testa att en sådan situation fungerar korrekt, eftersom man normalt sett måste lösa spelet för att komma till den punkten i spelet. Som programmerare kan vi göra några tillfälliga underlättningar i våra spel, för att göra det lättare att testa dem. Vi skulle till exempel kunna lägga till följande för att göra det tillfälligt lättare att lösa spelet:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spelet_lost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu returnerar metoden alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", vilket innebär att spelet är \"löst\" till att börja med. Detta gör det enkelt att kontrollera att notifieringen i slutet ser bra ut och att spelaren inte längre kan röra sig på rutnätet efter lösningen. När denna funktionalitet är noggrant testad kan vi återkalla ändringarna."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"ditt-spel-pa-github","style":"position:relative;"},"children":[{"type":"text","value":"Ditt spel på GitHub?"},{"type":"element","tagName":"a","properties":{"href":"#ditt-spel-pa-github","ariaLabel":"ditt spel pa github permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelet är nu färdigt. Om du vill ha ett enkelt sätt att leka med koden och bilderna kan du hämta källkoden från GitHub:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/moocfi/sokoban","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://github.com/moocfi/sokoban"}]}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"GitHub är en populär plats för många typer av programmeringsprojekt. Det kan användas för att lagra källkoden och annat material för alla dina egna programmeringsprojekt också, och ditt program kommer då att underhållas genom git-versionskontroll, och det kan enkelt delas med andra. Du kommer att bli mycket bekant med git och GitHub om du fortsätter med andra programmeringskurser på mooc.fi."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"hur-manga-drag-kravs","style":"position:relative;"},"children":[{"type":"text","value":"Hur många drag krävs?"},{"type":"element","tagName":"a","properties":{"href":"#hur-manga-drag-kravs","ariaLabel":"hur manga drag kravs permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rutnätet i det här spelet är ganska litet, men spelet är inte så lätt. Den första utmaningen är att helt enkelt klara spelet, men nästa steg är att försöka göra det med så få drag som möjligt. Hur kort är den kortaste vägen till en lösning?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att leta efter den kortaste möjliga lösningen är inte alls en lätt uppgift, men det finns beräkningslösningar för detta också. Detta är ett av ämnena i kursen Datastrukturer och algoritmer."}]}],"data":{"quirksMode":false}},"html":"

Vårt spel är redan ganska fungerande, så det är dags att lägga till några sista detaljer. Vi lägger till en räknare för att visa antalet drag, en möjlighet att starta ett nytt spel och stänga spelet med tangentbordsinmatning samt ett meddelande när spelaren lyckas vinna spelet.

\n

Räknare för mängden drag

\n

Dragräknaren i nedre kanten av spelfönstret visar antalet drag som spelaren har gjort hittills. Detta kan användas för att hitta den lösning som kräver minst antal drag.

\n

Räknaren kräver några ändringar i koden. Först ändrar vi konstruktorn så att det finns tillräckligt med utrymme för räknaren och att vi har ett lämpligt teckensnitt till vårt förfogande för att rita texten:

\n
    def __init__(self):\n        ...\n        self.fonster = pygame.display.set_mode((fonster_bredd, fonster_hojd + self.skala))\n\n        self.font = pygame.font.SysFont(\"Arial\", 24)\n        ...
\n

Dragräknaren ställs in till noll i början av spelet. Varje drag ökar den med ett:

\n
    def nytt_spel(self):\n        ...\n        self.drag = 0
\n
    def flytta(self, flytta_y, flytta_x):\n        ...\n        self.drag += 1\n
\n

Varje gång innehållet i fönstret uppdateras, bör även antalet drag som visas på skärmen uppdateras:

\n
    def rita_fonster(self):\n        ...\n        speltext = self.font.render(\"Drag: \" + str(self.drag), True, (255, 0, 0))\n        self.fonster.blit(speltext, (25, self.hojd * self.skala + 10))\n        ...
\n

Nytt spel och att avsluta spelet

\n

Nu ska vi lägga till tangentbordsinstruktioner för att starta ett nytt spel med F2 och avsluta spelet med Esc. Båda är ganska enkla att implementera:

\n
    def kolla_handelser(self):\n        ...\n                if handelse.key == pygame.K_F2:\n                    self.nytt_spel()\n                if handelse.key == pygame.K_ESCAPE:\n                    exit()\n        ...
\n

Vi bör också lägga till information om den här funktionen så att spelaren kan se den:

\n
    def rita_fonster(self):\n        ...\n        speltext = self.font.render(\"F2 = nytt spel\", True, (255, 0, 0))\n        self.fonster.blit(speltext, (200, self.hojd * self.skala + 10))\n\n        speltext = self.font.render(\"Esc = stäng spel\", True, (255, 0, 0))\n        self.fonster.blit(speltext, (400, self.hojd * self.skala + 10))\n        ...
\n

Vinna spelet

\n

Spelaren har vunnit spelet när varje låda befinner sig i en av målrutorna. Följande metod tar hand om att kontrollera detta:

\n
    def spelet_lost(self):\n        for y in range(self.hojd):\n            for x in range(self.bredd):\n                if self.karta[y][x] in [2, 6]:\n                    return False\n        return True
\n

Metoden går igenom alla rutor i rutnätet. Om någon av rutorna är en 2:a (en tom målruta) eller en 6:a (en robot i en målruta) är spelet ännu inte löst och metoden returnerar False. Om det inte finns någon sådan ruta i rutnätet måste alla målrutor vara upptagna av lådor, spelet är löst och metoden returnerar True.

\n

Om spelaren löser spelet bör vi visa ett lämpligt meddelande med metoden rita_fonster:

\n
    def rita_fonster(self):\n        ...\n        if self.spelet_lost():\n            speltext = self.font.render(\"Gratulerar, du löste spelet!\", True, (255, 0, 0))\n            speltext_x = self.skala * self.bredd / 2 - speltext.get_width() / 2\n            speltext_y = self.skala * self.hojd / 2 - speltext.get_height() / 2\n            pygame.draw.rect(self.fonster, (0, 0, 0), (speltext_x, speltext_y, speltext.get_width(), speltext.get_height()))\n            self.fonster.blit(speltext, (speltext_x, speltext_y))\n        ...
\n

För fullständighetens skull ändrar vi också flytta-metoden så att spelaren inte längre kan flytta när han eller hon har löst spelet:

\n
    def flytta(self, flytta_y, flytta_x):\n        if self.spelet_lost():\n            return\n        ...
\n

Spelaren kan dock fortfarande se rutnätet och det slutliga läget i spelet.

\n

Ett tips för testning

\n

När man utvecklar spel händer det ofta att man vill kontrollera vad som händer i någon senare situation i spelet. I det här spelet är till exempel det ögonblick då spelet vinns en sådan situation.

\n

Det kan vara svårt att testa att en sådan situation fungerar korrekt, eftersom man normalt sett måste lösa spelet för att komma till den punkten i spelet. Som programmerare kan vi göra några tillfälliga underlättningar i våra spel, för att göra det lättare att testa dem. Vi skulle till exempel kunna lägga till följande för att göra det tillfälligt lättare att lösa spelet:

\n
    def spelet_lost(self):\n        return True
\n

Nu returnerar metoden alltid True, vilket innebär att spelet är \"löst\" till att börja med. Detta gör det enkelt att kontrollera att notifieringen i slutet ser bra ut och att spelaren inte längre kan röra sig på rutnätet efter lösningen. När denna funktionalitet är noggrant testad kan vi återkalla ändringarna.

\n

Ditt spel på GitHub?

\n

Spelet är nu färdigt. Om du vill ha ett enkelt sätt att leka med koden och bilderna kan du hämta källkoden från GitHub:

\n\n

GitHub är en populär plats för många typer av programmeringsprojekt. Det kan användas för att lagra källkoden och annat material för alla dina egna programmeringsprojekt också, och ditt program kommer då att underhållas genom git-versionskontroll, och det kan enkelt delas med andra. Du kommer att bli mycket bekant med git och GitHub om du fortsätter med andra programmeringskurser på mooc.fi.

\n

Hur många drag krävs?

\n

Rutnätet i det här spelet är ganska litet, men spelet är inte så lätt. Den första utmaningen är att helt enkelt klara spelet, men nästa steg är att försöka göra det med så få drag som möjligt. Hur kort är den kortaste vägen till en lösning?

\n

Att leta efter den kortaste möjliga lösningen är inte alls en lätt uppgift, men det finns beräkningslösningar för detta också. Detta är ett av ämnena i kursen Datastrukturer och algoritmer.

","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/3-fardigstallande-av-spelet.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vårt spel är redan ganska fungerande, så det är dags att lägga till några sista detaljer. Vi lägger till en räknare för att visa antalet drag, en möjlighet att starta ett nytt spel och stänga spelet med tangentbordsinmatning samt ett meddelande när spelaren lyckas vinna spelet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"raknare-for-mangden-drag","style":"position:relative;"},"children":[{"type":"text","value":"Räknare för mängden drag"},{"type":"element","tagName":"a","properties":{"href":"#raknare-for-mangden-drag","ariaLabel":"raknare for mangden drag permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dragräknaren i nedre kanten av spelfönstret visar antalet drag som spelaren har gjort hittills. Detta kan användas för att hitta den lösning som kräver minst antal drag."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Räknaren kräver några ändringar i koden. Först ändrar vi konstruktorn så att det finns tillräckligt med utrymme för räknaren och att vi har ett lämpligt teckensnitt till vårt förfogande för att rita texten:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fonster_bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fonster_hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"SysFont"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arial\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dragräknaren ställs in till noll i början av spelet. Varje drag ökar den med ett:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nytt_spel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"flytta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje gång innehållet i fönstret uppdateras, bör även antalet drag som visas på skärmen uppdateras:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n speltext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drag: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"nytt-spel-och-att-avsluta-spelet","style":"position:relative;"},"children":[{"type":"text","value":"Nytt spel och att avsluta spelet"},{"type":"element","tagName":"a","properties":{"href":"#nytt-spel-och-att-avsluta-spelet","ariaLabel":"nytt spel och att avsluta spelet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu ska vi lägga till tangentbordsinstruktioner för att starta ett nytt spel med F2 och avsluta spelet med Esc. Båda är ganska enkla att implementera:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kolla_handelser"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_F2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nytt_spel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" handelse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_ESCAPE"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi bör också lägga till information om den här funktionen så att spelaren kan se den:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n speltext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"F2 = nytt spel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n speltext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Esc = stäng spel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"400"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"vinna-spelet","style":"position:relative;"},"children":[{"type":"text","value":"Vinna spelet"},{"type":"element","tagName":"a","properties":{"href":"#vinna-spelet","ariaLabel":"vinna spelet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelaren har vunnit spelet när varje låda befinner sig i en av målrutorna. Följande metod tar hand om att kontrollera detta:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spelet_lost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"karta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden går igenom alla rutor i rutnätet. Om någon av rutorna är en 2:a (en tom målruta) eller en 6:a (en robot i en målruta) är spelet ännu inte löst och metoden returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":". Om det inte finns någon sådan ruta i rutnätet måste alla målrutor vara upptagna av lådor, spelet är löst och metoden returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om spelaren löser spelet bör vi visa ett lämpligt meddelande med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rita_fonster"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelet_lost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n speltext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gratulerar, du löste spelet!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"255"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n speltext_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n speltext_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skala "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" speltext_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fonster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"speltext_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" speltext_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För fullständighetens skull ändrar vi också "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":"-metoden så att spelaren inte längre kan flytta när han eller hon har löst spelet:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"flytta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" flytta_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelet_lost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelaren kan dock fortfarande se rutnätet och det slutliga läget i spelet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"ett-tips-for-testning","style":"position:relative;"},"children":[{"type":"text","value":"Ett tips för testning"},{"type":"element","tagName":"a","properties":{"href":"#ett-tips-for-testning","ariaLabel":"ett tips for testning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man utvecklar spel händer det ofta att man vill kontrollera vad som händer i någon senare situation i spelet. I det här spelet är till exempel det ögonblick då spelet vinns en sådan situation."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det kan vara svårt att testa att en sådan situation fungerar korrekt, eftersom man normalt sett måste lösa spelet för att komma till den punkten i spelet. Som programmerare kan vi göra några tillfälliga underlättningar i våra spel, för att göra det lättare att testa dem. Vi skulle till exempel kunna lägga till följande för att göra det tillfälligt lättare att lösa spelet:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spelet_lost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu returnerar metoden alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", vilket innebär att spelet är \"löst\" till att börja med. Detta gör det enkelt att kontrollera att notifieringen i slutet ser bra ut och att spelaren inte längre kan röra sig på rutnätet efter lösningen. När denna funktionalitet är noggrant testad kan vi återkalla ändringarna."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"ditt-spel-pa-github","style":"position:relative;"},"children":[{"type":"text","value":"Ditt spel på GitHub?"},{"type":"element","tagName":"a","properties":{"href":"#ditt-spel-pa-github","ariaLabel":"ditt spel pa github permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelet är nu färdigt. Om du vill ha ett enkelt sätt att leka med koden och bilderna kan du hämta källkoden från GitHub:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/moocfi/sokoban","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://github.com/moocfi/sokoban"}]}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"GitHub är en populär plats för många typer av programmeringsprojekt. Det kan användas för att lagra källkoden och annat material för alla dina egna programmeringsprojekt också, och ditt program kommer då att underhållas genom git-versionskontroll, och det kan enkelt delas med andra. Du kommer att bli mycket bekant med git och GitHub om du fortsätter med andra programmeringskurser på mooc.fi."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"hur-manga-drag-kravs","style":"position:relative;"},"children":[{"type":"text","value":"Hur många drag krävs?"},{"type":"element","tagName":"a","properties":{"href":"#hur-manga-drag-kravs","ariaLabel":"hur manga drag kravs permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rutnätet i det här spelet är ganska litet, men spelet är inte så lätt. Den första utmaningen är att helt enkelt klara spelet, men nästa steg är att försöka göra det med så få drag som möjligt. Hur kort är den kortaste vägen till en lösning?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att leta efter den kortaste möjliga lösningen är inte alls en lätt uppgift, men det finns beräkningslösningar för detta också. Detta är ett av ämnena i kursen Datastrukturer och algoritmer."}]}],"data":{"quirksMode":false}},"html":"

Vårt spel är redan ganska fungerande, så det är dags att lägga till några sista detaljer. Vi lägger till en räknare för att visa antalet drag, en möjlighet att starta ett nytt spel och stänga spelet med tangentbordsinmatning samt ett meddelande när spelaren lyckas vinna spelet.

\n

Räknare för mängden drag

\n

Dragräknaren i nedre kanten av spelfönstret visar antalet drag som spelaren har gjort hittills. Detta kan användas för att hitta den lösning som kräver minst antal drag.

\n

Räknaren kräver några ändringar i koden. Först ändrar vi konstruktorn så att det finns tillräckligt med utrymme för räknaren och att vi har ett lämpligt teckensnitt till vårt förfogande för att rita texten:

\n
    def __init__(self):\n        ...\n        self.fonster = pygame.display.set_mode((fonster_bredd, fonster_hojd + self.skala))\n\n        self.font = pygame.font.SysFont(\"Arial\", 24)\n        ...
\n

Dragräknaren ställs in till noll i början av spelet. Varje drag ökar den med ett:

\n
    def nytt_spel(self):\n        ...\n        self.drag = 0
\n
    def flytta(self, flytta_y, flytta_x):\n        ...\n        self.drag += 1\n
\n

Varje gång innehållet i fönstret uppdateras, bör även antalet drag som visas på skärmen uppdateras:

\n
    def rita_fonster(self):\n        ...\n        speltext = self.font.render(\"Drag: \" + str(self.drag), True, (255, 0, 0))\n        self.fonster.blit(speltext, (25, self.hojd * self.skala + 10))\n        ...
\n

Nytt spel och att avsluta spelet

\n

Nu ska vi lägga till tangentbordsinstruktioner för att starta ett nytt spel med F2 och avsluta spelet med Esc. Båda är ganska enkla att implementera:

\n
    def kolla_handelser(self):\n        ...\n                if handelse.key == pygame.K_F2:\n                    self.nytt_spel()\n                if handelse.key == pygame.K_ESCAPE:\n                    exit()\n        ...
\n

Vi bör också lägga till information om den här funktionen så att spelaren kan se den:

\n
    def rita_fonster(self):\n        ...\n        speltext = self.font.render(\"F2 = nytt spel\", True, (255, 0, 0))\n        self.fonster.blit(speltext, (200, self.hojd * self.skala + 10))\n\n        speltext = self.font.render(\"Esc = stäng spel\", True, (255, 0, 0))\n        self.fonster.blit(speltext, (400, self.hojd * self.skala + 10))\n        ...
\n

Vinna spelet

\n

Spelaren har vunnit spelet när varje låda befinner sig i en av målrutorna. Följande metod tar hand om att kontrollera detta:

\n
    def spelet_lost(self):\n        for y in range(self.hojd):\n            for x in range(self.bredd):\n                if self.karta[y][x] in [2, 6]:\n                    return False\n        return True
\n

Metoden går igenom alla rutor i rutnätet. Om någon av rutorna är en 2:a (en tom målruta) eller en 6:a (en robot i en målruta) är spelet ännu inte löst och metoden returnerar False. Om det inte finns någon sådan ruta i rutnätet måste alla målrutor vara upptagna av lådor, spelet är löst och metoden returnerar True.

\n

Om spelaren löser spelet bör vi visa ett lämpligt meddelande med metoden rita_fonster:

\n
    def rita_fonster(self):\n        ...\n        if self.spelet_lost():\n            speltext = self.font.render(\"Gratulerar, du löste spelet!\", True, (255, 0, 0))\n            speltext_x = self.skala * self.bredd / 2 - speltext.get_width() / 2\n            speltext_y = self.skala * self.hojd / 2 - speltext.get_height() / 2\n            pygame.draw.rect(self.fonster, (0, 0, 0), (speltext_x, speltext_y, speltext.get_width(), speltext.get_height()))\n            self.fonster.blit(speltext, (speltext_x, speltext_y))\n        ...
\n

För fullständighetens skull ändrar vi också flytta-metoden så att spelaren inte längre kan flytta när han eller hon har löst spelet:

\n
    def flytta(self, flytta_y, flytta_x):\n        if self.spelet_lost():\n            return\n        ...
\n

Spelaren kan dock fortfarande se rutnätet och det slutliga läget i spelet.

\n

Ett tips för testning

\n

När man utvecklar spel händer det ofta att man vill kontrollera vad som händer i någon senare situation i spelet. I det här spelet är till exempel det ögonblick då spelet vinns en sådan situation.

\n

Det kan vara svårt att testa att en sådan situation fungerar korrekt, eftersom man normalt sett måste lösa spelet för att komma till den punkten i spelet. Som programmerare kan vi göra några tillfälliga underlättningar i våra spel, för att göra det lättare att testa dem. Vi skulle till exempel kunna lägga till följande för att göra det tillfälligt lättare att lösa spelet:

\n
    def spelet_lost(self):\n        return True
\n

Nu returnerar metoden alltid True, vilket innebär att spelet är \"löst\" till att börja med. Detta gör det enkelt att kontrollera att notifieringen i slutet ser bra ut och att spelaren inte längre kan röra sig på rutnätet efter lösningen. När denna funktionalitet är noggrant testad kan vi återkalla ändringarna.

\n

Ditt spel på GitHub?

\n

Spelet är nu färdigt. Om du vill ha ett enkelt sätt att leka med koden och bilderna kan du hämta källkoden från GitHub:

\n\n

GitHub är en populär plats för många typer av programmeringsprojekt. Det kan användas för att lagra källkoden och annat material för alla dina egna programmeringsprojekt också, och ditt program kommer då att underhållas genom git-versionskontroll, och det kan enkelt delas med andra. Du kommer att bli mycket bekant med git och GitHub om du fortsätter med andra programmeringskurser på mooc.fi.

\n

Hur många drag krävs?

\n

Rutnätet i det här spelet är ganska litet, men spelet är inte så lätt. Den första utmaningen är att helt enkelt klara spelet, men nästa steg är att försöka göra det med så få drag som möjligt. Hur kort är den kortaste vägen till en lösning?

\n

Att leta efter den kortaste möjliga lösningen är inte alls en lätt uppgift, men det finns beräkningslösningar för detta också. Detta är ett av ämnena i kursen Datastrukturer och algoritmer.

","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/3-fardigstallande-av-spelet.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-14/4-ditt-eget-sepl/page-data.json b/page-data/osa-14/4-ditt-eget-sepl/page-data.json index ea2bf8cb6..dbb9c5013 100644 --- a/page-data/osa-14/4-ditt-eget-sepl/page-data.json +++ b/page-data/osa-14/4-ditt-eget-sepl/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-14/4-ditt-eget-sepl", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"h2","properties":{"id":"den-sista-programmeringsovningen","style":"position:relative;"},"children":[{"type":"text","value":"Den sista programmeringsövningen"},{"type":"element","tagName":"a","properties":{"href":"#den-sista-programmeringsovningen","ariaLabel":"den sista programmeringsovningen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den sista programmeringsövningen på den här kursen är att skapa ett eget litet spel med pygame. Övningsmallen innehåller några bilder som du kan använda. Använd inte andra bilder i ditt spel för då kommer andra inte att kunna testa ditt spel enbart baserat på källkoden."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad spelet handlar om är upp till dig, men det bör vara ungefär lika komplicerat som Sokoban-exemplet i denna del av materialet. Spelet bör innehålla följande funktioner:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelet har en sprite som spelaren kan röra sig med på något sätt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelet har några föremål som går att samla på och/eller fiender"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelaren måste få en tydlig uppgift i spelet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelet innehåller en räknare som talar om för spelaren hur det går i spelet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Källkoden för spelet är indelad i funktioner som i Sokoban-exemplet"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När spelet är klart kan du skicka in det för granskning av andra spelare med hjälp av nedanstående formulär. Efter detta ska du bekanta dig med två av dina kurskamraters spel, ge spelen ett betyg som du anser lämpligt och skriva en kort recension av spelet. Ta hänsyn till både hur intressant och spelbart spelet var, och hur läsbar programkoden är när du betygsätter och recenserar spelen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"skicka-in-spelet","style":"position:relative;"},"children":[{"type":"text","value":"Skicka in spelet"},{"type":"element","tagName":"a","properties":{"href":"#skicka-in-spelet","ariaLabel":"skicka in spelet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du ska skicka in källkoden till ditt spel via TMC:s Paste-funktion och inkludera länken du får i formuläret nedan."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med din lösning öppen i redigeraren klickar du på TMC-menyknappen i Visual Studio Code (bredvid ögonsymbolen). Detta öppnar en meny där du kan leta efter TMC Paste-funktionen genom att skriva in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"send"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 626px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 10.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAIAAADXZGvcAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAWElEQVQI12PQsfGwDYy38o208Aw1tPcwsHM3tPcwtPfQtXbVwY307b2U1bUZhHXs+c1D+I19hY09lUwc5Y0c5IzslUwdhTUt+NXMBNSxIEF1M1lDeylFNQBUlRfSchJdXQAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bf3f5cce371044c253dc34c755ee566/a0b58/tmc_paste_1.webp 230w","/static/6bf3f5cce371044c253dc34c755ee566/bc10c/tmc_paste_1.webp 460w","/static/6bf3f5cce371044c253dc34c755ee566/63b03/tmc_paste_1.webp 626w"],"sizes":"(max-width: 626px) 100vw, 626px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bf3f5cce371044c253dc34c755ee566/81c8e/tmc_paste_1.png 230w","/static/6bf3f5cce371044c253dc34c755ee566/08a84/tmc_paste_1.png 460w","/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png 626w"],"sizes":"(max-width: 626px) 100vw, 626px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png","alt":"tmc paste 1","title":"tmc paste 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Välj alternativet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Send Exercise to TMC Paste"}]},{"type":"text","value":", sedan borde det finnas en notifikation i det nedre högra hörnet av fönstret som berättar att källkoden har skickats till TMC-servern:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 26.521739130434778%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA40lEQVQY02XKW3KEIBQEUPeRivLwKggCFy4wTh46TrKG/GQXs4asJFloasxnuk519Uc3nQ7KZevROJwOxidQpuXQyaGTYyugFcMBWK8Y6NGiDcTU3HAdqb68v+2X7bK9btu6Xvf96bxEjBHRe59SSjGlRJnIGtML7t1cIvbaNm0/aqVjooDR++BcQIz3Zy6ZckqZ8rFLrfXkneMTMkPdFDuNDUzhVAgxLsu5llJzrqVmIgAQQkgppRBS/EUKxvjnl7392Nv3w/rRtDCBsr0yoCzo+eg7Mej/eK9YeOb5yml/NPkXc5s2qFqAD9IAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8513d666220a4b7cb4b0fe987d121287/a0b58/tmc_paste_2.webp 230w","/static/8513d666220a4b7cb4b0fe987d121287/bc10c/tmc_paste_2.webp 460w","/static/8513d666220a4b7cb4b0fe987d121287/a7d21/tmc_paste_2.webp 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8513d666220a4b7cb4b0fe987d121287/81c8e/tmc_paste_2.png 230w","/static/8513d666220a4b7cb4b0fe987d121287/08a84/tmc_paste_2.png 460w","/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png","alt":"tmc paste 2","title":"tmc paste 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan kopiera länken genom att klicka på knappen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Open URL"}]},{"type":"text","value":" i meddelandet. En popup borde visas och den ska innehålla alternativet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Copy"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 31.304347826086953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABHUlEQVQY002Ly07CQBhG+8A+lQuXunGLISyUpAO4oUiAkrbQmfnn2s6FztRSA4nRLydfchYnqfKdrMsiP56KgjE2fMcYuj6GGG//j7v2IYbOex9jnEwmyWwLT0vpXFBtZ1zUJjY2qjYY17e2b13f2Nje6LUJXHXG9dfrMI5jmqbJ4/z88HLyPhDhhL5Q6YV2VFiuPIaWcINpQ7ipaUOFxdy1NgzDb+ycG8fRaCk5tEoqKSRnQIngjAGVQmgpgBJSny8XP973FzPGStBZXu8qWhDJOQcApZSUEgA4F0prxhgAUABCCMaYEGKtnU6nCRD8ecDPb+nrbJluq2N+QAiVZVmVxQItsmyDMc7W6/n7x2q5WiCEUoTSdLP52u/3P8PDR9k+xN7gAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9de4d62e51a1cf0025cc5500310d1edf/a0b58/tmc_paste_3.webp 230w","/static/9de4d62e51a1cf0025cc5500310d1edf/bc10c/tmc_paste_3.webp 460w","/static/9de4d62e51a1cf0025cc5500310d1edf/a7d21/tmc_paste_3.webp 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9de4d62e51a1cf0025cc5500310d1edf/81c8e/tmc_paste_3.png 230w","/static/9de4d62e51a1cf0025cc5500310d1edf/08a84/tmc_paste_3.png 460w","/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png","alt":"tmc paste 3","title":"tmc paste 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här är länken som du ska klistra in i formuläret nedan."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"hur-modul-14-betygsatts","style":"position:relative;"},"children":[{"type":"text","value":"Hur modul 14 betygsätts"},{"type":"element","tagName":"a","properties":{"href":"#hur-modul-14-betygsatts","ariaLabel":"hur modul 14 betygsatts permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Målet med den här delen är att bygga ett litet, fungerande spel. Eftersom den här delen bara innehåller en enda uppgift får du poäng för ansträngning. Skicka in ditt spel även om du inte kan få det att fungera riktigt som du ville."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om ditt spel inte fungerar som du tänkt dig, bifoga kommentarer i din kod om hur det borde fungera, eller eventuella funktioner som du tycker saknas men inte lyckats implementera."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om ett spel som du recenserar är ofullständigt eller inte fungerar riktigt som det ska får du gärna kommentera vad du tycker att man kunde ha gjort för att få det att fungera, ifall det är möjligt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"nagra-spelideer","style":"position:relative;"},"children":[{"type":"text","value":"Några spelidéer"},{"type":"element","tagName":"a","properties":{"href":"#nagra-spelideer","ariaLabel":"nagra spelideer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är helt upp till dig vad ditt spel ska handla om, men här är några idéer som kan hjälpa dig att komma igång. Kom ihåg materialet i modul 13; de exemplen kan också hjälpa dig att arbeta med dina idéer."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"ett-samlingsspel","style":"position:relative;"},"children":[{"type":"text","value":"Ett samlingsspel"},{"type":"element","tagName":"a","properties":{"href":"#ett-samlingsspel","ariaLabel":"ett samlingsspel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelaren flyttar roboten med piltangenterna."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Ett mynt dyker upp på en slumpmässig plats på skärmen. När roboten når fram till myntet flyttas det till en ny plats."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det finns också monster på skärmen och roboten måste undvika dem."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"myntregn","style":"position:relative;"},"children":[{"type":"text","value":"Myntregn"},{"type":"element","tagName":"a","properties":{"href":"#myntregn","ariaLabel":"myntregn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelaren flyttar roboten till vänster och höger längs skärmens nedre kant."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Mynt regnar från himlen. Dessa måste roboten samla in."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det regnar också monster från himlen. Roboten måste undvika dessa."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"kollegial-granskning","style":"position:relative;"},"children":[{"type":"text","value":"Kollegial granskning"},{"type":"element","tagName":"a","properties":{"href":"#kollegial-granskning","ariaLabel":"kollegial granskning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du ska bedöma spelet utifrån följande kriterier:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Hur ser spelet ut?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är spelet spelbart? Är det roligt att spela och lätt att använda?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är spelet spelbart? Är det roligt att spela och lätt att använda?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Hur väl är det programmerat? Är koden läsbar och används klasser och funktioner på rätt sätt?"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En bra recension brukar peka ut både bra funktioner och några förslag till förbättringar."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"037bc92c-a684-5a29-979a-85fa23e0882b"},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"frageformular-for-att-avsluta","style":"position:relative;"},"children":[{"type":"text","value":"Frågeformulär för att avsluta"},{"type":"element","tagName":"a","properties":{"href":"#frageformular-for-att-avsluta","ariaLabel":"frageformular for att avsluta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först ber vi dig svara på en snabb enkät om den här delen av kursen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d"},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara också på kursens feedbackformulär. Enkätens svar hjälper oss att förbättra kursen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"fa951288-f547-5603-8572-55f5359c209d"},"children":[]}]}],"data":{"quirksMode":false}},"html":"

Den sista programmeringsövningen

\n

Den sista programmeringsövningen på den här kursen är att skapa ett eget litet spel med pygame. Övningsmallen innehåller några bilder som du kan använda. Använd inte andra bilder i ditt spel för då kommer andra inte att kunna testa ditt spel enbart baserat på källkoden.

\n

Vad spelet handlar om är upp till dig, men det bör vara ungefär lika komplicerat som Sokoban-exemplet i denna del av materialet. Spelet bör innehålla följande funktioner:

\n
    \n
  • Spelet har en sprite som spelaren kan röra sig med på något sätt
  • \n
  • Spelet har några föremål som går att samla på och/eller fiender
  • \n
  • Spelaren måste få en tydlig uppgift i spelet
  • \n
  • Spelet innehåller en räknare som talar om för spelaren hur det går i spelet
  • \n
  • Källkoden för spelet är indelad i funktioner som i Sokoban-exemplet
  • \n
\n

När spelet är klart kan du skicka in det för granskning av andra spelare med hjälp av nedanstående formulär. Efter detta ska du bekanta dig med två av dina kurskamraters spel, ge spelen ett betyg som du anser lämpligt och skriva en kort recension av spelet. Ta hänsyn till både hur intressant och spelbart spelet var, och hur läsbar programkoden är när du betygsätter och recenserar spelen.

\n

Skicka in spelet

\n

Du ska skicka in källkoden till ditt spel via TMC:s Paste-funktion och inkludera länken du får i formuläret nedan.

\n

Med din lösning öppen i redigeraren klickar du på TMC-menyknappen i Visual Studio Code (bredvid ögonsymbolen). Detta öppnar en meny där du kan leta efter TMC Paste-funktionen genom att skriva in send:

\n\n \n \n \n \n \n \"tmc\n \n \n \n

Välj alternativet Send Exercise to TMC Paste, sedan borde det finnas en notifikation i det nedre högra hörnet av fönstret som berättar att källkoden har skickats till TMC-servern:

\n\n \n \n \n \n \n \"tmc\n \n \n \n

Det här är länken som du ska klistra in i formuläret nedan.

\n

Hur modul 14 betygsätts

\n

Målet med den här delen är att bygga ett litet, fungerande spel. Eftersom den här delen bara innehåller en enda uppgift får du poäng för ansträngning. Skicka in ditt spel även om du inte kan få det att fungera riktigt som du ville.

\n

Om ditt spel inte fungerar som du tänkt dig, bifoga kommentarer i din kod om hur det borde fungera, eller eventuella funktioner som du tycker saknas men inte lyckats implementera.

\n

Om ett spel som du recenserar är ofullständigt eller inte fungerar riktigt som det ska får du gärna kommentera vad du tycker att man kunde ha gjort för att få det att fungera, ifall det är möjligt.

\n

Några spelidéer

\n

Det är helt upp till dig vad ditt spel ska handla om, men här är några idéer som kan hjälpa dig att komma igång. Kom ihåg materialet i modul 13; de exemplen kan också hjälpa dig att arbeta med dina idéer.

\n

Ett samlingsspel

\n
    \n
  • Spelaren flyttar roboten med piltangenterna.
  • \n
  • Ett mynt dyker upp på en slumpmässig plats på skärmen. När roboten når fram till myntet flyttas det till en ny plats.
  • \n
  • Det finns också monster på skärmen och roboten måste undvika dem.
  • \n
\n

Myntregn

\n
    \n
  • Spelaren flyttar roboten till vänster och höger längs skärmens nedre kant.
  • \n
  • Mynt regnar från himlen. Dessa måste roboten samla in.
  • \n
  • Det regnar också monster från himlen. Roboten måste undvika dessa.
  • \n
\n

Kollegial granskning

\n

Du ska bedöma spelet utifrån följande kriterier:

\n
    \n
  • Hur ser spelet ut?
  • \n
  • Är spelet spelbart? Är det roligt att spela och lätt att använda?
  • \n
  • Är spelet spelbart? Är det roligt att spela och lätt att använda?
  • \n
  • Hur väl är det programmerat? Är koden läsbar och används klasser och funktioner på rätt sätt?
  • \n
\n

En bra recension brukar peka ut både bra funktioner och några förslag till förbättringar.

\n
\n

Frågeformulär för att avsluta

\n

Först ber vi dig svara på en snabb enkät om den här delen av kursen.

\n
\n

Vänligen svara också på kursens feedbackformulär. Enkätens svar hjälper oss att förbättra kursen.

\n
","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/4-ditt-eget-spel.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"h2","properties":{"id":"den-sista-programmeringsovningen","style":"position:relative;"},"children":[{"type":"text","value":"Den sista programmeringsövningen"},{"type":"element","tagName":"a","properties":{"href":"#den-sista-programmeringsovningen","ariaLabel":"den sista programmeringsovningen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den sista programmeringsövningen på den här kursen är att skapa ett eget litet spel med pygame. Övningsmallen innehåller några bilder som du kan använda. Använd inte andra bilder i ditt spel för då kommer andra inte att kunna testa ditt spel enbart baserat på källkoden."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad spelet handlar om är upp till dig, men det bör vara ungefär lika komplicerat som Sokoban-exemplet i denna del av materialet. Spelet bör innehålla följande funktioner:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelet har en sprite som spelaren kan röra sig med på något sätt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelet har några föremål som går att samla på och/eller fiender"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelaren måste få en tydlig uppgift i spelet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelet innehåller en räknare som talar om för spelaren hur det går i spelet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Källkoden för spelet är indelad i funktioner som i Sokoban-exemplet"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När spelet är klart kan du skicka in det för granskning av andra spelare med hjälp av nedanstående formulär. Efter detta ska du bekanta dig med två av dina kurskamraters spel, ge spelen ett betyg som du anser lämpligt och skriva en kort recension av spelet. Ta hänsyn till både hur intressant och spelbart spelet var, och hur läsbar programkoden är när du betygsätter och recenserar spelen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"skicka-in-spelet","style":"position:relative;"},"children":[{"type":"text","value":"Skicka in spelet"},{"type":"element","tagName":"a","properties":{"href":"#skicka-in-spelet","ariaLabel":"skicka in spelet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du ska skicka in källkoden till ditt spel via TMC:s Paste-funktion och inkludera länken du får i formuläret nedan."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med din lösning öppen i redigeraren klickar du på TMC-menyknappen i Visual Studio Code (bredvid ögonsymbolen). Detta öppnar en meny där du kan leta efter TMC Paste-funktionen genom att skriva in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"send"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 626px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 10.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAIAAADXZGvcAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAWElEQVQI12PQsfGwDYy38o208Aw1tPcwsHM3tPcwtPfQtXbVwY307b2U1bUZhHXs+c1D+I19hY09lUwc5Y0c5IzslUwdhTUt+NXMBNSxIEF1M1lDeylFNQBUlRfSchJdXQAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bf3f5cce371044c253dc34c755ee566/a0b58/tmc_paste_1.webp 230w","/static/6bf3f5cce371044c253dc34c755ee566/bc10c/tmc_paste_1.webp 460w","/static/6bf3f5cce371044c253dc34c755ee566/63b03/tmc_paste_1.webp 626w"],"sizes":"(max-width: 626px) 100vw, 626px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bf3f5cce371044c253dc34c755ee566/81c8e/tmc_paste_1.png 230w","/static/6bf3f5cce371044c253dc34c755ee566/08a84/tmc_paste_1.png 460w","/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png 626w"],"sizes":"(max-width: 626px) 100vw, 626px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png","alt":"tmc paste 1","title":"tmc paste 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Välj alternativet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Send Exercise to TMC Paste"}]},{"type":"text","value":", sedan borde det finnas en notifikation i det nedre högra hörnet av fönstret som berättar att källkoden har skickats till TMC-servern:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 26.521739130434778%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA40lEQVQY02XKW3KEIBQEUPeRivLwKggCFy4wTh46TrKG/GQXs4asJFloasxnuk519Uc3nQ7KZevROJwOxidQpuXQyaGTYyugFcMBWK8Y6NGiDcTU3HAdqb68v+2X7bK9btu6Xvf96bxEjBHRe59SSjGlRJnIGtML7t1cIvbaNm0/aqVjooDR++BcQIz3Zy6ZckqZ8rFLrfXkneMTMkPdFDuNDUzhVAgxLsu5llJzrqVmIgAQQkgppRBS/EUKxvjnl7392Nv3w/rRtDCBsr0yoCzo+eg7Mej/eK9YeOb5yml/NPkXc5s2qFqAD9IAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8513d666220a4b7cb4b0fe987d121287/a0b58/tmc_paste_2.webp 230w","/static/8513d666220a4b7cb4b0fe987d121287/bc10c/tmc_paste_2.webp 460w","/static/8513d666220a4b7cb4b0fe987d121287/a7d21/tmc_paste_2.webp 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8513d666220a4b7cb4b0fe987d121287/81c8e/tmc_paste_2.png 230w","/static/8513d666220a4b7cb4b0fe987d121287/08a84/tmc_paste_2.png 460w","/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png","alt":"tmc paste 2","title":"tmc paste 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan kopiera länken genom att klicka på knappen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Open URL"}]},{"type":"text","value":" i meddelandet. En popup borde visas och den ska innehålla alternativet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Copy"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 31.304347826086953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABHUlEQVQY002Ly07CQBhG+8A+lQuXunGLISyUpAO4oUiAkrbQmfnn2s6FztRSA4nRLydfchYnqfKdrMsiP56KgjE2fMcYuj6GGG//j7v2IYbOex9jnEwmyWwLT0vpXFBtZ1zUJjY2qjYY17e2b13f2Nje6LUJXHXG9dfrMI5jmqbJ4/z88HLyPhDhhL5Q6YV2VFiuPIaWcINpQ7ipaUOFxdy1NgzDb+ycG8fRaCk5tEoqKSRnQIngjAGVQmgpgBJSny8XP973FzPGStBZXu8qWhDJOQcApZSUEgA4F0prxhgAUABCCMaYEGKtnU6nCRD8ecDPb+nrbJluq2N+QAiVZVmVxQItsmyDMc7W6/n7x2q5WiCEUoTSdLP52u/3P8PDR9k+xN7gAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9de4d62e51a1cf0025cc5500310d1edf/a0b58/tmc_paste_3.webp 230w","/static/9de4d62e51a1cf0025cc5500310d1edf/bc10c/tmc_paste_3.webp 460w","/static/9de4d62e51a1cf0025cc5500310d1edf/a7d21/tmc_paste_3.webp 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9de4d62e51a1cf0025cc5500310d1edf/81c8e/tmc_paste_3.png 230w","/static/9de4d62e51a1cf0025cc5500310d1edf/08a84/tmc_paste_3.png 460w","/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png","alt":"tmc paste 3","title":"tmc paste 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här är länken som du ska klistra in i formuläret nedan."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"hur-modul-14-betygsatts","style":"position:relative;"},"children":[{"type":"text","value":"Hur modul 14 betygsätts"},{"type":"element","tagName":"a","properties":{"href":"#hur-modul-14-betygsatts","ariaLabel":"hur modul 14 betygsatts permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Målet med den här delen är att bygga ett litet, fungerande spel. Eftersom den här delen bara innehåller en enda uppgift får du poäng för ansträngning. Skicka in ditt spel även om du inte kan få det att fungera riktigt som du ville."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om ditt spel inte fungerar som du tänkt dig, bifoga kommentarer i din kod om hur det borde fungera, eller eventuella funktioner som du tycker saknas men inte lyckats implementera."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om ett spel som du recenserar är ofullständigt eller inte fungerar riktigt som det ska får du gärna kommentera vad du tycker att man kunde ha gjort för att få det att fungera, ifall det är möjligt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"nagra-spelideer","style":"position:relative;"},"children":[{"type":"text","value":"Några spelidéer"},{"type":"element","tagName":"a","properties":{"href":"#nagra-spelideer","ariaLabel":"nagra spelideer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är helt upp till dig vad ditt spel ska handla om, men här är några idéer som kan hjälpa dig att komma igång. Kom ihåg materialet i modul 13; de exemplen kan också hjälpa dig att arbeta med dina idéer."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"ett-samlingsspel","style":"position:relative;"},"children":[{"type":"text","value":"Ett samlingsspel"},{"type":"element","tagName":"a","properties":{"href":"#ett-samlingsspel","ariaLabel":"ett samlingsspel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelaren flyttar roboten med piltangenterna."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Ett mynt dyker upp på en slumpmässig plats på skärmen. När roboten når fram till myntet flyttas det till en ny plats."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det finns också monster på skärmen och roboten måste undvika dem."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"myntregn","style":"position:relative;"},"children":[{"type":"text","value":"Myntregn"},{"type":"element","tagName":"a","properties":{"href":"#myntregn","ariaLabel":"myntregn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Spelaren flyttar roboten till vänster och höger längs skärmens nedre kant."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Mynt regnar från himlen. Dessa måste roboten samla in."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Det regnar också monster från himlen. Roboten måste undvika dessa."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"kollegial-granskning","style":"position:relative;"},"children":[{"type":"text","value":"Kollegial granskning"},{"type":"element","tagName":"a","properties":{"href":"#kollegial-granskning","ariaLabel":"kollegial granskning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du ska bedöma spelet utifrån följande kriterier:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Hur ser spelet ut?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är spelet spelbart? Är det roligt att spela och lätt att använda?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är spelet spelbart? Är det roligt att spela och lätt att använda?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Hur väl är det programmerat? Är koden läsbar och används klasser och funktioner på rätt sätt?"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En bra recension brukar peka ut både bra funktioner och några förslag till förbättringar."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"037bc92c-a684-5a29-979a-85fa23e0882b"},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"frageformular-for-att-avsluta","style":"position:relative;"},"children":[{"type":"text","value":"Frågeformulär för att avsluta"},{"type":"element","tagName":"a","properties":{"href":"#frageformular-for-att-avsluta","ariaLabel":"frageformular for att avsluta permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först ber vi dig svara på en snabb enkät om den här delen av kursen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d"},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara också på kursens feedbackformulär. Enkätens svar hjälper oss att förbättra kursen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"fa951288-f547-5603-8572-55f5359c209d"},"children":[]}]}],"data":{"quirksMode":false}},"html":"

Den sista programmeringsövningen

\n

Den sista programmeringsövningen på den här kursen är att skapa ett eget litet spel med pygame. Övningsmallen innehåller några bilder som du kan använda. Använd inte andra bilder i ditt spel för då kommer andra inte att kunna testa ditt spel enbart baserat på källkoden.

\n

Vad spelet handlar om är upp till dig, men det bör vara ungefär lika komplicerat som Sokoban-exemplet i denna del av materialet. Spelet bör innehålla följande funktioner:

\n
    \n
  • Spelet har en sprite som spelaren kan röra sig med på något sätt
  • \n
  • Spelet har några föremål som går att samla på och/eller fiender
  • \n
  • Spelaren måste få en tydlig uppgift i spelet
  • \n
  • Spelet innehåller en räknare som talar om för spelaren hur det går i spelet
  • \n
  • Källkoden för spelet är indelad i funktioner som i Sokoban-exemplet
  • \n
\n

När spelet är klart kan du skicka in det för granskning av andra spelare med hjälp av nedanstående formulär. Efter detta ska du bekanta dig med två av dina kurskamraters spel, ge spelen ett betyg som du anser lämpligt och skriva en kort recension av spelet. Ta hänsyn till både hur intressant och spelbart spelet var, och hur läsbar programkoden är när du betygsätter och recenserar spelen.

\n

Skicka in spelet

\n

Du ska skicka in källkoden till ditt spel via TMC:s Paste-funktion och inkludera länken du får i formuläret nedan.

\n

Med din lösning öppen i redigeraren klickar du på TMC-menyknappen i Visual Studio Code (bredvid ögonsymbolen). Detta öppnar en meny där du kan leta efter TMC Paste-funktionen genom att skriva in send:

\n\n \n \n \n \n \n \"tmc\n \n \n \n

Välj alternativet Send Exercise to TMC Paste, sedan borde det finnas en notifikation i det nedre högra hörnet av fönstret som berättar att källkoden har skickats till TMC-servern:

\n\n \n \n \n \n \n \"tmc\n \n \n \n

Det här är länken som du ska klistra in i formuläret nedan.

\n

Hur modul 14 betygsätts

\n

Målet med den här delen är att bygga ett litet, fungerande spel. Eftersom den här delen bara innehåller en enda uppgift får du poäng för ansträngning. Skicka in ditt spel även om du inte kan få det att fungera riktigt som du ville.

\n

Om ditt spel inte fungerar som du tänkt dig, bifoga kommentarer i din kod om hur det borde fungera, eller eventuella funktioner som du tycker saknas men inte lyckats implementera.

\n

Om ett spel som du recenserar är ofullständigt eller inte fungerar riktigt som det ska får du gärna kommentera vad du tycker att man kunde ha gjort för att få det att fungera, ifall det är möjligt.

\n

Några spelidéer

\n

Det är helt upp till dig vad ditt spel ska handla om, men här är några idéer som kan hjälpa dig att komma igång. Kom ihåg materialet i modul 13; de exemplen kan också hjälpa dig att arbeta med dina idéer.

\n

Ett samlingsspel

\n
    \n
  • Spelaren flyttar roboten med piltangenterna.
  • \n
  • Ett mynt dyker upp på en slumpmässig plats på skärmen. När roboten når fram till myntet flyttas det till en ny plats.
  • \n
  • Det finns också monster på skärmen och roboten måste undvika dem.
  • \n
\n

Myntregn

\n
    \n
  • Spelaren flyttar roboten till vänster och höger längs skärmens nedre kant.
  • \n
  • Mynt regnar från himlen. Dessa måste roboten samla in.
  • \n
  • Det regnar också monster från himlen. Roboten måste undvika dessa.
  • \n
\n

Kollegial granskning

\n

Du ska bedöma spelet utifrån följande kriterier:

\n
    \n
  • Hur ser spelet ut?
  • \n
  • Är spelet spelbart? Är det roligt att spela och lätt att använda?
  • \n
  • Är spelet spelbart? Är det roligt att spela och lätt att använda?
  • \n
  • Hur väl är det programmerat? Är koden läsbar och används klasser och funktioner på rätt sätt?
  • \n
\n

En bra recension brukar peka ut både bra funktioner och några förslag till förbättringar.

\n
\n

Frågeformulär för att avsluta

\n

Först ber vi dig svara på en snabb enkät om den här delen av kursen.

\n
\n

Vänligen svara också på kursens feedbackformulär. Enkätens svar hjälper oss att förbättra kursen.

\n
","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/4-ditt-eget-spel.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-14/page-data.json b/page-data/osa-14/page-data.json index f3ba23f2b..9637a34fd 100644 --- a/page-data/osa-14/page-data.json +++ b/page-data/osa-14/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-14", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-14","title":"Modul 14"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-14","title":"Modul 14"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-14/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-2/1-terminologi/page-data.json b/page-data/osa-2/1-terminologi/page-data.json index 5ea253793..2ef126bd0 100644 --- a/page-data/osa-2/1-terminologi/page-data.json +++ b/page-data/osa-2/1-terminologi/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-2/1-terminologi", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till de väsentligaste termerna inom programmering"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad skillnaden mellan en sats och ett uttryck är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du ta reda på datatypen på värdet av ett uttryck"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"har du lärt dig hur du genom att debugga hittar fel i din kod."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Under den första modulen fokuserade vi inte så mycket på terminologi. Nu är det dags att ta en titt på några centrala begrepp inom programmering."}]},{"type":"element","tagName":"h2","properties":{"id":"sats","style":"position:relative;"},"children":[{"type":"text","value":"Sats"},{"type":"element","tagName":"a","properties":{"href":"#sats","ariaLabel":"sats permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En sats (statement) är en del av ett program som kör någonting. En sats syftar ofta, men inte alltid, till en enstaka instruktion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print(\"Hej\")"}]},{"type":"text","value":" är en sats som skriver ut en rad text. På samma sätt är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer = 2"}]},{"type":"text","value":" en sats som lagrar ett värde i en variabel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En sats kan också vara mera invecklad. Den kan till exempel bestå av flera satser. Följande sats består av tre rader:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovan finns två satser (print-sats och tilldelningssats) inom en if-sats."}]},{"type":"element","tagName":"h2","properties":{"id":"block","style":"position:relative;"},"children":[{"type":"text","value":"Block"},{"type":"element","tagName":"a","properties":{"href":"#block","ariaLabel":"block permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett block är en grupp av efter varandra följande satser som är på samma nivå i programmets struktur. Till exempel if-satsens block innehåller de satser som körs då villkoret är sant."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# if-satsens block inleds"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du är myndig!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du är nu ett år äldre...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# blocket avslutas"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är i ett annat block.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python markeras block genom att indentera all kod i blocket med samma antal mellanrum."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Pythons huvudblock ska aldrig indenteras, utan ska alltid vara så långt till vänster som möjligt i filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här programmet fungerar inte eftersom koden inte är indenterad till det vänstra hörnet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej allihopa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett dåligt program\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Uttryck"},{"type":"element","tagName":"a","properties":{"href":"#uttryck","ariaLabel":"uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett uttryck (expression) är en kodsnutt som resulterar i en viss datatyp. När programmet körs, får uttrycket ett värde som kan användas i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här följder några exempel på uttryck:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Uttryck"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Värde"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Datatyp"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Datatyp i Python"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 4 + 3"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"heltal"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abc\" + \"de\""}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abcde\""}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"sträng"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"11 / 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"5.5"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"flyttal"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 * 5 > 9"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"sanningsvärde"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom alla uttryck har en datatyp, kan de tilldelas till en variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# variabeln x får värdet på uttrycket 1 + 2"}]},{"type":"text","value":"\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enkla uttryck kan kombineras för att få ett mera komplicerat uttryck till stånd. Så här kan man till exempel utföra räkneoperationer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# variabeln y får värdet på uttrycket \"3 gånger x plus x upphöjt till två\""}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"funktion","style":"position:relative;"},"children":[{"type":"text","value":"Funktion"},{"type":"element","tagName":"a","properties":{"href":"#funktion","ariaLabel":"funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion kör någon slags funktionalitet. Funktioner kan också ta emot en eller flera parametrar, alltså data som funktionen tar emot och behandlar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion körs då den anropas – det vill säga då funktionen och dess möjliga parametrar nämns i koden. Följande sats anropar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-funktionen med parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"det här är en parameter\""}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"det här är en parameter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En annan funktion du redan känner till väl är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":", som används för att ta emot data från användaren. Parametern i funktionen är det meddelande som ska visas till användaren:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Berätta ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet returnerar funktionen också ett värde. När funktionen har körts ersätts den del i koden där funktionen anropades med det värde som funktionen returnerar – det är nu ett uttryck med ett värde. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":"-funktionen returnerar en sträng som innehåller den text som användaren gett programmet. Värdet som en funktion returnerar lagras ofta i en variabel för att programmet ska kunna använda det senare."}]},{"type":"element","tagName":"h2","properties":{"id":"datatyp","style":"position:relative;"},"children":[{"type":"text","value":"Datatyp"},{"type":"element","tagName":"a","properties":{"href":"#datatyp","ariaLabel":"datatyp permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datatyp syftar till de egenskaper ett värde har i ett program. I följande kodexempel är datatyperna sträng ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":") för variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och heltal ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":") för variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med hjälp av funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" kan man ta reda på datatypen hos ett uttryck. Så här kan du använda funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"syntax","style":"position:relative;"},"children":[{"type":"text","value":"Syntax"},{"type":"element","tagName":"a","properties":{"href":"#syntax","ariaLabel":"syntax permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt som vanliga språk har regler för hur man skriver har även programmeringsspråk en syntax – det vill säga ett regelverk för hur koden ska skrivas. Den skiljer sig för varje programmeringsspråk."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons syntax bestämmer bland annat att första raden i en if-sats ska sluta med ett kolon och att därefter följande block ska indenteras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följer man inte dessa regler, kommer ett fel att uppstå:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":" File \"testi.py\", line 1\n if namn == \"Anna\"\n ^\nSyntaxError: invalid syntax\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-debugga","style":"position:relative;"},"children":[{"type":"text","value":"Att debugga"},{"type":"element","tagName":"a","properties":{"href":"#att-debugga","ariaLabel":"att debugga permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När syntaxen i ett program är korrekt men programmet inte ändå fungerar på önskat sätt, finns det en bugg i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Buggar dyker upp i olika slags situationer. Vissa kan orsaka felmeddelanden medan programmet körs. Ta det här programmet som exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" y\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" dividerat med "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu får vi felet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"ZeroDivisionError: integer division or modulo by zero on line 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet har med matematik att göra – det går inte att dividera med noll, och det här stoppar programmet medan det körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel som uppstår medan programmet körs är relativt lätta att korrigera. Felmeddelandet berättar på vilken rad i koden det uppstod problem. Det är förstås möjligt att felet ligger på något annat ställe i koden än just den här specifika raden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland märker man en bugg eftersom det resultat som koden ger är fel. Att observera och hitta en sådan här bugg kan vara svårt. I programmeringsuppgifterna under den här kursen finns det olika tester som ska hjälpa med att hitta sådana här fel. Före en bugg kan korrigeras måste man ta reda på var felet uppstår."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmerare använder ofta termen debugga – att söka efter orsaker till fel som uppstår i koden. Det här är ett ytterst viktigt verktyg i en programmerares verktygslåda. I yrkeslivet använder programmerare ofta mera tid till att debugga än för att skriva ny kod."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett enkelt – men desto nyttigare – sätt att debugga sitt program är att lägga till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser i sin kod. Att verifiera vad som sker i koden med hjälp av print-instruktioner ger en bekräftelse att programmet gör det som du vill."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här är ett exempel på ett försök att lösa en av föregående modulens uppgifter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Timlön: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntimmar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arbetstimmar: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Veckodag: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet fungerar inte helt korrekt. När testen körs får vi följande resultat:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"FAIL: PalkkaTest: test_sunnuntai_1\n\nSyötteellä 23.0, 12, söndag oikeaa palkkaa 552.0 ei löydy tulosteestasi Lön 276.0 euro\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi debuggar den här kursens uppgifter är det första steget ofta att testa hur programmet fungerar när man ger det den data som orsakade ett fel i testet. Nu kan vi se att programmet faktiskt ger ett inkorrekt resultat:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lön 276.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att debugga innebär vanligtvis att vi kör programmet flera gånger. Det kan vara händigt att tillfälligt hårdkoda det problematiska värdet istället för att alltid fråga efter värdet från användaren. Så här kunde det se ut i vårt exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# timlon = float(input(\"Timlön: \"))"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# timmar = int(input(\"Arbetstimmar: \"))"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# dag = input(\"Veckodag: \")"}]},{"type":"text","value":"\ntimlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23.0"}]},{"type":"text","value":"\ntimmar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\ndag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndag\""}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nästa steg kan vara att lägga till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser för att debugga. Problemet i den här koden uppstår i den delen där söndagar behandlas. Låt oss lägga till ett par print-satser: en före lönen ska fördubblas och en efter det:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön i början:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön efter fördubbling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi kör koden märker vi att programmet inte alls skriver ut något på basis av de "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser vi lagt till i koden. Det verkar som att innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":"-blocket aldrig körs. Det finns visst ett problem med if-satsen. Låt oss skriva ut Boolean-uttryckets värde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"villkor:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön i början:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön efter fördubbling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", alltså kommer "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":"-blockets kod aldrig att köras:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"villkor: False\nLön 276.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problemet ligger alltså i if-satsens villkor. Som i flera andra situationer inom programmering har bokstavsstorleken också skillnad när man jämför värden. Observera att \"söndagg\" i Boolean-uttrycket är skrivet med två g medan det i indatat inte är det. Vi korrigerar felet – både i if-satsen och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"villkor:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön i början:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön efter fördubbling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu får vi följande utskrift när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"villkor: True\nlön i början: 276.0\nlön efter fördubbling: 276.0\nLön 276.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det verkar som att värdet lagrat i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lon"}]},{"type":"text","value":" är korrekt i början: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timlon = 20.0"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timmar = 12"}]},{"type":"text","value":", 20,0 * 6 = 120,0. Instruktionen som ska multiplicera det här med två fungerar dock inte. Det måste alltså vara ett problem med den instruktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen multiplicerar nog värdet, men resultatet lagras ingenstans. Vi ändrar på det:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi nu kör programmet, märker vi att resultatet är korrekt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"villkor: True\nlön i början: 276.0\nlön efter fördubbling: 552.0\nLön 552.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet fungerar som det ska, är det viktigt att ta bort "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser och annan kod som använts för att debugga."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här var ett ganska enkelt exempel och i fall som det här kan man eventuellt hitta buggar genom att läsa igenom koden med omtanke. Att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser för att debugga är ändå ofta ett snabbt sätt att få en ledtråd för var problemet kan ligga. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser kan också användas för att fastställa vilka delar av koden som fungerar korrekt. Då kan man fokusera på andra ställen där buggar med större sannolikhet gömmer sig."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser är bara ett sätt att debugga program. Vi återkommer till det här ämnet senare under kursen. Nu ska du bli van vid att debugga, med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktioner, för att hitta problematiska delar i din kod. Proffs klarar sig inte utan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser i debuggningssyfte – det är alltså en viktig resurs redan som nybörjare."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa felen","tmcname":"osa02-01_fixa_felen","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande program finns flera syntaxfel. Korrigera dem så att programmet fungerar enligt nedan presenterade exempel."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffran är större än 100\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\"Nu har siffran blivit "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":" mindre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Värdet är nu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" är visst mitt lyckotal!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\"Ha en trevlig fortsättning på dagen!"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"13"}]},{"type":"text","value":"\n13 är visst mitt lyckotal!\nHa en trevlig fortsättning på dagen!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"101"}]},{"type":"text","value":"\nSiffran är större än 100\nNu har siffran blivit 100 mindre\nVärdet är nu 1\n1 är visst mitt lyckotal!\nHa en trevlig fortsättning på dagen!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Antalet tecken","tmcname":"osa02-02_antalet_tecken"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" kan man bland annat räkna längden på en sträng. Funktionen returnerar antalet tecken i strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcd\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nord2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tjingeling\""}]},{"type":"text","value":"\nlangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntom_strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\nlangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tom_strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4\n6\n10\n0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren mata in ett ord. Programmet ska därefter skriva ut antalet tecken, om antalet överstiger ett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: hej\nI ordet hej finns det 3 bokstäver\nTack!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: basilika\nI ordet basilika finns det 8 bokstäver\nTack!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: b\nTack!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Konvertera värden","tmcname":"osa02-03_konvertera_varden"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python kan man ofta konvertera ett värde från en datatyp till en annan. Ett flyttal kan till exempel konverteras till ett heltal med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"temperatur "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange temperatur: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Temperaturen är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" temperatur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alltså ungefär\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"temperatur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange temperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5.15"}]},{"type":"text","value":"\nTemperaturen är 5.15\nAlltså ungefär 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att funktionen inte avrundar värdet på det sättet vi kunde anta från matematiken. Siffran avrundas alltid nedåt (golvfunktion):"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange temperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8.99"}]},{"type":"text","value":"\nTemperaturen är 8.99\nAlltså ungefär 8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":"-funktionen för att skapa ett program som frågar om ett decimaltal från användaren. Programmet ska därefter skriva ut heltals- och decimaldelen av talet på skilda rader."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att det givna decimaltalet är större än noll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1.34"}]},{"type":"text","value":"\nHeltalsdel: 1\nDecimaldel: 0.34"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • känner du till de väsentligaste termerna inom programmering
  • \n
  • vet du vad skillnaden mellan en sats och ett uttryck är
  • \n
  • kan du ta reda på datatypen på värdet av ett uttryck
  • \n
  • har du lärt dig hur du genom att debugga hittar fel i din kod.
  • \n

Under den första modulen fokuserade vi inte så mycket på terminologi. Nu är det dags att ta en titt på några centrala begrepp inom programmering.

Sats

En sats (statement) är en del av ett program som kör någonting. En sats syftar ofta, men inte alltid, till en enstaka instruktion.

Till exempel print(\"Hej\") är en sats som skriver ut en rad text. På samma sätt är nummer = 2 en sats som lagrar ett värde i en variabel.

En sats kan också vara mera invecklad. Den kan till exempel bestå av flera satser. Följande sats består av tre rader:

if namn == \"Anna\":\n    print(\"Hejsan!\")\n    nummer = 2

Ovan finns två satser (print-sats och tilldelningssats) inom en if-sats.

Block

Ett block är en grupp av efter varandra följande satser som är på samma nivå i programmets struktur. Till exempel if-satsens block innehåller de satser som körs då villkoret är sant.

if alder > 17:\n    # if-satsens block inleds\n    print(\"Du är myndig!\")\n    alder = alder + 1\n    print(\"Du är nu ett år äldre...\")\n    # blocket avslutas\n\nprint(\"Det här är i ett annat block.\")

I Python markeras block genom att indentera all kod i blocket med samma antal mellanrum.

Obs! Pythons huvudblock ska aldrig indenteras, utan ska alltid vara så långt till vänster som möjligt i filen:

# det här programmet fungerar inte eftersom koden inte är indenterad till det vänstra hörnet\n  print(\"Hej allihopa\")\n  print(\"Det här är ett dåligt program\")

Uttryck

Ett uttryck (expression) är en kodsnutt som resulterar i en viss datatyp. När programmet körs, får uttrycket ett värde som kan användas i programmet.

Här följder några exempel på uttryck:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
UttryckVärdeDatatypDatatyp i Python
2 + 4 + 39heltalint
\"abc\" + \"de\"\"abcde\"strängstr
11 / 25.5flyttalfloat
2 * 5 > 9Truesanningsvärdebool

Eftersom alla uttryck har en datatyp, kan de tilldelas till en variabel:

# variabeln x får värdet på uttrycket 1 + 2\nx = 1 + 2

Enkla uttryck kan kombineras för att få ett mera komplicerat uttryck till stånd. Så här kan man till exempel utföra räkneoperationer:

# variabeln y får värdet på uttrycket \"3 gånger x plus x upphöjt till två\"\ny = 3 * x + x**2

Funktion

En funktion kör någon slags funktionalitet. Funktioner kan också ta emot en eller flera parametrar, alltså data som funktionen tar emot och behandlar.

En funktion körs då den anropas – det vill säga då funktionen och dess möjliga parametrar nämns i koden. Följande sats anropar print-funktionen med parametern \"det här är en parameter\":

print(\"det här är en parameter\")

En annan funktion du redan känner till väl är input, som används för att ta emot data från användaren. Parametern i funktionen är det meddelande som ska visas till användaren:

namn = input(\"Berätta ditt namn: \")

I det här fallet returnerar funktionen också ett värde. När funktionen har körts ersätts den del i koden där funktionen anropades med det värde som funktionen returnerar – det är nu ett uttryck med ett värde. input-funktionen returnerar en sträng som innehåller den text som användaren gett programmet. Värdet som en funktion returnerar lagras ofta i en variabel för att programmet ska kunna använda det senare.

Datatyp

Datatyp syftar till de egenskaper ett värde har i ett program. I följande kodexempel är datatyperna sträng (str) för variabeln namn och heltal (int) för variabeln resultat:

namn = \"Anna\"\nresultat = 100

Med hjälp av funktionen type kan man ta reda på datatypen hos ett uttryck. Så här kan du använda funktionen:

print(type(\"Anna\"))\nprint(type(100))

<class 'str'>\n<class 'int'>

Syntax

På samma sätt som vanliga språk har regler för hur man skriver har även programmeringsspråk en syntax – det vill säga ett regelverk för hur koden ska skrivas. Den skiljer sig för varje programmeringsspråk.

Pythons syntax bestämmer bland annat att första raden i en if-sats ska sluta med ett kolon och att därefter följande block ska indenteras:

if namn == \"Anna\":\n    print(\"Hejsan!\")

Följer man inte dessa regler, kommer ett fel att uppstå:

if namn == \"Anna\"\n    print(\"Hejsan!\")
\n  File \"testi.py\", line 1\n    if namn == \"Anna\"\n                    ^\nSyntaxError: invalid syntax\n

Att debugga

När syntaxen i ett program är korrekt men programmet inte ändå fungerar på önskat sätt, finns det en bugg i programmet.

Buggar dyker upp i olika slags situationer. Vissa kan orsaka felmeddelanden medan programmet körs. Ta det här programmet som exempel:

x = 10\ny = 0\nresultat = x / y\n\nprint(f\"{x} dividerat med {y} är {resultat}\")

Nu får vi felet:

\nZeroDivisionError: integer division or modulo by zero on line 3\n

Det här felet har med matematik att göra – det går inte att dividera med noll, och det här stoppar programmet medan det körs.

Fel som uppstår medan programmet körs är relativt lätta att korrigera. Felmeddelandet berättar på vilken rad i koden det uppstod problem. Det är förstås möjligt att felet ligger på något annat ställe i koden än just den här specifika raden.

Ibland märker man en bugg eftersom det resultat som koden ger är fel. Att observera och hitta en sådan här bugg kan vara svårt. I programmeringsuppgifterna under den här kursen finns det olika tester som ska hjälpa med att hitta sådana här fel. Före en bugg kan korrigeras måste man ta reda på var felet uppstår.

Programmerare använder ofta termen debugga – att söka efter orsaker till fel som uppstår i koden. Det här är ett ytterst viktigt verktyg i en programmerares verktygslåda. I yrkeslivet använder programmerare ofta mera tid till att debugga än för att skriva ny kod.

Ett enkelt – men desto nyttigare – sätt att debugga sitt program är att lägga till print-satser i sin kod. Att verifiera vad som sker i koden med hjälp av print-instruktioner ger en bekräftelse att programmet gör det som du vill.

Det här är ett exempel på ett försök att lösa en av föregående modulens uppgifter:

timlon = float(input(\"Timlön: \"))\ntimmar = int(input(\"Arbetstimmar: \"))\ndag = input(\"Veckodag: \")\n\nlon = timlon * timmar\nif dag == \"söndagg\":\n    lon * 2\n\nprint(f\"Lön {lon} euro\")

Det här programmet fungerar inte helt korrekt. När testen körs får vi följande resultat:

\nFAIL: PalkkaTest: test_sunnuntai_1\n\nSyötteellä 23.0, 12, söndag oikeaa palkkaa 552.0 ei löydy tulosteestasi Lön 276.0 euro\n

När vi debuggar den här kursens uppgifter är det första steget ofta att testa hur programmet fungerar när man ger det den data som orsakade ett fel i testet. Nu kan vi se att programmet faktiskt ger ett inkorrekt resultat:

Lön 276.0 euro

Att debugga innebär vanligtvis att vi kör programmet flera gånger. Det kan vara händigt att tillfälligt hårdkoda det problematiska värdet istället för att alltid fråga efter värdet från användaren. Så här kunde det se ut i vårt exempel:

# timlon = float(input(\"Timlön: \"))\n# timmar = int(input(\"Arbetstimmar: \"))\n# dag = input(\"Veckodag: \")\ntimlon = 23.0\ntimmar = 12\ndag = \"söndag\"\n\nlon = timlon * timmar\nif dag == \"söndagg\":\n    lon * 2\n\nprint(f\"Lön {lon} euro\")

Nästa steg kan vara att lägga till print-satser för att debugga. Problemet i den här koden uppstår i den delen där söndagar behandlas. Låt oss lägga till ett par print-satser: en före lönen ska fördubblas och en efter det:

# ...\n\nlon = timlon * timmar\nif dag == \"söndagg\":\n    print(\"lön i början:\", lon)\n    lon * 2\n    print(\"lön efter fördubbling:\", lon)\n\nprint(f\"Lön {lon} euro\")

När vi kör koden märker vi att programmet inte alls skriver ut något på basis av de print-satser vi lagt till i koden. Det verkar som att innehållet i if-blocket aldrig körs. Det finns visst ett problem med if-satsen. Låt oss skriva ut Boolean-uttryckets värde:

# ...\n\nlon = timlon * timmar\nprint(\"villkor:\", dag==\"söndagg\")\nif dag == \"söndagg\":\n    print(\"lön i början:\", lon)\n    lon * 2\n    print(\"lön efter fördubbling:\", lon)\n\nprint(f\"Lön {lon} euro\")

Värdet är False, alltså kommer if-blockets kod aldrig att köras:

villkor: False\nLön 276.0 euro

Problemet ligger alltså i if-satsens villkor. Som i flera andra situationer inom programmering har bokstavsstorleken också skillnad när man jämför värden. Observera att \"söndagg\" i Boolean-uttrycket är skrivet med två g medan det i indatat inte är det. Vi korrigerar felet – både i if-satsen och print-instruktionen:

# ...\n\nlon = timlon * timmar\nprint(\"villkor:\", dag==\"söndag\")\nif dag == \"söndag\":\n    print(\"lön i början:\", lon)\n    lon * 2\n    print(\"lön efter fördubbling:\", lon)\n\nprint(f\"Lön {lon} euro\")

Nu får vi följande utskrift när programmet körs:

villkor: True\nlön i början: 276.0\nlön efter fördubbling: 276.0\nLön 276.0 euro

Det verkar som att värdet lagrat i lon är korrekt i början: timlon = 20.0 och timmar = 12, 20,0 * 6 = 120,0. Instruktionen som ska multiplicera det här med två fungerar dock inte. Det måste alltså vara ett problem med den instruktionen:

lon * 2

Instruktionen multiplicerar nog värdet, men resultatet lagras ingenstans. Vi ändrar på det:

lon *= 2

När vi nu kör programmet, märker vi att resultatet är korrekt:

villkor: True\nlön i början: 276.0\nlön efter fördubbling: 552.0\nLön 552.0 euro

När programmet fungerar som det ska, är det viktigt att ta bort print-satser och annan kod som använts för att debugga.

Det här var ett ganska enkelt exempel och i fall som det här kan man eventuellt hitta buggar genom att läsa igenom koden med omtanke. Att använda print-satser för att debugga är ändå ofta ett snabbt sätt att få en ledtråd för var problemet kan ligga. print-satser kan också användas för att fastställa vilka delar av koden som fungerar korrekt. Då kan man fokusera på andra ställen där buggar med större sannolikhet gömmer sig.

print-satser är bara ett sätt att debugga program. Vi återkommer till det här ämnet senare under kursen. Nu ska du bli van vid att debugga, med hjälp av print-instruktioner, för att hitta problematiska delar i din kod. Proffs klarar sig inte utan print-satser i debuggningssyfte – det är alltså en viktig resurs redan som nybörjare.

I följande program finns flera syntaxfel. Korrigera dem så att programmet fungerar enligt nedan presenterade exempel.

  nummer = input(\"Ge en siffra: \")\n  if nummer>100\n    print(\"Siffran är större än 100\")\n    nummer - 100\n    print(\"Nu har siffran blivit 100 mindre)\n     print(\"Värdet är nu\"+ nummer)\n print(nummer + \" är visst mitt lyckotal!\")\n print(\"Ha en trevlig fortsättning på dagen!)

Ge en siffra: 13\n13 är visst mitt lyckotal!\nHa en trevlig fortsättning på dagen!

Ge en siffra: 101\nSiffran är större än 100\nNu har siffran blivit 100 mindre\nVärdet är nu 1\n1 är visst mitt lyckotal!\nHa en trevlig fortsättning på dagen!

Med funktionen len kan man bland annat räkna längden på en sträng. Funktionen returnerar antalet tecken i strängen.

Exempel:

ord = \"abcd\"\nprint(len(ord))\n\nprint(len(\"hejsan\"))\n\nord2 = \"tjingeling\"\nlangd = len(ord2)\nprint(langd)\n\ntom_strang = \"\"\nlangd = len(tom_strang)\nprint(langd)

4\n6\n10\n0

Skapa ett program som ber användaren mata in ett ord. Programmet ska därefter skriva ut antalet tecken, om antalet överstiger ett.

Exempel:

Ge ett ord: hej\nI ordet hej finns det 3 bokstäver\nTack!

Ge ett ord: basilika\nI ordet basilika finns det 8 bokstäver\nTack!

Ge ett ord: b\nTack!

I Python kan man ofta konvertera ett värde från en datatyp till en annan. Ett flyttal kan till exempel konverteras till ett heltal med funktionen int:

temperatur = float(input(\"Ange temperatur: \"))\n\nprint(\"Temperaturen är\", temperatur)\n\nprint(\"Alltså ungefär\", int(temperatur))

Ange temperatur: 5.15\nTemperaturen är 5.15\nAlltså ungefär 5

Observera att funktionen inte avrundar värdet på det sättet vi kunde anta från matematiken. Siffran avrundas alltid nedåt (golvfunktion):

Ange temperatur: 8.99\nTemperaturen är 8.99\nAlltså ungefär 8

Använd int-funktionen för att skapa ett program som frågar om ett decimaltal från användaren. Programmet ska därefter skriva ut heltals- och decimaldelen av talet på skilda rader.

Du kan anta att det givna decimaltalet är större än noll.

Exempel:

Ge en siffra: 1.34\nHeltalsdel: 1\nDecimaldel: 0.34

","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-2/1-terminologi.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till de väsentligaste termerna inom programmering"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad skillnaden mellan en sats och ett uttryck är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du ta reda på datatypen på värdet av ett uttryck"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"har du lärt dig hur du genom att debugga hittar fel i din kod."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Under den första modulen fokuserade vi inte så mycket på terminologi. Nu är det dags att ta en titt på några centrala begrepp inom programmering."}]},{"type":"element","tagName":"h2","properties":{"id":"sats","style":"position:relative;"},"children":[{"type":"text","value":"Sats"},{"type":"element","tagName":"a","properties":{"href":"#sats","ariaLabel":"sats permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En sats (statement) är en del av ett program som kör någonting. En sats syftar ofta, men inte alltid, till en enstaka instruktion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print(\"Hej\")"}]},{"type":"text","value":" är en sats som skriver ut en rad text. På samma sätt är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer = 2"}]},{"type":"text","value":" en sats som lagrar ett värde i en variabel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En sats kan också vara mera invecklad. Den kan till exempel bestå av flera satser. Följande sats består av tre rader:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovan finns två satser (print-sats och tilldelningssats) inom en if-sats."}]},{"type":"element","tagName":"h2","properties":{"id":"block","style":"position:relative;"},"children":[{"type":"text","value":"Block"},{"type":"element","tagName":"a","properties":{"href":"#block","ariaLabel":"block permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett block är en grupp av efter varandra följande satser som är på samma nivå i programmets struktur. Till exempel if-satsens block innehåller de satser som körs då villkoret är sant."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# if-satsens block inleds"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du är myndig!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du är nu ett år äldre...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# blocket avslutas"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är i ett annat block.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python markeras block genom att indentera all kod i blocket med samma antal mellanrum."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Pythons huvudblock ska aldrig indenteras, utan ska alltid vara så långt till vänster som möjligt i filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här programmet fungerar inte eftersom koden inte är indenterad till det vänstra hörnet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej allihopa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett dåligt program\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"uttryck","style":"position:relative;"},"children":[{"type":"text","value":"Uttryck"},{"type":"element","tagName":"a","properties":{"href":"#uttryck","ariaLabel":"uttryck permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett uttryck (expression) är en kodsnutt som resulterar i en viss datatyp. När programmet körs, får uttrycket ett värde som kan användas i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här följder några exempel på uttryck:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Uttryck"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Värde"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Datatyp"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Datatyp i Python"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 4 + 3"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"heltal"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abc\" + \"de\""}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abcde\""}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"sträng"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"11 / 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"5.5"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"flyttal"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 * 5 > 9"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"sanningsvärde"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom alla uttryck har en datatyp, kan de tilldelas till en variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# variabeln x får värdet på uttrycket 1 + 2"}]},{"type":"text","value":"\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Enkla uttryck kan kombineras för att få ett mera komplicerat uttryck till stånd. Så här kan man till exempel utföra räkneoperationer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# variabeln y får värdet på uttrycket \"3 gånger x plus x upphöjt till två\""}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"funktion","style":"position:relative;"},"children":[{"type":"text","value":"Funktion"},{"type":"element","tagName":"a","properties":{"href":"#funktion","ariaLabel":"funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion kör någon slags funktionalitet. Funktioner kan också ta emot en eller flera parametrar, alltså data som funktionen tar emot och behandlar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion körs då den anropas – det vill säga då funktionen och dess möjliga parametrar nämns i koden. Följande sats anropar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-funktionen med parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"det här är en parameter\""}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"det här är en parameter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En annan funktion du redan känner till väl är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":", som används för att ta emot data från användaren. Parametern i funktionen är det meddelande som ska visas till användaren:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Berätta ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet returnerar funktionen också ett värde. När funktionen har körts ersätts den del i koden där funktionen anropades med det värde som funktionen returnerar – det är nu ett uttryck med ett värde. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":"-funktionen returnerar en sträng som innehåller den text som användaren gett programmet. Värdet som en funktion returnerar lagras ofta i en variabel för att programmet ska kunna använda det senare."}]},{"type":"element","tagName":"h2","properties":{"id":"datatyp","style":"position:relative;"},"children":[{"type":"text","value":"Datatyp"},{"type":"element","tagName":"a","properties":{"href":"#datatyp","ariaLabel":"datatyp permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datatyp syftar till de egenskaper ett värde har i ett program. I följande kodexempel är datatyperna sträng ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":") för variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och heltal ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":") för variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med hjälp av funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" kan man ta reda på datatypen hos ett uttryck. Så här kan du använda funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"syntax","style":"position:relative;"},"children":[{"type":"text","value":"Syntax"},{"type":"element","tagName":"a","properties":{"href":"#syntax","ariaLabel":"syntax permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt som vanliga språk har regler för hur man skriver har även programmeringsspråk en syntax – det vill säga ett regelverk för hur koden ska skrivas. Den skiljer sig för varje programmeringsspråk."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons syntax bestämmer bland annat att första raden i en if-sats ska sluta med ett kolon och att därefter följande block ska indenteras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följer man inte dessa regler, kommer ett fel att uppstå:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":" File \"testi.py\", line 1\n if namn == \"Anna\"\n ^\nSyntaxError: invalid syntax\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-debugga","style":"position:relative;"},"children":[{"type":"text","value":"Att debugga"},{"type":"element","tagName":"a","properties":{"href":"#att-debugga","ariaLabel":"att debugga permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När syntaxen i ett program är korrekt men programmet inte ändå fungerar på önskat sätt, finns det en bugg i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Buggar dyker upp i olika slags situationer. Vissa kan orsaka felmeddelanden medan programmet körs. Ta det här programmet som exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" y\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" dividerat med "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu får vi felet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"ZeroDivisionError: integer division or modulo by zero on line 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet har med matematik att göra – det går inte att dividera med noll, och det här stoppar programmet medan det körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel som uppstår medan programmet körs är relativt lätta att korrigera. Felmeddelandet berättar på vilken rad i koden det uppstod problem. Det är förstås möjligt att felet ligger på något annat ställe i koden än just den här specifika raden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland märker man en bugg eftersom det resultat som koden ger är fel. Att observera och hitta en sådan här bugg kan vara svårt. I programmeringsuppgifterna under den här kursen finns det olika tester som ska hjälpa med att hitta sådana här fel. Före en bugg kan korrigeras måste man ta reda på var felet uppstår."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmerare använder ofta termen debugga – att söka efter orsaker till fel som uppstår i koden. Det här är ett ytterst viktigt verktyg i en programmerares verktygslåda. I yrkeslivet använder programmerare ofta mera tid till att debugga än för att skriva ny kod."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett enkelt – men desto nyttigare – sätt att debugga sitt program är att lägga till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser i sin kod. Att verifiera vad som sker i koden med hjälp av print-instruktioner ger en bekräftelse att programmet gör det som du vill."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här är ett exempel på ett försök att lösa en av föregående modulens uppgifter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Timlön: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntimmar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arbetstimmar: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Veckodag: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet fungerar inte helt korrekt. När testen körs får vi följande resultat:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"FAIL: PalkkaTest: test_sunnuntai_1\n\nSyötteellä 23.0, 12, söndag oikeaa palkkaa 552.0 ei löydy tulosteestasi Lön 276.0 euro\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi debuggar den här kursens uppgifter är det första steget ofta att testa hur programmet fungerar när man ger det den data som orsakade ett fel i testet. Nu kan vi se att programmet faktiskt ger ett inkorrekt resultat:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lön 276.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att debugga innebär vanligtvis att vi kör programmet flera gånger. Det kan vara händigt att tillfälligt hårdkoda det problematiska värdet istället för att alltid fråga efter värdet från användaren. Så här kunde det se ut i vårt exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# timlon = float(input(\"Timlön: \"))"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# timmar = int(input(\"Arbetstimmar: \"))"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# dag = input(\"Veckodag: \")"}]},{"type":"text","value":"\ntimlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23.0"}]},{"type":"text","value":"\ntimmar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\ndag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndag\""}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nästa steg kan vara att lägga till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser för att debugga. Problemet i den här koden uppstår i den delen där söndagar behandlas. Låt oss lägga till ett par print-satser: en före lönen ska fördubblas och en efter det:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön i början:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön efter fördubbling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi kör koden märker vi att programmet inte alls skriver ut något på basis av de "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser vi lagt till i koden. Det verkar som att innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":"-blocket aldrig körs. Det finns visst ett problem med if-satsen. Låt oss skriva ut Boolean-uttryckets värde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"villkor:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndagg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön i början:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön efter fördubbling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", alltså kommer "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":"-blockets kod aldrig att köras:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"villkor: False\nLön 276.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problemet ligger alltså i if-satsens villkor. Som i flera andra situationer inom programmering har bokstavsstorleken också skillnad när man jämför värden. Observera att \"söndagg\" i Boolean-uttrycket är skrivet med två g medan det i indatat inte är det. Vi korrigerar felet – både i if-satsen och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\nlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timlon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" timmar\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"villkor:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"söndag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön i början:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lön efter fördubbling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Lön "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu får vi följande utskrift när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"villkor: True\nlön i början: 276.0\nlön efter fördubbling: 276.0\nLön 276.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det verkar som att värdet lagrat i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lon"}]},{"type":"text","value":" är korrekt i början: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timlon = 20.0"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timmar = 12"}]},{"type":"text","value":", 20,0 * 6 = 120,0. Instruktionen som ska multiplicera det här med två fungerar dock inte. Det måste alltså vara ett problem med den instruktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen multiplicerar nog värdet, men resultatet lagras ingenstans. Vi ändrar på det:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi nu kör programmet, märker vi att resultatet är korrekt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"villkor: True\nlön i början: 276.0\nlön efter fördubbling: 552.0\nLön 552.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet fungerar som det ska, är det viktigt att ta bort "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser och annan kod som använts för att debugga."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här var ett ganska enkelt exempel och i fall som det här kan man eventuellt hitta buggar genom att läsa igenom koden med omtanke. Att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser för att debugga är ändå ofta ett snabbt sätt att få en ledtråd för var problemet kan ligga. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser kan också användas för att fastställa vilka delar av koden som fungerar korrekt. Då kan man fokusera på andra ställen där buggar med större sannolikhet gömmer sig."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser är bara ett sätt att debugga program. Vi återkommer till det här ämnet senare under kursen. Nu ska du bli van vid att debugga, med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktioner, för att hitta problematiska delar i din kod. Proffs klarar sig inte utan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser i debuggningssyfte – det är alltså en viktig resurs redan som nybörjare."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fixa felen","tmcname":"osa02-01_fixa_felen","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande program finns flera syntaxfel. Korrigera dem så att programmet fungerar enligt nedan presenterade exempel."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffran är större än 100\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\"Nu har siffran blivit "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":" mindre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Värdet är nu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" är visst mitt lyckotal!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"\"Ha en trevlig fortsättning på dagen!"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"13"}]},{"type":"text","value":"\n13 är visst mitt lyckotal!\nHa en trevlig fortsättning på dagen!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"101"}]},{"type":"text","value":"\nSiffran är större än 100\nNu har siffran blivit 100 mindre\nVärdet är nu 1\n1 är visst mitt lyckotal!\nHa en trevlig fortsättning på dagen!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Antalet tecken","tmcname":"osa02-02_antalet_tecken"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" kan man bland annat räkna längden på en sträng. Funktionen returnerar antalet tecken i strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcd\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nord2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tjingeling\""}]},{"type":"text","value":"\nlangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntom_strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\nlangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tom_strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4\n6\n10\n0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren mata in ett ord. Programmet ska därefter skriva ut antalet tecken, om antalet överstiger ett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: hej\nI ordet hej finns det 3 bokstäver\nTack!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: basilika\nI ordet basilika finns det 8 bokstäver\nTack!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett ord: b\nTack!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Konvertera värden","tmcname":"osa02-03_konvertera_varden"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python kan man ofta konvertera ett värde från en datatyp till en annan. Ett flyttal kan till exempel konverteras till ett heltal med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"temperatur "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange temperatur: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Temperaturen är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" temperatur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alltså ungefär\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"temperatur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange temperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5.15"}]},{"type":"text","value":"\nTemperaturen är 5.15\nAlltså ungefär 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att funktionen inte avrundar värdet på det sättet vi kunde anta från matematiken. Siffran avrundas alltid nedåt (golvfunktion):"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange temperatur: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8.99"}]},{"type":"text","value":"\nTemperaturen är 8.99\nAlltså ungefär 8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":"-funktionen för att skapa ett program som frågar om ett decimaltal från användaren. Programmet ska därefter skriva ut heltals- och decimaldelen av talet på skilda rader."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att det givna decimaltalet är större än noll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1.34"}]},{"type":"text","value":"\nHeltalsdel: 1\nDecimaldel: 0.34"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • känner du till de väsentligaste termerna inom programmering
  • \n
  • vet du vad skillnaden mellan en sats och ett uttryck är
  • \n
  • kan du ta reda på datatypen på värdet av ett uttryck
  • \n
  • har du lärt dig hur du genom att debugga hittar fel i din kod.
  • \n

Under den första modulen fokuserade vi inte så mycket på terminologi. Nu är det dags att ta en titt på några centrala begrepp inom programmering.

Sats

En sats (statement) är en del av ett program som kör någonting. En sats syftar ofta, men inte alltid, till en enstaka instruktion.

Till exempel print(\"Hej\") är en sats som skriver ut en rad text. På samma sätt är nummer = 2 en sats som lagrar ett värde i en variabel.

En sats kan också vara mera invecklad. Den kan till exempel bestå av flera satser. Följande sats består av tre rader:

if namn == \"Anna\":\n    print(\"Hejsan!\")\n    nummer = 2

Ovan finns två satser (print-sats och tilldelningssats) inom en if-sats.

Block

Ett block är en grupp av efter varandra följande satser som är på samma nivå i programmets struktur. Till exempel if-satsens block innehåller de satser som körs då villkoret är sant.

if alder > 17:\n    # if-satsens block inleds\n    print(\"Du är myndig!\")\n    alder = alder + 1\n    print(\"Du är nu ett år äldre...\")\n    # blocket avslutas\n\nprint(\"Det här är i ett annat block.\")

I Python markeras block genom att indentera all kod i blocket med samma antal mellanrum.

Obs! Pythons huvudblock ska aldrig indenteras, utan ska alltid vara så långt till vänster som möjligt i filen:

# det här programmet fungerar inte eftersom koden inte är indenterad till det vänstra hörnet\n  print(\"Hej allihopa\")\n  print(\"Det här är ett dåligt program\")

Uttryck

Ett uttryck (expression) är en kodsnutt som resulterar i en viss datatyp. När programmet körs, får uttrycket ett värde som kan användas i programmet.

Här följder några exempel på uttryck:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
UttryckVärdeDatatypDatatyp i Python
2 + 4 + 39heltalint
\"abc\" + \"de\"\"abcde\"strängstr
11 / 25.5flyttalfloat
2 * 5 > 9Truesanningsvärdebool

Eftersom alla uttryck har en datatyp, kan de tilldelas till en variabel:

# variabeln x får värdet på uttrycket 1 + 2\nx = 1 + 2

Enkla uttryck kan kombineras för att få ett mera komplicerat uttryck till stånd. Så här kan man till exempel utföra räkneoperationer:

# variabeln y får värdet på uttrycket \"3 gånger x plus x upphöjt till två\"\ny = 3 * x + x**2

Funktion

En funktion kör någon slags funktionalitet. Funktioner kan också ta emot en eller flera parametrar, alltså data som funktionen tar emot och behandlar.

En funktion körs då den anropas – det vill säga då funktionen och dess möjliga parametrar nämns i koden. Följande sats anropar print-funktionen med parametern \"det här är en parameter\":

print(\"det här är en parameter\")

En annan funktion du redan känner till väl är input, som används för att ta emot data från användaren. Parametern i funktionen är det meddelande som ska visas till användaren:

namn = input(\"Berätta ditt namn: \")

I det här fallet returnerar funktionen också ett värde. När funktionen har körts ersätts den del i koden där funktionen anropades med det värde som funktionen returnerar – det är nu ett uttryck med ett värde. input-funktionen returnerar en sträng som innehåller den text som användaren gett programmet. Värdet som en funktion returnerar lagras ofta i en variabel för att programmet ska kunna använda det senare.

Datatyp

Datatyp syftar till de egenskaper ett värde har i ett program. I följande kodexempel är datatyperna sträng (str) för variabeln namn och heltal (int) för variabeln resultat:

namn = \"Anna\"\nresultat = 100

Med hjälp av funktionen type kan man ta reda på datatypen hos ett uttryck. Så här kan du använda funktionen:

print(type(\"Anna\"))\nprint(type(100))

<class 'str'>\n<class 'int'>

Syntax

På samma sätt som vanliga språk har regler för hur man skriver har även programmeringsspråk en syntax – det vill säga ett regelverk för hur koden ska skrivas. Den skiljer sig för varje programmeringsspråk.

Pythons syntax bestämmer bland annat att första raden i en if-sats ska sluta med ett kolon och att därefter följande block ska indenteras:

if namn == \"Anna\":\n    print(\"Hejsan!\")

Följer man inte dessa regler, kommer ett fel att uppstå:

if namn == \"Anna\"\n    print(\"Hejsan!\")
\n  File \"testi.py\", line 1\n    if namn == \"Anna\"\n                    ^\nSyntaxError: invalid syntax\n

Att debugga

När syntaxen i ett program är korrekt men programmet inte ändå fungerar på önskat sätt, finns det en bugg i programmet.

Buggar dyker upp i olika slags situationer. Vissa kan orsaka felmeddelanden medan programmet körs. Ta det här programmet som exempel:

x = 10\ny = 0\nresultat = x / y\n\nprint(f\"{x} dividerat med {y} är {resultat}\")

Nu får vi felet:

\nZeroDivisionError: integer division or modulo by zero on line 3\n

Det här felet har med matematik att göra – det går inte att dividera med noll, och det här stoppar programmet medan det körs.

Fel som uppstår medan programmet körs är relativt lätta att korrigera. Felmeddelandet berättar på vilken rad i koden det uppstod problem. Det är förstås möjligt att felet ligger på något annat ställe i koden än just den här specifika raden.

Ibland märker man en bugg eftersom det resultat som koden ger är fel. Att observera och hitta en sådan här bugg kan vara svårt. I programmeringsuppgifterna under den här kursen finns det olika tester som ska hjälpa med att hitta sådana här fel. Före en bugg kan korrigeras måste man ta reda på var felet uppstår.

Programmerare använder ofta termen debugga – att söka efter orsaker till fel som uppstår i koden. Det här är ett ytterst viktigt verktyg i en programmerares verktygslåda. I yrkeslivet använder programmerare ofta mera tid till att debugga än för att skriva ny kod.

Ett enkelt – men desto nyttigare – sätt att debugga sitt program är att lägga till print-satser i sin kod. Att verifiera vad som sker i koden med hjälp av print-instruktioner ger en bekräftelse att programmet gör det som du vill.

Det här är ett exempel på ett försök att lösa en av föregående modulens uppgifter:

timlon = float(input(\"Timlön: \"))\ntimmar = int(input(\"Arbetstimmar: \"))\ndag = input(\"Veckodag: \")\n\nlon = timlon * timmar\nif dag == \"söndagg\":\n    lon * 2\n\nprint(f\"Lön {lon} euro\")

Det här programmet fungerar inte helt korrekt. När testen körs får vi följande resultat:

\nFAIL: PalkkaTest: test_sunnuntai_1\n\nSyötteellä 23.0, 12, söndag oikeaa palkkaa 552.0 ei löydy tulosteestasi Lön 276.0 euro\n

När vi debuggar den här kursens uppgifter är det första steget ofta att testa hur programmet fungerar när man ger det den data som orsakade ett fel i testet. Nu kan vi se att programmet faktiskt ger ett inkorrekt resultat:

Lön 276.0 euro

Att debugga innebär vanligtvis att vi kör programmet flera gånger. Det kan vara händigt att tillfälligt hårdkoda det problematiska värdet istället för att alltid fråga efter värdet från användaren. Så här kunde det se ut i vårt exempel:

# timlon = float(input(\"Timlön: \"))\n# timmar = int(input(\"Arbetstimmar: \"))\n# dag = input(\"Veckodag: \")\ntimlon = 23.0\ntimmar = 12\ndag = \"söndag\"\n\nlon = timlon * timmar\nif dag == \"söndagg\":\n    lon * 2\n\nprint(f\"Lön {lon} euro\")

Nästa steg kan vara att lägga till print-satser för att debugga. Problemet i den här koden uppstår i den delen där söndagar behandlas. Låt oss lägga till ett par print-satser: en före lönen ska fördubblas och en efter det:

# ...\n\nlon = timlon * timmar\nif dag == \"söndagg\":\n    print(\"lön i början:\", lon)\n    lon * 2\n    print(\"lön efter fördubbling:\", lon)\n\nprint(f\"Lön {lon} euro\")

När vi kör koden märker vi att programmet inte alls skriver ut något på basis av de print-satser vi lagt till i koden. Det verkar som att innehållet i if-blocket aldrig körs. Det finns visst ett problem med if-satsen. Låt oss skriva ut Boolean-uttryckets värde:

# ...\n\nlon = timlon * timmar\nprint(\"villkor:\", dag==\"söndagg\")\nif dag == \"söndagg\":\n    print(\"lön i början:\", lon)\n    lon * 2\n    print(\"lön efter fördubbling:\", lon)\n\nprint(f\"Lön {lon} euro\")

Värdet är False, alltså kommer if-blockets kod aldrig att köras:

villkor: False\nLön 276.0 euro

Problemet ligger alltså i if-satsens villkor. Som i flera andra situationer inom programmering har bokstavsstorleken också skillnad när man jämför värden. Observera att \"söndagg\" i Boolean-uttrycket är skrivet med två g medan det i indatat inte är det. Vi korrigerar felet – både i if-satsen och print-instruktionen:

# ...\n\nlon = timlon * timmar\nprint(\"villkor:\", dag==\"söndag\")\nif dag == \"söndag\":\n    print(\"lön i början:\", lon)\n    lon * 2\n    print(\"lön efter fördubbling:\", lon)\n\nprint(f\"Lön {lon} euro\")

Nu får vi följande utskrift när programmet körs:

villkor: True\nlön i början: 276.0\nlön efter fördubbling: 276.0\nLön 276.0 euro

Det verkar som att värdet lagrat i lon är korrekt i början: timlon = 20.0 och timmar = 12, 20,0 * 6 = 120,0. Instruktionen som ska multiplicera det här med två fungerar dock inte. Det måste alltså vara ett problem med den instruktionen:

lon * 2

Instruktionen multiplicerar nog värdet, men resultatet lagras ingenstans. Vi ändrar på det:

lon *= 2

När vi nu kör programmet, märker vi att resultatet är korrekt:

villkor: True\nlön i början: 276.0\nlön efter fördubbling: 552.0\nLön 552.0 euro

När programmet fungerar som det ska, är det viktigt att ta bort print-satser och annan kod som använts för att debugga.

Det här var ett ganska enkelt exempel och i fall som det här kan man eventuellt hitta buggar genom att läsa igenom koden med omtanke. Att använda print-satser för att debugga är ändå ofta ett snabbt sätt att få en ledtråd för var problemet kan ligga. print-satser kan också användas för att fastställa vilka delar av koden som fungerar korrekt. Då kan man fokusera på andra ställen där buggar med större sannolikhet gömmer sig.

print-satser är bara ett sätt att debugga program. Vi återkommer till det här ämnet senare under kursen. Nu ska du bli van vid att debugga, med hjälp av print-instruktioner, för att hitta problematiska delar i din kod. Proffs klarar sig inte utan print-satser i debuggningssyfte – det är alltså en viktig resurs redan som nybörjare.

I följande program finns flera syntaxfel. Korrigera dem så att programmet fungerar enligt nedan presenterade exempel.

  nummer = input(\"Ge en siffra: \")\n  if nummer>100\n    print(\"Siffran är större än 100\")\n    nummer - 100\n    print(\"Nu har siffran blivit 100 mindre)\n     print(\"Värdet är nu\"+ nummer)\n print(nummer + \" är visst mitt lyckotal!\")\n print(\"Ha en trevlig fortsättning på dagen!)

Ge en siffra: 13\n13 är visst mitt lyckotal!\nHa en trevlig fortsättning på dagen!

Ge en siffra: 101\nSiffran är större än 100\nNu har siffran blivit 100 mindre\nVärdet är nu 1\n1 är visst mitt lyckotal!\nHa en trevlig fortsättning på dagen!

Med funktionen len kan man bland annat räkna längden på en sträng. Funktionen returnerar antalet tecken i strängen.

Exempel:

ord = \"abcd\"\nprint(len(ord))\n\nprint(len(\"hejsan\"))\n\nord2 = \"tjingeling\"\nlangd = len(ord2)\nprint(langd)\n\ntom_strang = \"\"\nlangd = len(tom_strang)\nprint(langd)

4\n6\n10\n0

Skapa ett program som ber användaren mata in ett ord. Programmet ska därefter skriva ut antalet tecken, om antalet överstiger ett.

Exempel:

Ge ett ord: hej\nI ordet hej finns det 3 bokstäver\nTack!

Ge ett ord: basilika\nI ordet basilika finns det 8 bokstäver\nTack!

Ge ett ord: b\nTack!

I Python kan man ofta konvertera ett värde från en datatyp till en annan. Ett flyttal kan till exempel konverteras till ett heltal med funktionen int:

temperatur = float(input(\"Ange temperatur: \"))\n\nprint(\"Temperaturen är\", temperatur)\n\nprint(\"Alltså ungefär\", int(temperatur))

Ange temperatur: 5.15\nTemperaturen är 5.15\nAlltså ungefär 5

Observera att funktionen inte avrundar värdet på det sättet vi kunde anta från matematiken. Siffran avrundas alltid nedåt (golvfunktion):

Ange temperatur: 8.99\nTemperaturen är 8.99\nAlltså ungefär 8

Använd int-funktionen för att skapa ett program som frågar om ett decimaltal från användaren. Programmet ska därefter skriva ut heltals- och decimaldelen av talet på skilda rader.

Du kan anta att det givna decimaltalet är större än noll.

Exempel:

Ge en siffra: 1.34\nHeltalsdel: 1\nDecimaldel: 0.34

","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-2/1-terminologi.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-2/2-else-elif/page-data.json b/page-data/osa-2/2-else-elif/page-data.json index 06df34a64..65f4f7e45 100644 --- a/page-data/osa-2/2-else-elif/page-data.json +++ b/page-data/osa-2/2-else-elif/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-2/2-else-elif", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa grenar inom if-satser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"förstår du skillnaden mellan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda restoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" i Boolean-uttryck."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi tar nu en titt på ett program som ber användaren att ge en siffra och därefter skriver ut ett meddelande vars innehåll beror på om siffran är negativ, positiv eller lika med noll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är negativt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är positivt eller noll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här verkar något klumpigt och det finns en del upprepning. Vi vill ju bara köra ett av if-blocken eftersom numret alltid är antingen under noll, eller noll eller över. Det vill säga bara ett av villkoren "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra < 0"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra >= 0"}]},{"type":"text","value":" är samtidigt sant. Därför är den fösta if-satsen den enda som behövs – om villkoret är sant är siffran negativ, annars är siffran noll eller över."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I stället för att skapa två if-satser kan vi skapa en gren som körs då alla villkor är osanna. Det här kallas else-sats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan vi skriva om det föregående exemplet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är negativt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är positivt eller noll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi bygger upp en if-else-sats kommer exakt en av grenarna att köras. Se följande bild:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 400px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/3b891467aebcf64f494137108d124bcb/e17e5/2_2_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 148.2608695652174%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAYAAAAsEj5rAAAACXBIWXMAAAsSAAALEgHS3X78AAACPUlEQVRIx5WWx84qMQyFef+nQrCCDSxAFAkQvfdeffVZ16OQyczwW4oSPMnJcQ05+S+fzyeabfh65HK5yPv9/tK5kksC9MFNt9vt5Pl8Bi//AvRv9Nf2u9fryfV61TVM/UtjDF+vl+z3+xgQ8ng8pFKpyGw2kySJAd5uN1kul1+68/msutVqJePxWOf1eq2D/TYgEwO83++y2WwikxB+TyYTBajX6zIYDGS73eqMjvVisZD5fB5maICuyYDjCtjxnWj7/udbJqAfIBjCyA0KQuQV0A99EkMCAsN8Pi/dbjeKdBDwF5PZjJk4nkGQ8LdJBOibBJMQoDkedqw57AJiPvockSmXy9JoNKRarUq73Q4CEk2qBFDMBRBrTEitfr8vOZAxAcqsMcsPCoL/0MPC2HLOhMtqtdp36YXykItswGw0Gikb3/eRyaEoYx5CiVmFWALDJFTjXMje1DzELDdYgPKNgwD7gJmJDaCVn6WJG3W/UrgwtZYBdPvgcDjUbAC82WzqwK+Hw0HPsD/IEOo+IDoOMjCv0+lob2T/8XjUgVtigGw2hjAxPQcwibwF3A1OsB+GurTfF5m5DCahJyLYsd3I+YAIplrr8jt66iNlDE1/Op3U5FKppCVG5YTaW6xS3E3G0ErPapdv+NKaQyagbeCQpUOhUNDmUSwWNV0oQdeXiYD+C2gPEF0IxtPpVJnZA/UTYOhfAWyoDgABZv2TD9P+lljFuG0rMcpZTG1utVoa8aRH7M+AmOw+UD7Df9LKKMYwbDnoAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/3b891467aebcf64f494137108d124bcb/a0b58/2_2_1.webp 230w","/static/3b891467aebcf64f494137108d124bcb/7f61c/2_2_1.webp 400w"],"sizes":"(max-width: 400px) 100vw, 400px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/3b891467aebcf64f494137108d124bcb/81c8e/2_2_1.png 230w","/static/3b891467aebcf64f494137108d124bcb/e17e5/2_2_1.png 400w"],"sizes":"(max-width: 400px) 100vw, 400px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/3b891467aebcf64f494137108d124bcb/e17e5/2_2_1.png","alt":"2 2 1","title":"2 2 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Det kan aldrig finnas en else-gren före en if-gren. En if-gren och en else-gren bildar en if-else-sats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel kollar om den siffra användaren anger är jämnt eller inte. Det här kan restoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" användas för. Restoperatorn anger resten när två heltal divideras. När ett tal divideras med två är det jämnt då resten är noll. Annars är talet inte jämnt."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är jämnt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är ojämnt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTalet är ojämnt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat exempel där strängar jämförs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"korrekt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"katt\""}]},{"type":"text","value":"\nlosenord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange lösenord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" losenord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Välkommen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tillträde förbjudet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan det se ut när koden körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange lösenord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"katt"}]},{"type":"text","value":"\nVälkommen"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange lösenord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"koltrast"}]},{"type":"text","value":"\nTillträde förbjudet"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Myndig eller inte?","tmcname":"osa02-04_myndig","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge sin ålder. Programmet berättar om användaren är myndig (> 18 år) eller inte."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur gammal är du? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\nDu är inte myndig!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur gammal är du? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"32"}]},{"type":"text","value":"\nDu är myndig!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"flera-grenar-med-elif-satser","style":"position:relative;"},"children":[{"type":"text","value":"Flera grenar med elif-satser"},{"type":"element","tagName":"a","properties":{"href":"#flera-grenar-med-elif-satser","ariaLabel":"flera grenar med elif satser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ofta finns det fler än två alternativ som ett program måste ta i beaktande. Till exempel resultatet av en fotbollsmatch kan se ut på tre sätt: hemmalaget vinner, bortalaget vinner eller oavgjort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En if-sats kan bestå av elif-grenar – \"else if\". Till den här grenen kommer man om villkoret i någon av de tidigare grenarna inte uppfylls."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 498px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/465dc71ba18611f41d4424c41f6d771c/79e1b/2_2_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 168.69565217391306%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAiCAYAAABfqvm9AAAACXBIWXMAAAsSAAALEgHS3X78AAACRUlEQVRIx52W167CMBBE8/9/xhMSDwghihC99158dSwN2jg2gWvJZHGZna1J5pxzr9fL6RmTw//3+91dLpfoucxeSMlWCQOw/X6f29fMQobX6zXJUGM0Grl+v+9iIwf4fD7dcrmMamYcj0e/3+l03Hg89vLhcPCMz+ezezweRcDNZuNSfmVvMpl4wNls5mVAMX+327nValUEXK/XpSZzcT6fF8zl7kfAFNN6ve4ajcb7Duu6WwoYgrK/3W69+cj2HGulJltmClqv1/OgTNaUm4vF4nsfag3fAUoQABAg/9vtdrnJrGmyRnpwBuDb7ZY7600OTRKgtHJR/mJKDsHk30wR4kliWkB8pIuh6aE7cmkTLlqGKkWU4ngqA9/hRyoHhUzIsJbxgzO73a6v0RhDwACh3KiOZrPpBoOBGw6HPh+pa5RTMRl1qBRgIQYIM2TG6XTyfp1Op+8WVmgOKZMFiDmqcZ1JNZHM/gmjbBlaQJoD05aewJN5GPoQHzHwJWdoEMywPEsBYUcyq7soiDzVEznzNUPlpkwEAPNJHxjCHPnfJhNlNVT2YP8zYBgUO1DwMyBmiyGXkdlD/hegeh0s9T5hj7OU30dAmwq2lvEVoK1Wy5co574CtL6ygBpcJiiKdNTk1DvEAlqlNBJysTQo4TsEFvYbRhagiCczanKsgYohfiMAyFRGpVJx1WrVs6zVar6N2e4dBUyxJtr0Q4IBc5TZsou2r9hLPvzyUh2H/i01Oex1WsdnqW+gpMmxl5IuwxBzY3t/jzRkZT8rPSQAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/465dc71ba18611f41d4424c41f6d771c/a0b58/2_2_2.webp 230w","/static/465dc71ba18611f41d4424c41f6d771c/bc10c/2_2_2.webp 460w","/static/465dc71ba18611f41d4424c41f6d771c/71ce1/2_2_2.webp 498w"],"sizes":"(max-width: 498px) 100vw, 498px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/465dc71ba18611f41d4424c41f6d771c/81c8e/2_2_2.png 230w","/static/465dc71ba18611f41d4424c41f6d771c/08a84/2_2_2.png 460w","/static/465dc71ba18611f41d4424c41f6d771c/79e1b/2_2_2.png 498w"],"sizes":"(max-width: 498px) 100vw, 498px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/465dc71ba18611f41d4424c41f6d771c/79e1b/2_2_2.png","alt":"2 2 2","title":"2 2 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kollar på ett program som bestämmer vem som vunnit en match:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"mal_hem "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hemmalagets mål: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmal_borta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bortalagets mål: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mal_hem "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" mal_borta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hemmalaget vann!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" mal_borta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" mal_hem"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bortalaget vann!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Oavgjort!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet kan ge tre olika resultat baserat på de värden som ges:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hemmalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nBortalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nHemmalaget vann!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hemmalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nBortalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nBortalaget vann!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hemmalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nBortalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nOavgjort!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan finns tre grenar varav exakt en körs. En if-sats kan dock bestå av fler än en elif-gren. Dessutom är en else-gren inte obligatorisk."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här är också en helt korrekt if-sats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Julkalender\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndatum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilken dag är det? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"24/12\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det är julafton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"25/12\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det är juldag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"26/12\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det är annandag jul\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tack och hej.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Julkalender\nVilken dag är det? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25/12"}]},{"type":"text","value":"\nDet är juldag\nTack och hej."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att det föregående exemplet saknar else-gren. Om användaren ger ett datum som inte uppfyller villkoret på någon av if- eller elif-grenarna, kommer ingen av grenarna att köras."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Julkalender\nVilken dag är det? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1/1"}]},{"type":"text","value":"\nTack och hej."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Större eller lika med","tmcname":"osa02-05_storre_lika_med","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge två heltal. Programmet ska skriva ut det större av de givna talen. Om talen är de samma, observerar programmet även det här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge tal ett: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe tal två: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nDet större talet: 5"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge tal ett: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe tal två: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nDet större talet: 8"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge tal ett: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe tal två: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTalen är lika stora!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Äldre","tmcname":"osa02-06_aldre","height":"550px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter namnen och åldrarna hos två personer. Programmet ska skirva ut namnet på den äldre personen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person 1:\nNamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Thomas"}]},{"type":"text","value":"\nÅlder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"26"}]},{"type":"text","value":"\nPerson 2:\nNamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tilde"}]},{"type":"text","value":"\nÅlder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\nTilde är äldre"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person 1:\nNamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Amadeus"}]},{"type":"text","value":"\nÅlder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nPerson 2:\nNamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Vilma"}]},{"type":"text","value":"\nÅlder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nAmadeus och Vilma är lika gamla"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Sist i alfabetet","tmcname":"osa02-07_sist_i_alfabetet","height":"500px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förutom med siffror, kan man också jämföra storleken på strängar i Python. Strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" är mindre än strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" om strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" kommer före i alfabetet än strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". Observera att det här gäller då"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"man jämför bokstäver med samma storlek (små/STORA)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"strängarna innehåller enbart tecknen a-z eller A-Z."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge två ord. Programmet skriver ut det av orden som kommer senare i alfabetet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att orden endast innehåller små bokstäver."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ord 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"antik"}]},{"type":"text","value":"\nGe ord 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kusudama"}]},{"type":"text","value":"\nkusudama kommer senare i alfabetet"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ord 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"vhs"}]},{"type":"text","value":"\nGe ord 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dvd"}]},{"type":"text","value":"\nvhs kommer senare i alfabetet"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ord 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nGe ord 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nDu gav samma ord två gånger."}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"908fdd39-c6bc-5365-9731-442a7877a47d"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa grenar inom if-satser
  • \n
  • förstår du skillnaden mellan if, elif och else
  • \n
  • kan du använda restoperatorn % i Boolean-uttryck.
  • \n

Vi tar nu en titt på ett program som ber användaren att ge en siffra och därefter skriver ut ett meddelande vars innehåll beror på om siffran är negativ, positiv eller lika med noll:

siffra = int(input(\"Ge ett tal: \"))\n\nif siffra < 0:\n    print(\"Talet är negativt\")\n\nif siffra >= 0:\n    print(\"Talet är positivt eller noll\")

Det här verkar något klumpigt och det finns en del upprepning. Vi vill ju bara köra ett av if-blocken eftersom numret alltid är antingen under noll, eller noll eller över. Det vill säga bara ett av villkoren siffra < 0 och siffra >= 0 är samtidigt sant. Därför är den fösta if-satsen den enda som behövs – om villkoret är sant är siffran negativ, annars är siffran noll eller över.

I stället för att skapa två if-satser kan vi skapa en gren som körs då alla villkor är osanna. Det här kallas else-sats.

Så här kan vi skriva om det föregående exemplet:

siffra = int(input(\"Ge ett tal: \"))\n\nif siffra < 0:\n    print(\"Talet är negativt\")\nelse:\n    print(\"Talet är positivt eller noll\")

När vi bygger upp en if-else-sats kommer exakt en av grenarna att köras. Se följande bild:

\n \n \n \n \n \n \"2\n \n \n

Obs! Det kan aldrig finnas en else-gren före en if-gren. En if-gren och en else-gren bildar en if-else-sats.

Följande exempel kollar om den siffra användaren anger är jämnt eller inte. Det här kan restoperatorn % användas för. Restoperatorn anger resten när två heltal divideras. När ett tal divideras med två är det jämnt då resten är noll. Annars är talet inte jämnt.

siffra = int(input(\"Ge ett tal: \"))\n\nif siffra % 2 == 0:\n    print(\"Talet är jämnt\")\nelse:\n    print(\"Talet är ojämnt\")

Ge ett tal: 5\nTalet är ojämnt

Ett annat exempel där strängar jämförs:

korrekt = \"katt\"\nlosenord = input(\"Ange lösenord: \")\n\nif losenord == korrekt:\n    print(\"Välkommen\")\nelse:\n    print(\"Tillträde förbjudet\")

Så här kan det se ut när koden körs:

Ange lösenord: katt\nVälkommen

Ange lösenord: koltrast\nTillträde förbjudet

Skapa ett program som ber användaren ge sin ålder. Programmet berättar om användaren är myndig (> 18 år) eller inte.

Exempel:

Hur gammal är du? 12\nDu är inte myndig!

Hur gammal är du? 32\nDu är myndig!

Flera grenar med elif-satser

Ofta finns det fler än två alternativ som ett program måste ta i beaktande. Till exempel resultatet av en fotbollsmatch kan se ut på tre sätt: hemmalaget vinner, bortalaget vinner eller oavgjort.

En if-sats kan bestå av elif-grenar – \"else if\". Till den här grenen kommer man om villkoret i någon av de tidigare grenarna inte uppfylls.

\n \n 18 år) eller inte."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur gammal är du? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\nDu är inte myndig!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur gammal är du? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"32"}]},{"type":"text","value":"\nDu är myndig!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"flera-grenar-med-elif-satser","style":"position:relative;"},"children":[{"type":"text","value":"Flera grenar med elif-satser"},{"type":"element","tagName":"a","properties":{"href":"#flera-grenar-med-elif-satser","ariaLabel":"flera grenar med elif satser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ofta finns det fler än två alternativ som ett program måste ta i beaktande. Till exempel resultatet av en fotbollsmatch kan se ut på tre sätt: hemmalaget vinner, bortalaget vinner eller oavgjort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En if-sats kan bestå av elif-grenar – \"else if\". Till den här grenen kommer man om villkoret i någon av de tidigare grenarna inte uppfylls."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 498px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/465dc71ba18611f41d4424c41f6d771c/79e1b/2_2_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 168.69565217391306%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAiCAYAAABfqvm9AAAACXBIWXMAAAsSAAALEgHS3X78AAACRUlEQVRIx52W167CMBBE8/9/xhMSDwghihC99158dSwN2jg2gWvJZHGZna1J5pxzr9fL6RmTw//3+91dLpfoucxeSMlWCQOw/X6f29fMQobX6zXJUGM0Grl+v+9iIwf4fD7dcrmMamYcj0e/3+l03Hg89vLhcPCMz+ezezweRcDNZuNSfmVvMpl4wNls5mVAMX+327nValUEXK/XpSZzcT6fF8zl7kfAFNN6ve4ajcb7Duu6WwoYgrK/3W69+cj2HGulJltmClqv1/OgTNaUm4vF4nsfag3fAUoQABAg/9vtdrnJrGmyRnpwBuDb7ZY7600OTRKgtHJR/mJKDsHk30wR4kliWkB8pIuh6aE7cmkTLlqGKkWU4ngqA9/hRyoHhUzIsJbxgzO73a6v0RhDwACh3KiOZrPpBoOBGw6HPh+pa5RTMRl1qBRgIQYIM2TG6XTyfp1Op+8WVmgOKZMFiDmqcZ1JNZHM/gmjbBlaQJoD05aewJN5GPoQHzHwJWdoEMywPEsBYUcyq7soiDzVEznzNUPlpkwEAPNJHxjCHPnfJhNlNVT2YP8zYBgUO1DwMyBmiyGXkdlD/hegeh0s9T5hj7OU30dAmwq2lvEVoK1Wy5co574CtL6ygBpcJiiKdNTk1DvEAlqlNBJysTQo4TsEFvYbRhagiCczanKsgYohfiMAyFRGpVJx1WrVs6zVar6N2e4dBUyxJtr0Q4IBc5TZsou2r9hLPvzyUh2H/i01Oex1WsdnqW+gpMmxl5IuwxBzY3t/jzRkZT8rPSQAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/465dc71ba18611f41d4424c41f6d771c/a0b58/2_2_2.webp 230w","/static/465dc71ba18611f41d4424c41f6d771c/bc10c/2_2_2.webp 460w","/static/465dc71ba18611f41d4424c41f6d771c/71ce1/2_2_2.webp 498w"],"sizes":"(max-width: 498px) 100vw, 498px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/465dc71ba18611f41d4424c41f6d771c/81c8e/2_2_2.png 230w","/static/465dc71ba18611f41d4424c41f6d771c/08a84/2_2_2.png 460w","/static/465dc71ba18611f41d4424c41f6d771c/79e1b/2_2_2.png 498w"],"sizes":"(max-width: 498px) 100vw, 498px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/465dc71ba18611f41d4424c41f6d771c/79e1b/2_2_2.png","alt":"2 2 2","title":"2 2 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kollar på ett program som bestämmer vem som vunnit en match:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"mal_hem "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hemmalagets mål: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmal_borta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bortalagets mål: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mal_hem "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" mal_borta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hemmalaget vann!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" mal_borta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" mal_hem"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bortalaget vann!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Oavgjort!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet kan ge tre olika resultat baserat på de värden som ges:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hemmalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nBortalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nHemmalaget vann!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hemmalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nBortalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nBortalaget vann!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hemmalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nBortalagets mål: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nOavgjort!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan finns tre grenar varav exakt en körs. En if-sats kan dock bestå av fler än en elif-gren. Dessutom är en else-gren inte obligatorisk."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här är också en helt korrekt if-sats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Julkalender\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndatum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilken dag är det? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"24/12\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det är julafton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"25/12\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det är juldag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"26/12\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det är annandag jul\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tack och hej.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Julkalender\nVilken dag är det? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25/12"}]},{"type":"text","value":"\nDet är juldag\nTack och hej."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att det föregående exemplet saknar else-gren. Om användaren ger ett datum som inte uppfyller villkoret på någon av if- eller elif-grenarna, kommer ingen av grenarna att köras."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Julkalender\nVilken dag är det? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1/1"}]},{"type":"text","value":"\nTack och hej."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Större eller lika med","tmcname":"osa02-05_storre_lika_med","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge två heltal. Programmet ska skriva ut det större av de givna talen. Om talen är de samma, observerar programmet även det här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge tal ett: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe tal två: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nDet större talet: 5"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge tal ett: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe tal två: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nDet större talet: 8"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge tal ett: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe tal två: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTalen är lika stora!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Äldre","tmcname":"osa02-06_aldre","height":"550px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter namnen och åldrarna hos två personer. Programmet ska skirva ut namnet på den äldre personen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person 1:\nNamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Thomas"}]},{"type":"text","value":"\nÅlder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"26"}]},{"type":"text","value":"\nPerson 2:\nNamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tilde"}]},{"type":"text","value":"\nÅlder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\nTilde är äldre"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person 1:\nNamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Amadeus"}]},{"type":"text","value":"\nÅlder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nPerson 2:\nNamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Vilma"}]},{"type":"text","value":"\nÅlder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nAmadeus och Vilma är lika gamla"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Sist i alfabetet","tmcname":"osa02-07_sist_i_alfabetet","height":"500px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förutom med siffror, kan man också jämföra storleken på strängar i Python. Strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" är mindre än strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" om strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" kommer före i alfabetet än strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". Observera att det här gäller då"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"man jämför bokstäver med samma storlek (små/STORA)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"strängarna innehåller enbart tecknen a-z eller A-Z."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge två ord. Programmet skriver ut det av orden som kommer senare i alfabetet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att orden endast innehåller små bokstäver."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ord 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"antik"}]},{"type":"text","value":"\nGe ord 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kusudama"}]},{"type":"text","value":"\nkusudama kommer senare i alfabetet"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ord 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"vhs"}]},{"type":"text","value":"\nGe ord 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dvd"}]},{"type":"text","value":"\nvhs kommer senare i alfabetet"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ord 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nGe ord 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nDu gav samma ord två gånger."}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"908fdd39-c6bc-5365-9731-442a7877a47d"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa grenar inom if-satser
  • \n
  • förstår du skillnaden mellan if, elif och else
  • \n
  • kan du använda restoperatorn % i Boolean-uttryck.
  • \n

Vi tar nu en titt på ett program som ber användaren att ge en siffra och därefter skriver ut ett meddelande vars innehåll beror på om siffran är negativ, positiv eller lika med noll:

siffra = int(input(\"Ge ett tal: \"))\n\nif siffra < 0:\n    print(\"Talet är negativt\")\n\nif siffra >= 0:\n    print(\"Talet är positivt eller noll\")

Det här verkar något klumpigt och det finns en del upprepning. Vi vill ju bara köra ett av if-blocken eftersom numret alltid är antingen under noll, eller noll eller över. Det vill säga bara ett av villkoren siffra < 0 och siffra >= 0 är samtidigt sant. Därför är den fösta if-satsen den enda som behövs – om villkoret är sant är siffran negativ, annars är siffran noll eller över.

I stället för att skapa två if-satser kan vi skapa en gren som körs då alla villkor är osanna. Det här kallas else-sats.

Så här kan vi skriva om det föregående exemplet:

siffra = int(input(\"Ge ett tal: \"))\n\nif siffra < 0:\n    print(\"Talet är negativt\")\nelse:\n    print(\"Talet är positivt eller noll\")

När vi bygger upp en if-else-sats kommer exakt en av grenarna att köras. Se följande bild:

\n
\n \n \n \n \n \"2\n \n \n

Obs! Det kan aldrig finnas en else-gren före en if-gren. En if-gren och en else-gren bildar en if-else-sats.

Följande exempel kollar om den siffra användaren anger är jämnt eller inte. Det här kan restoperatorn % användas för. Restoperatorn anger resten när två heltal divideras. När ett tal divideras med två är det jämnt då resten är noll. Annars är talet inte jämnt.

siffra = int(input(\"Ge ett tal: \"))\n\nif siffra % 2 == 0:\n    print(\"Talet är jämnt\")\nelse:\n    print(\"Talet är ojämnt\")

Ge ett tal: 5\nTalet är ojämnt

Ett annat exempel där strängar jämförs:

korrekt = \"katt\"\nlosenord = input(\"Ange lösenord: \")\n\nif losenord == korrekt:\n    print(\"Välkommen\")\nelse:\n    print(\"Tillträde förbjudet\")

Så här kan det se ut när koden körs:

Ange lösenord: katt\nVälkommen

Ange lösenord: koltrast\nTillträde förbjudet

Skapa ett program som ber användaren ge sin ålder. Programmet berättar om användaren är myndig (> 18 år) eller inte.

Exempel:

Hur gammal är du? 12\nDu är inte myndig!

Hur gammal är du? 32\nDu är myndig!

Flera grenar med elif-satser

Ofta finns det fler än två alternativ som ett program måste ta i beaktande. Till exempel resultatet av en fotbollsmatch kan se ut på tre sätt: hemmalaget vinner, bortalaget vinner eller oavgjort.

En if-sats kan bestå av elif-grenar – \"else if\". Till den här grenen kommer man om villkoret i någon av de tidigare grenarna inte uppfylls.

\n \n

Efter den här delen

    \n
  • vet du hur man använder operatorerna and, or och not i villkor
  • \n
  • kan du skriva kapslade if-satser.
  • \n

Logiska operatorer

Du kan kombinera villkor med de logiska operatorerna and och or. Operatorn and innebär att alla villkor måste vara sanna samtidigt. Operatorn or kräver att minst ett av villkoren ska vara sant.

Till exempel villkoret nummer >= 5 and nummer <= 8 bestämmer att nummer måste samtidigt vara minst fem och högst åtta – alltså mellan fem och åtta.

nummer = int(input(\"Ge en siffra: \"))\nif nummer >= 5 and nummer <= 8:\n    print(\"Siffran är mellan 5 och 8\")

Villkoret nummer < 5 or nummer > 8 bestämmer att nummer måste vara mindre än fem eller större än åtta – alltså inte mellan fem till åtta.

nummer = int(input(\"Ge en siffra: \"))\nif nummer < 5 or nummer > 8:\n    print(\"Siffran är inte mellan 5 och 8\")

Den här sanningstabellen beskriver hur operatorerna fungerar i olika situationer:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
aba and ba or b
FalseFalseFalseFalse
TrueFalseFalseTrue
FalseTrueFalseTrue
TrueTrueTrueTrue

Ibland kan det vara händigt att veta om något värde inte är sant. Operatorn not byter värdet på ett villkor till det motsatta:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
anot a
TrueFalse
FalseTrue

Det ovanstående exemplet med talen 5–8 borträknade kan också skrivas så här:

nummer = int(input(\"Ge en siffra: \"))\nif not (nummer >= 5 and nummer <= 8):\n    print(\"Siffran är inte mellan 5 och 8\")

Framför allt inom programmering kallas logiska operatorer Boolean-operatorer.

Villkoret x >= a and x <= b är ett mycket vanligt sätt att bestämma om talet x är i intervallet a till b. Den här strukturen fungerar i flera programmeringsspråk.

Python ger också oss möjligheten att använda oss av ett förenklat uttryckssätt för att kombinera villkor: a <= x <= b ger samma resultat som den längre versionen med and. Den här notationen är kanske bekant från matematikens värld men den används inte så ofta i Python – kanske för att många andra programmeringsspråk saknar liknande syntax.

Kombinera och kedja villkor

Det här programmet ber användaren att ge fyra siffror. Sedan kollar programmet vilken siffra som är störst med hjälp av några villkor:

n1 = int(input(\"Ge siffra 1: \"))\nn2 = int(input(\"Ge siffra 2: \"))\nn3 = int(input(\"Ge siffra 3: \"))\nn4 = int(input(\"Ge siffra 4: \"))\n\nif n1 > n2 and n1 > n3 and n1 > n4:\n    storst = n1\nelif n2 > n3 and n2 > n4:\n    storst = n2\nelif n3 > n4:\n    storst = n3\nelse:\n    storst = n4\n\nprint(f\" {storst} är det största talet.\")

Ge siffra 1: 2\nGe siffra 2: 4\nGe siffra 3: 1\nGe siffra 4: 1\n4 är det största talet.

I exemplet ovan är n1 > n2 and n1 > n3 and n1 > n4 sant endast då alla tre \"delvillkor\" är sanna.

Skapa ett program som frågar om användarens ålder. Om åldern är under fem, ska programmet kommentera det här.

Se exemplen nedan:

Vad är din ålder? 13\nOkej, du är alltså 13 år

Vad är din ålder? 2\nJag tror inte att du kan skriva...

Vad är din ålder? -4\nDu måste ha skrivit fel.

Skapa ett program som frågar efter användarens namn. Om namnet är Knatte, Fnatte eller Tjatte, antar programmet att användaren är Kalle Ankas brorson.

Om namnet är Teddi eller Freddi, antar programmet att användaren är Musse Piggs brorson.

Exempel:

Ange ditt namn: Teddi\nDu är antagligen Musse Piggs brorson.

Ange ditt namn: Fnatte\nDu är antagligen Kalle Ankas brorson.

Ange ditt namn: Kid\nJag vet inte vems brorson du är.

Följande tabell beskriver hur vitsordet på en kurs räknas. Skapa ett program som meddelar vitsordet på basis av den här tabellen.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
PoängVitsord
< 0omöjligt!
0-49underkänt
50-591
60-692
70-793
80-894
90-1005
> 100omöjligt!

Exempel på programmets funktion:

Ge poäng [0-100]: 37\nVitsord: underkänt

Ge poäng [0-100]: 76\nVitsord: 3

Ge poäng [0-100]: -3\nVitsord: omöjligt!

Det här programmet ska be användaren att ge en siffra. Om siffran är delbar med tre, skriver man ut Fizz. Om talet är delbart med fem, skriver man ut Buzz. Om talet är delbart med de båda talen, skriver man ut FizzBuzz.

Exempel:

Tal: 9\nFizz

Tal: 7

Tal: 20\nBuzz

Tal: 45\nFizzBuzz

Kapslade if-satser

If-satser kan kapslas inom andra if-satser. Till exempel följande program kollar först om en siffra är noll före det kollar om talet är jämnt eller inte.

nummer = int(input(\"Ge en siffra: \"))\n\nif nummer > 0:\n    if nummer % 2 == 0:\n        print(\"Talet är jämnt\")\n    else:\n        print(\"Talet är ojämnt\")\nelse:\n    print(\"Talet är negativt\")

Så här kan programmet fungera:

Ge en siffra: 3\nTalet är ojämnt

Ge en siffra: 18\nTalet är jämnt

Ge en siffra: -4\nTalet är negativt

När man kapslar if-satser är det kritiskt att indenteringen blir rätt. Indenteringen bestämmer vilka grenar som är länkade ihop. Till exempel en if-gren och en else-gren med samma inledande mellanrum tolkas som grenar av en och samma if-sats.

Ofta kan likadana resultat åstadkommas både med logiska operatorer och kapslade if-satser. Det följande exemplet fungerar helt på samma sätt som det tidigare exemplet:

nummer = int(input(\"Ge en siffra: \"))\n\nif nummer > 0 and nummer % 2 == 0:\n    print(\"Talet är jämnt\")\nelif nummer > 0 and nummer % 2 != 0:\n    print(\"Talet är ojämnt\")\nelse:\n    print(\"Talet är negativt\")

Man kan inte på rak arm säga vilkendera lösning är bättre. Situationen bestämmer ofta hur det lönar sig att bygga upp if-satsen på ett logiskt sätt. I det här exemplet tycker flera personer att versionen med kapsling är mera intuitiv.

Ett år är ett skottår om det är delbart med fyra. Om ett år är delbart med 100 är det ett skottår bara då det är delbart med 400.

Skapa ett program som frågar användaren om ett årtal. Programmet meddelar om året är ett skottår eller inte.

Ange år: 2011\nÅret är inte ett skottår.

Ange år: 2020\nÅret är ett skottår.

Ange år: 1800\nÅret är inte ett skottår.

Skapa ett program som frågar användaren efter tre bokstäver. Programmet skriver ut bokstaven som är mellerst i alfabetisk ordning.

Du kan anta att alla bokstäver är antingen gemener eller versaler (små/STORA).

Exempel:

Ge bokstav 1: x\nGe bokstav 2: c\nGe bokstav 3: p\nDen mellersta bokstaven är p

Ge bokstav 1: C\nGe bokstav 2: B\nGe bokstav 3: A\nDen mellersta bokstaven är B

Enligt skattemyndigheten är en gåva sådan egendom som överlåts till en annan person utan ersättning. Om en person får gåvor från samma person till ett värde på 5 000 euro eller mera, ska gåvoskatt betalas.

När gåvan kommer från en nära släkting, räknas gåvoskatten enligt följande tabell:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Gåvans värdeSkatt vid nedre gränsenSkatteprocent för överstigande andel
5 000 - 25 0001008
25 000 - 55 0001 70010
55 000 - 200 0004 70012
200 000 - 1 000 00022 10015
1 000 000 -142 10017

Till exempel för en gåva på 6 000 euro ska man betala 180 euro skatt (100 + (6000 - 5000) * 0,08). För en gåva på 75 000 euro betalar man 7 100 euro i beskattningen (4700 + (75000 - 55000) * 0,12).

Skapa ett program som räknar gåvoskatten för en gåva från en nära släkting. Nedan följer några exempel.

Gåvans värde? 3500\nIngen gåvoskatt!

Gåvans värde? 5000\nGåvoskatt: 100.0 euro

Gåvans värde? 27500\nGåvoskatt: 1950.0 euro

","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-2/3-kombinera-villkor.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man använder operatorerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" i villkor"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skriva kapslade if-satser."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"logiska-operatorer","style":"position:relative;"},"children":[{"type":"text","value":"Logiska operatorer"},{"type":"element","tagName":"a","properties":{"href":"#logiska-operatorer","ariaLabel":"logiska operatorer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan kombinera villkor med de logiska operatorerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":". Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" innebär att alla villkor måste vara sanna samtidigt. Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" kräver att minst ett av villkoren ska vara sant."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer >= 5 and nummer <= 8"}]},{"type":"text","value":" bestämmer att nummer måste samtidigt vara minst fem och högst åtta – alltså mellan fem och åtta."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffran är mellan 5 och 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer < 5 or nummer > 8"}]},{"type":"text","value":" bestämmer att nummer måste vara mindre än fem eller större än åtta – alltså inte mellan fem till åtta."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffran är inte mellan 5 och 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här sanningstabellen beskriver hur operatorerna fungerar i olika situationer:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"a"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"b"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"a and b"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"a or b"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland kan det vara händigt att veta om något värde inte är sant. Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" byter värdet på ett villkor till det motsatta:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"a"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"not a"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det ovanstående exemplet med talen 5–8 borträknade kan också skrivas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffran är inte mellan 5 och 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Framför allt inom programmering kallas logiska operatorer Boolean-operatorer."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Kombinerade villkor – förenklat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x >= a and x <= b"}]},{"type":"text","value":" är ett mycket vanligt sätt att bestämma om talet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" är i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". Den här strukturen fungerar i flera programmeringsspråk."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python ger också oss möjligheten att använda oss av ett förenklat uttryckssätt för att kombinera villkor: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a <= x <= b"}]},{"type":"text","value":" ger samma resultat som den längre versionen med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":". Den här notationen är kanske bekant från matematikens värld men den används inte så ofta i Python – kanske för att många andra programmeringsspråk saknar liknande syntax."}]}]},{"type":"element","tagName":"h2","properties":{"id":"kombinera-och-kedja-villkor","style":"position:relative;"},"children":[{"type":"text","value":"Kombinera och kedja villkor"},{"type":"element","tagName":"a","properties":{"href":"#kombinera-och-kedja-villkor","ariaLabel":"kombinera och kedja villkor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet ber användaren att ge fyra siffror. Sedan kollar programmet vilken siffra som är störst med hjälp av några villkor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge siffra 1: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nn2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge siffra 2: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nn3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge siffra 3: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nn4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge siffra 4: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n storst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n1\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n storst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n2\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n storst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n3\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n storst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n4\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är det största talet.\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge siffra 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nGe siffra 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nGe siffra 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nGe siffra 4: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n4 är det största talet."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n1 > n2 and n1 > n3 and n1 > n4"}]},{"type":"text","value":" sant endast då alla tre \"delvillkor\" är sanna."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Kontroll av ålder","tmcname":"osa02-08_alderskoll"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar om användarens ålder. Om åldern är under fem, ska programmet kommentera det här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Se exemplen nedan:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad är din ålder? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"13"}]},{"type":"text","value":"\nOkej, du är alltså 13 år"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad är din ålder? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nJag tror inte att du kan skriva..."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad är din ålder? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-4"}]},{"type":"text","value":"\nDu måste ha skrivit fel."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Brorsöner","tmcname":"osa02-09_brorsoner"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter användarens namn. Om namnet är Knatte, Fnatte eller Tjatte, antar programmet att användaren är Kalle Ankas brorson."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om namnet är Teddi eller Freddi, antar programmet att användaren är Musse Piggs brorson."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Teddi"}]},{"type":"text","value":"\nDu är antagligen Musse Piggs brorson."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Fnatte"}]},{"type":"text","value":"\nDu är antagligen Kalle Ankas brorson."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Kid"}]},{"type":"text","value":"\nJag vet inte vems brorson du är."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Vitsord och poäng","tmcname":"osa02-10_vitsord_poang"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande tabell beskriver hur vitsordet på en kurs räknas. Skapa ett program som meddelar vitsordet på basis av den här tabellen."}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Poäng"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Vitsord"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"< 0"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"omöjligt!"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0-49"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"underkänt"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"50-59"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"60-69"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"70-79"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"80-89"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"90-100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"> 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"omöjligt!"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på programmets funktion:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge poäng [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"37"}]},{"type":"text","value":"\nVitsord: underkänt"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge poäng [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"76"}]},{"type":"text","value":"\nVitsord: 3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge poäng [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nVitsord: omöjligt!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"FizzBuzz","tmcname":"osa02-11_fizzbuzz"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet ska be användaren att ge en siffra. Om siffran är delbar med tre, skriver man ut Fizz. Om talet är delbart med fem, skriver man ut Buzz. Om talet är delbart med de båda talen, skriver man ut FizzBuzz."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nFizz"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\nBuzz"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nFizzBuzz"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"kapslade-if-satser","style":"position:relative;"},"children":[{"type":"text","value":"Kapslade if-satser"},{"type":"element","tagName":"a","properties":{"href":"#kapslade-if-satser","ariaLabel":"kapslade if satser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If-satser kan kapslas inom andra if-satser. Till exempel följande program kollar först om en siffra är noll före det kollar om talet är jämnt eller inte."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är jämnt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är ojämnt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är negativt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nTalet är ojämnt"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nTalet är jämnt"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-4"}]},{"type":"text","value":"\nTalet är negativt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man kapslar if-satser är det kritiskt att indenteringen blir rätt. Indenteringen bestämmer vilka grenar som är länkade ihop. Till exempel en if-gren och en else-gren med samma inledande mellanrum tolkas som grenar av en och samma if-sats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ofta kan likadana resultat åstadkommas både med logiska operatorer och kapslade if-satser. Det följande exemplet fungerar helt på samma sätt som det tidigare exemplet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är jämnt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är ojämnt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talet är negativt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan inte på rak arm säga vilkendera lösning är bättre. Situationen bestämmer ofta hur det lönar sig att bygga upp if-satsen på ett logiskt sätt. I det här exemplet tycker flera personer att versionen med kapsling är mera intuitiv."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Skottår","tmcname":"osa02-12_skottar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett år är ett skottår om det är delbart med fyra. Om ett år är delbart med 100 är det ett skottår bara då det är delbart med 400."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar användaren om ett årtal. Programmet meddelar om året är ett skottår eller inte."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange år: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2011"}]},{"type":"text","value":"\nÅret är inte ett skottår."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange år: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]},{"type":"text","value":"\nÅret är ett skottår."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange år: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1800"}]},{"type":"text","value":"\nÅret är inte ett skottår."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"I mitten av alfabetet","tmcname":"osa02-13_mellerst_i_alfabetet"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar användaren efter tre bokstäver. Programmet skriver ut bokstaven som är mellerst i alfabetisk ordning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att alla bokstäver är antingen gemener eller versaler (små/STORA)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge bokstav 1: x\nGe bokstav 2: c\nGe bokstav 3: p\nDen mellersta bokstaven är p"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge bokstav 1: C\nGe bokstav 2: B\nGe bokstav 3: A\nDen mellersta bokstaven är B"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Gåvoskatt","tmcname":"osa02-14_gavoskatt","height":"500px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://www.vero.fi/sv/privatpersoner/egendom/gava/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Enligt skattemyndigheten"}]},{"type":"text","value":" är en gåva sådan egendom som överlåts till en annan person utan ersättning. Om en person får gåvor från samma person till ett värde på 5 000 euro eller mera, ska gåvoskatt betalas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När gåvan kommer från en nära släkting, räknas gåvoskatten "},{"type":"element","tagName":"a","properties":{"href":"https://www.vero.fi/sv/privatpersoner/egendom/gava/g%C3%A5voskatter%C3%A4knaren/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"enligt följande tabell"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Gåvans värde"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Skatt vid nedre gränsen"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Skatteprocent för överstigande andel"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5 000 - 25 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"25 000 - 55 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1 700"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"10"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"55 000 - 200 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4 700"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"12"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"200 000 - 1 000 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"22 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"15"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1 000 000 -"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"142 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"17"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel för en gåva på 6 000 euro ska man betala 180 euro skatt (100 + (6000 - 5000) * 0,08). För en gåva på 75 000 euro betalar man 7 100 euro i beskattningen (4700 + (75000 - 55000) * 0,12)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som räknar gåvoskatten för en gåva från en nära släkting. Nedan följer några exempel."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gåvans värde? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3500"}]},{"type":"text","value":"\nIngen gåvoskatt!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gåvans värde? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5000"}]},{"type":"text","value":"\nGåvoskatt: 100.0 euro"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gåvans värde? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27500"}]},{"type":"text","value":"\nGåvoskatt: 1950.0 euro"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du hur man använder operatorerna and, or och not i villkor
  • \n
  • kan du skriva kapslade if-satser.
  • \n

Logiska operatorer

Du kan kombinera villkor med de logiska operatorerna and och or. Operatorn and innebär att alla villkor måste vara sanna samtidigt. Operatorn or kräver att minst ett av villkoren ska vara sant.

Till exempel villkoret nummer >= 5 and nummer <= 8 bestämmer att nummer måste samtidigt vara minst fem och högst åtta – alltså mellan fem och åtta.

nummer = int(input(\"Ge en siffra: \"))\nif nummer >= 5 and nummer <= 8:\n    print(\"Siffran är mellan 5 och 8\")

Villkoret nummer < 5 or nummer > 8 bestämmer att nummer måste vara mindre än fem eller större än åtta – alltså inte mellan fem till åtta.

nummer = int(input(\"Ge en siffra: \"))\nif nummer < 5 or nummer > 8:\n    print(\"Siffran är inte mellan 5 och 8\")

Den här sanningstabellen beskriver hur operatorerna fungerar i olika situationer:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
aba and ba or b
FalseFalseFalseFalse
TrueFalseFalseTrue
FalseTrueFalseTrue
TrueTrueTrueTrue

Ibland kan det vara händigt att veta om något värde inte är sant. Operatorn not byter värdet på ett villkor till det motsatta:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
anot a
TrueFalse
FalseTrue

Det ovanstående exemplet med talen 5–8 borträknade kan också skrivas så här:

nummer = int(input(\"Ge en siffra: \"))\nif not (nummer >= 5 and nummer <= 8):\n    print(\"Siffran är inte mellan 5 och 8\")

Framför allt inom programmering kallas logiska operatorer Boolean-operatorer.

Villkoret x >= a and x <= b är ett mycket vanligt sätt att bestämma om talet x är i intervallet a till b. Den här strukturen fungerar i flera programmeringsspråk.

Python ger också oss möjligheten att använda oss av ett förenklat uttryckssätt för att kombinera villkor: a <= x <= b ger samma resultat som den längre versionen med and. Den här notationen är kanske bekant från matematikens värld men den används inte så ofta i Python – kanske för att många andra programmeringsspråk saknar liknande syntax.

Kombinera och kedja villkor

Det här programmet ber användaren att ge fyra siffror. Sedan kollar programmet vilken siffra som är störst med hjälp av några villkor:

n1 = int(input(\"Ge siffra 1: \"))\nn2 = int(input(\"Ge siffra 2: \"))\nn3 = int(input(\"Ge siffra 3: \"))\nn4 = int(input(\"Ge siffra 4: \"))\n\nif n1 > n2 and n1 > n3 and n1 > n4:\n    storst = n1\nelif n2 > n3 and n2 > n4:\n    storst = n2\nelif n3 > n4:\n    storst = n3\nelse:\n    storst = n4\n\nprint(f\" {storst} är det största talet.\")

Ge siffra 1: 2\nGe siffra 2: 4\nGe siffra 3: 1\nGe siffra 4: 1\n4 är det största talet.

I exemplet ovan är n1 > n2 and n1 > n3 and n1 > n4 sant endast då alla tre \"delvillkor\" är sanna.

Skapa ett program som frågar om användarens ålder. Om åldern är under fem, ska programmet kommentera det här.

Se exemplen nedan:

Vad är din ålder? 13\nOkej, du är alltså 13 år

Vad är din ålder? 2\nJag tror inte att du kan skriva...

Vad är din ålder? -4\nDu måste ha skrivit fel.

Skapa ett program som frågar efter användarens namn. Om namnet är Knatte, Fnatte eller Tjatte, antar programmet att användaren är Kalle Ankas brorson.

Om namnet är Teddi eller Freddi, antar programmet att användaren är Musse Piggs brorson.

Exempel:

Ange ditt namn: Teddi\nDu är antagligen Musse Piggs brorson.

Ange ditt namn: Fnatte\nDu är antagligen Kalle Ankas brorson.

Ange ditt namn: Kid\nJag vet inte vems brorson du är.

Följande tabell beskriver hur vitsordet på en kurs räknas. Skapa ett program som meddelar vitsordet på basis av den här tabellen.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
PoängVitsord
< 0omöjligt!
0-49underkänt
50-591
60-692
70-793
80-894
90-1005
> 100omöjligt!

Exempel på programmets funktion:

Ge poäng [0-100]: 37\nVitsord: underkänt

Ge poäng [0-100]: 76\nVitsord: 3

Ge poäng [0-100]: -3\nVitsord: omöjligt!

Det här programmet ska be användaren att ge en siffra. Om siffran är delbar med tre, skriver man ut Fizz. Om talet är delbart med fem, skriver man ut Buzz. Om talet är delbart med de båda talen, skriver man ut FizzBuzz.

Exempel:

Tal: 9\nFizz

Tal: 7

Tal: 20\nBuzz

Tal: 45\nFizzBuzz

Kapslade if-satser

If-satser kan kapslas inom andra if-satser. Till exempel följande program kollar först om en siffra är noll före det kollar om talet är jämnt eller inte.

nummer = int(input(\"Ge en siffra: \"))\n\nif nummer > 0:\n    if nummer % 2 == 0:\n        print(\"Talet är jämnt\")\n    else:\n        print(\"Talet är ojämnt\")\nelse:\n    print(\"Talet är negativt\")

Så här kan programmet fungera:

Ge en siffra: 3\nTalet är ojämnt

Ge en siffra: 18\nTalet är jämnt

Ge en siffra: -4\nTalet är negativt

När man kapslar if-satser är det kritiskt att indenteringen blir rätt. Indenteringen bestämmer vilka grenar som är länkade ihop. Till exempel en if-gren och en else-gren med samma inledande mellanrum tolkas som grenar av en och samma if-sats.

Ofta kan likadana resultat åstadkommas både med logiska operatorer och kapslade if-satser. Det följande exemplet fungerar helt på samma sätt som det tidigare exemplet:

nummer = int(input(\"Ge en siffra: \"))\n\nif nummer > 0 and nummer % 2 == 0:\n    print(\"Talet är jämnt\")\nelif nummer > 0 and nummer % 2 != 0:\n    print(\"Talet är ojämnt\")\nelse:\n    print(\"Talet är negativt\")

Man kan inte på rak arm säga vilkendera lösning är bättre. Situationen bestämmer ofta hur det lönar sig att bygga upp if-satsen på ett logiskt sätt. I det här exemplet tycker flera personer att versionen med kapsling är mera intuitiv.

Ett år är ett skottår om det är delbart med fyra. Om ett år är delbart med 100 är det ett skottår bara då det är delbart med 400.

Skapa ett program som frågar användaren om ett årtal. Programmet meddelar om året är ett skottår eller inte.

Ange år: 2011\nÅret är inte ett skottår.

Ange år: 2020\nÅret är ett skottår.

Ange år: 1800\nÅret är inte ett skottår.

Skapa ett program som frågar användaren efter tre bokstäver. Programmet skriver ut bokstaven som är mellerst i alfabetisk ordning.

Du kan anta att alla bokstäver är antingen gemener eller versaler (små/STORA).

Exempel:

Ge bokstav 1: x\nGe bokstav 2: c\nGe bokstav 3: p\nDen mellersta bokstaven är p

Ge bokstav 1: C\nGe bokstav 2: B\nGe bokstav 3: A\nDen mellersta bokstaven är B

Enligt skattemyndigheten är en gåva sådan egendom som överlåts till en annan person utan ersättning. Om en person får gåvor från samma person till ett värde på 5 000 euro eller mera, ska gåvoskatt betalas.

När gåvan kommer från en nära släkting, räknas gåvoskatten enligt följande tabell:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Gåvans värdeSkatt vid nedre gränsenSkatteprocent för överstigande andel
5 000 - 25 0001008
25 000 - 55 0001 70010
55 000 - 200 0004 70012
200 000 - 1 000 00022 10015
1 000 000 -142 10017

Till exempel för en gåva på 6 000 euro ska man betala 180 euro skatt (100 + (6000 - 5000) * 0,08). För en gåva på 75 000 euro betalar man 7 100 euro i beskattningen (4700 + (75000 - 55000) * 0,12).

Skapa ett program som räknar gåvoskatten för en gåva från en nära släkting. Nedan följer några exempel.

Gåvans värde? 3500\nIngen gåvoskatt!

Gåvans värde? 5000\nGåvoskatt: 100.0 euro

Gåvans värde? 27500\nGåvoskatt: 1950.0 euro

","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-2/3-kombinera-villkor.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-2/4-loopar/page-data.json b/page-data/osa-2/4-loopar/page-data.json index 8957895b5..f38514d8c 100644 --- a/page-data/osa-2/4-loopar/page-data.json +++ b/page-data/osa-2/4-loopar/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-2/4-loopar", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad en loop betyder i programmeringssammanhang"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" -loop i dina program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kommer du att kunna använda dig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen för att avbryta loopen."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har nu undersökt if-satser. Ett annat viktigt koncept inom programmering är repetition. De här två koncepten är grundläggande strukturer som varje programmerare väntas kunna. De är kontrollstrukturer – de ger dig möjligheten att påverka vilka kodrader körs och när de körs. Medan if-satser låter dig välja mellan olika delar av kod, finns loopar till för att köra en del av koden på nytt. Programmet går alltså tillbaka till en viss rad i koden ett antal gånger. En iteration är en \"runda\" av en loop – det vill säga den process då loopens kod körs från början till slutet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här delen presenterar vi en enkel while-loop. Dess struktur påminner om if-satsen. I nästa del dyker vi djupare in i det som loopar kan erbjuda oss."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi tar en titt på ett program som ber användaren att ge en siffra som skrivs upp upphöjt med två. Programmet körs tills användaren ger siffran -1:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tack och hej!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n4\nGe en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n16\nGe en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n100\nGe en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nTack och hej!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du ser ovan, frågar programmet om ett nummer flera gånger. Detta tack vare while-satsen. När användaren anger siffran -1 kommer "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen att köras. Loopen avbryts och programmet fortsätter efter while-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man arbetar med loopar är det viktigt att loopen avslutas vid något skede. Om man inte tar det här i beaktande kan loopen fortsätta för evigt. Vi ändrar lite på ovanstående exempel för att åstadkomma en sådan här situation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tack och hej!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här versionen frågar programmet användaren efter siffran utanför loopen. Om användaren ger någon annan siffra än -1 kommer loopen aldrig att avslutas. Vi har en oändlig loop vilket i princip betyder att koden körs oavbrutet, för evigt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n4\n4\n4\n4\n4\n4\n4\n4\n(fortsätter oändligt...)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program har en mycket liknande struktur jämfört med exemplet ovan, men för användaren ser det ganska annorlunda ut. Det här programmet låter användaren fortsätta endast då den korrekta pin-koden 1234 anges:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel! Försök igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Korrekt pin-kod\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nKorrekt pin-kod"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fortsätter vi?","tmcname":"osa02-15_fortsatt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv enligt föregående exempel ett program som skriver ut texten \"hej\" och frågar \"Fortsätter vi?\" tills användaren svarar \"nej\". Efter det här skriver programmet ut \"inte då\" och programmet avslutas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"hej\nFortsätter vi? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ja"}]},{"type":"text","value":"\nhej\nFortsätter vi? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"yes"}]},{"type":"text","value":"\nhej\nFortsätter vi? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jawohl"}]},{"type":"text","value":"\nhej\nFortsätter vi? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej"}]},{"type":"text","value":"\ninte då"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Valida värden","tmcname":"osa02-16_validering"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter siffror från användaren. Om siffran är negativ, skriver man ut meddelandet \"Ogiltigt tal\" och ett nytt tal tillfrågas. Om siffran är noll, avslutas loopen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om siffran är positiv, skriver programmet ut siffrans kvadratrot med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":"-funktionen som är tillgänglig tack vare "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen. Exempel på funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här ska vara i början av programmet för att sqrt ska fungera"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på programmet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"16"}]},{"type":"text","value":"\n4.0\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n2.0\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nOgiltigt tal\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n1.0\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nAvslutar..."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Nedräkning","tmcname":"osa02-17_nedrakning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nedräkning till start!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gå!!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...borde fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedräkning till start!\n5\n4\n3\n2\n1\nGå!!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigera problemet i koden."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Lösenord på nytt","tmcname":"osa02-18_losenord"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren att ange ett lösenord och upprepa lösenordet, tills användaren anger det första lösenordet på nytt."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lösenord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hemligt?"}]},{"type":"text","value":"\nUpprepa lösenordet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hemlighet"}]},{"type":"text","value":"\nStämmer inte!\nUpprepa lösenordet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"intminnsjanumer321"}]},{"type":"text","value":"\nStämmer inte!\nUpprepa lösenordet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hemligt?"}]},{"type":"text","value":"\nKontot skapat!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"loopar-och-hjalpvariabler","style":"position:relative;"},"children":[{"type":"text","value":"Loopar och hjälpvariabler"},{"type":"element","tagName":"a","properties":{"href":"#loopar-och-hjalpvariabler","ariaLabel":"loopar och hjalpvariabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi gör föregående exemplet en aning mer realistiskt. Det här exemplet tillåter användaren endast tre försök att ge korrekt pin-kod."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet består av två hjälpvariabler: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forsok"}]},{"type":"text","value":" håller reda på hur många gånger användaren angett en pin-kod och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lyckades"}]},{"type":"text","value":" är antingen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" beroende på om användaren ger den korrekta koden eller inte."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi kommer hit om koden är fel OCH tre försök inte ännu gjorts"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel! Försök igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" lyckades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Korrekt pin-kod\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"För många försök...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nKorrekt pin-kod"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nFör många försök..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Loopen avslutas antingen om pin-koden är korrekt eller då maxantalet försök har uppnåtts. Efterföljande if-sats kollar variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lyckades"}]},{"type":"text","value":" värde och skriver ut ett meddelande baserat på värdet."}]},{"type":"element","tagName":"h2","properties":{"id":"print-satser-for-debuggning-i-loopar","style":"position:relative;"},"children":[{"type":"text","value":"Print-satser för debuggning i loopar"},{"type":"element","tagName":"a","properties":{"href":"#print-satser-for-debuggning-i-loopar","ariaLabel":"print satser for debuggning i loopar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att introducera loopar i ett program ökar på möjligheten för buggar. Därför är det viktigt att senast nu utnyttja print-satser i debuggningssyfte – dem såg vi på i den första delen av den pågående modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kikar på ett nästan identiskt program som i det föregående exemplet. Dock finns det en märkbar skillnad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel! Försök igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" lyckades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Korrekt pin-kod\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"För många försök...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här versionen fungerar konstigt när användaren anger den korrekta koden på det tredje försöket:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nFör många försök..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu borde vi alltså reda ut det här problemet. Några "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser borde hjälpa oss att debugga – så låt oss lägga till sådana i loopen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"while-blocket inleds:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"försök:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forsok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"villkor 1:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kod:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"villkor 2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel! Försök igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"while-blocket inleds:\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2233"}]},{"type":"text","value":"\nförsök: 1\nvillkor 1: False\nkod: 2233\nvillkor 2: False\nFel! Försök igen\nwhile-blocket inleds:\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4545"}]},{"type":"text","value":"\nförsök: 2\nvillkor 1: False\nkod: 4545\nvillkor 2: False\nFel! Försök igen\nwhile-blocket inleds:\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nförsök: 3\nvillkor 1: True\nFör många försök..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Från utskriften ovan märker vi att under den tredje iterationen kommer villkoret i den första if-satsen att vara sant och därmed hinner vi aldrig fram till den andra if-satsen i och med att loopen avslutas. Därmed kontrolleras koden aldrig:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ...."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här blocket kommer för tidigt"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi når inte hit på det tredje försöket"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordningen på if-satser eller grenar inom if-satser är vanliga orsaker till buggar – framför allt inom loopar. Debuggning med hjälp av print-satser hjälper förvånansvärt ofta."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Pin och antal försök","tmcname":"osa02-19_pin"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar användaren om en pin-kod tills hon anger den korrekta pin-koden 4321. Programmet berättar hur många försök som har gjorts:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3245"}]},{"type":"text","value":"\nFel\nPin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nFel\nPin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel\nPin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nKorrekt, du gjorde 4 försök"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften skiljer sig då pin-koden är korrekt på det första försöket:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nKorrekt, du behövde bara ett försök!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Nästa skottår","tmcname":"osa02-20_nasta_skottar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange ett årtal. Programmet ska berätta när nästa skottår är."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"År: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2019"}]},{"type":"text","value":"\nNästa skottår efter 2019 är 2020"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om året användaren anger är ett skottår (t.ex. 2020), ska programmet ändå berätta när det följande skottåret är:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"År: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]},{"type":"text","value":"\nNästa skottår efter 2020 är 2024"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"kombinera-strangar-med--operatorn","style":"position:relative;"},"children":[{"type":"text","value":"Kombinera strängar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-operatorn"},{"type":"element","tagName":"a","properties":{"href":"#kombinera-strangar-med--operatorn","ariaLabel":"kombinera strangar med operatorn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exemplet ovan använde hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forsok"}]},{"type":"text","value":" för att hålla koll på hur många gånger användaren försökt skriva in en kod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln tilldelas värdet noll utanför loopen och varje iteration ökar på siffran med ett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Något liknande kan man också göra med strängar. Programmet kan till exempel hålla koll på de pin-koder användaren angett:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"koder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\nforsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n koder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", \""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hjälpvariabeln kan tilldelas värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":" – det vill säga en tom sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"koder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje iteration blir strängen längre i och med att koden användaren angett läggs till i slutet av strängen tillsammans med ett komma och ett mellanslag."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n koder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", \""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren anger koderna 1111 2222 1234 kommer värdet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"koder"}]},{"type":"text","value":" till slut att vara:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1111, 2222, 1234,"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Berättelse","tmcname":"osa02-21_berattelse"},"children":[{"type":"element","tagName":"h3","properties":{"id":"del-1","style":"position:relative;"},"children":[{"type":"text","value":"Del 1"},{"type":"element","tagName":"a","properties":{"href":"#del-1","ariaLabel":"del 1 permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange ord. Då användaren anger ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"slut"}]},{"type":"text","value":", skriver programmet ut en berättelse som bildas av orden, varefter programmet avslutas."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Det"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"var"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"en"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"gång"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"..."}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"slut"}]},{"type":"text","value":"\nDet var en gång ..."}]}]},{"type":"element","tagName":"h3","properties":{"id":"del-2","style":"position:relative;"},"children":[{"type":"text","value":"Del 2"},{"type":"element","tagName":"a","properties":{"href":"#del-2","ariaLabel":"del 2 permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Redigera på programmet så att programmet slutar fråga efter ord då ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"slut"}]},{"type":"text","value":" anges eller då det föregående ordet upprepas."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"I"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"början"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"fanns"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hönan"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eller"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ägget"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ägget"}]},{"type":"text","value":"\nI början fanns hönan eller ägget"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Behandling av tal","tmcname":"osa02-22_tal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange heltal. Programmet ber efter tal tills talet noll anges."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange heltal, 0 avslutar programmet:\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"22"}]},{"type":"text","value":"\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-2"}]},{"type":"text","value":"\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"del-1-antal","style":"position:relative;"},"children":[{"type":"text","value":"Del 1: Antal"},{"type":"element","tagName":"a","properties":{"href":"#del-1-antal","ariaLabel":"del 1 antal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter att siffrorna har angetts, ska programmet skriva ut antalet siffror. Nollan ska inte tas i beaktande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du behöver en ny variabel som håller koll på antalet angivna siffror."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(användaren anger tal)\nTillsammans 4 tal"}]}]},{"type":"element","tagName":"h3","properties":{"id":"del-2-summa","style":"position:relative;"},"children":[{"type":"text","value":"Del 2: Summa"},{"type":"element","tagName":"a","properties":{"href":"#del-2-summa","ariaLabel":"del 2 summa permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utvidga programmet så att det skriver ut summan av de angivna talen. Nollan ska inte tas i beaktande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ser utskriften ut nu:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34"}]}]},{"type":"element","tagName":"h3","properties":{"id":"del-3-medelvarde","style":"position:relative;"},"children":[{"type":"text","value":"Del 3: Medelvärde"},{"type":"element","tagName":"a","properties":{"href":"#del-3-medelvarde","ariaLabel":"del 3 medelvarde permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utveckla programmet så att det räknar medelvärdet av de angivna talen. Nollan ska inte tas i beaktande. Du kan anta att användaren ger minst ett tal."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34\nMedelvärdet av talen är 8.5"}]}]},{"type":"element","tagName":"h4","properties":{"id":"del-4-positiva-och-negativa","style":"position:relative;"},"children":[{"type":"text","value":"Del 4: Positiva och negativa"},{"type":"element","tagName":"a","properties":{"href":"#del-4-positiva-och-negativa","ariaLabel":"del 4 positiva och negativa permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör nu så att programmet också skriver ut antalet positiva och negativa tal."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34\nMedelvärdet av talen är 8.5\nPositiva 3\nNegativa 1"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät som behandlar den här veckans material."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad en loop betyder i programmeringssammanhang
  • \n
  • kan du använda dig av en while True -loop i dina program
  • \n
  • kommer du att kunna använda dig av break-instruktionen för att avbryta loopen.
  • \n

Vi har nu undersökt if-satser. Ett annat viktigt koncept inom programmering är repetition. De här två koncepten är grundläggande strukturer som varje programmerare väntas kunna. De är kontrollstrukturer – de ger dig möjligheten att påverka vilka kodrader körs och när de körs. Medan if-satser låter dig välja mellan olika delar av kod, finns loopar till för att köra en del av koden på nytt. Programmet går alltså tillbaka till en viss rad i koden ett antal gånger. En iteration är en \"runda\" av en loop – det vill säga den process då loopens kod körs från början till slutet.

I den här delen presenterar vi en enkel while-loop. Dess struktur påminner om if-satsen. I nästa del dyker vi djupare in i det som loopar kan erbjuda oss.

Vi tar en titt på ett program som ber användaren att ge en siffra som skrivs upp upphöjt med två. Programmet körs tills användaren ger siffran -1:

while True:\n    siffra = int(input(\"Ge en siffra, -1 avslutar programmet: \"))\n\n    if siffra == -1:\n        break\n\n    print(siffra ** 2)\n\nprint(\"Tack och hej!\")

Så här kan det se ut när programmet körs:

Ge en siffra, -1 avslutar programmet: 2\n4\nGe en siffra, -1 avslutar programmet: 4\n16\nGe en siffra, -1 avslutar programmet: 10\n100\nGe en siffra, -1 avslutar programmet: -1\nTack och hej!

Som du ser ovan, frågar programmet om ett nummer flera gånger. Detta tack vare while-satsen. När användaren anger siffran -1 kommer break-instruktionen att köras. Loopen avbryts och programmet fortsätter efter while-blocket.

När man arbetar med loopar är det viktigt att loopen avslutas vid något skede. Om man inte tar det här i beaktande kan loopen fortsätta för evigt. Vi ändrar lite på ovanstående exempel för att åstadkomma en sådan här situation:

siffra = int(input(\"Ge en siffra, -1 avslutar programmet: \"))\nwhile True:\n    if siffra == -1:\n        break\n\n    print(siffra ** 2)\n\nprint(\"Tack och hej!\")

I den här versionen frågar programmet användaren efter siffran utanför loopen. Om användaren ger någon annan siffra än -1 kommer loopen aldrig att avslutas. Vi har en oändlig loop vilket i princip betyder att koden körs oavbrutet, för evigt:

Ge en siffra, -1 avslutar programmet: 2\n4\n4\n4\n4\n4\n4\n4\n4\n(fortsätter oändligt...)

Följande program har en mycket liknande struktur jämfört med exemplet ovan, men för användaren ser det ganska annorlunda ut. Det här programmet låter användaren fortsätta endast då den korrekta pin-koden 1234 anges:

while True:\n    kod = input(\"Ange pin-kod: \")\n    if kod == \"1234\":\n        break\n    print(\"Fel! Försök igen\")\n\nprint(\"Korrekt pin-kod\")

Ange pin-kod: 0000\nFel! Försök igen\nAnge pin-kod: 9999\nFel! Försök igen\nAnge pin-kod: 1234\nKorrekt pin-kod

Skriv enligt föregående exempel ett program som skriver ut texten \"hej\" och frågar \"Fortsätter vi?\" tills användaren svarar \"nej\". Efter det här skriver programmet ut \"inte då\" och programmet avslutas.

Exempel:

hej\nFortsätter vi? ja\nhej\nFortsätter vi? yes\nhej\nFortsätter vi? jawohl\nhej\nFortsätter vi? nej\ninte då

Skapa ett program som frågar efter siffror från användaren. Om siffran är negativ, skriver man ut meddelandet \"Ogiltigt tal\" och ett nytt tal tillfrågas. Om siffran är noll, avslutas loopen.

Om siffran är positiv, skriver programmet ut siffrans kvadratrot med hjälp av sqrt-funktionen som är tillgänglig tack vare import-satsen. Exempel på funktionen:

# det här ska vara i början av programmet för att sqrt ska fungera\nfrom math import sqrt\n\nprint(sqrt(9))

3.0

Exempel på programmet:

Ange tal: 16\n4.0\nAnge tal: 4\n2.0\nAnge tal: -3\nOgiltigt tal\nAnge tal: 1\n1.0\nAnge tal: 0\nAvslutar...

Det här programmet...

siffra = 5\nprint(\"Nedräkning till start!\")\nwhile True:\n  print(siffra)\n  siffra = siffra - 1\n  if siffra > 0:\n    break\n\nprint(\"Gå!!\")

...borde fungera så här:

Nedräkning till start!\n5\n4\n3\n2\n1\nGå!!

Korrigera problemet i koden.

Skapa ett program som ber användaren att ange ett lösenord och upprepa lösenordet, tills användaren anger det första lösenordet på nytt.

Lösenord: hemligt?\nUpprepa lösenordet: hemlighet\nStämmer inte!\nUpprepa lösenordet: intminnsjanumer321\nStämmer inte!\nUpprepa lösenordet: hemligt?\nKontot skapat!

Loopar och hjälpvariabler

Vi gör föregående exemplet en aning mer realistiskt. Det här exemplet tillåter användaren endast tre försök att ge korrekt pin-kod.

Programmet består av två hjälpvariabler: forsok håller reda på hur många gånger användaren angett en pin-kod och lyckades är antingen True eller False beroende på om användaren ger den korrekta koden eller inte.

forsok = 0\n\nwhile True:\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n\n    if kod == \"1234\":\n        lyckades = True\n        break\n\n    if forsok == 3:\n        lyckades = False\n        break\n\n    # vi kommer hit om koden är fel OCH tre försök inte ännu gjorts\n    print(\"Fel! Försök igen\")\n\nif lyckades:\n    print(\"Korrekt pin-kod\")\nelse:\n    print(\"För många försök...\")

Ange pin-kod: 0000\nFel! Försök igen\nAnge pin-kod: 1234\nKorrekt pin-kod

Ange pin-kod: 0000\nFel! Försök igen\nAnge pin-kod: 9999\nFel! Försök igen\nAnge pin-kod: 4321\nFör många försök...

Loopen avslutas antingen om pin-koden är korrekt eller då maxantalet försök har uppnåtts. Efterföljande if-sats kollar variabeln lyckades värde och skriver ut ett meddelande baserat på värdet.

Print-satser för debuggning i loopar

Att introducera loopar i ett program ökar på möjligheten för buggar. Därför är det viktigt att senast nu utnyttja print-satser i debuggningssyfte – dem såg vi på i den första delen av den pågående modulen.

Vi kikar på ett nästan identiskt program som i det föregående exemplet. Dock finns det en märkbar skillnad:

forsok = 0\n\nwhile True:\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n\n    if forsok == 3:\n        lyckades = False\n        break\n\n    if kod == \"1234\":\n        lyckades = True\n        break\n\n    print(\"Fel! Försök igen\")\n\nif lyckades:\n    print(\"Korrekt pin-kod\")\nelse:\n    print(\"För många försök...\")

Den här versionen fungerar konstigt när användaren anger den korrekta koden på det tredje försöket:

Ange pin-kod: 0000\nFel! Försök igen\nAnge pin-kod: 9999\nFel! Försök igen\nAnge pin-kod: 1234\nFör många försök...

Nu borde vi alltså reda ut det här problemet. Några print-satser borde hjälpa oss att debugga – så låt oss lägga till sådana i loopen:

while True:\n    print(\"while-blocket inleds:\")\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n\n    print(\"försök:\", forsok)\n    print(\"villkor 1:\", forsok == 3)\n    if forsok == 3:\n        lyckades = False\n        break\n\n    print(\"kod:\", kod)\n    print(\"villkor 2:\", kod == \"1234\")\n    if kod == \"1234\":\n        lyckades = True\n        break\n\n    print(\"Fel! Försök igen\")

while-blocket inleds:\nAnge pin-kod: 2233\nförsök: 1\nvillkor 1: False\nkod: 2233\nvillkor 2: False\nFel! Försök igen\nwhile-blocket inleds:\nAnge pin-kod: 4545\nförsök: 2\nvillkor 1: False\nkod: 4545\nvillkor 2: False\nFel! Försök igen\nwhile-blocket inleds:\nAnge pin-kod: 1234\nförsök: 3\nvillkor 1: True\nFör många försök...

Från utskriften ovan märker vi att under den tredje iterationen kommer villkoret i den första if-satsen att vara sant och därmed hinner vi aldrig fram till den andra if-satsen i och med att loopen avslutas. Därmed kontrolleras koden aldrig:

  while True:\n    # ....\n\n    # det här blocket kommer för tidigt\n    if forsok == 3:\n        lyckades = False\n        break\n\n    # vi når inte hit på det tredje försöket\n    if kod == \"1234\":\n        lyckades = True\n        break

Ordningen på if-satser eller grenar inom if-satser är vanliga orsaker till buggar – framför allt inom loopar. Debuggning med hjälp av print-satser hjälper förvånansvärt ofta.

Skapa ett program som frågar användaren om en pin-kod tills hon anger den korrekta pin-koden 4321. Programmet berättar hur många försök som har gjorts:

Pin-kod: 3245\nFel\nPin-kod: 1234\nFel\nPin-kod: 0000\nFel\nPin-kod: 4321\nKorrekt, du gjorde 4 försök

Utskriften skiljer sig då pin-koden är korrekt på det första försöket:

Pin-kod: 4321\nKorrekt, du behövde bara ett försök!

Skapa ett program som ber användaren ange ett årtal. Programmet ska berätta när nästa skottår är.

År: 2019\nNästa skottår efter 2019 är 2020

Om året användaren anger är ett skottår (t.ex. 2020), ska programmet ändå berätta när det följande skottåret är:

År: 2020\nNästa skottår efter 2020 är 2024

Kombinera strängar med +-operatorn

Exemplet ovan använde hjälpvariabeln forsok för att hålla koll på hur många gånger användaren försökt skriva in en kod:

forsok = 0\n\nwhile True:\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n    # ...

Variabeln tilldelas värdet noll utanför loopen och varje iteration ökar på siffran med ett.

Något liknande kan man också göra med strängar. Programmet kan till exempel hålla koll på de pin-koder användaren angett:

koder = \"\"\nforsok = 0\n\nwhile True:\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n    koder += kod + \", \"\n    # ...

Hjälpvariabeln kan tilldelas värdet \"\" – det vill säga en tom sträng:

koder = \"\"

För varje iteration blir strängen längre i och med att koden användaren angett läggs till i slutet av strängen tillsammans med ett komma och ett mellanslag.

    kod = input(\"Ange pin-kod: \")\n    koder += kod + \", \"

Om användaren anger koderna 1111 2222 1234 kommer värdet på koder till slut att vara:

1111, 2222, 1234,

Del 1

Skapa ett program som ber användaren ange ord. Då användaren anger ordet slut, skriver programmet ut en berättelse som bildas av orden, varefter programmet avslutas.

Ange ord: Det\nAnge ord: var\nAnge ord: en\nAnge ord: gång\nAnge ord: ...\nAnge ord: slut\nDet var en gång ...

Del 2

Redigera på programmet så att programmet slutar fråga efter ord då ordet slut anges eller då det föregående ordet upprepas.

Ange ord: I\nAnge ord: början\nAnge ord: fanns\nAnge ord: hönan\nAnge ord: eller\nAnge ord: ägget\nAnge ord: ägget\nI början fanns hönan eller ägget

Skapa ett program som ber användaren ange heltal. Programmet ber efter tal tills talet noll anges.

Ange heltal, 0 avslutar programmet:\nTal: 5\nTal: 22\nTal: 9\nTal: -2\nTal: 0

Del 1: Antal

Efter att siffrorna har angetts, ska programmet skriva ut antalet siffror. Nollan ska inte tas i beaktande.

Du behöver en ny variabel som håller koll på antalet angivna siffror.

(användaren anger tal)\nTillsammans 4 tal

Del 2: Summa

Utvidga programmet så att det skriver ut summan av de angivna talen. Nollan ska inte tas i beaktande.

Så här ser utskriften ut nu:

(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34

Del 3: Medelvärde

Utveckla programmet så att det räknar medelvärdet av de angivna talen. Nollan ska inte tas i beaktande. Du kan anta att användaren ger minst ett tal.

(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34\nMedelvärdet av talen är 8.5

Del 4: Positiva och negativa

Gör nu så att programmet också skriver ut antalet positiva och negativa tal.

(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34\nMedelvärdet av talen är 8.5\nPositiva 3\nNegativa 1

Vänligen svara på en kort enkät som behandlar den här veckans material.

","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-2/4-loopar.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad en loop betyder i programmeringssammanhang"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" -loop i dina program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kommer du att kunna använda dig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen för att avbryta loopen."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har nu undersökt if-satser. Ett annat viktigt koncept inom programmering är repetition. De här två koncepten är grundläggande strukturer som varje programmerare väntas kunna. De är kontrollstrukturer – de ger dig möjligheten att påverka vilka kodrader körs och när de körs. Medan if-satser låter dig välja mellan olika delar av kod, finns loopar till för att köra en del av koden på nytt. Programmet går alltså tillbaka till en viss rad i koden ett antal gånger. En iteration är en \"runda\" av en loop – det vill säga den process då loopens kod körs från början till slutet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här delen presenterar vi en enkel while-loop. Dess struktur påminner om if-satsen. I nästa del dyker vi djupare in i det som loopar kan erbjuda oss."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi tar en titt på ett program som ber användaren att ge en siffra som skrivs upp upphöjt med två. Programmet körs tills användaren ger siffran -1:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tack och hej!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n4\nGe en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n16\nGe en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n100\nGe en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nTack och hej!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du ser ovan, frågar programmet om ett nummer flera gånger. Detta tack vare while-satsen. När användaren anger siffran -1 kommer "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen att köras. Loopen avbryts och programmet fortsätter efter while-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man arbetar med loopar är det viktigt att loopen avslutas vid något skede. Om man inte tar det här i beaktande kan loopen fortsätta för evigt. Vi ändrar lite på ovanstående exempel för att åstadkomma en sådan här situation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge en siffra, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tack och hej!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här versionen frågar programmet användaren efter siffran utanför loopen. Om användaren ger någon annan siffra än -1 kommer loopen aldrig att avslutas. Vi har en oändlig loop vilket i princip betyder att koden körs oavbrutet, för evigt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en siffra, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n4\n4\n4\n4\n4\n4\n4\n4\n(fortsätter oändligt...)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program har en mycket liknande struktur jämfört med exemplet ovan, men för användaren ser det ganska annorlunda ut. Det här programmet låter användaren fortsätta endast då den korrekta pin-koden 1234 anges:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel! Försök igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Korrekt pin-kod\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nKorrekt pin-kod"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fortsätter vi?","tmcname":"osa02-15_fortsatt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv enligt föregående exempel ett program som skriver ut texten \"hej\" och frågar \"Fortsätter vi?\" tills användaren svarar \"nej\". Efter det här skriver programmet ut \"inte då\" och programmet avslutas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"hej\nFortsätter vi? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ja"}]},{"type":"text","value":"\nhej\nFortsätter vi? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"yes"}]},{"type":"text","value":"\nhej\nFortsätter vi? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jawohl"}]},{"type":"text","value":"\nhej\nFortsätter vi? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nej"}]},{"type":"text","value":"\ninte då"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Valida värden","tmcname":"osa02-16_validering"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter siffror från användaren. Om siffran är negativ, skriver man ut meddelandet \"Ogiltigt tal\" och ett nytt tal tillfrågas. Om siffran är noll, avslutas loopen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om siffran är positiv, skriver programmet ut siffrans kvadratrot med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":"-funktionen som är tillgänglig tack vare "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen. Exempel på funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här ska vara i början av programmet för att sqrt ska fungera"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på programmet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"16"}]},{"type":"text","value":"\n4.0\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n2.0\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nOgiltigt tal\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n1.0\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nAvslutar..."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Nedräkning","tmcname":"osa02-17_nedrakning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nedräkning till start!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gå!!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...borde fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedräkning till start!\n5\n4\n3\n2\n1\nGå!!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigera problemet i koden."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Lösenord på nytt","tmcname":"osa02-18_losenord"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren att ange ett lösenord och upprepa lösenordet, tills användaren anger det första lösenordet på nytt."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lösenord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hemligt?"}]},{"type":"text","value":"\nUpprepa lösenordet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hemlighet"}]},{"type":"text","value":"\nStämmer inte!\nUpprepa lösenordet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"intminnsjanumer321"}]},{"type":"text","value":"\nStämmer inte!\nUpprepa lösenordet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hemligt?"}]},{"type":"text","value":"\nKontot skapat!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"loopar-och-hjalpvariabler","style":"position:relative;"},"children":[{"type":"text","value":"Loopar och hjälpvariabler"},{"type":"element","tagName":"a","properties":{"href":"#loopar-och-hjalpvariabler","ariaLabel":"loopar och hjalpvariabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi gör föregående exemplet en aning mer realistiskt. Det här exemplet tillåter användaren endast tre försök att ge korrekt pin-kod."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet består av två hjälpvariabler: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forsok"}]},{"type":"text","value":" håller reda på hur många gånger användaren angett en pin-kod och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lyckades"}]},{"type":"text","value":" är antingen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" beroende på om användaren ger den korrekta koden eller inte."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi kommer hit om koden är fel OCH tre försök inte ännu gjorts"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel! Försök igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" lyckades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Korrekt pin-kod\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"För många försök...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nKorrekt pin-kod"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nFör många försök..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Loopen avslutas antingen om pin-koden är korrekt eller då maxantalet försök har uppnåtts. Efterföljande if-sats kollar variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lyckades"}]},{"type":"text","value":" värde och skriver ut ett meddelande baserat på värdet."}]},{"type":"element","tagName":"h2","properties":{"id":"print-satser-for-debuggning-i-loopar","style":"position:relative;"},"children":[{"type":"text","value":"Print-satser för debuggning i loopar"},{"type":"element","tagName":"a","properties":{"href":"#print-satser-for-debuggning-i-loopar","ariaLabel":"print satser for debuggning i loopar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att introducera loopar i ett program ökar på möjligheten för buggar. Därför är det viktigt att senast nu utnyttja print-satser i debuggningssyfte – dem såg vi på i den första delen av den pågående modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kikar på ett nästan identiskt program som i det föregående exemplet. Dock finns det en märkbar skillnad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel! Försök igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" lyckades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Korrekt pin-kod\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"För många försök...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här versionen fungerar konstigt när användaren anger den korrekta koden på det tredje försöket:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nFel! Försök igen\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nFör många försök..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu borde vi alltså reda ut det här problemet. Några "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser borde hjälpa oss att debugga – så låt oss lägga till sådana i loopen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"while-blocket inleds:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"försök:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forsok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"villkor 1:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kod:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"villkor 2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel! Försök igen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"while-blocket inleds:\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2233"}]},{"type":"text","value":"\nförsök: 1\nvillkor 1: False\nkod: 2233\nvillkor 2: False\nFel! Försök igen\nwhile-blocket inleds:\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4545"}]},{"type":"text","value":"\nförsök: 2\nvillkor 1: False\nkod: 4545\nvillkor 2: False\nFel! Försök igen\nwhile-blocket inleds:\nAnge pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nförsök: 3\nvillkor 1: True\nFör många försök..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Från utskriften ovan märker vi att under den tredje iterationen kommer villkoret i den första if-satsen att vara sant och därmed hinner vi aldrig fram till den andra if-satsen i och med att loopen avslutas. Därmed kontrolleras koden aldrig:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ...."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här blocket kommer för tidigt"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi når inte hit på det tredje försöket"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lyckades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ordningen på if-satser eller grenar inom if-satser är vanliga orsaker till buggar – framför allt inom loopar. Debuggning med hjälp av print-satser hjälper förvånansvärt ofta."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Pin och antal försök","tmcname":"osa02-19_pin"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar användaren om en pin-kod tills hon anger den korrekta pin-koden 4321. Programmet berättar hur många försök som har gjorts:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3245"}]},{"type":"text","value":"\nFel\nPin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nFel\nPin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nFel\nPin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nKorrekt, du gjorde 4 försök"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften skiljer sig då pin-koden är korrekt på det första försöket:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pin-kod: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nKorrekt, du behövde bara ett försök!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Nästa skottår","tmcname":"osa02-20_nasta_skottar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange ett årtal. Programmet ska berätta när nästa skottår är."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"År: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2019"}]},{"type":"text","value":"\nNästa skottår efter 2019 är 2020"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om året användaren anger är ett skottår (t.ex. 2020), ska programmet ändå berätta när det följande skottåret är:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"År: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]},{"type":"text","value":"\nNästa skottår efter 2020 är 2024"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"kombinera-strangar-med--operatorn","style":"position:relative;"},"children":[{"type":"text","value":"Kombinera strängar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-operatorn"},{"type":"element","tagName":"a","properties":{"href":"#kombinera-strangar-med--operatorn","ariaLabel":"kombinera strangar med operatorn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exemplet ovan använde hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forsok"}]},{"type":"text","value":" för att hålla koll på hur många gånger användaren försökt skriva in en kod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln tilldelas värdet noll utanför loopen och varje iteration ökar på siffran med ett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Något liknande kan man också göra med strängar. Programmet kan till exempel hålla koll på de pin-koder användaren angett:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"koder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\nforsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n koder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", \""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hjälpvariabeln kan tilldelas värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":" – det vill säga en tom sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"koder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje iteration blir strängen längre i och med att koden användaren angett läggs till i slutet av strängen tillsammans med ett komma och ett mellanslag."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange pin-kod: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n koder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", \""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren anger koderna 1111 2222 1234 kommer värdet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"koder"}]},{"type":"text","value":" till slut att vara:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1111, 2222, 1234,"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Berättelse","tmcname":"osa02-21_berattelse"},"children":[{"type":"element","tagName":"h3","properties":{"id":"del-1","style":"position:relative;"},"children":[{"type":"text","value":"Del 1"},{"type":"element","tagName":"a","properties":{"href":"#del-1","ariaLabel":"del 1 permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange ord. Då användaren anger ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"slut"}]},{"type":"text","value":", skriver programmet ut en berättelse som bildas av orden, varefter programmet avslutas."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Det"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"var"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"en"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"gång"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"..."}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"slut"}]},{"type":"text","value":"\nDet var en gång ..."}]}]},{"type":"element","tagName":"h3","properties":{"id":"del-2","style":"position:relative;"},"children":[{"type":"text","value":"Del 2"},{"type":"element","tagName":"a","properties":{"href":"#del-2","ariaLabel":"del 2 permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Redigera på programmet så att programmet slutar fråga efter ord då ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"slut"}]},{"type":"text","value":" anges eller då det föregående ordet upprepas."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"I"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"början"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"fanns"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hönan"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eller"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ägget"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ägget"}]},{"type":"text","value":"\nI början fanns hönan eller ägget"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Behandling av tal","tmcname":"osa02-22_tal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange heltal. Programmet ber efter tal tills talet noll anges."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange heltal, 0 avslutar programmet:\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"22"}]},{"type":"text","value":"\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-2"}]},{"type":"text","value":"\nTal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"del-1-antal","style":"position:relative;"},"children":[{"type":"text","value":"Del 1: Antal"},{"type":"element","tagName":"a","properties":{"href":"#del-1-antal","ariaLabel":"del 1 antal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter att siffrorna har angetts, ska programmet skriva ut antalet siffror. Nollan ska inte tas i beaktande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du behöver en ny variabel som håller koll på antalet angivna siffror."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(användaren anger tal)\nTillsammans 4 tal"}]}]},{"type":"element","tagName":"h3","properties":{"id":"del-2-summa","style":"position:relative;"},"children":[{"type":"text","value":"Del 2: Summa"},{"type":"element","tagName":"a","properties":{"href":"#del-2-summa","ariaLabel":"del 2 summa permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utvidga programmet så att det skriver ut summan av de angivna talen. Nollan ska inte tas i beaktande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ser utskriften ut nu:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34"}]}]},{"type":"element","tagName":"h3","properties":{"id":"del-3-medelvarde","style":"position:relative;"},"children":[{"type":"text","value":"Del 3: Medelvärde"},{"type":"element","tagName":"a","properties":{"href":"#del-3-medelvarde","ariaLabel":"del 3 medelvarde permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utveckla programmet så att det räknar medelvärdet av de angivna talen. Nollan ska inte tas i beaktande. Du kan anta att användaren ger minst ett tal."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34\nMedelvärdet av talen är 8.5"}]}]},{"type":"element","tagName":"h4","properties":{"id":"del-4-positiva-och-negativa","style":"position:relative;"},"children":[{"type":"text","value":"Del 4: Positiva och negativa"},{"type":"element","tagName":"a","properties":{"href":"#del-4-positiva-och-negativa","ariaLabel":"del 4 positiva och negativa permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör nu så att programmet också skriver ut antalet positiva och negativa tal."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34\nMedelvärdet av talen är 8.5\nPositiva 3\nNegativa 1"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät som behandlar den här veckans material."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad en loop betyder i programmeringssammanhang
  • \n
  • kan du använda dig av en while True -loop i dina program
  • \n
  • kommer du att kunna använda dig av break-instruktionen för att avbryta loopen.
  • \n

Vi har nu undersökt if-satser. Ett annat viktigt koncept inom programmering är repetition. De här två koncepten är grundläggande strukturer som varje programmerare väntas kunna. De är kontrollstrukturer – de ger dig möjligheten att påverka vilka kodrader körs och när de körs. Medan if-satser låter dig välja mellan olika delar av kod, finns loopar till för att köra en del av koden på nytt. Programmet går alltså tillbaka till en viss rad i koden ett antal gånger. En iteration är en \"runda\" av en loop – det vill säga den process då loopens kod körs från början till slutet.

I den här delen presenterar vi en enkel while-loop. Dess struktur påminner om if-satsen. I nästa del dyker vi djupare in i det som loopar kan erbjuda oss.

Vi tar en titt på ett program som ber användaren att ge en siffra som skrivs upp upphöjt med två. Programmet körs tills användaren ger siffran -1:

while True:\n    siffra = int(input(\"Ge en siffra, -1 avslutar programmet: \"))\n\n    if siffra == -1:\n        break\n\n    print(siffra ** 2)\n\nprint(\"Tack och hej!\")

Så här kan det se ut när programmet körs:

Ge en siffra, -1 avslutar programmet: 2\n4\nGe en siffra, -1 avslutar programmet: 4\n16\nGe en siffra, -1 avslutar programmet: 10\n100\nGe en siffra, -1 avslutar programmet: -1\nTack och hej!

Som du ser ovan, frågar programmet om ett nummer flera gånger. Detta tack vare while-satsen. När användaren anger siffran -1 kommer break-instruktionen att köras. Loopen avbryts och programmet fortsätter efter while-blocket.

När man arbetar med loopar är det viktigt att loopen avslutas vid något skede. Om man inte tar det här i beaktande kan loopen fortsätta för evigt. Vi ändrar lite på ovanstående exempel för att åstadkomma en sådan här situation:

siffra = int(input(\"Ge en siffra, -1 avslutar programmet: \"))\nwhile True:\n    if siffra == -1:\n        break\n\n    print(siffra ** 2)\n\nprint(\"Tack och hej!\")

I den här versionen frågar programmet användaren efter siffran utanför loopen. Om användaren ger någon annan siffra än -1 kommer loopen aldrig att avslutas. Vi har en oändlig loop vilket i princip betyder att koden körs oavbrutet, för evigt:

Ge en siffra, -1 avslutar programmet: 2\n4\n4\n4\n4\n4\n4\n4\n4\n(fortsätter oändligt...)

Följande program har en mycket liknande struktur jämfört med exemplet ovan, men för användaren ser det ganska annorlunda ut. Det här programmet låter användaren fortsätta endast då den korrekta pin-koden 1234 anges:

while True:\n    kod = input(\"Ange pin-kod: \")\n    if kod == \"1234\":\n        break\n    print(\"Fel! Försök igen\")\n\nprint(\"Korrekt pin-kod\")

Ange pin-kod: 0000\nFel! Försök igen\nAnge pin-kod: 9999\nFel! Försök igen\nAnge pin-kod: 1234\nKorrekt pin-kod

Skriv enligt föregående exempel ett program som skriver ut texten \"hej\" och frågar \"Fortsätter vi?\" tills användaren svarar \"nej\". Efter det här skriver programmet ut \"inte då\" och programmet avslutas.

Exempel:

hej\nFortsätter vi? ja\nhej\nFortsätter vi? yes\nhej\nFortsätter vi? jawohl\nhej\nFortsätter vi? nej\ninte då

Skapa ett program som frågar efter siffror från användaren. Om siffran är negativ, skriver man ut meddelandet \"Ogiltigt tal\" och ett nytt tal tillfrågas. Om siffran är noll, avslutas loopen.

Om siffran är positiv, skriver programmet ut siffrans kvadratrot med hjälp av sqrt-funktionen som är tillgänglig tack vare import-satsen. Exempel på funktionen:

# det här ska vara i början av programmet för att sqrt ska fungera\nfrom math import sqrt\n\nprint(sqrt(9))

3.0

Exempel på programmet:

Ange tal: 16\n4.0\nAnge tal: 4\n2.0\nAnge tal: -3\nOgiltigt tal\nAnge tal: 1\n1.0\nAnge tal: 0\nAvslutar...

Det här programmet...

siffra = 5\nprint(\"Nedräkning till start!\")\nwhile True:\n  print(siffra)\n  siffra = siffra - 1\n  if siffra > 0:\n    break\n\nprint(\"Gå!!\")

...borde fungera så här:

Nedräkning till start!\n5\n4\n3\n2\n1\nGå!!

Korrigera problemet i koden.

Skapa ett program som ber användaren att ange ett lösenord och upprepa lösenordet, tills användaren anger det första lösenordet på nytt.

Lösenord: hemligt?\nUpprepa lösenordet: hemlighet\nStämmer inte!\nUpprepa lösenordet: intminnsjanumer321\nStämmer inte!\nUpprepa lösenordet: hemligt?\nKontot skapat!

Loopar och hjälpvariabler

Vi gör föregående exemplet en aning mer realistiskt. Det här exemplet tillåter användaren endast tre försök att ge korrekt pin-kod.

Programmet består av två hjälpvariabler: forsok håller reda på hur många gånger användaren angett en pin-kod och lyckades är antingen True eller False beroende på om användaren ger den korrekta koden eller inte.

forsok = 0\n\nwhile True:\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n\n    if kod == \"1234\":\n        lyckades = True\n        break\n\n    if forsok == 3:\n        lyckades = False\n        break\n\n    # vi kommer hit om koden är fel OCH tre försök inte ännu gjorts\n    print(\"Fel! Försök igen\")\n\nif lyckades:\n    print(\"Korrekt pin-kod\")\nelse:\n    print(\"För många försök...\")

Ange pin-kod: 0000\nFel! Försök igen\nAnge pin-kod: 1234\nKorrekt pin-kod

Ange pin-kod: 0000\nFel! Försök igen\nAnge pin-kod: 9999\nFel! Försök igen\nAnge pin-kod: 4321\nFör många försök...

Loopen avslutas antingen om pin-koden är korrekt eller då maxantalet försök har uppnåtts. Efterföljande if-sats kollar variabeln lyckades värde och skriver ut ett meddelande baserat på värdet.

Print-satser för debuggning i loopar

Att introducera loopar i ett program ökar på möjligheten för buggar. Därför är det viktigt att senast nu utnyttja print-satser i debuggningssyfte – dem såg vi på i den första delen av den pågående modulen.

Vi kikar på ett nästan identiskt program som i det föregående exemplet. Dock finns det en märkbar skillnad:

forsok = 0\n\nwhile True:\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n\n    if forsok == 3:\n        lyckades = False\n        break\n\n    if kod == \"1234\":\n        lyckades = True\n        break\n\n    print(\"Fel! Försök igen\")\n\nif lyckades:\n    print(\"Korrekt pin-kod\")\nelse:\n    print(\"För många försök...\")

Den här versionen fungerar konstigt när användaren anger den korrekta koden på det tredje försöket:

Ange pin-kod: 0000\nFel! Försök igen\nAnge pin-kod: 9999\nFel! Försök igen\nAnge pin-kod: 1234\nFör många försök...

Nu borde vi alltså reda ut det här problemet. Några print-satser borde hjälpa oss att debugga – så låt oss lägga till sådana i loopen:

while True:\n    print(\"while-blocket inleds:\")\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n\n    print(\"försök:\", forsok)\n    print(\"villkor 1:\", forsok == 3)\n    if forsok == 3:\n        lyckades = False\n        break\n\n    print(\"kod:\", kod)\n    print(\"villkor 2:\", kod == \"1234\")\n    if kod == \"1234\":\n        lyckades = True\n        break\n\n    print(\"Fel! Försök igen\")

while-blocket inleds:\nAnge pin-kod: 2233\nförsök: 1\nvillkor 1: False\nkod: 2233\nvillkor 2: False\nFel! Försök igen\nwhile-blocket inleds:\nAnge pin-kod: 4545\nförsök: 2\nvillkor 1: False\nkod: 4545\nvillkor 2: False\nFel! Försök igen\nwhile-blocket inleds:\nAnge pin-kod: 1234\nförsök: 3\nvillkor 1: True\nFör många försök...

Från utskriften ovan märker vi att under den tredje iterationen kommer villkoret i den första if-satsen att vara sant och därmed hinner vi aldrig fram till den andra if-satsen i och med att loopen avslutas. Därmed kontrolleras koden aldrig:

  while True:\n    # ....\n\n    # det här blocket kommer för tidigt\n    if forsok == 3:\n        lyckades = False\n        break\n\n    # vi når inte hit på det tredje försöket\n    if kod == \"1234\":\n        lyckades = True\n        break

Ordningen på if-satser eller grenar inom if-satser är vanliga orsaker till buggar – framför allt inom loopar. Debuggning med hjälp av print-satser hjälper förvånansvärt ofta.

Skapa ett program som frågar användaren om en pin-kod tills hon anger den korrekta pin-koden 4321. Programmet berättar hur många försök som har gjorts:

Pin-kod: 3245\nFel\nPin-kod: 1234\nFel\nPin-kod: 0000\nFel\nPin-kod: 4321\nKorrekt, du gjorde 4 försök

Utskriften skiljer sig då pin-koden är korrekt på det första försöket:

Pin-kod: 4321\nKorrekt, du behövde bara ett försök!

Skapa ett program som ber användaren ange ett årtal. Programmet ska berätta när nästa skottår är.

År: 2019\nNästa skottår efter 2019 är 2020

Om året användaren anger är ett skottår (t.ex. 2020), ska programmet ändå berätta när det följande skottåret är:

År: 2020\nNästa skottår efter 2020 är 2024

Kombinera strängar med +-operatorn

Exemplet ovan använde hjälpvariabeln forsok för att hålla koll på hur många gånger användaren försökt skriva in en kod:

forsok = 0\n\nwhile True:\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n    # ...

Variabeln tilldelas värdet noll utanför loopen och varje iteration ökar på siffran med ett.

Något liknande kan man också göra med strängar. Programmet kan till exempel hålla koll på de pin-koder användaren angett:

koder = \"\"\nforsok = 0\n\nwhile True:\n    kod = input(\"Ange pin-kod: \")\n    forsok += 1\n    koder += kod + \", \"\n    # ...

Hjälpvariabeln kan tilldelas värdet \"\" – det vill säga en tom sträng:

koder = \"\"

För varje iteration blir strängen längre i och med att koden användaren angett läggs till i slutet av strängen tillsammans med ett komma och ett mellanslag.

    kod = input(\"Ange pin-kod: \")\n    koder += kod + \", \"

Om användaren anger koderna 1111 2222 1234 kommer värdet på koder till slut att vara:

1111, 2222, 1234,

Del 1

Skapa ett program som ber användaren ange ord. Då användaren anger ordet slut, skriver programmet ut en berättelse som bildas av orden, varefter programmet avslutas.

Ange ord: Det\nAnge ord: var\nAnge ord: en\nAnge ord: gång\nAnge ord: ...\nAnge ord: slut\nDet var en gång ...

Del 2

Redigera på programmet så att programmet slutar fråga efter ord då ordet slut anges eller då det föregående ordet upprepas.

Ange ord: I\nAnge ord: början\nAnge ord: fanns\nAnge ord: hönan\nAnge ord: eller\nAnge ord: ägget\nAnge ord: ägget\nI början fanns hönan eller ägget

Skapa ett program som ber användaren ange heltal. Programmet ber efter tal tills talet noll anges.

Ange heltal, 0 avslutar programmet:\nTal: 5\nTal: 22\nTal: 9\nTal: -2\nTal: 0

Del 1: Antal

Efter att siffrorna har angetts, ska programmet skriva ut antalet siffror. Nollan ska inte tas i beaktande.

Du behöver en ny variabel som håller koll på antalet angivna siffror.

(användaren anger tal)\nTillsammans 4 tal

Del 2: Summa

Utvidga programmet så att det skriver ut summan av de angivna talen. Nollan ska inte tas i beaktande.

Så här ser utskriften ut nu:

(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34

Del 3: Medelvärde

Utveckla programmet så att det räknar medelvärdet av de angivna talen. Nollan ska inte tas i beaktande. Du kan anta att användaren ger minst ett tal.

(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34\nMedelvärdet av talen är 8.5

Del 4: Positiva och negativa

Gör nu så att programmet också skriver ut antalet positiva och negativa tal.

(användaren anger tal)\nTillsammans 4 tal\nSumman av talen är 34\nMedelvärdet av talen är 8.5\nPositiva 3\nNegativa 1

Vänligen svara på en kort enkät som behandlar den här veckans material.

","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-2/4-loopar.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-2/page-data.json b/page-data/osa-2/page-data.json index 9952eaf56..c087bf4f3 100644 --- a/page-data/osa-2/page-data.json +++ b/page-data/osa-2/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-2", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-2","title":"Modul 2"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-2/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-2","title":"Modul 2"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-2/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-3/1-loopar-med-villkor/page-data.json b/page-data/osa-3/1-loopar-med-villkor/page-data.json index 453300361..483eeb2b9 100644 --- a/page-data/osa-3/1-loopar-med-villkor/page-data.json +++ b/page-data/osa-3/1-loopar-med-villkor/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-3/1-loopar-med-villkor", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa en loop med ett villkor"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad vilka roller initiering, villkoret och att uppdatering av variabler har i en loop"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa loopar med olika typer av villkor."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"osallistu-tutkimukseen---vastaa-kyselyyn","style":"position:relative;"},"children":[{"type":"text","value":"Osallistu tutkimukseen - vastaa kyselyyn"},{"type":"element","tagName":"a","properties":{"href":"#osallistu-tutkimukseen---vastaa-kyselyyn","ariaLabel":"osallistu tutkimukseen vastaa kyselyyn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla olevan linkin takaa löytyy Hannu Pesosen väitöskirjatutkimukseen liittyvä kysely. Kurssin suorittaminen ei edellytä kyselyyn vastaamista, mutta yliopistoissa tutkimus on luonnollisesti tärkeässä osassa myös opetuksen kehittämisessä."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jos sinulta siis löytyy hetki aikaa, niin "},{"type":"element","tagName":"a","properties":{"href":"https://link.webropolsurveys.com/S/EB89556E704FA59B","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"vastaa kyselyyn"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kyselyyn ei ole pakko antaa henkilötietoja (esimerkiksi nimeä), vaikka niitä ensimmäisellä sivulla kysytäänkin."}]},{"type":"comment","value":"vastaava teksti löytyy osioista 3-1, 5-1 ja 6-1, tsekkaa kaikki jos muokkaat tätä"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Om uppgifterna i den här kursen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den förra delen bekantade vi oss med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" -loopen som ett medel att upprepa delar av kod. Så som loopen är uppbyggd är villkoret alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", alltså sant. Vi måste då avsluta loopen manuellt vid något skede för att undvika en oändlig loop. Exempelvis:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriv ut siffror tills värdet på variabeln a är 5"}]},{"type":"text","value":"\na "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men förstås behöver villkoret inte alltid vara True, utan det kan vara vilket som helst Boolean-uttryck. while-satsens struktur ser ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"villkor"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"block"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén är att koden körs om och om igen – villkoret kollas för varje iteration. Om villkoret vid något skede inte är sant kommer programmet att fortsätta med koden som kommer efter while-blocket."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 417px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f04729f75a5887689930a906bc04ff72/f27fb/3_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 103.04347826086956%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAYAAABG1c6oAAAACXBIWXMAAAsSAAALEgHS3X78AAABsklEQVQ4y42VW6+CMBCE+f8/S5+MMdEX3zQao3hXwBvear49GVKBI92EtJTt7OzstkQuwN7vt42Px8Ndr9diTeu+ReVNvqPmer/dbi5Jkop/LWD5o7/h9XrZHHaj0agSzH8qDM/ns8uyrBIA0M1m4/r9vvnUpVub8uFwsA2y6XTqJpOJ2+/3brlc2vtut7M5I6zxv1wufwxFVWkdj8dCeNYGg4Gbz+cWKI5jNx6P3Xa7NbaM+KPrYrEwjaMyZT4KMM/zIv3n82ksAfUzkKVpavsinIgKC6Ks12ujj93vd2MhA7zX633pKqmUWQQjoq5WK9OE0QdkTWDo1+12LYh8fEDWGlMWQ0be1QloKpbY6XSyxv9ZFHSDPWAwn81m1jbI4rcWe+kEZKttm3I6YkgVxRDt/QYnGDWoALJJVfSPHSmhJwEZBe5/t5RDjp6OHfrSHjwU7GdRfl0OGLoBSGGYK2VpD3Nrm6arSxsQvdVquU6nY33bbrcLYB1FWEehdyEbeGCCXlRfAf0GDwYkXVqHh3Nc1wlf11cIKCmp/+ruxGCGcgZsOByG39hNDKmwtPvvn/IBsvFoyw9tBp8AAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f04729f75a5887689930a906bc04ff72/a0b58/3_1_1.webp 230w","/static/f04729f75a5887689930a906bc04ff72/b6b2f/3_1_1.webp 417w"],"sizes":"(max-width: 417px) 100vw, 417px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f04729f75a5887689930a906bc04ff72/81c8e/3_1_1.png 230w","/static/f04729f75a5887689930a906bc04ff72/f27fb/3_1_1.png 417w"],"sizes":"(max-width: 417px) 100vw, 417px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f04729f75a5887689930a906bc04ff72/f27fb/3_1_1.png","alt":"3 1 1","title":"3 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den följande loopen har vi villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer < 10"}]},{"type":"text","value":". Blocket inom loopen kommer bara att köras då variabeln nummer är mindre än tio."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programmet är klart.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften skulle kunna se ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n5\n6\n7\n8\n9\nProgrammet är klart."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med den här strukturen kommer villkoret att kollas före blocket inom loopen körs. Det är möjligt att det här blocket inte kommer att köras en enda gång. Så här till exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\nProgrammet är klart."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tolv är inte mindre än tio, så programmet skriver inte ut någon siffra."}]},{"type":"element","tagName":"h2","properties":{"id":"initialisering-villkor-och-uppdatering","style":"position:relative;"},"children":[{"type":"text","value":"Initialisering, villkor och uppdatering"},{"type":"element","tagName":"a","properties":{"href":"#initialisering-villkor-och-uppdatering","ariaLabel":"initialisering villkor och uppdatering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att skapa en loop behövs ofta tre olika steg: initialisering, ett villkor och uppdatering av variabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Initialisering syftar till att ge startvärden till de variabler som används i loopens villkor. Det här görs före man kommer till loopen. Villkoret bestämmer hur länge loopen körs. Det skrivs i början av loopen. För varje iteration ska variablerna som används i villkoret uppdateras, så att loopen steg för steg närmar sitt slut. Här presenterar vi stegen i ett exempel:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 701px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a9e9068a5a4abf5977e87c6b64318418/49217/3_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 40.43478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABaUlEQVQoz1WRy3KbMABF2cfGBiT0RggBxnEn7Uw3/f8fOx3JidMuzkIj6c59NDruTOsPTDxw6wdue2DXD0I+CNuOzzdcynTKMSjHRTpaYWmF4aoCvZnpdKQ3iV5HmjEk3LLTl0ufGJcVMd9xc2TaZuwccWlCaI80nsvouUhbRTs1MRQhk5B+Q9hMY+xGCHd8fhC3n2iXcWFB+judzgx2o1OJwa1If2OwS/1c3BW3xd11DJw6yalXNM5mvFkwU8bEFaE8xk50KnCtbhyX8Rn1X74cCpdr7FJBoTkNilYHxHRjDBmbDoTPdHqujwtjuNWY50FxHvSLUz/y1gneOsm5f941ba9Q887x+w/LfrC9/2J/PJA6cC1OpKeVtgp+83TY67nG/+qvCl5UQPpEXFd0iJg411VNiCgfkG56jfDi81y6K3GLcFm4Cop5Z1x2bHSkY2F9z3VVFQLTllC+dKb/i/qNqkOcPykd/gWh5s8zQ4VpAgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a9e9068a5a4abf5977e87c6b64318418/a0b58/3_1_2.webp 230w","/static/a9e9068a5a4abf5977e87c6b64318418/bc10c/3_1_2.webp 460w","/static/a9e9068a5a4abf5977e87c6b64318418/e2a71/3_1_2.webp 701w"],"sizes":"(max-width: 701px) 100vw, 701px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a9e9068a5a4abf5977e87c6b64318418/81c8e/3_1_2.png 230w","/static/a9e9068a5a4abf5977e87c6b64318418/08a84/3_1_2.png 460w","/static/a9e9068a5a4abf5977e87c6b64318418/49217/3_1_2.png 701w"],"sizes":"(max-width: 701px) 100vw, 701px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a9e9068a5a4abf5977e87c6b64318418/49217/3_1_2.png","alt":"3 1 2","title":"3 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om någon av de här tre komponenterna fattas kommer loopen antagligen inte att fungera korrekt. Ett vanligt misstag är att låta bli att uppdatera variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programmet är klart.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här kommer värdet på variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nummer"}]},{"type":"text","value":" aldrig att ändras. Programmet är fast i en oändlig loop. Samma kod upprepas tills användaren avslutar programmet, till exempel med tangentkombinationen Control + C:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n1\n1\n1\n1\n(det här fortsätter i all oändlighet...)"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Skriv ut siffror","tmcname":"osa03-00_utskrift_av_siffror"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som i en loop skriver ut varannat tal från två till trettio. Varje tal skrivs ut på en ny rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ser utskriften ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n2\n4\n6\n8\no.s.v. ...\n"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Nedräkning","tmcname":"osa03-01_nedrakning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigera uppgiftens program..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Klara?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gå!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...så att det fungerar på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klara?\nGe ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n5\n4\n3\n2\n1\nGå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd inte en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" -loop!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"skriva-villkor","style":"position:relative;"},"children":[{"type":"text","value":"Skriva villkor"},{"type":"element","tagName":"a","properties":{"href":"#skriva-villkor","ariaLabel":"skriva villkor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla Boolean-uttryck och kombinationer av dem kan användas som villkor i en loop. Till exempel följande program skriver ut var tredje nummer förutsatt att det är mindre än 100 och inte dividerbart med fem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här följer två exempel på utskriften från programmet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28"}]},{"type":"text","value":"\n28\n31\n34\n37"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"96"}]},{"type":"text","value":"\n96\n99"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man ger programmet värdet 28 kommer loopen att avslutas med numret 37, eftersom nästa siffra är 40 – och är dividerbart med fem. När man ger värdet 96 kommer loopen att avsluta med numret 99 eftersom nästa siffra är 102 – som inte är mindre än 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du skriver en loop är det viktigt att se till att loopen alltid kommer att avslutas vid något skede. Det här programmet avslutas – eller inte – beroende på det värde som ges:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om man ger ett jämnt tal som är lika med tio eller mindre, kommer loopen att avslutas:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n6\n8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I övriga fall kommer loopen att fortsätta oändligt eftersom det inte då finns något sätt för variabeln att vara lika med tio. Till exempel tre och tolv är värden som skulle förorsaka en oändlig loop."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Siffror","tmcname":"osa03-02_siffror"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som skriver ut alla tal under den siffra som användaren angett. Börja från ett."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd inte "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" som villkor i while-loopen!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"tips-for-debuggning","style":"position:relative;"},"children":[{"type":"text","value":"Tips för debuggning"},{"type":"element","tagName":"a","properties":{"href":"#tips-for-debuggning","ariaLabel":"tips for debuggning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Föreställ att du håller på att skapa ett lite mera komplicerat program, som det i den följande uppgiften – "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Potenser av två"}]},{"type":"text","value":". Så här skulle man kunna starta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"stanna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fram till siffran\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" stanna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu börjar programmet med att läsa in den data användaren ger och fortsätter till en loop med ett villkor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är sannolikt att koden inte kommer att fungera på önskat sätt från början. Den kan behöva testas tio- eller till och med hundratals gånger före den fungerar korrekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här kodsnutten frågar alltid efter indata från användaren vilket gör testandet långsamt och arbetsdrygt. Varje gång programmet testas måste ett värde anges."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett sätt att bli av med problemet är att hårdkoda ett värde i koden medan den testas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi hårdkodar värdet här tills vidare"}]},{"type":"text","value":"\nstanna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# int(input(\"Fram till siffran\"))"}]},{"type":"text","value":"\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" stanna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet fungerar med det hårdkodade värdet, kan man enkelt testa med andra hårdkodade värden. När allt fungerar korrekt kan man testa på programmet så att användaren anger värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här tricket fungerar väl med flera av de tester som används i betygsättningen av den här kursens uppgifter. Om testet berättar att något är fel med till exempel värdet 42 så kan värdet tillfälligt hårdkodas i programmet medan du letar efter buggen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testet meddelade att koden inte fungerade korrekt då indatat är 42"}]},{"type":"text","value":"\nstanna "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# int(input(\"Fram till siffran\"))"}]},{"type":"text","value":"\nnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" stanna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Debuggning med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satsen nämndes några gånger under förra modulen i den här kursen. De program som du skapar kommer att bli mer invecklade i och med att kursen framskrider. Då kommer mängden debuggning som du behöver göra också antagligen att öka i samma proportion. Vanliga orsaker till buggar finns ofta i de villkor som avslutar loopar – de fungerar eventuellt korrekt för vissa värden, medan andra värden orsakar problem. Alltid är det inte heller lätt att observera det här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Därför är det nu dags att använda dig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser för att debugga – om du inte redan har gjort det. Du hittar instruktioner i den första och fjärde delen av den föregående modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vid sidan om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser finns även andra verktyg som kan använda för debuggning. Ett av dem är "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyget"}]},{"type":"text","value":" på "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Python Tutor"}]},{"type":"text","value":" -webbsidan. Verktyget låter dig köra din kod rad för rad och visar också de värden som är lagrade i variabler vid varje steg."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden – med några problem – från den förra delen visualiseras med Python Tutor i följande bild:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d0cc22babf6824495497d96f07b56664/95fa1/3_1_0.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 69.1304347826087%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAABYlAAAWJQFJUiTwAAABYUlEQVQoz42S3VLcMAxG8/4v15vOMFN6AdklIRv/xIkVSbYlM8Sww7alcCbjKx99kpVOVetBBHwaLn6JIqIHtVZVned5mqZ2Lsuy77scqGqnqoi4huD86gPAnkTetHaez+e+78dxGMcRAFqSqopIV2sFAGstQBQpOTEzIWIppV26ttZoseWgy4lFxFo7jqO1LoSAuMcYc85N45Q4F2s982vhEMK1UCdMrdLpdHLOxRhb5pVfD88/7h5/nxyyXMd5lw8z5+ycI6J930spH7vtR/vzvn94ski53tKVUkQkpTQMw7Zt3vuPyX/P/KfcOKZFVSUiACCiZrbF/VsWEUT03htjAIA5AQAiMrO+82myiGzbdrlcvHNh8d6/Pngzv25bVUspKXHYYJoX4wLR//Z8Izvn2uqK1JRVbm9+1XbJxGystda0b57nEEJbYfuHP08ODM4Ha2Zj+r6fpmldVyL6TvIL8JgySYPZRccAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d0cc22babf6824495497d96f07b56664/a0b58/3_1_0.webp 230w","/static/d0cc22babf6824495497d96f07b56664/bc10c/3_1_0.webp 460w","/static/d0cc22babf6824495497d96f07b56664/966d8/3_1_0.webp 920w","/static/d0cc22babf6824495497d96f07b56664/445df/3_1_0.webp 1380w","/static/d0cc22babf6824495497d96f07b56664/a8768/3_1_0.webp 1816w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d0cc22babf6824495497d96f07b56664/81c8e/3_1_0.png 230w","/static/d0cc22babf6824495497d96f07b56664/08a84/3_1_0.png 460w","/static/d0cc22babf6824495497d96f07b56664/c0255/3_1_0.png 920w","/static/d0cc22babf6824495497d96f07b56664/b1001/3_1_0.png 1380w","/static/d0cc22babf6824495497d96f07b56664/95fa1/3_1_0.png 1816w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d0cc22babf6824495497d96f07b56664/c0255/3_1_0.png","alt":"3 1 0","title":"3 1 0","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den röda pilen visar var programmet körs för tillfället. Verktyget visar vad som har skrivits ut fram till pilen och visar också vilka värden varje variabel har i varje steg."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det enda du behöver för att köra visualiseringsverktyget är att kopiera och klistra in din kod i verktygets "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"kodfönster"}]},{"type":"text","value":". Verktyget har en del begränsningar jämfört med den Python-version som används under den här kursen. Om du stöter på konstiga felmeddelanden kan det löna sig att använda någon annan metod för att debugga."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De som har sysslat med programmering en längre tid använder sällan visualiseringsverktyg men för en nybörjare kan verktyget verkligen vara till hjälp. Det är osannolikt att man av en slump får något program att fungera. Det är nödvändigt att man som programmerare förstår vilka värden ens programkod skapar vid ett visst skede medan programmet körs. Om de värden som lagras i variabler inte är sådana som man förväntar sig, finns det högst sannolikt en bugg i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Visualiseringsverktyget och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser är båda bra sätt för en programmerare att med egna ögon se att programmet gör exakt det som det ska göra."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Potenser av två","tmcname":"osa03-03_potenser_av_tva"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som först skriver ut siffran ett och därefter föregående siffra multiplicerat med två."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet avslutas då man skrivit ut det tal som är högst lika stort som det värde användaren angett. Man skriver inte alltså ut något tal som är större än det användaren angett!"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n1\n2\n4\n8"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\n1\n2\n4\n8\n16"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n1\n2\n4\n8\n16\n32\n64"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd inte villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" i while-satsen!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur räknar man potenser av två? Den första potensen av två är 1. Den nästa potensen får man genom att multiplicera 1 med 2, resultatet är 2. Den nästa potensen får man på samma sätt: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 * 2 = 4"}]},{"type":"text","value":". Därefter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4 * 2 = 8"}]},{"type":"text","value":" o.s.v."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"n:te potensen","tmcname":"osa03-04_nte_potensen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ändra på det föregående programmet så att programmet räknar den n:te potensen. Användaren anger värdet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" (i föregående programmet var värdet alltid 2)."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\nUpphöjt till: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1\n3\n9\n27"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234567"}]},{"type":"text","value":"\nUpphöjt till: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n1\n10\n100\n1000\n10000\n100000\n1000000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd inte villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" i while-satsen i den här uppgiften!"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Summa av varandra följande tal, version 1","tmcname":"osa03-04a_varandra_foljandes_summa_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som räknar summan av varanda följande tal "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 + 2 + 3 + ..."}]},{"type":"text","value":", tills värdet är minst lika stort som det tal användaren angett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här fungerar programmet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n10"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\n21"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att det tal anvädaren anger är två eller större."}]}]},{"type":"element","tagName":"h2","properties":{"id":"bilda-strangar","style":"position:relative;"},"children":[{"type":"text","value":"Bilda strängar"},{"type":"element","tagName":"a","properties":{"href":"#bilda-strangar","ariaLabel":"bilda strangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Under kursens första vecka lärde vi oss att det är möjligt att bilda strängar av kortare strängar med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-operatorn. Till exempel detta är valid Python-kod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ris\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", sås\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" och Python\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ris, sås och Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":"-operatorn låter oss skriva ovanstående lite mer kompakt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ris\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", sås\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" och Python\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här gäller också f-strängar som kan vara nyttiga då värden lagrade i strängar behövs som delar av en resulterande sträng. Det här skulle till exempel fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kurs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introkurs i programmering\""}]},{"type":"text","value":"\nvitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n\ntext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du fick \""}]},{"type":"text","value":"\ntext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"vitsordet "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" \""}]}]},{"type":"text","value":"\ntext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"i kursen "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du fick vitsordet 4 i kursen Introkurs i programmering"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det förra exemplet räknade du summan av varandra påföljande siffror genom att alltid öka på värdet i loopen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma fungerar också för strängar – du kan lägga till nya delar i en sträng inom en loop. Den här tekniken kan vara till nytta i följande uppgift."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Summa av varandra följande tal, version 2","tmcname":"osa03-05_varandra_foljandes_summa_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en mer avancerad version av det föregående programmet. Nu ska programmet visa hur man kom till det resultat som man kommit fram till:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nRäknade 1 + 2 = 3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nRäknade 1 + 2 + 3 + 4 = 10"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fram till siffran: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nRäknade 1 + 2 + 3 + 4 + 5 + 6 = 21"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att den siffra användaren anger är två eller större."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7a997c9b-0897-524d-9725-07def37b0272"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa en loop med ett villkor
  • \n
  • vet du vad vilka roller initiering, villkoret och att uppdatering av variabler har i en loop
  • \n
  • kan du skapa loopar med olika typer av villkor.
  • \n

Osallistu tutkimukseen - vastaa kyselyyn

Alla olevan linkin takaa löytyy Hannu Pesosen väitöskirjatutkimukseen liittyvä kysely. Kurssin suorittaminen ei edellytä kyselyyn vastaamista, mutta yliopistoissa tutkimus on luonnollisesti tärkeässä osassa myös opetuksen kehittämisessä.

Jos sinulta siis löytyy hetki aikaa, niin vastaa kyselyyn.

Kyselyyn ei ole pakko antaa henkilötietoja (esimerkiksi nimeä), vaikka niitä ensimmäisellä sivulla kysytäänkin.

Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen.

En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida.

Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter.

En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad.

I den förra delen bekantade vi oss med while True -loopen som ett medel att upprepa delar av kod. Så som loopen är uppbyggd är villkoret alltid True, alltså sant. Vi måste då avsluta loopen manuellt vid något skede för att undvika en oändlig loop. Exempelvis:

# skriv ut siffror tills värdet på variabeln a är 5\na = 1\nwhile True:\n    print(a)\n    a += 1\n    if a == 5:\n        break

1\n2\n3\n4

Men förstås behöver villkoret inte alltid vara True, utan det kan vara vilket som helst Boolean-uttryck. while-satsens struktur ser ut så här:

while <villkor>:\n    <block>

Idén är att koden körs om och om igen – villkoret kollas för varje iteration. Om villkoret vid något skede inte är sant kommer programmet att fortsätta med koden som kommer efter while-blocket.

\n \n \n \n \n \n \"3\n \n \n

Om någon av de här tre komponenterna fattas kommer loopen antagligen inte att fungera korrekt. Ett vanligt misstag är att låta bli att uppdatera variabler:

nummer = 1\n\nwhile nummer < 10:\n    print(nummer)\n\nprint(\"Programmet är klart.\")

Här kommer värdet på variabeln nummer aldrig att ändras. Programmet är fast i en oändlig loop. Samma kod upprepas tills användaren avslutar programmet, till exempel med tangentkombinationen Control + C:

1\n1\n1\n1\n1\n(det här fortsätter i all oändlighet...)

Skapa ett program som i en loop skriver ut varannat tal från två till trettio. Varje tal skrivs ut på en ny rad.

Så här ser utskriften ut:

\n2\n4\n6\n8\no.s.v. ...\n

Korrigera uppgiftens program...

print(\"Klara?\")\nnummer = int(input(\"Ge ett tal: \"))\nwhile nummer = 0:\nprint(nummer)\nprint(\"Gå!\")

...så att det fungerar på följande sätt:

Klara?\nGe ett tal: 5\n5\n4\n3\n2\n1\nGå!

Använd inte en while True -loop!

Skriva villkor

Alla Boolean-uttryck och kombinationer av dem kan användas som villkor i en loop. Till exempel följande program skriver ut var tredje nummer förutsatt att det är mindre än 100 och inte dividerbart med fem:

nummer = int(input(\"Ge ett tal: \"))\n\nwhile nummer < 100 and nummer % 5 != 0:\n    print(nummer)\n    nummer += 3

Här följer två exempel på utskriften från programmet:

Ge ett tal: 28\n28\n31\n34\n37

Ge ett tal: 96\n96\n99

När man ger programmet värdet 28 kommer loopen att avslutas med numret 37, eftersom nästa siffra är 40 – och är dividerbart med fem. När man ger värdet 96 kommer loopen att avsluta med numret 99 eftersom nästa siffra är 102 – som inte är mindre än 100.

När du skriver en loop är det viktigt att se till att loopen alltid kommer att avslutas vid något skede. Det här programmet avslutas – eller inte – beroende på det värde som ges:

nummer = int(input(\"Ge ett tal: \"))\n\nwhile nummer != 10:\n    print(nummer)\n    nummer += 2

Om man ger ett jämnt tal som är lika med tio eller mindre, kommer loopen att avslutas:

Ge ett tal: 4\n4\n6\n8

I övriga fall kommer loopen att fortsätta oändligt eftersom det inte då finns något sätt för variabeln att vara lika med tio. Till exempel tre och tolv är värden som skulle förorsaka en oändlig loop.

Skapa ett program som skriver ut alla tal under den siffra som användaren angett. Börja från ett.

Fram till siffran: 5\n1\n2\n3\n4

Använd inte True som villkor i while-loopen!

Tips för debuggning

Föreställ att du håller på att skapa ett lite mera komplicerat program, som det i den följande uppgiften – Potenser av två. Så här skulle man kunna starta:

stanna = int(input(\"Fram till siffran\"))\nnummer = 1\nwhile nummer == stanna:\n   # kod

Nu börjar programmet med att läsa in den data användaren ger och fortsätter till en loop med ett villkor.

Det är sannolikt att koden inte kommer att fungera på önskat sätt från början. Den kan behöva testas tio- eller till och med hundratals gånger före den fungerar korrekt.

Den här kodsnutten frågar alltid efter indata från användaren vilket gör testandet långsamt och arbetsdrygt. Varje gång programmet testas måste ett värde anges.

Ett sätt att bli av med problemet är att hårdkoda ett värde i koden medan den testas:

# vi hårdkodar värdet här tills vidare\nstanna = 8 # int(input(\"Fram till siffran\"))\nnummer = 1\nwhile nummer == stanna:\n   # kod

När programmet fungerar med det hårdkodade värdet, kan man enkelt testa med andra hårdkodade värden. När allt fungerar korrekt kan man testa på programmet så att användaren anger värdet.

Det här tricket fungerar väl med flera av de tester som används i betygsättningen av den här kursens uppgifter. Om testet berättar att något är fel med till exempel värdet 42 så kan värdet tillfälligt hårdkodas i programmet medan du letar efter buggen:

# testet meddelade att koden inte fungerade korrekt då indatat är 42\nstanna = 42 # int(input(\"Fram till siffran\"))\nnummer = 1\nwhile nummer == stanna:\n   # kod

Debuggning med hjälp av print-satsen nämndes några gånger under förra modulen i den här kursen. De program som du skapar kommer att bli mer invecklade i och med att kursen framskrider. Då kommer mängden debuggning som du behöver göra också antagligen att öka i samma proportion. Vanliga orsaker till buggar finns ofta i de villkor som avslutar loopar – de fungerar eventuellt korrekt för vissa värden, medan andra värden orsakar problem. Alltid är det inte heller lätt att observera det här.

Därför är det nu dags att använda dig av print-satser för att debugga – om du inte redan har gjort det. Du hittar instruktioner i den första och fjärde delen av den föregående modulen.

Vid sidan om print-satser finns även andra verktyg som kan använda för debuggning. Ett av dem är visualiseringsverktygetPython Tutor -webbsidan. Verktyget låter dig köra din kod rad för rad och visar också de värden som är lagrade i variabler vid varje steg.

Koden – med några problem – från den förra delen visualiseras med Python Tutor i följande bild:

\n \n

Efter den här delen

    \n
  • kan du skapa en loop med ett villkor
  • \n
  • vet du vad vilka roller initiering, villkoret och att uppdatering av variabler har i en loop
  • \n
  • kan du skapa loopar med olika typer av villkor.
  • \n

Osallistu tutkimukseen - vastaa kyselyyn

Alla olevan linkin takaa löytyy Hannu Pesosen väitöskirjatutkimukseen liittyvä kysely. Kurssin suorittaminen ei edellytä kyselyyn vastaamista, mutta yliopistoissa tutkimus on luonnollisesti tärkeässä osassa myös opetuksen kehittämisessä.

Jos sinulta siis löytyy hetki aikaa, niin vastaa kyselyyn.

Kyselyyn ei ole pakko antaa henkilötietoja (esimerkiksi nimeä), vaikka niitä ensimmäisellä sivulla kysytäänkin.

Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen.

En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida.

Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter.

En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad.

I den förra delen bekantade vi oss med while True -loopen som ett medel att upprepa delar av kod. Så som loopen är uppbyggd är villkoret alltid True, alltså sant. Vi måste då avsluta loopen manuellt vid något skede för att undvika en oändlig loop. Exempelvis:

# skriv ut siffror tills värdet på variabeln a är 5\na = 1\nwhile True:\n    print(a)\n    a += 1\n    if a == 5:\n        break

1\n2\n3\n4

Men förstås behöver villkoret inte alltid vara True, utan det kan vara vilket som helst Boolean-uttryck. while-satsens struktur ser ut så här:

while <villkor>:\n    <block>

Idén är att koden körs om och om igen – villkoret kollas för varje iteration. Om villkoret vid något skede inte är sant kommer programmet att fortsätta med koden som kommer efter while-blocket.

\n \n \n \n \n \n \"3\n \n \n

Om någon av de här tre komponenterna fattas kommer loopen antagligen inte att fungera korrekt. Ett vanligt misstag är att låta bli att uppdatera variabler:

nummer = 1\n\nwhile nummer < 10:\n    print(nummer)\n\nprint(\"Programmet är klart.\")

Här kommer värdet på variabeln nummer aldrig att ändras. Programmet är fast i en oändlig loop. Samma kod upprepas tills användaren avslutar programmet, till exempel med tangentkombinationen Control + C:

1\n1\n1\n1\n1\n(det här fortsätter i all oändlighet...)

Skapa ett program som i en loop skriver ut varannat tal från två till trettio. Varje tal skrivs ut på en ny rad.

Så här ser utskriften ut:

\n2\n4\n6\n8\no.s.v. ...\n

Korrigera uppgiftens program...

print(\"Klara?\")\nnummer = int(input(\"Ge ett tal: \"))\nwhile nummer = 0:\nprint(nummer)\nprint(\"Gå!\")

...så att det fungerar på följande sätt:

Klara?\nGe ett tal: 5\n5\n4\n3\n2\n1\nGå!

Använd inte en while True -loop!

Skriva villkor

Alla Boolean-uttryck och kombinationer av dem kan användas som villkor i en loop. Till exempel följande program skriver ut var tredje nummer förutsatt att det är mindre än 100 och inte dividerbart med fem:

nummer = int(input(\"Ge ett tal: \"))\n\nwhile nummer < 100 and nummer % 5 != 0:\n    print(nummer)\n    nummer += 3

Här följer två exempel på utskriften från programmet:

Ge ett tal: 28\n28\n31\n34\n37

Ge ett tal: 96\n96\n99

När man ger programmet värdet 28 kommer loopen att avslutas med numret 37, eftersom nästa siffra är 40 – och är dividerbart med fem. När man ger värdet 96 kommer loopen att avsluta med numret 99 eftersom nästa siffra är 102 – som inte är mindre än 100.

När du skriver en loop är det viktigt att se till att loopen alltid kommer att avslutas vid något skede. Det här programmet avslutas – eller inte – beroende på det värde som ges:

nummer = int(input(\"Ge ett tal: \"))\n\nwhile nummer != 10:\n    print(nummer)\n    nummer += 2

Om man ger ett jämnt tal som är lika med tio eller mindre, kommer loopen att avslutas:

Ge ett tal: 4\n4\n6\n8

I övriga fall kommer loopen att fortsätta oändligt eftersom det inte då finns något sätt för variabeln att vara lika med tio. Till exempel tre och tolv är värden som skulle förorsaka en oändlig loop.

Skapa ett program som skriver ut alla tal under den siffra som användaren angett. Börja från ett.

Fram till siffran: 5\n1\n2\n3\n4

Använd inte True som villkor i while-loopen!

Tips för debuggning

Föreställ att du håller på att skapa ett lite mera komplicerat program, som det i den följande uppgiften – Potenser av två. Så här skulle man kunna starta:

stanna = int(input(\"Fram till siffran\"))\nnummer = 1\nwhile nummer == stanna:\n   # kod

Nu börjar programmet med att läsa in den data användaren ger och fortsätter till en loop med ett villkor.

Det är sannolikt att koden inte kommer att fungera på önskat sätt från början. Den kan behöva testas tio- eller till och med hundratals gånger före den fungerar korrekt.

Den här kodsnutten frågar alltid efter indata från användaren vilket gör testandet långsamt och arbetsdrygt. Varje gång programmet testas måste ett värde anges.

Ett sätt att bli av med problemet är att hårdkoda ett värde i koden medan den testas:

# vi hårdkodar värdet här tills vidare\nstanna = 8 # int(input(\"Fram till siffran\"))\nnummer = 1\nwhile nummer == stanna:\n   # kod

När programmet fungerar med det hårdkodade värdet, kan man enkelt testa med andra hårdkodade värden. När allt fungerar korrekt kan man testa på programmet så att användaren anger värdet.

Det här tricket fungerar väl med flera av de tester som används i betygsättningen av den här kursens uppgifter. Om testet berättar att något är fel med till exempel värdet 42 så kan värdet tillfälligt hårdkodas i programmet medan du letar efter buggen:

# testet meddelade att koden inte fungerade korrekt då indatat är 42\nstanna = 42 # int(input(\"Fram till siffran\"))\nnummer = 1\nwhile nummer == stanna:\n   # kod

Debuggning med hjälp av print-satsen nämndes några gånger under förra modulen i den här kursen. De program som du skapar kommer att bli mer invecklade i och med att kursen framskrider. Då kommer mängden debuggning som du behöver göra också antagligen att öka i samma proportion. Vanliga orsaker till buggar finns ofta i de villkor som avslutar loopar – de fungerar eventuellt korrekt för vissa värden, medan andra värden orsakar problem. Alltid är det inte heller lätt att observera det här.

Därför är det nu dags att använda dig av print-satser för att debugga – om du inte redan har gjort det. Du hittar instruktioner i den första och fjärde delen av den föregående modulen.

Vid sidan om print-satser finns även andra verktyg som kan använda för debuggning. Ett av dem är visualiseringsverktygetPython Tutor -webbsidan. Verktyget låter dig köra din kod rad för rad och visar också de värden som är lagrade i variabler vid varje steg.

Koden – med några problem – från den förra delen visualiseras med Python Tutor i följande bild:

\n \n ="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Hittades på platsen "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"plats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittades inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ed"}]},{"type":"text","value":"\nHittades på platsen 1\nVad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":"\nHittades inte\nVad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ampa"}]},{"type":"text","value":"\nHittades på platsen 5\n..."}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Metoder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi använde ovan metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" hos strängar. Metoder fungerar ganska lika jämfört med funktioner som vi såg på i den föregående modulen. Skillnaden mellan dem är att metoder alltid är kopplade till det objekt som de kallas på. Objektet är den entitet som namnges före metoden som kallas. I det här fallet är objektet strängen där metoden söker efter delsträngen som ges som argument till metoden."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Hitta första delsträngen","tmcname":"osa03-13c_forsta_delstrangen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng samt ett tecken. Programmet ska skriva ut den första delsträngen på tre tecken som börjar med det tecknet användaren angett. Du kan anta att strängen består av minst tre tecken."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"algeriet"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nalg"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"insändare"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"\nnsä"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"vindpust"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"x"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Hitta alla delsträngar","tmcname":"osa03-14_alla_delstrangar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utveckla ditt föregående program, så att programmet skriver ut varje delsträng på tre tecken och som börjar med det angivna tecknet. Du kan anta att den angivna strängen består av minst tre tecken."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ljudlös"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nlju\nlös"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nemi"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"\nnem"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Följande exempel kan ge dig inspiration för uppgiften."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"solgass"}]},{"type":"text","value":"\nsolgass\nolgass\nlgass\ngass\nass\nss\ns"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Andra förekomsten","tmcname":"osa03-15_andra_forekomsten"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som söker efter det andra av en given delsträng i en sträng. Om delsträngen inte hittas två gånger eller ens en gång, ska programmet meddela om detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här kontexten kan delsträngar inte vara överlappande. I strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aaaa"}]},{"type":"text","value":" hittas delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aa"}]},{"type":"text","value":" för den andra gången vid index 2."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"abcabc"}]},{"type":"text","value":"\nAnge delsträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ab"}]},{"type":"text","value":"\nDelsträngens andra förekomst är vid index 3."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"laserpenna"}]},{"type":"text","value":"\nAnge delsträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nDelsträngens andra förekomst är vid index 9."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dynga"}]},{"type":"text","value":"\nAnge delsträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dy"}]},{"type":"text","value":"\nDelsträngen förekommer inte två gånger."}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du använda operatorerna + och * med strängar
  • \n
  • vet du hur man tar reda på längden hos en sträng
  • \n
  • vet du vad strängindexering betyder
  • \n
  • kan du hitta en delsträng i en sträng.
  • \n

Strängoperationer

Strängar kan kombineras med +-operatorn:

start = \"exe\"\nslut = \"mpel\"\nord = start+slut\nprint(ord)

exempel

Operatorn * kan också användas med strängar då den andra operanden är ett heltal. Strängoperanden kommer då att upprepas det antal gånger som specificeras av heltalet. Till exempel följande skulle fungera:

ord = \"pegas\"\nprint(ord*3)

pegaspegaspegas

Med hjälp av att kombinera strängoperationer med en loop kan vi skriva ett program som ritar en pyramid:

n = 10 # våningar i pyramiden\nrad = \"*\"\n\nwhile n > 0:\n    print(\" \" * n + rad)\n    rad += \"**\"\n    n -= 1

Så här ser utskriften ut:

          *\n         ***\n        *****\n       *******\n      *********\n     ***********\n    *************\n   ***************\n  *****************\n *******************

Instruktionen print inom loopen skriver ut en rad som börjar med n mellanslag följt av värdet som är lagrat i variabeln rad. Sedan läggs två stjärnor till i slutet av variabeln rad och variabeln n:s värde subtraheras med ett.

Skapa ett program som ber användaren ange en sträng samt en siffra. Programmet ska sedan skriva ut strängen så många gången som siffran anger, enligt följande exempel:

Ange sträng: origami\nAnge antal: 2\norigamiorigami

Längden på en sträng och dess index

Funktionen len returnerar antalet tecken i en sträng som ett heltal. Till exempel len(\"hej\") returnerar 3 eftersom det finns tre tecken i strängen hej.

Följande program ber användaren att ge en sträng och skriver sedan ut ned med \"understrykning\". Programmet skriver alltså ut en andra rad som innehåller så många streck (-) som det finns tecken i den givna strängen:

strang = input(\"Ange sträng: \")\nprint(strang)\nprint(\"-\"*len(strang))

Ange sträng: Hejsan svejsan!

\nHejsan svejsan!\n---------------\n

Strängens längd innehåller alla tecken i strängen – också mellanslag. Till exempel längden på strängen hej igen är 8.

Skapa ett program som ber användaren ange två strängar. Programmet ska skriva ut den längre av de angivna strängarna. Om båda är lika långa skrivs meddelandet \"Strängarna är lika långa\" ut.

Exempel:

Ange sträng 1: nja\nAnge sträng 2: betongsäck\nbetongsäck är längre

Ange sträng 1: sommartider hej hej\nAnge sträng 2: sol, vind o vatten\nsommartider hej hej är längre

Ange sträng 1: glasgolv\nAnge sträng 2: vildsvin\nSträngarna är lika långa

Eftersom strängar i grunden är sekvenser av tecken kan också vilken som helst specifik bokstav i en sträng hämtas. Operatorn [] hittar tecknet vid ett specifikt index som ges mellan parenteserna.

Indexet syftar till ett ställe i strängen och börjar från talet noll. Det första tecknet i en sträng har indexet 0 medan nästa tecken har indexet 1 och så vidare.

\n \n

I Python är intervallet [a:b] i halvöppet då vi behandlar strängar. Det här innebär att tecknet i det inledande indexet a inkluderas i intervallet medan tecknet i det avslutande indexet b lämnas bort. Varför så?

Det finns ingen tydlig orsak till detta. Det är helt enkelt en vana som har sitt ursprung i andra programmeringsspråk.

Halvöppna intervall kan kännas jobbiga men i praktiken har de sina nyttiga sidor. Du kan till exempel enkelt räkna längden på en delsträng med b - a. Å andra sidan måste du komma ihåg att tecknet i slutet vid indexet b inte kommer att inkluderas i delsträngen.

Skapa ett program som ber användaren ange en sträng. Därefter skriver programmet ut alla delsträngar börjande med det första tecket, i storleksordning.

Exempel:

Ange sträng: test\nt\nte\ntes\ntest

Skapa ett program som ber användaren om en sträng. Därefter skriver man ut alla delsträngar som slutar med det sista tecknet, i storleksordning.

Exempel:

Ange sträng: test\nt\nst\nest\ntest

Söka efter delsträngar

Operatorn in berättar oss om en sträng innehåller en specifik delsträng. Boolean-uttrycket a in b är sant om b innehåller delsträngen a.

Till exempel den här kodsnutten…

strang = \"test\"\n\nprint(\"t\" in strang)\nprint(\"x\" in strang)\nprint(\"est\" in strang)\nprint(\"ets\" in strang)

…skriver ut följande:

True\nFalse\nTrue\nFalse

Programmet nedan låter användaren söka efter delsträngar i en sträng som är hårdkodad i programmet:

strang = \"eu-val\"\n\nwhile True:\n    delstrang = input(\"Vad söker du efter? \")\n    if delstrang in strang:\n        print(\"Hittades\")\n    else:\n        print(\"Hittades inte\")

Vad söker du efter? eu\nHittades\nVad söker du efter? euval\nHittades inte\nVad söker du efter? u-v\nHittades\n...

Skapa ett program som ber användaren ange en sträng. Programmet ska därefter meddela om vokalerna a, e eller o hittas i strängen.

Du kan anta att strängen är angiven i gemener.

Exempel:

Ange sträng: hej på dig\na hittas inte\ne hittas\no hittas inte

Ange sträng: morjens\na hittas inte\ne hittas\no hittas

Operatorn in returnerar ett Boolean-värde. Det berättar oss alltså bara att en delsträng existerar i en sträng, men baserat på den informationen vet vi inte var delsträngen befinner sig. Däremot kan metoden find hos strängar användas för det här syftet. Som argument ger man delsträngen som söks efter. Tillbaka får vi ett värde som indikerar det första indexet där delsträngen hittades – eller -1 om delsträngen inte hittas i strängen.

Så här fungerar det:

\n \n \n \n \n \n \"3\n \n \n

Några exempel där vi använder find:

strang = \"test\"\n\nprint(strang.find(\"t\"))\nprint(strang.find(\"x\"))\nprint(strang.find(\"est\"))\nprint(strang.find(\"ets\"))

0\n-1\n1\n-1

Ovanstående delsträngsexempel gjort med find:

strang = \"led-lampa\"\n\nwhile True:\n    delstrang = input(\"Vad söker du efter? \")\n    plats = strang.find(delstrang)\n    if plats >= 0:\n        print(f\"Hittades på platsen {plats}\")\n    else:\n        print(\"Hittades inte\")

Vad söker du efter? ed\nHittades på platsen 1\nVad söker du efter? abc\nHittades inte\nVad söker du efter? ampa\nHittades på platsen 5\n...

Vi använde ovan metoden find hos strängar. Metoder fungerar ganska lika jämfört med funktioner som vi såg på i den föregående modulen. Skillnaden mellan dem är att metoder alltid är kopplade till det objekt som de kallas på. Objektet är den entitet som namnges före metoden som kallas. I det här fallet är objektet strängen där metoden söker efter delsträngen som ges som argument till metoden.

Skapa ett program som ber användaren ange en sträng samt ett tecken. Programmet ska skriva ut den första delsträngen på tre tecken som börjar med det tecknet användaren angett. Du kan anta att strängen består av minst tre tecken.

Ord: algeriet\nTecken: a\nalg

Ord: insändare\nTecken: n\nnsä

Ord: vindpust\nTecken: x

Ord: python\nTecken: n

Utveckla ditt föregående program, så att programmet skriver ut varje delsträng på tre tecken och som börjar med det angivna tecknet. Du kan anta att den angivna strängen består av minst tre tecken.

Ord: ljudlös\nTecken: l\nlju\nlös

Ord: nemi\nTecken: n\nnem

Tips: Följande exempel kan ge dig inspiration för uppgiften.

ord = input(\"Ord: \")\nwhile True:\n    if len(ord) == 0:\n        break\n    print(ord)\n    ord = ord[2:]

Ord: solgass\nsolgass\nolgass\nlgass\ngass\nass\nss\ns

Skapa ett program som söker efter det andra av en given delsträng i en sträng. Om delsträngen inte hittas två gånger eller ens en gång, ska programmet meddela om detta.

I den här kontexten kan delsträngar inte vara överlappande. I strängen aaaa hittas delsträngen aa för den andra gången vid index 2.

Exempel:

Ange sträng: abcabc\nAnge delsträng: ab\nDelsträngens andra förekomst är vid index 3.

Ange sträng: laserpenna\nAnge delsträng: a\nDelsträngens andra förekomst är vid index 9.

Ange sträng: dynga\nAnge delsträng: dy\nDelsträngen förekommer inte två gånger.

","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-3/2-behandla-strangar.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda operatorerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" med strängar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man tar reda på längden hos en sträng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad strängindexering betyder"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du hitta en delsträng i en sträng."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"strangoperationer","style":"position:relative;"},"children":[{"type":"text","value":"Strängoperationer"},{"type":"element","tagName":"a","properties":{"href":"#strangoperationer","ariaLabel":"strangoperationer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar kan kombineras med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-operatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"start "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exe\""}]},{"type":"text","value":"\nslut "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mpel\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" start"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"slut\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"exempel"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" kan också användas med strängar då den andra operanden är ett heltal. Strängoperanden kommer då att upprepas det antal gånger som specificeras av heltalet. Till exempel följande skulle fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pegas\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"pegaspegaspegas"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med hjälp av att kombinera strängoperationer med en loop kan vi skriva ett program som ritar en pyramid:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# våningar i pyramiden"}]},{"type":"text","value":"\nrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"**\""}]},{"type":"text","value":"\n n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ser utskriften ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":" *\n ***\n *****\n *******\n *********\n ***********\n *************\n ***************\n *****************\n *******************"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" inom loopen skriver ut en rad som börjar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" mellanslag följt av värdet som är lagrat i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rad"}]},{"type":"text","value":". Sedan läggs två stjärnor till i slutet av variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rad"}]},{"type":"text","value":" och variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":":s värde subtraheras med ett."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Flera strängar","tmcname":"osa03-05a_flera_strangar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng samt en siffra. Programmet ska sedan skriva ut strängen så många gången som siffran anger, enligt följande exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"origami"}]},{"type":"text","value":"\nAnge antal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\norigamiorigami"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"langden-pa-en-strang-och-dess-index","style":"position:relative;"},"children":[{"type":"text","value":"Längden på en sträng och dess index"},{"type":"element","tagName":"a","properties":{"href":"#langden-pa-en-strang-och-dess-index","ariaLabel":"langden pa en strang och dess index permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" returnerar antalet tecken i en sträng som ett heltal. Till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(\"hej\")"}]},{"type":"text","value":" returnerar 3 eftersom det finns tre tecken i strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hej"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program ber användaren att ge en sträng och skriver sedan ut ned med \"understrykning\". Programmet skriver alltså ut en andra rad som innehåller så många streck ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":") som det finns tecken i den givna strängen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hejsan svejsan!"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Hejsan svejsan!\n---------------\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängens längd innehåller alla tecken i strängen – också mellanslag. Till exempel längden på strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hej igen"}]},{"type":"text","value":" är 8."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Den längre strängen","tmcname":"osa03-05b_langre_strangen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange två strängar. Programmet ska skriva ut den längre av de angivna strängarna. Om båda är lika långa skrivs meddelandet \"Strängarna är lika långa\" ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nja"}]},{"type":"text","value":"\nAnge sträng 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"betongsäck"}]},{"type":"text","value":"\nbetongsäck är längre"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sommartider hej hej"}]},{"type":"text","value":"\nAnge sträng 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sol, vind o vatten"}]},{"type":"text","value":"\nsommartider hej hej är längre"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"glasgolv"}]},{"type":"text","value":"\nAnge sträng 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"vildsvin"}]},{"type":"text","value":"\nSträngarna är lika långa"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom strängar i grunden är sekvenser av tecken kan också vilken som helst specifik bokstav i en sträng hämtas. Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" hittar tecknet vid ett specifikt index som ges mellan parenteserna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Indexet syftar till ett ställe i strängen och börjar från talet noll. Det första tecknet i en sträng har indexet 0 medan nästa tecken har indexet 1 och så vidare."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 324px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e116e9f21180d2fb703bd8819378c8ad/67580/3_2_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 30.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAABB0lEQVQY021RCW6DMBDk/38jqBym4CpgjpSGBExtLnNMZaO0qsRIa69m5dnR2Op7iWuSIGMMURThfq+xLAuKosB7SBBFMXzfQ8pSM9PY9/20NKx5nvEtBKQUuJUlmqY1Qy1a13cjkucZ2pYb7iV4Bs1b+hiHAUJKKLX8Dqdp+vd43bY/ft+hlMKyKKzraha9llnjOCAkAWzbxjVJDanmGb4fQAiBqqrQ8RYpY+CcI8tzNM0TjGV4PGp8Vl+4lQXyvDgEz6xrEcdx8EEpSBgiDAn8gICQAK7rwfM82JcLKI3x5rqglCKOY+PeOgtW56rd6Q+TUppbR3L0PbquA+860wtx8Lq2bcMPpH/Jsen448cAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e116e9f21180d2fb703bd8819378c8ad/a0b58/3_2_1.webp 230w","/static/e116e9f21180d2fb703bd8819378c8ad/4e123/3_2_1.webp 324w"],"sizes":"(max-width: 324px) 100vw, 324px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e116e9f21180d2fb703bd8819378c8ad/81c8e/3_2_1.png 230w","/static/e116e9f21180d2fb703bd8819378c8ad/67580/3_2_1.png 324w"],"sizes":"(max-width: 324px) 100vw, 324px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e116e9f21180d2fb703bd8819378c8ad/67580/3_2_1.png","alt":"3 2 1","title":"3 2 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skulle skriva ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pegas"}]},{"type":"text","value":"\np\ne\na"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom det första tecknet i en sträng har indexet noll, har det sista tecknet indexet längd ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":") - 1. Följande program skriver ut det första och sista tecknet i en sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Första: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test!"}]},{"type":"text","value":"\nFörsta: t\nSista: !"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet går igenom varje tecken i en sträng – från det första till det sista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nplats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" plats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"plats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n plats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test"}]},{"type":"text","value":"\nt\ne\ns\nt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan också använda negativa index för att komma åt tecknen i en sträng från börjande från det sista tecknet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det sista tecknet i en sträng har alltså indexet -1, det näst sista indexet -2 och så vidare. Du kan tänka att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str[-1]"}]},{"type":"text","value":" är en genväg för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str[len(str) – 1]"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 324px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d5705c6b55a5f47c3372f8565bccdff6/67580/3_2_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 30.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA9UlEQVQY03VQDU+DMBTk//80EqeCttJSijC3lTKtQFapgTMtjhAzL3m9S/t67yOy9oKikHjb78EYR9u2cM7heDhA5Dk456CUQpavUKqBxzzPN8MjcuOID2PQ9x2qqoLWGtM8YxxHNEoFYykltF4KXQ3/48gf387hYi2macIVW71g+eRNvfKNYMu/xaIva/FCKeI4BuN8fazrGkopCCFwPJ1Qhi41CpGj0S1KWaA9vyPnPEzo84MhbsAb7XZ3yIUAIRScZUjSFISQwGmShHvy/IT7h0dwxpBlGay1y8h/F+v313UdhmFYue/7NYwxMOYz6G2eX9MPf7zJPqGusD4AAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d5705c6b55a5f47c3372f8565bccdff6/a0b58/3_2_2.webp 230w","/static/d5705c6b55a5f47c3372f8565bccdff6/4e123/3_2_2.webp 324w"],"sizes":"(max-width: 324px) 100vw, 324px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d5705c6b55a5f47c3372f8565bccdff6/81c8e/3_2_2.png 230w","/static/d5705c6b55a5f47c3372f8565bccdff6/67580/3_2_2.png 324w"],"sizes":"(max-width: 324px) 100vw, 324px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d5705c6b55a5f47c3372f8565bccdff6/67580/3_2_2.png","alt":"3 2 2","title":"3 2 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovanstående exempel kan förenklas med negativ indexering."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Första: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test!"}]},{"type":"text","value":"\nFörsta: t\nSista: !"}]}]},{"type":"element","tagName":"h2","properties":{"id":"indexerror-string-index-out-of-range","style":"position:relative;"},"children":[{"type":"text","value":"IndexError: string index out of range"},{"type":"element","tagName":"a","properties":{"href":"#indexerror-string-index-out-of-range","ariaLabel":"indexerror string index out of range permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du har testat på exemplen ovan har du kanske stött på felmeddelandet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IndexError"}]},{"type":"text","value":". Det här felet uppstår då du försöker använda ett index som inte finns i en sträng."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tionde tecknet: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"introkursen i programmering"}]},{"type":"text","value":"\nTionde tecknet: e"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nIndexError: string index out of range"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland orsakas indexeringsfel av en bugg i koden. Det är till exempel relativt vanligt att man försöker indexera för långt då man försöker komma åt det sista tecknet i en sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista tecknet: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom strängindexeringen börjar med noll kommer det sista tecknet att ha indexet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(str) - 1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns situationer där programmet borde förbereda sig för fel orsakade av indata från användaren:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Första tecknet: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Strängen är tom, alltså finns det inget första tecken\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan skulle en sträng med längden noll ha orsakat problem om programmeraren inte skulle ha lagt till en längdkontroll i koden. En sträng med längden noll är en tom sträng. I det här fallet kan vi orsaka en sådan med att helt enkelt trycka på tangenten Enter utan att skriva något annat."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Omvänt","tmcname":"osa03-05c_omvant"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng. Programmet ska skriva ut tecknen i omvänd ordning på varsin rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"geting"}]},{"type":"text","value":"\ng\nn\ni\nt\ne\ng"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Andra och näst sista","tmcname":"osa03-06_andra_nast_sista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ger användaren mata in ett ord. Programmet ska därefter berätta om ordets andra och näst sista bokstäver är de samma."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ett ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nDen andra och den näst sista bokstaven skiljer sig från varandra"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ett ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pascal"}]},{"type":"text","value":"\nDen andra och den näst sista bokstaven är a"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Speciellt streck","tmcname":"osa03-09_speciellt_streck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ritar ut ett streck bestående så många nummertecken ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":") som användaren anger."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bredd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"###\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bredd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"########\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fyrkant","tmcname":"osa03-10_fyrkant"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utveckla det föregående programmet så att användaren också kan definiera höjden på figuren."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bredd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nHöjd: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n##########\n##########\n##########"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Understrykning","tmcname":"osa03-11_understrykning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren om strängar. Programmet skriver ut strängarna med understrykning enligt exemplet. Programmet avslutas då användaren ger en tom sträng, dvs. en sträng med längden noll."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hejsan svejsan!"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Hejsan svejsan!\n---------------\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Clara steg upp på en tremetersstege"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Clara steg upp på en tremetersstege\n-----------------------------------\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a\n-\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng:"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Högerjusterat","tmcname":"osa03-12_hogerjusterat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng. Programmet ska sedan skriva ut strängen så att utskriften är exakt 20 tecken. Om strängen är kortare ska det finnas ett passligt antal asterisker ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":") i början av strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att strängen består av högst 20 tecken."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"**************python\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ganskalångsträng"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"****ganskalångsträng\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sssuperlångstränggg"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*sssuperlångstränggg\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Ordlåda","tmcname":"osa03-13_ordlada"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge ett ord. Programmet ska skriva ut ordet i mitten av en ram som består av asterisker. Bredden av ramen är 30 tecken. Du kan anta att ordet som anges ryms i ramen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om ordets längd är ojämnt, kan du skriva ut det på valfritt mittställe."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aj!"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"******************************\n* aj! *\n******************************\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"******************************\n* python *\n******************************\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"delstrangar","style":"position:relative;"},"children":[{"type":"text","value":"Delsträngar"},{"type":"element","tagName":"a","properties":{"href":"#delstrangar","ariaLabel":"delstrangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En delsträng av en sträng är en sekvens av tecken som formar en del av den ursprungliga delen. Till exempel strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exempel"}]},{"type":"text","value":" innehåller delsträngarna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exem"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mpe"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"el"}]},{"type":"text","value":" med flera. I Python kallas extrahering av delsträngar slicing."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vet vad det inledande och avslutande indexet för en delsträng är, kan du extrahera (slice) delsträngen med notationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[a:b]"}]},{"type":"text","value":". Då kommer delsträngen att börja vid index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och ta slut vid det sista tecknet före indexet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" – det vill säga inklusive den första bokstaven men exklusive den sista. Du kan tänka att indexen är streck ritade på den vänstra sidan av det indexerade tecknet – som vi illustrerar nedan:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 324px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/08007c903384a23f941bcd08cdb88343/67580/3_2_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 37.391304347826086%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABW0lEQVQoz3WRwW7aQBRF53+6yoaq60hdkf5QfyR/0P5CF92GKgkoBpIaiPF4YgV7jD0GBxsM5kQ2CVXS9kpP83RHc959GrHb7UiNYbVaEUUReZ5TlluybMk8jtFRxHxeV9x4r9rv9/xLoigKhoM+vu/T6XQYjcYslhlhMOPecbi5seheXWL1BwTBjE25aYa/Qv9U4xyAk8mYIAjo9XpYlsXjLMCdTtDzBGMM221JnMT86lzQ7V6TGPMCrN5Am4TvI6/X62NfVdXLQ96c/1MNFWW5QSmvSTZ13eNlGIYU65zlsoZ+R+sP7PmE1i2qqkUYttiWH0nMCUX+gyjKDwnrTzFJ0qysddSYWZYx6PeZuvdImeD7X7m9FTw8CIZDgfIEd78FnhTYtkDKc2z7kaenxd8r10rTFMdxUEoi3QBPfcNxTlHqC6NxG+m2sUdtPHnG1P2M7//EU5rFIuUZIHMCtnQB98YAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/08007c903384a23f941bcd08cdb88343/a0b58/3_2_3.webp 230w","/static/08007c903384a23f941bcd08cdb88343/4e123/3_2_3.webp 324w"],"sizes":"(max-width: 324px) 100vw, 324px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/08007c903384a23f941bcd08cdb88343/81c8e/3_2_3.png 230w","/static/08007c903384a23f941bcd08cdb88343/67580/3_2_3.png 324w"],"sizes":"(max-width: 324px) 100vw, 324px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/08007c903384a23f941bcd08cdb88343/67580/3_2_3.png","alt":"3 2 3","title":"3 2 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss kika lite mer på extraherade (sliced) strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"glassbil\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# om inledande index fattas, är det 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# om avslutande index fattas, är det lika med strängens längd"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"gla\nsbi\ngla\nsbil"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Halvöppna intervall"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python är intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[a:b]"}]},{"type":"text","value":" i halvöppet då vi behandlar strängar. Det här innebär att tecknet i det inledande indexet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" inkluderas i intervallet medan tecknet i det avslutande indexet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" lämnas bort. Varför så?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns ingen tydlig orsak till detta. Det är helt enkelt en vana som har sitt ursprung i andra programmeringsspråk."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Halvöppna intervall kan kännas jobbiga men i praktiken har de sina nyttiga sidor. Du kan till exempel enkelt räkna längden på en delsträng med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b - a"}]},{"type":"text","value":". Å andra sidan måste du komma ihåg att tecknet i slutet vid indexet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" inte kommer att inkluderas i delsträngen."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Delsträngar 1","tmcname":"osa03-07_delstrangar_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng. Därefter skriver programmet ut alla delsträngar börjande med det första tecket, i storleksordning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test"}]},{"type":"text","value":"\nt\nte\ntes\ntest"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Delsträngar 2","tmcname":"osa03-08_delstrangar_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren om en sträng. Därefter skriver man ut alla delsträngar som slutar med det sista tecknet, i storleksordning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test"}]},{"type":"text","value":"\nt\nst\nest\ntest"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"soka-efter-delstrangar","style":"position:relative;"},"children":[{"type":"text","value":"Söka efter delsträngar"},{"type":"element","tagName":"a","properties":{"href":"#soka-efter-delstrangar","ariaLabel":"soka efter delstrangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" berättar oss om en sträng innehåller en specifik delsträng. Boolean-uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a in b"}]},{"type":"text","value":" är sant om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" innehåller delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel den här kodsnutten…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"t\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"x\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"est\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ets\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skriver ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse\nTrue\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet nedan låter användaren söka efter delsträngar i en sträng som är hårdkodad i programmet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eu-val\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad söker du efter? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delstrang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittades\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittades inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eu"}]},{"type":"text","value":"\nHittades\nVad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"euval"}]},{"type":"text","value":"\nHittades inte\nVad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"u-v"}]},{"type":"text","value":"\nHittades\n..."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Hittas vokaler?","tmcname":"osa03-13b_vokaler"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng. Programmet ska därefter meddela om vokalerna a, e eller o hittas i strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att strängen är angiven i gemener."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hej på dig"}]},{"type":"text","value":"\na hittas inte\ne hittas\no hittas inte"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"morjens"}]},{"type":"text","value":"\na hittas inte\ne hittas\no hittas"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" returnerar ett Boolean-värde. Det berättar oss alltså bara att en delsträng existerar i en sträng, men baserat på den informationen vet vi inte var delsträngen befinner sig. Däremot kan metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" hos strängar användas för det här syftet. Som argument ger man delsträngen som söks efter. Tillbaka får vi ett värde som indikerar det första indexet där delsträngen hittades – eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":" om delsträngen inte hittas i strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här fungerar det:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 227px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b2812ef85c8e7deabfe929f68d397de5/9b5be/3_2_4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 62.11453744493392%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsSAAALEgHS3X78AAABw0lEQVQoz31Ta3OiQBDk//8iP1zKOjwpfMWyBIzIBSQIiDEX5b2B7dQsmEDuLlM1xcxub+9szyDhq3HeuAibb1mWeHJd7B0H7n4Px3EQ+L6I0zT9xHIOiYKe02YL4O0dRVlicX8PRVUxn88hyzKMhwf8lGVEp1ND2GL/Juw40hT89RWIY6AoAMbEGpKkyfNcxPxyAY9jcUbCd0YEnPflqCrUb2+9NU45eV1DusYxbMfBk+fBDwKYu53Ifd9HludYr9cIggA7y4JuGAITtDhN05o8DBFGUfPky/MzPNvGiTY8D79NE6amYbVYoGQMURTB8zzYtg3LsvDnfBbrdIFpWQiOR/C6xjEMG0IqU2iRZcD5TC1tNKL1fxlpd702Z0jbPAd/efnACw0pZFXV6xZjDFVVfTSormvhtM+KAmWagsUxSiLujJmUJAkURcFwOMRkMoFpmuI7Go0wGAxgGEZvJumiX+MxftzdYawoYoQIf5tHiQJd17HZbLDdbjGbzbBarXA4HASooCd+IaRm6JoG+/ER0+kUqqp+EmZZJqogEurscrkUwxu2It/IuoRUAP0truuKKaA8p6ffNPyfdYm6VX6HfwfDLJMCIdxKRgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b2812ef85c8e7deabfe929f68d397de5/7d72c/3_2_4.webp 227w"],"sizes":"(max-width: 227px) 100vw, 227px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b2812ef85c8e7deabfe929f68d397de5/9b5be/3_2_4.png 227w"],"sizes":"(max-width: 227px) 100vw, 227px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b2812ef85c8e7deabfe929f68d397de5/9b5be/3_2_4.png","alt":"3 2 4","title":"3 2 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Några exempel där vi använder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"t\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"x\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"est\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ets\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n-1\n1\n-1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovanstående delsträngsexempel gjort med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"led-lampa\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad söker du efter? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n plats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delstrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" plats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Hittades på platsen "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"plats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittades inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ed"}]},{"type":"text","value":"\nHittades på platsen 1\nVad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":"\nHittades inte\nVad söker du efter? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ampa"}]},{"type":"text","value":"\nHittades på platsen 5\n..."}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Metoder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi använde ovan metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" hos strängar. Metoder fungerar ganska lika jämfört med funktioner som vi såg på i den föregående modulen. Skillnaden mellan dem är att metoder alltid är kopplade till det objekt som de kallas på. Objektet är den entitet som namnges före metoden som kallas. I det här fallet är objektet strängen där metoden söker efter delsträngen som ges som argument till metoden."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Hitta första delsträngen","tmcname":"osa03-13c_forsta_delstrangen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng samt ett tecken. Programmet ska skriva ut den första delsträngen på tre tecken som börjar med det tecknet användaren angett. Du kan anta att strängen består av minst tre tecken."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"algeriet"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nalg"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"insändare"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"\nnsä"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"vindpust"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"x"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Hitta alla delsträngar","tmcname":"osa03-14_alla_delstrangar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utveckla ditt föregående program, så att programmet skriver ut varje delsträng på tre tecken och som börjar med det angivna tecknet. Du kan anta att den angivna strängen består av minst tre tecken."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ljudlös"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nlju\nlös"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nemi"}]},{"type":"text","value":"\nTecken: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"\nnem"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Följande exempel kan ge dig inspiration för uppgiften."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"solgass"}]},{"type":"text","value":"\nsolgass\nolgass\nlgass\ngass\nass\nss\ns"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Andra förekomsten","tmcname":"osa03-15_andra_forekomsten"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som söker efter det andra av en given delsträng i en sträng. Om delsträngen inte hittas två gånger eller ens en gång, ska programmet meddela om detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här kontexten kan delsträngar inte vara överlappande. I strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aaaa"}]},{"type":"text","value":" hittas delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aa"}]},{"type":"text","value":" för den andra gången vid index 2."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"abcabc"}]},{"type":"text","value":"\nAnge delsträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ab"}]},{"type":"text","value":"\nDelsträngens andra förekomst är vid index 3."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"laserpenna"}]},{"type":"text","value":"\nAnge delsträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nDelsträngens andra förekomst är vid index 9."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dynga"}]},{"type":"text","value":"\nAnge delsträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dy"}]},{"type":"text","value":"\nDelsträngen förekommer inte två gånger."}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du använda operatorerna + och * med strängar
  • \n
  • vet du hur man tar reda på längden hos en sträng
  • \n
  • vet du vad strängindexering betyder
  • \n
  • kan du hitta en delsträng i en sträng.
  • \n

Strängoperationer

Strängar kan kombineras med +-operatorn:

start = \"exe\"\nslut = \"mpel\"\nord = start+slut\nprint(ord)

exempel

Operatorn * kan också användas med strängar då den andra operanden är ett heltal. Strängoperanden kommer då att upprepas det antal gånger som specificeras av heltalet. Till exempel följande skulle fungera:

ord = \"pegas\"\nprint(ord*3)

pegaspegaspegas

Med hjälp av att kombinera strängoperationer med en loop kan vi skriva ett program som ritar en pyramid:

n = 10 # våningar i pyramiden\nrad = \"*\"\n\nwhile n > 0:\n    print(\" \" * n + rad)\n    rad += \"**\"\n    n -= 1

Så här ser utskriften ut:

          *\n         ***\n        *****\n       *******\n      *********\n     ***********\n    *************\n   ***************\n  *****************\n *******************

Instruktionen print inom loopen skriver ut en rad som börjar med n mellanslag följt av värdet som är lagrat i variabeln rad. Sedan läggs två stjärnor till i slutet av variabeln rad och variabeln n:s värde subtraheras med ett.

Skapa ett program som ber användaren ange en sträng samt en siffra. Programmet ska sedan skriva ut strängen så många gången som siffran anger, enligt följande exempel:

Ange sträng: origami\nAnge antal: 2\norigamiorigami

Längden på en sträng och dess index

Funktionen len returnerar antalet tecken i en sträng som ett heltal. Till exempel len(\"hej\") returnerar 3 eftersom det finns tre tecken i strängen hej.

Följande program ber användaren att ge en sträng och skriver sedan ut ned med \"understrykning\". Programmet skriver alltså ut en andra rad som innehåller så många streck (-) som det finns tecken i den givna strängen:

strang = input(\"Ange sträng: \")\nprint(strang)\nprint(\"-\"*len(strang))

Ange sträng: Hejsan svejsan!

\nHejsan svejsan!\n---------------\n

Strängens längd innehåller alla tecken i strängen – också mellanslag. Till exempel längden på strängen hej igen är 8.

Skapa ett program som ber användaren ange två strängar. Programmet ska skriva ut den längre av de angivna strängarna. Om båda är lika långa skrivs meddelandet \"Strängarna är lika långa\" ut.

Exempel:

Ange sträng 1: nja\nAnge sträng 2: betongsäck\nbetongsäck är längre

Ange sträng 1: sommartider hej hej\nAnge sträng 2: sol, vind o vatten\nsommartider hej hej är längre

Ange sträng 1: glasgolv\nAnge sträng 2: vildsvin\nSträngarna är lika långa

Eftersom strängar i grunden är sekvenser av tecken kan också vilken som helst specifik bokstav i en sträng hämtas. Operatorn [] hittar tecknet vid ett specifikt index som ges mellan parenteserna.

Indexet syftar till ett ställe i strängen och börjar från talet noll. Det första tecknet i en sträng har indexet 0 medan nästa tecken har indexet 1 och så vidare.

\n \n

I Python är intervallet [a:b] i halvöppet då vi behandlar strängar. Det här innebär att tecknet i det inledande indexet a inkluderas i intervallet medan tecknet i det avslutande indexet b lämnas bort. Varför så?

Det finns ingen tydlig orsak till detta. Det är helt enkelt en vana som har sitt ursprung i andra programmeringsspråk.

Halvöppna intervall kan kännas jobbiga men i praktiken har de sina nyttiga sidor. Du kan till exempel enkelt räkna längden på en delsträng med b - a. Å andra sidan måste du komma ihåg att tecknet i slutet vid indexet b inte kommer att inkluderas i delsträngen.

Skapa ett program som ber användaren ange en sträng. Därefter skriver programmet ut alla delsträngar börjande med det första tecket, i storleksordning.

Exempel:

Ange sträng: test\nt\nte\ntes\ntest

Skapa ett program som ber användaren om en sträng. Därefter skriver man ut alla delsträngar som slutar med det sista tecknet, i storleksordning.

Exempel:

Ange sträng: test\nt\nst\nest\ntest

Söka efter delsträngar

Operatorn in berättar oss om en sträng innehåller en specifik delsträng. Boolean-uttrycket a in b är sant om b innehåller delsträngen a.

Till exempel den här kodsnutten…

strang = \"test\"\n\nprint(\"t\" in strang)\nprint(\"x\" in strang)\nprint(\"est\" in strang)\nprint(\"ets\" in strang)

…skriver ut följande:

True\nFalse\nTrue\nFalse

Programmet nedan låter användaren söka efter delsträngar i en sträng som är hårdkodad i programmet:

strang = \"eu-val\"\n\nwhile True:\n    delstrang = input(\"Vad söker du efter? \")\n    if delstrang in strang:\n        print(\"Hittades\")\n    else:\n        print(\"Hittades inte\")

Vad söker du efter? eu\nHittades\nVad söker du efter? euval\nHittades inte\nVad söker du efter? u-v\nHittades\n...

Skapa ett program som ber användaren ange en sträng. Programmet ska därefter meddela om vokalerna a, e eller o hittas i strängen.

Du kan anta att strängen är angiven i gemener.

Exempel:

Ange sträng: hej på dig\na hittas inte\ne hittas\no hittas inte

Ange sträng: morjens\na hittas inte\ne hittas\no hittas

Operatorn in returnerar ett Boolean-värde. Det berättar oss alltså bara att en delsträng existerar i en sträng, men baserat på den informationen vet vi inte var delsträngen befinner sig. Däremot kan metoden find hos strängar användas för det här syftet. Som argument ger man delsträngen som söks efter. Tillbaka får vi ett värde som indikerar det första indexet där delsträngen hittades – eller -1 om delsträngen inte hittas i strängen.

Så här fungerar det:

\n \n \n \n \n \n \"3\n \n \n

Några exempel där vi använder find:

strang = \"test\"\n\nprint(strang.find(\"t\"))\nprint(strang.find(\"x\"))\nprint(strang.find(\"est\"))\nprint(strang.find(\"ets\"))

0\n-1\n1\n-1

Ovanstående delsträngsexempel gjort med find:

strang = \"led-lampa\"\n\nwhile True:\n    delstrang = input(\"Vad söker du efter? \")\n    plats = strang.find(delstrang)\n    if plats >= 0:\n        print(f\"Hittades på platsen {plats}\")\n    else:\n        print(\"Hittades inte\")

Vad söker du efter? ed\nHittades på platsen 1\nVad söker du efter? abc\nHittades inte\nVad söker du efter? ampa\nHittades på platsen 5\n...

Vi använde ovan metoden find hos strängar. Metoder fungerar ganska lika jämfört med funktioner som vi såg på i den föregående modulen. Skillnaden mellan dem är att metoder alltid är kopplade till det objekt som de kallas på. Objektet är den entitet som namnges före metoden som kallas. I det här fallet är objektet strängen där metoden söker efter delsträngen som ges som argument till metoden.

Skapa ett program som ber användaren ange en sträng samt ett tecken. Programmet ska skriva ut den första delsträngen på tre tecken som börjar med det tecknet användaren angett. Du kan anta att strängen består av minst tre tecken.

Ord: algeriet\nTecken: a\nalg

Ord: insändare\nTecken: n\nnsä

Ord: vindpust\nTecken: x

Ord: python\nTecken: n

Utveckla ditt föregående program, så att programmet skriver ut varje delsträng på tre tecken och som börjar med det angivna tecknet. Du kan anta att den angivna strängen består av minst tre tecken.

Ord: ljudlös\nTecken: l\nlju\nlös

Ord: nemi\nTecken: n\nnem

Tips: Följande exempel kan ge dig inspiration för uppgiften.

ord = input(\"Ord: \")\nwhile True:\n    if len(ord) == 0:\n        break\n    print(ord)\n    ord = ord[2:]

Ord: solgass\nsolgass\nolgass\nlgass\ngass\nass\nss\ns

Skapa ett program som söker efter det andra av en given delsträng i en sträng. Om delsträngen inte hittas två gånger eller ens en gång, ska programmet meddela om detta.

I den här kontexten kan delsträngar inte vara överlappande. I strängen aaaa hittas delsträngen aa för den andra gången vid index 2.

Exempel:

Ange sträng: abcabc\nAnge delsträng: ab\nDelsträngens andra förekomst är vid index 3.

Ange sträng: laserpenna\nAnge delsträng: a\nDelsträngens andra förekomst är vid index 9.

Ange sträng: dynga\nAnge delsträng: dy\nDelsträngen förekommer inte två gånger.

","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-3/2-behandla-strangar.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-3/3-mer-om-loopar/page-data.json b/page-data/osa-3/3-mer-om-loopar/page-data.json index bffa86721..17a6b441b 100644 --- a/page-data/osa-3/3-mer-om-loopar/page-data.json +++ b/page-data/osa-3/3-mer-om-loopar/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-3/3-mer-om-loopar", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen behövs för att avsluta en loop"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"-instruktionen för att fortsätta till nästa iteration"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"förstår du hur kapslade loopar fungerar."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"break-instruktionen","style":"position:relative;"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen"},{"type":"element","tagName":"a","properties":{"href":"#break-instruktionen","ariaLabel":"break instruktionen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du har redan bekantat dig med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen. Det kan användas för att direkt avsluta en loop. Ett exempel på ett användningsområde för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen är då man ber användaren om någon information och loopen ska avslutas när ett visst värde ges."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma funktionalitet kan skapas utan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen med ett passligt villkor. De här två programmen ber användaren ge siffor som adderas ihop tills användaren skriver siffran -1."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# version 1 med break-instruktion"}]},{"type":"text","value":"\n\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" siffra\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# version 2 utan break-instruktion"}]},{"type":"text","value":"\n\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" siffra\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Båda programmen skriver ut samma saker med samma indata, exempelvis:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSumman är 14"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Båda programmen är alltså identiska till deras funktion men den första metoden är ofta enklare eftersom villkoret "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra == 1"}]},{"type":"text","value":" endast finns på ett ställe och variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" behöver inte initieras utanför loopen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen kan kombineras med ett passligt villkor. Till exempel följande loop upprepas så länge summan av siffrorna är högst 100, men avslutas också då man ger siffran -1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" siffra\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSumman är 44"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"17"}]},{"type":"text","value":"\nSumman är 106"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det första exemplet avslutas loopen eftersom användaren ger siffran -1. I det andra exemplet avslutas loopen eftersom summan blir större än 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som alltid inom programmering finns det flera sätt att uppnå samma resultat. Följande program fungerar på identiskt sätt jämfört med de två exemplen ovan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" siffra\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"continue-instruktionen","style":"position:relative;"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"-instruktionen"},{"type":"element","tagName":"a","properties":{"href":"#continue-instruktionen","ariaLabel":"continue instruktionen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt att påverka hur en loop körs är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"-instruktionen. Det får loopen att hoppa till början, där villkoret för loopen finns. Loopen fortsätter köra normalt därifrån börjandes från att kolla villkoret:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 546px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d4a8870c64b23280ff582c9a97ac0bd1/76aed/3_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsSAAALEgHS3X78AAAB0ElEQVQ4y41U2VLCMBTt/3+Qb+rog8v45AyDIEhZCoXKVlqaljY9zrmQGCqjZibNzc3puWvi4TTqukae51Z210s6Yi+dexTMZrFY2EN3ujqtteyjKLKyi/WMBVrsdDo4HA5oDtcTDhJ1u10kSfID61VVheVyifl8Dt/3Rab17XYrM8syC55OpxiNRoIhIXGcm81GsDQgIe/3e6zXa8zDUA65p74oCgFy0HMSpWkqOsrEkoROcXJvQy418N77QOVER1AcxyKTnIbM8Ac+cpWfpYVYLy8OUOkGatFC6/EKxWcbdZWfCMszQnqVqQzT9QS3zzfoBm2oMjsn3MYJ8iRCNrxD1L6GGt2jLlMbpiFkIehhsk/QCl7x9PaAl/4TYnVMSaUrSZunlLpQ1ePKgvR6Pdsevw2mZzAYwDN50fqY2LrWtk3K8jvkZk8Sq09YM485PN2O3W4nFTQE5kfq3V6kt9SxugzR9V4I+RkOh9JXYRhKL3LPVLA92AouIb1maJzsyX6/L7mlERZNGptNTUI2LmVeQf5IUq5NwiAIBEcPZ7OZFI9YrrYPGe5qtbp4ndz8mWoTywia19JzFb89Dk3iPx8HHo7H438/X5PJxD4krqEvw5jbr+/6e4sAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d4a8870c64b23280ff582c9a97ac0bd1/a0b58/3_3.webp 230w","/static/d4a8870c64b23280ff582c9a97ac0bd1/bc10c/3_3.webp 460w","/static/d4a8870c64b23280ff582c9a97ac0bd1/a3666/3_3.webp 546w"],"sizes":"(max-width: 546px) 100vw, 546px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d4a8870c64b23280ff582c9a97ac0bd1/81c8e/3_3.png 230w","/static/d4a8870c64b23280ff582c9a97ac0bd1/08a84/3_3.png 460w","/static/d4a8870c64b23280ff582c9a97ac0bd1/76aed/3_3.png 546w"],"sizes":"(max-width: 546px) 100vw, 546px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d4a8870c64b23280ff582c9a97ac0bd1/76aed/3_3.png","alt":"3 3","title":"3 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel följande program adderar siffor som användaren ger, men bara då den givna siffran är mindre än tio. Om siffran är tio eller större, hoppar man till början av loopen utan att addera siffran."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal, -1 avslutar programmet: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" siffra\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"99"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe ett tal, -1 avslutar programmet: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSumman är 16"}]}]},{"type":"element","tagName":"h2","properties":{"id":"kapslade-loopar","style":"position:relative;"},"children":[{"type":"text","value":"Kapslade loopar"},{"type":"element","tagName":"a","properties":{"href":"#kapslade-loopar","ariaLabel":"kapslade loopar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Precis som med if-satser kan loopar placeras inom andra loopar. Till exempel följande program använder sig av en loop för att fråga efter en siffra från användaren. Inom den här loopen finns en annan loop som räknar ner från det givna talet till ett:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n3\n2\n1\nGe ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n3\n2\n1\nGe ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n6\n5\n4\n3\n2\n1\nGe ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I kapslade loopar är det bra att märka att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" endast påverkar den innersta loopen de är i. Föregående exemplet skulle kunna skrivas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här avslutar den andra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"-instruktionen endast den inre loopen som används för att skriva ut siffrorna."}]},{"type":"element","tagName":"h2","properties":{"id":"hjalpvariabler-med-loopar","style":"position:relative;"},"children":[{"type":"text","value":"Hjälpvariabler med loopar"},{"type":"element","tagName":"a","properties":{"href":"#hjalpvariabler-med-loopar","ariaLabel":"hjalpvariabler med loopar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan använt oss av hjälpvariabler – som ökar eller minskar för varje iteration av en loop – flera gånger, så följande program borde till sin struktur se ganska bekant ut. Programmet skriver ut alla jämna tal från noll fram till det tal som användaren angett:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"grans "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ni "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" grans"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n0\n2\n4\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" har tilldelats värdet 0 före loopen, som ökar på talet med två för varje iteration."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man använder kapslade loopar kan det uppstå ett behov för en ny hjälpvariabel för den inre loopen. Följande program skriver ut en \"sifferpyramid\" baserat på den siffra som användaren angett:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge ett tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" \""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n0 1 2 3 4\n0 1 2 3\n0 1 2\n0 1\n0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I programmet använder den yttre loopen hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" som minskar med ett tills det når till noll. Hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" tilldelas värdet 0 före man fortsätter till den inre loopen – varje gång den yttre loopen upprepas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den inre loopen använder sig av hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" som ökar med talet 1 för varje iteration av den inre loopen. Den inre loopen fortsätter tills "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" är lika med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":", och skriver ut varje värde hos "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" med mellanslag emellan. När loopen avslutas skapar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen i den yttre loopen en ny rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I och med att värdet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" minskar för varje iteration av den yttre loopen, kommer antalet iterationer hos den inre loopen att minska. Vid varje upprepning blir sifferraden kortare, vilket bildar \"pyramiden\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kapslade loopar kan vara svårtolkade på en första titt, men det är viktigt att förstå hur de fungerar. Du kan använda dig av Python Tutors "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyg"}]},{"type":"text","value":" för att bättre förstå hur ovanstående exempel fungerar. Kopiera koden ovan till kodfönstret och följ hur utskriften formar sig och hur hjälpvariablernas värden ändras medan programmet körs."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Multiplikationstabell","tmcname":"osa03-15b_multiplikationstabell"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber om ett positivt heltal från användare. Programmet ska skriva ut multiplikationsoperationer fram till talet, enligt exemplen nedan:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: 2\n1 x 1 = 1\n1 x 2 = 2\n2 x 1 = 2\n2 x 2 = 4"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: 3\n1 x 1 = 1\n1 x 2 = 2\n1 x 3 = 3\n2 x 1 = 2\n2 x 2 = 4\n2 x 3 = 6\n3 x 1 = 3\n3 x 2 = 6\n3 x 3 = 9"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Ordens första bokstäver","tmcname":"osa03-16_forsta_bokstaverna"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en mening. Programmet skriver därefter ut den första bokstaven i varje ord på sin egen rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge en mening: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Kira gillade klara glaskulor"}]},{"type":"text","value":"\nK\ng\nk\ng"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fakulteter","tmcname":"osa03-17_fakulteter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange ett heltal. Om talet är negativt eller noll, avslutas programmet. I övriga fall skriver programmet ut talets fakultet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fakultet räknas genom att multiplicera talet med sig själv samt alla mindre positiva heltal. Fakulteten för 5 är t.ex. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 * 2 * 3 * 4 * 5 = 120"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nTalets 3 fakultet är 6\nGe ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nTalets 4 fakultet är 24\nGe ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nTack och hej!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nTalets 1 fakultet är 1\nGe ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nTack och hej!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Sväng paren","tmcname":"osa03-18_svang_paren"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som skriver ut talen från ett till det tal användaren angett. Talen ska parvis vara omvända."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n2\n1\n4\n3\n5"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n2\n1\n4\n3\n6\n5"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Turvis","tmcname":"osa03-19_turvis"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange ett tal. Programmet ska skriva ut talen turvis enligt följande exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n1\n5\n2\n4\n3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge ett tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n1\n6\n2\n5\n3\n4"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du när break-instruktionen behövs för att avsluta en loop
  • \n
  • kan du använda continue-instruktionen för att fortsätta till nästa iteration
  • \n
  • förstår du hur kapslade loopar fungerar.
  • \n

break-instruktionen

Du har redan bekantat dig med break-instruktionen. Det kan användas för att direkt avsluta en loop. Ett exempel på ett användningsområde för break-instruktionen är då man ber användaren om någon information och loopen ska avslutas när ett visst värde ges.

Samma funktionalitet kan skapas utan break-instruktionen med ett passligt villkor. De här två programmen ber användaren ge siffor som adderas ihop tills användaren skriver siffran -1.

# version 1 med break-instruktion\n\nsumma = 0\n\nwhile True:\n    siffra = int(input(\"Ge ett tal, -1 avslutar programmet: \"))\n    if siffra == -1:\n        break\n    summa += siffra\n\nprint (f\"Summan är {summa}\")
# version 2 utan break-instruktion\n\nsumma = 0\nsiffra = 0\n\nwhile siffra != -1:\n    siffra = int(input(\"Ge ett tal, -1 avslutar programmet: \"))\n    if siffra != -1:\n        summa += siffra\n\nprint (f\"Summan är {summa}\")

Båda programmen skriver ut samma saker med samma indata, exempelvis:

Ge ett tal, -1 avslutar programmet: 2\nGe ett tal, -1 avslutar programmet: 4\nGe ett tal, -1 avslutar programmet: 5\nGe ett tal, -1 avslutar programmet: 3\nGe ett tal, -1 avslutar programmet: -1\nSumman är 14

Båda programmen är alltså identiska till deras funktion men den första metoden är ofta enklare eftersom villkoret siffra == 1 endast finns på ett ställe och variabeln siffra behöver inte initieras utanför loopen.

break-instruktionen kan kombineras med ett passligt villkor. Till exempel följande loop upprepas så länge summan av siffrorna är högst 100, men avslutas också då man ger siffran -1.

Så här kan det se ut när programmet körs:

summa = 0\n\nwhile summa <= 100:\n    siffra = int(input(\"Ge ett tal, -1 avslutar programmet: \"))\n    if siffra == -1:\n        break\n    summa += siffra\n\nprint (f\"Summan är {summa}\")

Exempel:

Ge ett tal, -1 avslutar programmet: 15\nGe ett tal, -1 avslutar programmet: 8\nGe ett tal, -1 avslutar programmet: 21\nGe ett tal, -1 avslutar programmet: -1\nSumman är 44

Ge ett tal, -1 avslutar programmet: 15\nGe ett tal, -1 avslutar programmet: 8\nGe ett tal, -1 avslutar programmet: 21\nGe ett tal, -1 avslutar programmet: 45\nGe ett tal, -1 avslutar programmet: 17\nSumman är 106

I det första exemplet avslutas loopen eftersom användaren ger siffran -1. I det andra exemplet avslutas loopen eftersom summan blir större än 100.

Som alltid inom programmering finns det flera sätt att uppnå samma resultat. Följande program fungerar på identiskt sätt jämfört med de två exemplen ovan:

summa = 0\n\nwhile True:\n    siffra = int(input(\"Ge ett tal, -1 avslutar programmet: \"))\n    if siffra == -1:\n        break\n    summa += siffra\n    if summa > 100:\n        break\n\nprint (f\"Summan är {summa}\")

continue-instruktionen

Ett annat sätt att påverka hur en loop körs är continue-instruktionen. Det får loopen att hoppa till början, där villkoret för loopen finns. Loopen fortsätter köra normalt därifrån börjandes från att kolla villkoret:

\n \n

Efter den här delen

    \n
  • vet du när break-instruktionen behövs för att avsluta en loop
  • \n
  • kan du använda continue-instruktionen för att fortsätta till nästa iteration
  • \n
  • förstår du hur kapslade loopar fungerar.
  • \n

break-instruktionen

Du har redan bekantat dig med break-instruktionen. Det kan användas för att direkt avsluta en loop. Ett exempel på ett användningsområde för break-instruktionen är då man ber användaren om någon information och loopen ska avslutas när ett visst värde ges.

Samma funktionalitet kan skapas utan break-instruktionen med ett passligt villkor. De här två programmen ber användaren ge siffor som adderas ihop tills användaren skriver siffran -1.

# version 1 med break-instruktion\n\nsumma = 0\n\nwhile True:\n    siffra = int(input(\"Ge ett tal, -1 avslutar programmet: \"))\n    if siffra == -1:\n        break\n    summa += siffra\n\nprint (f\"Summan är {summa}\")
# version 2 utan break-instruktion\n\nsumma = 0\nsiffra = 0\n\nwhile siffra != -1:\n    siffra = int(input(\"Ge ett tal, -1 avslutar programmet: \"))\n    if siffra != -1:\n        summa += siffra\n\nprint (f\"Summan är {summa}\")

Båda programmen skriver ut samma saker med samma indata, exempelvis:

Ge ett tal, -1 avslutar programmet: 2\nGe ett tal, -1 avslutar programmet: 4\nGe ett tal, -1 avslutar programmet: 5\nGe ett tal, -1 avslutar programmet: 3\nGe ett tal, -1 avslutar programmet: -1\nSumman är 14

Båda programmen är alltså identiska till deras funktion men den första metoden är ofta enklare eftersom villkoret siffra == 1 endast finns på ett ställe och variabeln siffra behöver inte initieras utanför loopen.

break-instruktionen kan kombineras med ett passligt villkor. Till exempel följande loop upprepas så länge summan av siffrorna är högst 100, men avslutas också då man ger siffran -1.

Så här kan det se ut när programmet körs:

summa = 0\n\nwhile summa <= 100:\n    siffra = int(input(\"Ge ett tal, -1 avslutar programmet: \"))\n    if siffra == -1:\n        break\n    summa += siffra\n\nprint (f\"Summan är {summa}\")

Exempel:

Ge ett tal, -1 avslutar programmet: 15\nGe ett tal, -1 avslutar programmet: 8\nGe ett tal, -1 avslutar programmet: 21\nGe ett tal, -1 avslutar programmet: -1\nSumman är 44

Ge ett tal, -1 avslutar programmet: 15\nGe ett tal, -1 avslutar programmet: 8\nGe ett tal, -1 avslutar programmet: 21\nGe ett tal, -1 avslutar programmet: 45\nGe ett tal, -1 avslutar programmet: 17\nSumman är 106

I det första exemplet avslutas loopen eftersom användaren ger siffran -1. I det andra exemplet avslutas loopen eftersom summan blir större än 100.

Som alltid inom programmering finns det flera sätt att uppnå samma resultat. Följande program fungerar på identiskt sätt jämfört med de två exemplen ovan:

summa = 0\n\nwhile True:\n    siffra = int(input(\"Ge ett tal, -1 avslutar programmet: \"))\n    if siffra == -1:\n        break\n    summa += siffra\n    if summa > 100:\n        break\n\nprint (f\"Summan är {summa}\")

continue-instruktionen

Ett annat sätt att påverka hur en loop körs är continue-instruktionen. Det får loopen att hoppa till början, där villkoret för loopen finns. Loopen fortsätter köra normalt därifrån börjandes från att kolla villkoret:

\n \n

Efter den här delen

    \n
  • kan du skriva och kalla på dina egna funktioner
  • \n
  • förstår du vad ett argument och en parameter hos en funktion är
  • \n
  • kan du definiera parametrar i dina egna funktioner.
  • \n

Vi har redan använt funktioner som len, print och input i våra program. Dessa är funktioner som är inbyggda i Python och de är därmed alltid tillgängliga. Det är dessutom möjligt att definiera egna funktioner.

Definiera en funktion

Före en funktion kan användas måste den definieras. Man börjar definieringen av en funktion med nyckelordet def (define). Därefter följer namnet på funktionen, följt av parentes och kolon. Efter det här följer funktionens innehåll – indenterat precis som med while- och if-blocken.

Den här kodsnutten definierar till exempel funktionen meddelande:

def meddelande():\n    print(\"Det här är min funktion!\")

Om programmet körs, verkar det som att inget händer. Det beror på att funktionens innehåll endast körs då funktionen anropas.

Man kan anropa en funktion enkelt – genom att nämna dess namn i koden. Så här kan vi utveckla det föregående exemplet:

def meddelande():\n    print(\"Det här är min funktion!\")\n\nmeddelande()

Här är resultatet:

Det här är min funktion!

När en funktion har definierats kan den kallas flera gånger.

def meddelande():\n    print(\"Det här är min funktion!\")\n\nmeddelande()\nmeddelande()\nmeddelande()

Det här är min funktion!\nDet här är min funktion!\nDet här är min funktion!

Obs! Från och med nu kommer de flesta av kursens uppgifter förutsätta att du definierar dina egna funktioner.

När ett program består bara av funktioner verkar ingenting hända då programmet körs. Följande kod skriver inte ut någonting, även om den innehåller en print-sats:

def halsa():\n    print(\"Hejps!\")

Det här beror på att koden i halsa-funktionen endast körs då funktionen anropas.

\"Huvudprogrammet\" nedan ska innehålla alla funktionsanrop för att kunna testa funktionerna. Python tolkar all kod utanför funktionsdefinitioner som en del av huvudfunktionen, som körs automatiskt när själva filen körs. Låt oss anropa funktionen:

def halsa():\n    print(\"Hejps!\")\n\n# huvudprogrammet är den del av programmet som inte är inom någon funktion\n# vi anropar vår funktion\n\nhalsa()

Viktigt! De automatiska testerna i den här kursen kräver att övningsfilernas huvudfunktion är tom. Inga instruktioner bör lämnas i huvudfunktionen i din lösning. All kod som du använder för att testa funktioner ska istället vara innanför ett speciellt if-block:

def halsa():\n    print(\"Hejps!\")\n\n# vi skriver huvudprogrammet inom ett block som det här\nif __name__ == \"__main__\":\n    halsa()

Kod utanför blocket orsakar ett felmeddelande som detta:

\n \n \n \n \n \n \"3\n \n \n

Det lönar sig också att märka att testen inte kör kod i if __name__ == \"__main__\" -block. Dit ska man inte placera kod som behövs för uppgifterna.

Skapa funktionen sju_broder som skriver ut namnet på sju bröder i alfabetisk ordning:

Aapo\nEero\nJuhani\nLauri\nSimeoni\nTimo\nTuomas

Argument hos funktioner

Funktioner tar ofta emot ett eller fler argument som kan påverka på vad funktionen gör. Till exempel de inbyggda print- och input-funktionerna i Python tar emot texten som ska visas som argument:

print(\"Hejps!\")                      # som parameter strängen \"Hejps!\"\nnamn = input(\"Berätta ditt namn: \")  # som parameter strängen \"Berätta ditt namn: \"\nprint(namn)                          # som parameter värdet på variabeln namn

Det har tidigare nämnts att termerna argument och parameter ofta syftar till samma sak. Skillnaden är att argument används för den data som ges till funktionen vid ett anrop, medan dessa inom funktionen tilldelas till variabler som kallas parametrar. Vi ger alltså argument till en funktion då vi anropar den. När vi definierar en funktion talar vi istället om parametrar.

Det här kan kännas som en meningslös skillnad, och dessutom följer inte alla källor den här definitionen. Vi strävar att göra den här skillnaden tydlig under den här kursen. Med korrekt terminologi är det lättare att förstå andra material förutom det som den här kursen erbjuder.

Låt oss definiera några funktioner som tar emot argument. I funktionsdefinitionen följer parametrarna funktionens namn, inom parenteserna:

def halsa(sak):\n    print(\"Hej\", sak)

När funktionen anropas två gånger…

halsa(\"Beatrice\")\nhalsa(\"världen!\")

…får vi två hälsningar:

Hej Beatrice\nHej världen!

Vi tar ännu en titt på funktionsdefinitionen:

def halsa(sak):\n    print(\"Hej\", sak)

På den första raden har vi definierat att funktionen tar emot ett argument och tilldelar det till en variabel med namnet sak. Inom funktionen använder print-instruktionen det värde som finns lagrat i variabeln sak.

När funktionen anropas får parametern sak det värde som getts som argument i funktionsanropet. Till exempel följande funktionsanrop…

namn = \"Alice\"\nhalsa(namn)

…resulterar i att variabeln sak får värdet \"Alice\".

Namnet på en funktion ges enligt samma principer som variabelnamn. De ska vara beskrivande, i regel innehålla små bokstäver och understreck. Undantag finns igen, men vi ignorerar dem tills vidare.

Gör så att funktionen forsta i den här uppgiften skriver ut det första tecknet i den sträng som ges som argument.

def forsta(strang):\n     # skriv kod här\n\n# vi testar på funktionen\nif __name__ == \"__main__\":\n    forsta('python')\n    forsta('yxa')\n    forsta('tapet')\n    forsta('himmel')\n    forsta('opera')\n    forsta('normal')

p\ny\nt\nh\no\nn

Då du testar på funktioner som tar emot ett eller fler argument, kan det vara till nytta att testa den med olika argument.

Håll koll på möjliga \"speciella fall\". Hur kommer funktionen att fungera om argumentet till exempel är noll eller negativt? Eller ett flyttal istället för ett heltal? Vad händer om argumentet är en tom sträng?

Om en uppgift inte förutsätter att du anropar en funktion kan du ändå göra det. Använd då ett if-block enligt det som beskrevs tidigare. Testen kommer att ignorera koden inom det här blocket.

Fler exempel

Vi tar en titt på fler exempelfunktioner som tar emot argument. Här är parametern en siffra:

def kvadrat(x):\n    print(f\"Kvadraten av {x} är {x * x}\")\n\nkvadrat(2)\nkvadrat(5)

Kvadraten av 2 är 4\nKvadraten av 5 är 25

Här har vi en if-sats inom en funktion:

def halsa(namn):\n    if namn == \"Beatrice\":\n        print(\"Hejps,\", namn)\n    else:\n        print(\"God dag,\", namn)\n\nhalsa(\"Beatrice\")\nhalsa(\"Mårten\")

Hejps, Beatrice\nGod dag, Mårten

Den här funktionen tar emot två argument:

def summa(x, y):\n    resultat = x + y\n    print(f\"Summan av {x} och {y} är {resultat}\")\n\nsumma(1, 2)\nsumma(5, 24)

Summan av 1 och 2 är 3\nSumman av 5 och 24 är 29

Funktionen har också en hjälpvariabel, resultat, som används för att lagra summan av argumentens värden.

Märk att namnen på parametrarna i funktionsdefinitionen inte har några kopplingar till andra variabler utanför definitionen. Vi kan anropa ovanstående funktion på följande sätt:

x = 100\ny = 30\nsumma(1, 2)\nsumma(x + y, 10)

Det här skriver ut:

Summan av 1 och 2 är 3\nSumman av 130 och 10 är 140

I det första funktionsanropet får parametrarna värdena x = 1 och y = 2. I det andra anropet får de värdena x = 130 och y = 10. Det här oavsett att vi i anropet använder variabler med samma namn.

I nästa modul återkommer vi till funktionsdefinitioner.

Varning: globala variabler inom funktioner

I exemplen ovan observerade vi att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler.

Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra.

Här är ett exempel på en funktion som använder en global variabel \"av misstag\":

# global variabel\nnamn = \"Beatrice\"\n\ndef halsa(fornamn):\n    # vi skriver av misstag ut den globala variabelns värde istället för parametern\n    print(\"Hej\", namn)\n\nhalsa(\"Alice\")\nhalsa(\"Beatrice\")

Hej Beatrice\nHej Beatrice

Oavsett vilka argument vi anropar funktionen med skrivs värdet \"Beatrice\" från den globala variabeln ut.

Skapa funktionen medeltal som tar emot tre heltal som argument. Funktionen ska skriva ut medelvärdet av dessa tal.

medeltal(5, 3, 1)\nmedeltal(10, 1, 1)

3.0\n4.0

Skapa funktionen skriv_ut_flera_ganger(strang, ganger), som får en sträng och ett heltal som argument. Strängen ska skrivas ut så många gånger som heltalet indikerar:

skriv_ut_flera_ganger(\"hej!!\", 5)\n\nprint()\n\nmerkkijono = \"Allt började då grannbondens gamla traktor började ryka...\"\nkertaa = 3\nskriv_ut_flera_ganger(merkkijono, kertaa)

hej!!\nhej!!\nhej!!\nhej!!\nhej!!

Allt började då grannbondens gamla traktor började ryka....\nAllt började då grannbondens gamla traktor började ryka....\nAllt började då grannbondens gamla traktor började ryka....

Skapa funktionen fyrkant(langd), som får som argument ett heltal som indikerar hur stor fyrkant programmet ska skriva ut:

fyrkant(3)\nprint()\nfyrkant(5)
\n###\n###\n###\n\n#####\n#####\n#####\n#####\n#####\n

Skapa funktionen schackbrade som skapar ett schackbräde av siffrorna noll och ett enligt exemplen nedan.

schackbrade(3)\nprint()\nschackbrade(6)
\n101\n010\n101\n\n101010\n010101\n101010\n010101\n101010\n010101\n

Skapa funktionen kvadrat som skriver ut en \"ordkvadrat\" enligt exemplen nedan.

kvadrat(\"ab\", 3)\nprint()\nkvadrat(\"aybabtu\", 5)
\naba\nbab\naba\n\naybab\ntuayb\nabtua\nybabt\nuayba\n

Vänligen svara på en kort enkät gällande den här veckans material.

","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-3/4-definiera-funktioner.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skriva och kalla på dina egna funktioner"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"förstår du vad ett argument och en parameter hos en funktion är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du definiera parametrar i dina egna funktioner."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan använt funktioner som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" i våra program. Dessa är funktioner som är inbyggda i Python och de är därmed alltid tillgängliga. Det är dessutom möjligt att definiera egna funktioner."}]},{"type":"element","tagName":"h2","properties":{"id":"definiera-en-funktion","style":"position:relative;"},"children":[{"type":"text","value":"Definiera en funktion"},{"type":"element","tagName":"a","properties":{"href":"#definiera-en-funktion","ariaLabel":"definiera en funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Före en funktion kan användas måste den definieras. Man börjar definieringen av en funktion med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" (define). Därefter följer namnet på funktionen, följt av parentes och kolon. Efter det här följer funktionens innehåll – indenterat precis som med while- och if-blocken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här kodsnutten definierar till exempel funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"meddelande"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"meddelande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är min funktion!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om programmet körs, verkar det som att inget händer. Det beror på att funktionens innehåll endast körs då funktionen anropas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan anropa en funktion enkelt – genom att nämna dess namn i koden. Så här kan vi utveckla det föregående exemplet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"meddelande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är min funktion!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmeddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är resultatet:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här är min funktion!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När en funktion har definierats kan den kallas flera gånger."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"meddelande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är min funktion!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmeddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmeddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmeddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här är min funktion!\nDet här är min funktion!\nDet här är min funktion!"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Testa dina egna funktioner"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Från och med nu kommer de flesta av kursens uppgifter förutsätta att du definierar dina egna funktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När ett program består bara av funktioner verkar ingenting hända då programmet körs. Följande kod skriver inte ut någonting, även om den innehåller en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-sats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejps!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här beror på att koden i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa"}]},{"type":"text","value":"-funktionen endast körs då funktionen anropas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Huvudprogrammet\" nedan ska innehålla alla funktionsanrop för att kunna testa funktionerna. Python tolkar all kod utanför funktionsdefinitioner som en del av huvudfunktionen, som körs automatiskt när själva filen körs. Låt oss anropa funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejps!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# huvudprogrammet är den del av programmet som inte är inom någon funktion"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi anropar vår funktion"}]},{"type":"text","value":"\n\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Viktigt! De automatiska testerna i den här kursen kräver att övningsfilernas huvudfunktion är tom. Inga instruktioner bör lämnas i huvudfunktionen i din lösning. All kod som du använder för att testa funktioner ska istället vara innanför ett speciellt if-block:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejps!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi skriver huvudprogrammet inom ett block som det här"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n halsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kod utanför blocket orsakar ett felmeddelande som detta:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/198bfb36dc5d02ba030280b89c26ecee/7575b/3_4_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 70.43478260869564%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAIAAACgpqunAAAACXBIWXMAABYlAAAWJQFJUiTwAAABpUlEQVQoz8WTPW8TQRCG7ycixEdFigiQECKipOEn0OQXEIFMESw7HyBEkFKkCDFWFBGwIM7Zudjn+4739nbvbmd3ZsEBghREFCoePZpiRm81M07Gsr7fLwVIWZZClKUEALwcjgJQ2jBWRUGWjkc5m2oNRHixiAbROKDBoOE8LwrOeS5kcTa7QK2hrmtHa20t2d/QJSWis/A/S4T/MWzOh2cgERAZol8tIkvC2B+WpxWQnKU9eWVZzq2Kmy15tcFuPI2vLSULa/njd8XDV/mddn5vlV9virmVotUdNXeG7a7X6IQvtr3NQ+483xO31tX9DZjfwNvPooUnnx8sHjS2sjcfi9aH/GWHtXeyR2/l3ddyfXe00nHXuoPmbrj8frjlcke5B/aob09SmwR2GluWWpZiNIEw0HGo01gbTajJ2op+WqOt0Goip+ztn/Q+HXve5GgY+/5oMDh23WQ8zpN4GoV5moqiYIwRnb+C2Z6r3n729Ys3CcIwGvt+EIaFkELKqq65EAbR/oVZuHQPIYkBUSkFAEopRCQiY8zsSo3Rp5g/+P4Y3wBFRBaDdLVPZAAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/198bfb36dc5d02ba030280b89c26ecee/a0b58/3_4_1.webp 230w","/static/198bfb36dc5d02ba030280b89c26ecee/bc10c/3_4_1.webp 460w","/static/198bfb36dc5d02ba030280b89c26ecee/966d8/3_4_1.webp 920w","/static/198bfb36dc5d02ba030280b89c26ecee/445df/3_4_1.webp 1380w","/static/198bfb36dc5d02ba030280b89c26ecee/40fa0/3_4_1.webp 1608w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/198bfb36dc5d02ba030280b89c26ecee/81c8e/3_4_1.png 230w","/static/198bfb36dc5d02ba030280b89c26ecee/08a84/3_4_1.png 460w","/static/198bfb36dc5d02ba030280b89c26ecee/c0255/3_4_1.png 920w","/static/198bfb36dc5d02ba030280b89c26ecee/b1001/3_4_1.png 1380w","/static/198bfb36dc5d02ba030280b89c26ecee/7575b/3_4_1.png 1608w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/198bfb36dc5d02ba030280b89c26ecee/c0255/3_4_1.png","alt":"3 4 1","title":"3 4 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det lönar sig också att märka att testen inte kör kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -block. Dit ska man inte placera kod som behövs för uppgifterna."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Sju bröder","tmcname":"osa03-21_sju_broder"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sju_broder"}]},{"type":"text","value":" som skriver ut namnet på sju bröder i alfabetisk ordning:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Aapo\nEero\nJuhani\nLauri\nSimeoni\nTimo\nTuomas"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"argument-hos-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Argument hos funktioner"},{"type":"element","tagName":"a","properties":{"href":"#argument-hos-funktioner","ariaLabel":"argument hos funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner tar ofta emot ett eller fler argument som kan påverka på vad funktionen gör. Till exempel de inbyggda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":"-funktionerna i Python tar emot texten som ska visas som argument:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejps!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# som parameter strängen \"Hejps!\""}]},{"type":"text","value":"\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Berätta ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# som parameter strängen \"Berätta ditt namn: \""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# som parameter värdet på variabeln namn"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det har tidigare nämnts att termerna argument och parameter ofta syftar till samma sak. Skillnaden är att argument används för den data som ges till funktionen vid ett anrop, medan dessa inom funktionen tilldelas till variabler som kallas parametrar. Vi ger alltså argument till en funktion då vi anropar den. När vi definierar en funktion talar vi istället om parametrar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kan kännas som en meningslös skillnad, och dessutom följer inte alla källor den här definitionen. Vi strävar att göra den här skillnaden tydlig under den här kursen. Med korrekt terminologi är det lättare att förstå andra material förutom det som den här kursen erbjuder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss definiera några funktioner som tar emot argument. I funktionsdefinitionen följer parametrarna funktionens namn, inom parenteserna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När funktionen anropas två gånger…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"halsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Beatrice\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"världen!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…får vi två hälsningar:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Beatrice\nHej världen!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi tar ännu en titt på funktionsdefinitionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På den första raden har vi definierat att funktionen tar emot ett argument och tilldelar det till en variabel med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sak"}]},{"type":"text","value":". Inom funktionen använder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen det värde som finns lagrat i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sak"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När funktionen anropas får parametern sak det värde som getts som argument i funktionsanropet. Till exempel följande funktionsanrop…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alice\""}]},{"type":"text","value":"\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…resulterar i att variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sak"}]},{"type":"text","value":" får värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Alice\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Namnet på en funktion ges enligt samma principer som variabelnamn. De ska vara beskrivande, i regel innehålla små bokstäver och understreck. Undantag finns igen, men vi ignorerar dem tills vidare."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Första tecknet","tmcname":"osa03-22_forsta_tecknet"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Gör så att funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forsta"}]},{"type":"text","value":" i den här uppgiften skriver ut det första tecknet i den sträng som ges som argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"forsta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriv kod här"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi testar på funktionen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n forsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'python'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'yxa'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'tapet'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'himmel'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'opera'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n forsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'normal'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"p\ny\nt\nh\no\nn"}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Testa funktioner med argument"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Då du testar på funktioner som tar emot ett eller fler argument, kan det vara till nytta att testa den med olika argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Håll koll på möjliga \"speciella fall\". Hur kommer funktionen att fungera om argumentet till exempel är noll eller negativt? Eller ett flyttal istället för ett heltal? Vad händer om argumentet är en tom sträng?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en uppgift inte förutsätter att du anropar en funktion kan du ändå göra det. Använd då ett if-block enligt det som beskrevs tidigare. Testen kommer att ignorera koden inom det här blocket."}]}]},{"type":"element","tagName":"h2","properties":{"id":"fler-exempel","style":"position:relative;"},"children":[{"type":"text","value":"Fler exempel"},{"type":"element","tagName":"a","properties":{"href":"#fler-exempel","ariaLabel":"fler exempel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi tar en titt på fler exempelfunktioner som tar emot argument. Här är parametern en siffra:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kvadrat"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Kvadraten av "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kvadraten av 2 är 4\nKvadraten av 5 är 25"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en if-sats inom en funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Beatrice\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejps,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"God dag,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Beatrice\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mårten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hejps, Beatrice\nGod dag, Mårten"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här funktionen tar emot två argument:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Summan av "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsumma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Summan av 1 och 2 är 3\nSumman av 5 och 24 är 29"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen har också en hjälpvariabel, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":", som används för att lagra summan av argumentens värden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att namnen på parametrarna i funktionsdefinitionen inte har några kopplingar till andra variabler utanför definitionen. Vi kan anropa ovanstående funktion på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\nsumma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Summan av 1 och 2 är 3\nSumman av 130 och 10 är 140"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det första funktionsanropet får parametrarna värdena "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 1"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y = 2"}]},{"type":"text","value":". I det andra anropet får de värdena "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 130"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y = 10"}]},{"type":"text","value":". Det här oavsett att vi i anropet använder variabler med samma namn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I nästa modul återkommer vi till funktionsdefinitioner."}]},{"type":"comment","value":"vastaava varoitusteksti löytyy osioista 3-4, 4-6 ja 5-1, tsekkaa kaikki jos muokkaat tätä"},{"type":"element","tagName":"h2","properties":{"id":"varning-globala-variabler-inom-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Varning: globala variabler inom funktioner"},{"type":"element","tagName":"a","properties":{"href":"#varning-globala-variabler-inom-funktioner","ariaLabel":"varning globala variabler inom funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan observerade vi att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel på en funktion som använder en global variabel \"av misstag\":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Beatrice\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fornamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi skriver av misstag ut den globala variabelns värde istället för parametern"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alice\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Beatrice\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Beatrice\nHej Beatrice"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Oavsett vilka argument vi anropar funktionen med skrivs värdet \"Beatrice\" från den globala variabeln ut."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Medelvärde","tmcname":"osa03-25_medelvarde"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"medeltal"}]},{"type":"text","value":" som tar emot tre heltal som argument. Funktionen ska skriva ut medelvärdet av dessa tal."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmedeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3.0\n4.0"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Flera utskrifter","tmcname":"osa03-24_flera_utskrifter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger(strang, ganger)"}]},{"type":"text","value":", som får en sträng och ett heltal som argument. Strängen ska skrivas ut så många gånger som heltalet indikerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hej!!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmerkkijono "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Allt började då grannbondens gamla traktor började ryka...\""}]},{"type":"text","value":"\nkertaa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nskriv_ut_flera_ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"merkkijono"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kertaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"hej!!\nhej!!\nhej!!\nhej!!\nhej!!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Allt började då grannbondens gamla traktor började ryka....\nAllt började då grannbondens gamla traktor började ryka....\nAllt började då grannbondens gamla traktor började ryka...."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fyrkant","tmcname":"osa03-23_fyrkant"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fyrkant(langd)"}]},{"type":"text","value":", som får som argument ett heltal som indikerar hur stor fyrkant programmet ska skriva ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fyrkant"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfyrkant"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"###\n###\n###\n\n#####\n#####\n#####\n#####\n#####\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Schackbräde","tmcname":"osa03-26_schackbrade"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"schackbrade"}]},{"type":"text","value":" som skapar ett schackbräde av siffrorna noll och ett enligt exemplen nedan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"schackbrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nschackbrade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"101\n010\n101\n\n101010\n010101\n101010\n010101\n101010\n010101\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Ordkvadrat","tmcname":"osa03-27_ordkvadrat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kvadrat"}]},{"type":"text","value":" som skriver ut en \"ordkvadrat\" enligt exemplen nedan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ab\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aybabtu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"aba\nbab\naba\n\naybab\ntuayb\nabtua\nybabt\nuayba\n"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät gällande den här veckans material."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skriva och kalla på dina egna funktioner
  • \n
  • förstår du vad ett argument och en parameter hos en funktion är
  • \n
  • kan du definiera parametrar i dina egna funktioner.
  • \n

Vi har redan använt funktioner som len, print och input i våra program. Dessa är funktioner som är inbyggda i Python och de är därmed alltid tillgängliga. Det är dessutom möjligt att definiera egna funktioner.

Definiera en funktion

Före en funktion kan användas måste den definieras. Man börjar definieringen av en funktion med nyckelordet def (define). Därefter följer namnet på funktionen, följt av parentes och kolon. Efter det här följer funktionens innehåll – indenterat precis som med while- och if-blocken.

Den här kodsnutten definierar till exempel funktionen meddelande:

def meddelande():\n    print(\"Det här är min funktion!\")

Om programmet körs, verkar det som att inget händer. Det beror på att funktionens innehåll endast körs då funktionen anropas.

Man kan anropa en funktion enkelt – genom att nämna dess namn i koden. Så här kan vi utveckla det föregående exemplet:

def meddelande():\n    print(\"Det här är min funktion!\")\n\nmeddelande()

Här är resultatet:

Det här är min funktion!

När en funktion har definierats kan den kallas flera gånger.

def meddelande():\n    print(\"Det här är min funktion!\")\n\nmeddelande()\nmeddelande()\nmeddelande()

Det här är min funktion!\nDet här är min funktion!\nDet här är min funktion!

Obs! Från och med nu kommer de flesta av kursens uppgifter förutsätta att du definierar dina egna funktioner.

När ett program består bara av funktioner verkar ingenting hända då programmet körs. Följande kod skriver inte ut någonting, även om den innehåller en print-sats:

def halsa():\n    print(\"Hejps!\")

Det här beror på att koden i halsa-funktionen endast körs då funktionen anropas.

\"Huvudprogrammet\" nedan ska innehålla alla funktionsanrop för att kunna testa funktionerna. Python tolkar all kod utanför funktionsdefinitioner som en del av huvudfunktionen, som körs automatiskt när själva filen körs. Låt oss anropa funktionen:

def halsa():\n    print(\"Hejps!\")\n\n# huvudprogrammet är den del av programmet som inte är inom någon funktion\n# vi anropar vår funktion\n\nhalsa()

Viktigt! De automatiska testerna i den här kursen kräver att övningsfilernas huvudfunktion är tom. Inga instruktioner bör lämnas i huvudfunktionen i din lösning. All kod som du använder för att testa funktioner ska istället vara innanför ett speciellt if-block:

def halsa():\n    print(\"Hejps!\")\n\n# vi skriver huvudprogrammet inom ett block som det här\nif __name__ == \"__main__\":\n    halsa()

Kod utanför blocket orsakar ett felmeddelande som detta:

\n \n \n \n \n \n \"3\n \n \n

Det lönar sig också att märka att testen inte kör kod i if __name__ == \"__main__\" -block. Dit ska man inte placera kod som behövs för uppgifterna.

Skapa funktionen sju_broder som skriver ut namnet på sju bröder i alfabetisk ordning:

Aapo\nEero\nJuhani\nLauri\nSimeoni\nTimo\nTuomas

Argument hos funktioner

Funktioner tar ofta emot ett eller fler argument som kan påverka på vad funktionen gör. Till exempel de inbyggda print- och input-funktionerna i Python tar emot texten som ska visas som argument:

print(\"Hejps!\")                      # som parameter strängen \"Hejps!\"\nnamn = input(\"Berätta ditt namn: \")  # som parameter strängen \"Berätta ditt namn: \"\nprint(namn)                          # som parameter värdet på variabeln namn

Det har tidigare nämnts att termerna argument och parameter ofta syftar till samma sak. Skillnaden är att argument används för den data som ges till funktionen vid ett anrop, medan dessa inom funktionen tilldelas till variabler som kallas parametrar. Vi ger alltså argument till en funktion då vi anropar den. När vi definierar en funktion talar vi istället om parametrar.

Det här kan kännas som en meningslös skillnad, och dessutom följer inte alla källor den här definitionen. Vi strävar att göra den här skillnaden tydlig under den här kursen. Med korrekt terminologi är det lättare att förstå andra material förutom det som den här kursen erbjuder.

Låt oss definiera några funktioner som tar emot argument. I funktionsdefinitionen följer parametrarna funktionens namn, inom parenteserna:

def halsa(sak):\n    print(\"Hej\", sak)

När funktionen anropas två gånger…

halsa(\"Beatrice\")\nhalsa(\"världen!\")

…får vi två hälsningar:

Hej Beatrice\nHej världen!

Vi tar ännu en titt på funktionsdefinitionen:

def halsa(sak):\n    print(\"Hej\", sak)

På den första raden har vi definierat att funktionen tar emot ett argument och tilldelar det till en variabel med namnet sak. Inom funktionen använder print-instruktionen det värde som finns lagrat i variabeln sak.

När funktionen anropas får parametern sak det värde som getts som argument i funktionsanropet. Till exempel följande funktionsanrop…

namn = \"Alice\"\nhalsa(namn)

…resulterar i att variabeln sak får värdet \"Alice\".

Namnet på en funktion ges enligt samma principer som variabelnamn. De ska vara beskrivande, i regel innehålla små bokstäver och understreck. Undantag finns igen, men vi ignorerar dem tills vidare.

Gör så att funktionen forsta i den här uppgiften skriver ut det första tecknet i den sträng som ges som argument.

def forsta(strang):\n     # skriv kod här\n\n# vi testar på funktionen\nif __name__ == \"__main__\":\n    forsta('python')\n    forsta('yxa')\n    forsta('tapet')\n    forsta('himmel')\n    forsta('opera')\n    forsta('normal')

p\ny\nt\nh\no\nn

Då du testar på funktioner som tar emot ett eller fler argument, kan det vara till nytta att testa den med olika argument.

Håll koll på möjliga \"speciella fall\". Hur kommer funktionen att fungera om argumentet till exempel är noll eller negativt? Eller ett flyttal istället för ett heltal? Vad händer om argumentet är en tom sträng?

Om en uppgift inte förutsätter att du anropar en funktion kan du ändå göra det. Använd då ett if-block enligt det som beskrevs tidigare. Testen kommer att ignorera koden inom det här blocket.

Fler exempel

Vi tar en titt på fler exempelfunktioner som tar emot argument. Här är parametern en siffra:

def kvadrat(x):\n    print(f\"Kvadraten av {x} är {x * x}\")\n\nkvadrat(2)\nkvadrat(5)

Kvadraten av 2 är 4\nKvadraten av 5 är 25

Här har vi en if-sats inom en funktion:

def halsa(namn):\n    if namn == \"Beatrice\":\n        print(\"Hejps,\", namn)\n    else:\n        print(\"God dag,\", namn)\n\nhalsa(\"Beatrice\")\nhalsa(\"Mårten\")

Hejps, Beatrice\nGod dag, Mårten

Den här funktionen tar emot två argument:

def summa(x, y):\n    resultat = x + y\n    print(f\"Summan av {x} och {y} är {resultat}\")\n\nsumma(1, 2)\nsumma(5, 24)

Summan av 1 och 2 är 3\nSumman av 5 och 24 är 29

Funktionen har också en hjälpvariabel, resultat, som används för att lagra summan av argumentens värden.

Märk att namnen på parametrarna i funktionsdefinitionen inte har några kopplingar till andra variabler utanför definitionen. Vi kan anropa ovanstående funktion på följande sätt:

x = 100\ny = 30\nsumma(1, 2)\nsumma(x + y, 10)

Det här skriver ut:

Summan av 1 och 2 är 3\nSumman av 130 och 10 är 140

I det första funktionsanropet får parametrarna värdena x = 1 och y = 2. I det andra anropet får de värdena x = 130 och y = 10. Det här oavsett att vi i anropet använder variabler med samma namn.

I nästa modul återkommer vi till funktionsdefinitioner.

Varning: globala variabler inom funktioner

I exemplen ovan observerade vi att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler.

Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra.

Här är ett exempel på en funktion som använder en global variabel \"av misstag\":

# global variabel\nnamn = \"Beatrice\"\n\ndef halsa(fornamn):\n    # vi skriver av misstag ut den globala variabelns värde istället för parametern\n    print(\"Hej\", namn)\n\nhalsa(\"Alice\")\nhalsa(\"Beatrice\")

Hej Beatrice\nHej Beatrice

Oavsett vilka argument vi anropar funktionen med skrivs värdet \"Beatrice\" från den globala variabeln ut.

Skapa funktionen medeltal som tar emot tre heltal som argument. Funktionen ska skriva ut medelvärdet av dessa tal.

medeltal(5, 3, 1)\nmedeltal(10, 1, 1)

3.0\n4.0

Skapa funktionen skriv_ut_flera_ganger(strang, ganger), som får en sträng och ett heltal som argument. Strängen ska skrivas ut så många gånger som heltalet indikerar:

skriv_ut_flera_ganger(\"hej!!\", 5)\n\nprint()\n\nmerkkijono = \"Allt började då grannbondens gamla traktor började ryka...\"\nkertaa = 3\nskriv_ut_flera_ganger(merkkijono, kertaa)

hej!!\nhej!!\nhej!!\nhej!!\nhej!!

Allt började då grannbondens gamla traktor började ryka....\nAllt började då grannbondens gamla traktor började ryka....\nAllt började då grannbondens gamla traktor började ryka....

Skapa funktionen fyrkant(langd), som får som argument ett heltal som indikerar hur stor fyrkant programmet ska skriva ut:

fyrkant(3)\nprint()\nfyrkant(5)
\n###\n###\n###\n\n#####\n#####\n#####\n#####\n#####\n

Skapa funktionen schackbrade som skapar ett schackbräde av siffrorna noll och ett enligt exemplen nedan.

schackbrade(3)\nprint()\nschackbrade(6)
\n101\n010\n101\n\n101010\n010101\n101010\n010101\n101010\n010101\n

Skapa funktionen kvadrat som skriver ut en \"ordkvadrat\" enligt exemplen nedan.

kvadrat(\"ab\", 3)\nprint()\nkvadrat(\"aybabtu\", 5)
\naba\nbab\naba\n\naybab\ntuayb\nabtua\nybabt\nuayba\n

Vänligen svara på en kort enkät gällande den här veckans material.

","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-3/4-definiera-funktioner.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-3/page-data.json b/page-data/osa-3/page-data.json index 1acadd3c7..4db401c39 100644 --- a/page-data/osa-3/page-data.json +++ b/page-data/osa-3/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-3", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-3","title":"Modul 3"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-3/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-3","title":"Modul 3"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-3/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-4/1-vscode/page-data.json b/page-data/osa-4/1-vscode/page-data.json index 53f7252af..6752552ef 100644 --- a/page-data/osa-4/1-vscode/page-data.json +++ b/page-data/osa-4/1-vscode/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-4/1-vscode", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kommer du att vara förberedd på att använda Visual Studio Code för att göra den här kursens övningar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kommer du att vara bekant med den interaktiva Pythontolken, som du kan använda för att köra kod."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har alla övningar i den här kursen gjorts direkt på kurssidorna i de inbäddade kodeditorerna. Att programmera i webbläddraren fungerar bra när man börjar lära sig att programmering. Nu är det ändå dags att börja använda en separat kodeditor som är anpassad just för att redigera kod."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns en hel del olika editorer som är skräddarsydda för programmering. På den här kursen kommer vi att använda "},{"type":"element","tagName":"a","properties":{"href":"https://code.visualstudio.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":" – en editor som under de senaste åren fått allt större fotfäste på marknaden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din uppgift är att nu installera Visual Studio Code på din dator. Det kan hända att du också måste installera Python och Python-tillägget (plugin) i Visual Studio Code. Du behöver också TMC-tillägget som tar hand om att köra de tester som finns i samband med övningarna. I TMC:s inställningar ska du välja MOOC som organisation och Python-programmering 2024 som kurs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/installation/vscode/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Här hittar du en guide"}]},{"type":"text","value":" som berättar hur du kan installera allt som nämnts ovan. Läs instruktionerna och gör sedan uppgiften nedan:"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hello Visual Studio Code","tmcname":"osa04-01_vs_code"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar vilken kodeditor användaren har. Programmet fortsätter köra tills användaren svarar Visual Studio Code."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Editor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emacs"}]},{"type":"text","value":"\ninte bra\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Vim"}]},{"type":"text","value":"\ninte bra\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Word"}]},{"type":"text","value":"\nusel\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Atom"}]},{"type":"text","value":"\ninte bra\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":"\nperfekt val!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren svarar Word eller Notepad ger programmet kommentaren \"usel\". I övriga icke-giltiga fall \"inte bra\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera oberoende hur namnet skrivs, dvs. bruket av gemener/versaler ska inte påverka:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Editor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NOTEPAD"}]},{"type":"text","value":"\nusel\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"viSUal STudiO cODe"}]},{"type":"text","value":"\nperfekt val!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kirjainten koon voi jättää huomiotta esim. muuttamalla kirjaimet pieniksi merkkijonojen metodilla "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower"}]},{"type":"text","value":", jota voi käyttää seuraavasti:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kan åstadkomma t.ex. genom att ändra bokstavsstorleken med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower"}]},{"type":"text","value":", och bara använda gemener:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Visual Studio CODE\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"visual studio code\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lower"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"strängen hittades\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"kora-kod","style":"position:relative;"},"children":[{"type":"text","value":"Köra kod"},{"type":"element","tagName":"a","properties":{"href":"#kora-kod","ariaLabel":"kora kod permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Visual Studio Code är det enklaste sättet att köra kod att klicka på triangeln uppe i hörnet till höger. Ibland kan det hända att något program blir och köra i bakgrunden – det kanske väntar på att användaren ger någon data eller är fast i en oändlig loop – utan att du märker det. Du märker det eventuellt först när du försöker köra nästa program som helt enkelt inte kommer att köra eftersom det föregående programmet tar upp alla resurser. En snabb lösning på det här är att trycka på tangenterna Control + C samtidigt. Det här avslutar den pågående processen. Nästa program borde nu kunna köra normalt."}]},{"type":"element","tagName":"h2","properties":{"id":"den-interaktiva-pythontolken","style":"position:relative;"},"children":[{"type":"text","value":"Den interaktiva Pythontolken"},{"type":"element","tagName":"a","properties":{"href":"#den-interaktiva-pythontolken","ariaLabel":"den interaktiva pythontolken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett av de viktigaste verktygen för en Python-programmerare är den interaktiva Python-tolken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur du får tolken igång beror eventuellt på den miljö där du kör Python i. På Linux och Mac kan du skriva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python3"}]},{"type":"text","value":" i terminalen. På Windows kan kommandot heta "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python"}]},{"type":"text","value":". Så här ser tolken ut på Mac:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/0f246/4_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 17.82608695652174%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAArUlEQVQI13XLywqDMBCFYd//tbroMhiYZFppBG/YJAQTN+o4sVS77bc6HPiL2/Pe1E3ftc8HlmWJiFprKWVVVQAgpUT8/gCglBJCGGPatm1ORV2Zruve49s5F2O01nrvnXMppb7vQwjHfwU+cBxHRBRCAIB5GaUUIg7DoLUGgJRSjDGEME2T995aO8/zLyainDMzExEzH8ex7zsz55yvvW0bEW2ndV2XZSGiK/4AItbceE5Q2z8AAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/a0b58/4_1_1.webp 230w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/bc10c/4_1_1.webp 460w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/966d8/4_1_1.webp 920w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/34186/4_1_1.webp 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/81c8e/4_1_1.png 230w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/08a84/4_1_1.png 460w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/c0255/4_1_1.png 920w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/0f246/4_1_1.png 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/c0255/4_1_1.png","alt":"4 1 1","title":"4 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att starta tolken i Visual Studio Code. Kör först ett program genom att klicka på triangeln. Nu borde en \"Terminal\"-del öppnas på din skärm. Där kan du skriva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python3"}]},{"type":"text","value":" (eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"):"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/895b071970fc467ecad71d69e6d70d5a/e431d/4_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 49.130434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAABYlAAAWJQFJUiTwAAABF0lEQVQoz52RWW7DIBCGuUcLw2I2Ox42O7hZrtCXqve/SgVOokh5aNVPCM32s8wQjDHFlHIOIRhjnfP78r7ttgVuEWedMuZo58mNNak4e+JHnxYsBXPBcfbaaGOsbSKntZZqMM4111qplGfySyYL4h2AASeUUiG4EJxzAAD6BOs8uwr4G6MUGPQU6Wl2K/wNym4y9hD/GwIAfyyFO7vdxLajtUbEaZqsMcZZLsUDKSXnfBzHdV0Ph8M8z62kt5PUWtd1RcRSSs4ZQ9hy+UjLmkuKMYSwx2OMy7KkNtOcOqUUcr1eL5dLrXXbtvPpVOrxO54/h4jSDEoJIeD+yNdfkG3bEHE/MvarQgiD1koPzjml1GNmrw37AVG5Vxy3ozSFAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/895b071970fc467ecad71d69e6d70d5a/a0b58/4_1_2.webp 230w","/static/895b071970fc467ecad71d69e6d70d5a/bc10c/4_1_2.webp 460w","/static/895b071970fc467ecad71d69e6d70d5a/966d8/4_1_2.webp 920w","/static/895b071970fc467ecad71d69e6d70d5a/445df/4_1_2.webp 1380w","/static/895b071970fc467ecad71d69e6d70d5a/78de1/4_1_2.webp 1840w","/static/895b071970fc467ecad71d69e6d70d5a/609ff/4_1_2.webp 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/895b071970fc467ecad71d69e6d70d5a/81c8e/4_1_2.png 230w","/static/895b071970fc467ecad71d69e6d70d5a/08a84/4_1_2.png 460w","/static/895b071970fc467ecad71d69e6d70d5a/c0255/4_1_2.png 920w","/static/895b071970fc467ecad71d69e6d70d5a/b1001/4_1_2.png 1380w","/static/895b071970fc467ecad71d69e6d70d5a/161ec/4_1_2.png 1840w","/static/895b071970fc467ecad71d69e6d70d5a/e431d/4_1_2.png 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/895b071970fc467ecad71d69e6d70d5a/c0255/4_1_2.png","alt":"4 1 2","title":"4 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också testa på en webbaserad Pythontolk, till exempel: "},{"type":"element","tagName":"a","properties":{"href":"https://www.python.org/shell/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://www.python.org/shell/"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med hjälp av tolken kan du köra Python-kod rad för rad, i realtid. När du skriver en rad kod och trycker på Enter, kommer tolken att köra den omedelbart och visa dess resultat:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7977922a8e37fd253a7fd34e364e6d58/0f246/4_1_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 19.565217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAkElEQVQI143LwQqDMAwAUP//53qQRq0zKmJALe0wB3HRZtDtOti7v6Kua0QchmEcRwCoqso5BwDOub7vjTFEpKoi8srO8xQRzYppmh4ZESHiuq7HcXjvY4zMHELw3jPzM4sxLsvCzN88z3PTNGVZtm2773tKSUTu+9Y/FETUdZ21FgAQcdu2kF3Xparph09+Az024L8JLoKAAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7977922a8e37fd253a7fd34e364e6d58/a0b58/4_1_3.webp 230w","/static/7977922a8e37fd253a7fd34e364e6d58/bc10c/4_1_3.webp 460w","/static/7977922a8e37fd253a7fd34e364e6d58/966d8/4_1_3.webp 920w","/static/7977922a8e37fd253a7fd34e364e6d58/34186/4_1_3.webp 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7977922a8e37fd253a7fd34e364e6d58/81c8e/4_1_3.png 230w","/static/7977922a8e37fd253a7fd34e364e6d58/08a84/4_1_3.png 460w","/static/7977922a8e37fd253a7fd34e364e6d58/c0255/4_1_3.png 920w","/static/7977922a8e37fd253a7fd34e364e6d58/0f246/4_1_3.png 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7977922a8e37fd253a7fd34e364e6d58/c0255/4_1_3.png","alt":"4 1 3","title":"4 1 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All Python-kod som skrivs i en fil kan också skrivas i tolken. Du kan till och med tilldela variabler och definiera metoder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" t "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"absolutbelopp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"siffra\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffra\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolutbelopp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nTraceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call last"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n File "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\nNameError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'siffra'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" defined\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolutbelopp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolutbelopp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tolken kan framför allt användas för att göra små kontroller. Du kan till exempel testa funktioner eller metoder – eller kolla om de existerar över huvud taget:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"teXt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"toupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nTraceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call last"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n File "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\nAttributeError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'str'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"object"}]},{"type":"text","value":" has no attribute "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'toupper'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"teXt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'TEXT'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det finns en metod som du behöver, och du minns ungefär vad dess namn är, kan det ibland vara snabbare att skippa Google och istället använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":"-funktionen i tolken. Funktionen berättar vilka metoder kan användas hos ett specifikt objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"text\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__add__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__class__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__contains__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__delattr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__dir__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__doc__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__eq__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__format__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__ge__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getattribute__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getitem__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getnewargs__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__gt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__hash__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__init__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__init_subclass__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__iter__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__le__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__len__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__lt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__mod__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__mul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__ne__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__new__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reduce__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reduce_ex__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__repr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__rmod__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__rmul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__setattr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__sizeof__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__str__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__subclasshook__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'capitalize'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'casefold'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'count'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'encode'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'endswith'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'expandtabs'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'find'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'format'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'format_map'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'index'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isalnum'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isalpha'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isascii'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isdecimal'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isdigit'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isidentifier'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'islower'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isnumeric'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isprintable'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isspace'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'istitle'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isupper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'join'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'ljust'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'lower'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'lstrip'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'maketrans'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'partition'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'replace'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rfind'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rindex'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rjust'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rpartition'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rsplit'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rstrip'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'split'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'splitlines'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'startswith'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'strip'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'swapcase'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'title'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'translate'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'upper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'zfill'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du ser ovan, har strängar i Python har en hel del metoder. För tillfället lönar det sig att ignorera metoder som har understreck i sina namn, men andra metoder kan visa sig vara nyttiga. Vissa får du kanske att fungera på egen hand, andra kan du söka mera information om på nätet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listor i Python verkar inte ha så många metoder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__add__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__class__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__contains__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__delattr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__delitem__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__dir__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__doc__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__eq__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__format__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__ge__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getattribute__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getitem__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__gt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__hash__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__iadd__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__imul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__init__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__init_subclass__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__iter__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__le__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__len__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__lt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__mul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__ne__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__new__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reduce__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reduce_ex__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__repr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reversed__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__rmul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__setattr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__setitem__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__sizeof__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__str__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__subclasshook__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'append'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'clear'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'copy'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'count'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'extend'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'index'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'insert'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'pop'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'remove'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'reverse'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'sort'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi testar på några av dem – "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"clear"}]},{"type":"text","value":" verkar lovande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reverse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan notera, gör dessa metoder just det man skulle kunna anta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att tolken inte skriver ut någonting när du kör instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror.reverse()"}]},{"type":"text","value":". Detta beror på att tolken endast skriver ut något då kodraden har ett värde. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse()"}]},{"type":"text","value":" här returnerar inget värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det ovanstående exemplet skrev vi ut värdet på listan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffor"}]},{"type":"text","value":" med att skriva namnet på variabeln. I tolken behöver man sällan använda sig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen. Det är däremot inte förbjudet att använda dem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att stänga tolken när du är klar. Kommandona "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"quit()"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit()"}]},{"type":"text","value":" fungerar bra för det här ändamålet. Även tangentkombinationerna Control + D (Linux/Mac) och Control + Z (Windows) har samma funktion. Speciellt i Visual Studio Code är det här viktigt. Om du nämligen försöker köra ett annat Python-program medan tolken är igång, kommer du att få ett ganska konstigt felmeddelande:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/030410f1be5c4a9d02209afb96fe30e4/966ce/4_1_4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA7klEQVQY023G0W6EIBCFYd9jBQZQgWEERdx1FTdbTd//lZqYthdNT76T/FXOkyMa0phzTikhekRy1nXGInrvyVqrlP4DVBNJVwbtOA8u9tA2QggAkFJKgO+4kv+MMc6uc8YAZKV0E4YQxwRaMyFqzm91fWOXK2rGfoGSoJSQwEEwwatnTFtM52MtcVrD+KA4O8rWz67PjiaDFz9Znwy+0v2dl1ea15BWjNVxnp/HeX4ceynPZdme615KWbfXvpdtC9QTInnfe+o92c5YYwjRIS4uVHcfHxgWGobGRt1FbaI2wyXqLsj2H6oj1S7KfgH1/DpLnHkC8gAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/030410f1be5c4a9d02209afb96fe30e4/a0b58/4_1_4.webp 230w","/static/030410f1be5c4a9d02209afb96fe30e4/bc10c/4_1_4.webp 460w","/static/030410f1be5c4a9d02209afb96fe30e4/966d8/4_1_4.webp 920w","/static/030410f1be5c4a9d02209afb96fe30e4/445df/4_1_4.webp 1380w","/static/030410f1be5c4a9d02209afb96fe30e4/78de1/4_1_4.webp 1840w","/static/030410f1be5c4a9d02209afb96fe30e4/df82e/4_1_4.webp 2050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/030410f1be5c4a9d02209afb96fe30e4/81c8e/4_1_4.png 230w","/static/030410f1be5c4a9d02209afb96fe30e4/08a84/4_1_4.png 460w","/static/030410f1be5c4a9d02209afb96fe30e4/c0255/4_1_4.png 920w","/static/030410f1be5c4a9d02209afb96fe30e4/b1001/4_1_4.png 1380w","/static/030410f1be5c4a9d02209afb96fe30e4/161ec/4_1_4.png 1840w","/static/030410f1be5c4a9d02209afb96fe30e4/966ce/4_1_4.png 2050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/030410f1be5c4a9d02209afb96fe30e4/c0255/4_1_4.png","alt":"4 1 4","title":"4 1 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"det-inbyggda-debuggningsverktyget","style":"position:relative;"},"children":[{"type":"text","value":"Det inbyggda debuggningsverktyget"},{"type":"element","tagName":"a","properties":{"href":"#det-inbyggda-debuggningsverktyget","ariaLabel":"det inbyggda debuggningsverktyget permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan övat en hel del på att debugga – främst med hjälp av print-satser. Visual Studio Code erbjuder dig ett ytterligare verktyg för ändamålet: en inbyggd visuell debuggare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att börja debugga måste du lägga till en breakpoint – ett ställe där debuggaren stannar körandet av koden. Du kan lägga till en breakpoint genom att klicka på det vänstra hörnet av vilken som helst kodrad i ditt program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande exemplet innehåller en icke-fungerande lösning på övningen Summan av på varandra följande tal från den förra modulen. På rad fem finns en breakpoint:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b2aa8f4cae6fd0db736f2cf1d62607f9/6e29b/4_1_5.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 39.130434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAAAv0lEQVQY042NW66DMAwFWQiO47wdY7glJEXd/8Iq+nelVmJ0/o5GM6WUZN3O8zyOo/euuubCXEVk0UVFFpGFuX7dpLrlwr33R2vtHM/R/oRrrarKzKWUlBIAmG9MsqwAUHLWbQUENEAWLV7fPM8A8y/zkpPzAbGyvJ6vozWugmjxHlMM0dEV2x/7GCPmQuSN+Vn7V/YhOu+ttTnmMUZlRgAD9+QQovc+xhBT3PemqvnDXdkYJLJEZD+Qc0R0R34DaJdB2A9pXSUAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b2aa8f4cae6fd0db736f2cf1d62607f9/a0b58/4_1_5.webp 230w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/bc10c/4_1_5.webp 460w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/966d8/4_1_5.webp 920w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/445df/4_1_5.webp 1380w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/a9e74/4_1_5.webp 1610w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b2aa8f4cae6fd0db736f2cf1d62607f9/81c8e/4_1_5.png 230w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/08a84/4_1_5.png 460w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/c0255/4_1_5.png 920w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/b1001/4_1_5.png 1380w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/6e29b/4_1_5.png 1610w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b2aa8f4cae6fd0db736f2cf1d62607f9/c0255/4_1_5.png","alt":"4 1 5","title":"4 1 5","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När breakpointen har skapats ska du välja Start debugging från Run-menyn. Det här öppnar en lista med alternativ av vilka du bör välja Python File:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/4ab241b5d1246646156624044be64914/adef7/4_1_6.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 36.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/klEQVQY043PyW6DMBSFYT9HEyZPYBsINpMxeGjSpqCmUt//aaoIRUpX7a9vec/igrxgSspgFzebYJdz8NvH9XNbb9v6/XVb398uwbvZWDO52aim4ZwLIUQpyroCVVnjUsmwnexVTBfSWdq7fPC4tVAtmVwyOaPW3nUuwiw6HqJHQHBR1Kp/3U7mzAcrRi9GX2kP6+FQNLuXfCePkCRJnDwCKINS8ODcYiajR933Y9fpYagFpxjlBD+DaZo8BSDEZVVZ67quH7WWbdtIJVVbMH6/J7+kaRY/BTAmCCFKKUKYMQ4hiuN4fyn+K0AITdMMoYxQUjBGKS0YRxj/Z/wD++1E6Al0/nYAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/4ab241b5d1246646156624044be64914/a0b58/4_1_6.webp 230w","/static/4ab241b5d1246646156624044be64914/bc10c/4_1_6.webp 460w","/static/4ab241b5d1246646156624044be64914/966d8/4_1_6.webp 920w","/static/4ab241b5d1246646156624044be64914/445df/4_1_6.webp 1380w","/static/4ab241b5d1246646156624044be64914/211fa/4_1_6.webp 1716w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/4ab241b5d1246646156624044be64914/81c8e/4_1_6.png 230w","/static/4ab241b5d1246646156624044be64914/08a84/4_1_6.png 460w","/static/4ab241b5d1246646156624044be64914/c0255/4_1_6.png 920w","/static/4ab241b5d1246646156624044be64914/b1001/4_1_6.png 1380w","/static/4ab241b5d1246646156624044be64914/adef7/4_1_6.png 1716w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/4ab241b5d1246646156624044be64914/c0255/4_1_6.png","alt":"4 1 6","title":"4 1 6","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här startar debuggaren som kör din kod normalt tills man när breakpointen: här stannar körandet upp. Om koden ber om indata ska du minnas att skriva det in i terminalen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8a01f8e6fba73266b2dd512d79dfb681/3145a/4_1_7.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 46.95652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABUElEQVQoz2WP4Y6kIBCEfYvbyyAigjoobdOgpwaGGe/93+myzGVzuf1SP6p/VLqq8j7ElPZ9n6fZGKOUBgBH5Dwtdp5hASQAmMxdiLZt5b+qHDq0ON/t2I9KaSmlUsoui1RStaJtOOd1zeq6rvk3KnSEM9rJKqW1HrTqei1/P93jMaPjFn5Y+AD4CfBhv6miM3v0k5lLuFdKCSGMMUQkpeS8bpr/HzYNbwoVEbWl6ied6rpOylZpZQG01iX/DvyFc84Yu91ujLHKr3vY431GY53FVY9Tmd11neqH4Sv/3swYE0JM0wQAxphq3X7tR9qPeMYc02tBH0Ig8tu2necZQti2jYi890QUCjnn67pSShUiNo2QUpbCn7uv63o+nyml1+v1eDxyzsdxvM3XeRxHjLFyzoWwLguO473vh74fUko55xjjuq6I6JxbClh4e1f4A1GXWrWtxlxFAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8a01f8e6fba73266b2dd512d79dfb681/a0b58/4_1_7.webp 230w","/static/8a01f8e6fba73266b2dd512d79dfb681/bc10c/4_1_7.webp 460w","/static/8a01f8e6fba73266b2dd512d79dfb681/966d8/4_1_7.webp 920w","/static/8a01f8e6fba73266b2dd512d79dfb681/445df/4_1_7.webp 1380w","/static/8a01f8e6fba73266b2dd512d79dfb681/78de1/4_1_7.webp 1840w","/static/8a01f8e6fba73266b2dd512d79dfb681/407bd/4_1_7.webp 2112w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8a01f8e6fba73266b2dd512d79dfb681/81c8e/4_1_7.png 230w","/static/8a01f8e6fba73266b2dd512d79dfb681/08a84/4_1_7.png 460w","/static/8a01f8e6fba73266b2dd512d79dfb681/c0255/4_1_7.png 920w","/static/8a01f8e6fba73266b2dd512d79dfb681/b1001/4_1_7.png 1380w","/static/8a01f8e6fba73266b2dd512d79dfb681/161ec/4_1_7.png 1840w","/static/8a01f8e6fba73266b2dd512d79dfb681/3145a/4_1_7.png 2112w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8a01f8e6fba73266b2dd512d79dfb681/c0255/4_1_7.png","alt":"4 1 7","title":"4 1 7","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till vänster finns nu en Variables-vy som innehåller de nuvarande värdena på alla aktiva variabler i koden. Du kan fortsätta att köra koden rad för rad genom att klicka på pilen som riktar neråt – Step into."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den följande bilden har loopen i koden upprepat några gånger:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/748cafbc68941c7eaaebec0f9b422806/cc418/4_1_8.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 51.73913043478261%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAABYlAAAWJQFJUiTwAAABbElEQVQoz3WP7Y6jMAxFeY62gUmcLyctJAETKKCWoWjf/41WVNpqd6U5Orp/ri3LxTB20zKv6zcRpRCNxZjaIR8QUUtE40x5HMecQmMRjbEfiybEcbjffG2UUUoJAVqbJkTjvbNGAwhQnENVccaq0+lyPrOPRVPXjWsa36BBpbSWEvgXdYG6G6JQkkk4w1spL/9ZhH7sQ3fzN3VgjJJSVCnq16tLrfT+7P3pJ4tIg0M02mqljdIAEqT8qngI0TlfspKx6ieLEKKQGpSGd3JQXHClQBt7fGEM55yxC2OsLNnflCUrunx/bL8i3euUh3mtU2+MtcYgYgjBe3+9XhFRvlFKAQAiTtO0LEvRtl1ThxhSjG3b0tVfu66bpomIcs79m6Mg6vv+kznnY7mua865ECAEAEghxLLM+74/n89931+v17Zt67ouy/L9h23b5vmYKVJKj8eDqHfOaW2U0p/LMUb8F+ccIlprtdaI+Bvm42YuwG95FAAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/748cafbc68941c7eaaebec0f9b422806/a0b58/4_1_8.webp 230w","/static/748cafbc68941c7eaaebec0f9b422806/bc10c/4_1_8.webp 460w","/static/748cafbc68941c7eaaebec0f9b422806/966d8/4_1_8.webp 920w","/static/748cafbc68941c7eaaebec0f9b422806/445df/4_1_8.webp 1380w","/static/748cafbc68941c7eaaebec0f9b422806/78de1/4_1_8.webp 1840w","/static/748cafbc68941c7eaaebec0f9b422806/75f48/4_1_8.webp 1992w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/748cafbc68941c7eaaebec0f9b422806/81c8e/4_1_8.png 230w","/static/748cafbc68941c7eaaebec0f9b422806/08a84/4_1_8.png 460w","/static/748cafbc68941c7eaaebec0f9b422806/c0255/4_1_8.png 920w","/static/748cafbc68941c7eaaebec0f9b422806/b1001/4_1_8.png 1380w","/static/748cafbc68941c7eaaebec0f9b422806/161ec/4_1_8.png 1840w","/static/748cafbc68941c7eaaebec0f9b422806/cc418/4_1_8.png 1992w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/748cafbc68941c7eaaebec0f9b422806/c0255/4_1_8.png","alt":"4 1 8","title":"4 1 8","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Debuggaren har en Debug console -flik som låter dig köra uttryck med de nuvarande variabelvärdena. Du kan till exempel kolla värdet på Boolean-uttrycket som finns i loopens villkor:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cb417e312ed6b45c9910c5a50d7fa175/cb1ac/4_1_9.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 43.913043478260875%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABLUlEQVQoz2WQ207EIBRF+Qs1GW7n1tJSyghMp3WeNPr/n6TTiUmjKysEEnbOBnXdrp9fX+u6nnMehxFZSrtTW4sxxpTnesmvpbQyhgAAeECVc63nGjgwCxEDABGmNDlnrTXGGGuNd87Zx1kfVUM/zDGFPnTSCQsTCMPnR3l/n2uDNL+k9Pzr02F/V01jarn1nYSuYxZEdM5OcSj1zILOa+e039f/qpTmaUpEzCx7mBABAV9LGcfRe2+tM8Zo/VdjrGIWACTiPUbeg3OWCIkZkFjEWqv3u0e01qfTSW3b2+WyxDjV2mpt3b08E9HjY4koxigix7zWGhFzzqrWy7Jsy3Ldttu63sIwxpRDCHthCwAiAgB/JjvnREQ93vmoTcTeQ993BGB+R+kd84+f2t90809R3YMDEQAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cb417e312ed6b45c9910c5a50d7fa175/a0b58/4_1_9.webp 230w","/static/cb417e312ed6b45c9910c5a50d7fa175/bc10c/4_1_9.webp 460w","/static/cb417e312ed6b45c9910c5a50d7fa175/966d8/4_1_9.webp 920w","/static/cb417e312ed6b45c9910c5a50d7fa175/445df/4_1_9.webp 1380w","/static/cb417e312ed6b45c9910c5a50d7fa175/78de1/4_1_9.webp 1840w","/static/cb417e312ed6b45c9910c5a50d7fa175/cb61f/4_1_9.webp 1936w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cb417e312ed6b45c9910c5a50d7fa175/81c8e/4_1_9.png 230w","/static/cb417e312ed6b45c9910c5a50d7fa175/08a84/4_1_9.png 460w","/static/cb417e312ed6b45c9910c5a50d7fa175/c0255/4_1_9.png 920w","/static/cb417e312ed6b45c9910c5a50d7fa175/b1001/4_1_9.png 1380w","/static/cb417e312ed6b45c9910c5a50d7fa175/161ec/4_1_9.png 1840w","/static/cb417e312ed6b45c9910c5a50d7fa175/cb1ac/4_1_9.png 1936w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cb417e312ed6b45c9910c5a50d7fa175/c0255/4_1_9.png","alt":"4 1 9","title":"4 1 9","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan ha flera breakpoints i din kod. När körandet av koden har stannat upp kan du starta det genom att klicka på den blå triangeln. Körandet fortsätter tills nästa breakpoint nås."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den inbyggda visuella debuggaren är ett bra alternativ till debuggning med print-satser. Det är upp till dig själv vilken metod du använder dig av i fortsättningen. Alla programmerare har sina egna preferenser, men det är alltid en bra idé att testa på olika alternativ förrän man bestämmer sig för att göra på ett visst sätt."}]}]},"html":"

Efter den här delen

    \n
  • kommer du att vara förberedd på att använda Visual Studio Code för att göra den här kursens övningar
  • \n
  • kommer du att vara bekant med den interaktiva Pythontolken, som du kan använda för att köra kod.
  • \n

Hittills har alla övningar i den här kursen gjorts direkt på kurssidorna i de inbäddade kodeditorerna. Att programmera i webbläddraren fungerar bra när man börjar lära sig att programmering. Nu är det ändå dags att börja använda en separat kodeditor som är anpassad just för att redigera kod.

Det finns en hel del olika editorer som är skräddarsydda för programmering. På den här kursen kommer vi att använda Visual Studio Code – en editor som under de senaste åren fått allt större fotfäste på marknaden.

Din uppgift är att nu installera Visual Studio Code på din dator. Det kan hända att du också måste installera Python och Python-tillägget (plugin) i Visual Studio Code. Du behöver också TMC-tillägget som tar hand om att köra de tester som finns i samband med övningarna. I TMC:s inställningar ska du välja MOOC som organisation och Python-programmering 2024 som kurs.

Här hittar du en guide som berättar hur du kan installera allt som nämnts ovan. Läs instruktionerna och gör sedan uppgiften nedan:

Skapa ett program som frågar vilken kodeditor användaren har. Programmet fortsätter köra tills användaren svarar Visual Studio Code.

Exempel:

Editor: Emacs\ninte bra\nEditor: Vim\ninte bra\nEditor: Word\nusel\nEditor: Atom\ninte bra\nEditor: Visual Studio Code\nperfekt val!

Om användaren svarar Word eller Notepad ger programmet kommentaren \"usel\". I övriga icke-giltiga fall \"inte bra\".

Programmet ska fungera oberoende hur namnet skrivs, dvs. bruket av gemener/versaler ska inte påverka:

Editor: NOTEPAD\nusel\nEditor: viSUal STudiO cODe\nperfekt val!

Kirjainten koon voi jättää huomiotta esim. muuttamalla kirjaimet pieniksi merkkijonojen metodilla lower, jota voi käyttää seuraavasti:

Det här kan åstadkomma t.ex. genom att ändra bokstavsstorleken med metoden lower, och bara använda gemener:

strang = \"Visual Studio CODE\"\nif \"visual studio code\" == strang.lower():\n    print(\"strängen hittades\")

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i dessa uppgifter, om du inte ombeds göra det.

Köra kod

I Visual Studio Code är det enklaste sättet att köra kod att klicka på triangeln uppe i hörnet till höger. Ibland kan det hända att något program blir och köra i bakgrunden – det kanske väntar på att användaren ger någon data eller är fast i en oändlig loop – utan att du märker det. Du märker det eventuellt först när du försöker köra nästa program som helt enkelt inte kommer att köra eftersom det föregående programmet tar upp alla resurser. En snabb lösning på det här är att trycka på tangenterna Control + C samtidigt. Det här avslutar den pågående processen. Nästa program borde nu kunna köra normalt.

Den interaktiva Pythontolken

Ett av de viktigaste verktygen för en Python-programmerare är den interaktiva Python-tolken.

Hur du får tolken igång beror eventuellt på den miljö där du kör Python i. På Linux och Mac kan du skriva python3 i terminalen. På Windows kan kommandot heta python. Så här ser tolken ut på Mac:

\n \n \n \n \n \n \"4\n \n \n

Du kan också testa på en webbaserad Pythontolk, till exempel: https://www.python.org/shell/.

Med hjälp av tolken kan du köra Python-kod rad för rad, i realtid. När du skriver en rad kod och trycker på Enter, kommer tolken att köra den omedelbart och visa dess resultat:

\n \n \n \n \n \n \"4\n \n \n

All Python-kod som skrivs i en fil kan också skrivas i tolken. Du kan till och med tilldela variabler och definiera metoder:

>>> t = [1,2,3,4,5]\n>>> for siffra in t:\n...   print(siffra)\n...\n1\n2\n3\n4\n5\n>>> def absolutbelopp(siffra):\n...   if siffra<0:\n...      siffra = -siffra\n...   return siffra\n...\n>>> x = 10\n>>> y = -7\n>>> absolutbelopp(siffra)\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nNameError: name 'siffra' is not defined\n>>> absolutbelopp(x)\n10\n>>> absolutbelopp(y)\n7\n>>>

Tolken kan framför allt användas för att göra små kontroller. Du kan till exempel testa funktioner eller metoder – eller kolla om de existerar över huvud taget:

>>> \"teXt\".toupper()\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nAttributeError: 'str' object has no attribute 'toupper'\n>>> \"teXt\".upper()\n'TEXT'\n>>>

Om det finns en metod som du behöver, och du minns ungefär vad dess namn är, kan det ibland vara snabbare att skippa Google och istället använda dir-funktionen i tolken. Funktionen berättar vilka metoder kan användas hos ett specifikt objekt:

>>> dir(\"text\")\n['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',\n'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',\n'__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',\n'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',\n'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',\n'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',\n'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit',\n'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join',\n'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust','rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',\n'title', 'translate', 'upper', 'zfill']

Som du ser ovan, har strängar i Python har en hel del metoder. För tillfället lönar det sig att ignorera metoder som har understreck i sina namn, men andra metoder kan visa sig vara nyttiga. Vissa får du kanske att fungera på egen hand, andra kan du söka mera information om på nätet.

Listor i Python verkar inte ha så många metoder:

>>> dir([])\n['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__',\n'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__',\n'__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',\n'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',\n'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',\n'__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop',\n'remove', 'reverse', 'sort']\n>>>

Vi testar på några av dem – reverse och clear verkar lovande:

>>> siffror = [1,2,3,4,5]\n>>> siffror.reverse()\n>>> siffror\n[5, 4, 3, 2, 1]\n>>> siffror.clear()\n>>> siffror\n[]

Som du kan notera, gör dessa metoder just det man skulle kunna anta.

Märk att tolken inte skriver ut någonting när du kör instruktionen siffror.reverse(). Detta beror på att tolken endast skriver ut något då kodraden har ett värde. Metoden reverse() här returnerar inget värde.

I det ovanstående exemplet skrev vi ut värdet på listan siffor med att skriva namnet på variabeln. I tolken behöver man sällan använda sig av print-instruktionen. Det är däremot inte förbjudet att använda dem.

Kom ihåg att stänga tolken när du är klar. Kommandona quit() och exit() fungerar bra för det här ändamålet. Även tangentkombinationerna Control + D (Linux/Mac) och Control + Z (Windows) har samma funktion. Speciellt i Visual Studio Code är det här viktigt. Om du nämligen försöker köra ett annat Python-program medan tolken är igång, kommer du att få ett ganska konstigt felmeddelande:

\n \n \n \n \n \n \"4\n \n \n

Det inbyggda debuggningsverktyget

Vi har redan övat en hel del på att debugga – främst med hjälp av print-satser. Visual Studio Code erbjuder dig ett ytterligare verktyg för ändamålet: en inbyggd visuell debuggare.

För att börja debugga måste du lägga till en breakpoint – ett ställe där debuggaren stannar körandet av koden. Du kan lägga till en breakpoint genom att klicka på det vänstra hörnet av vilken som helst kodrad i ditt program.

Det följande exemplet innehåller en icke-fungerande lösning på övningen Summan av på varandra följande tal från den förra modulen. På rad fem finns en breakpoint:

\n \n \n \n \n \n \"4\n \n \n

Till vänster finns nu en Variables-vy som innehåller de nuvarande värdena på alla aktiva variabler i koden. Du kan fortsätta att köra koden rad för rad genom att klicka på pilen som riktar neråt – Step into.

I den följande bilden har loopen i koden upprepat några gånger:

\n \n \n \n \n \n \"4\n \n \n

Debuggaren har en Debug console -flik som låter dig köra uttryck med de nuvarande variabelvärdena. Du kan till exempel kolla värdet på Boolean-uttrycket som finns i loopens villkor:

\n \n \n \n \n \n \"4\n \n \n

Du kan ha flera breakpoints i din kod. När körandet av koden har stannat upp kan du starta det genom att klicka på den blå triangeln. Körandet fortsätter tills nästa breakpoint nås.

Den inbyggda visuella debuggaren är ett bra alternativ till debuggning med print-satser. Det är upp till dig själv vilken metod du använder dig av i fortsättningen. Alla programmerare har sina egna preferenser, men det är alltid en bra idé att testa på olika alternativ förrän man bestämmer sig för att göra på ett visst sätt.

","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/1-vscode.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kommer du att vara förberedd på att använda Visual Studio Code för att göra den här kursens övningar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kommer du att vara bekant med den interaktiva Pythontolken, som du kan använda för att köra kod."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har alla övningar i den här kursen gjorts direkt på kurssidorna i de inbäddade kodeditorerna. Att programmera i webbläddraren fungerar bra när man börjar lära sig att programmering. Nu är det ändå dags att börja använda en separat kodeditor som är anpassad just för att redigera kod."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns en hel del olika editorer som är skräddarsydda för programmering. På den här kursen kommer vi att använda "},{"type":"element","tagName":"a","properties":{"href":"https://code.visualstudio.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":" – en editor som under de senaste åren fått allt större fotfäste på marknaden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din uppgift är att nu installera Visual Studio Code på din dator. Det kan hända att du också måste installera Python och Python-tillägget (plugin) i Visual Studio Code. Du behöver också TMC-tillägget som tar hand om att köra de tester som finns i samband med övningarna. I TMC:s inställningar ska du välja MOOC som organisation och Python-programmering 2024 som kurs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/installation/vscode/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Här hittar du en guide"}]},{"type":"text","value":" som berättar hur du kan installera allt som nämnts ovan. Läs instruktionerna och gör sedan uppgiften nedan:"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hello Visual Studio Code","tmcname":"osa04-01_vs_code"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar vilken kodeditor användaren har. Programmet fortsätter köra tills användaren svarar Visual Studio Code."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Editor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emacs"}]},{"type":"text","value":"\ninte bra\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Vim"}]},{"type":"text","value":"\ninte bra\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Word"}]},{"type":"text","value":"\nusel\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Atom"}]},{"type":"text","value":"\ninte bra\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":"\nperfekt val!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om användaren svarar Word eller Notepad ger programmet kommentaren \"usel\". I övriga icke-giltiga fall \"inte bra\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera oberoende hur namnet skrivs, dvs. bruket av gemener/versaler ska inte påverka:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Editor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NOTEPAD"}]},{"type":"text","value":"\nusel\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"viSUal STudiO cODe"}]},{"type":"text","value":"\nperfekt val!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kirjainten koon voi jättää huomiotta esim. muuttamalla kirjaimet pieniksi merkkijonojen metodilla "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower"}]},{"type":"text","value":", jota voi käyttää seuraavasti:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kan åstadkomma t.ex. genom att ändra bokstavsstorleken med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower"}]},{"type":"text","value":", och bara använda gemener:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Visual Studio CODE\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"visual studio code\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lower"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"strängen hittades\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"kora-kod","style":"position:relative;"},"children":[{"type":"text","value":"Köra kod"},{"type":"element","tagName":"a","properties":{"href":"#kora-kod","ariaLabel":"kora kod permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Visual Studio Code är det enklaste sättet att köra kod att klicka på triangeln uppe i hörnet till höger. Ibland kan det hända att något program blir och köra i bakgrunden – det kanske väntar på att användaren ger någon data eller är fast i en oändlig loop – utan att du märker det. Du märker det eventuellt först när du försöker köra nästa program som helt enkelt inte kommer att köra eftersom det föregående programmet tar upp alla resurser. En snabb lösning på det här är att trycka på tangenterna Control + C samtidigt. Det här avslutar den pågående processen. Nästa program borde nu kunna köra normalt."}]},{"type":"element","tagName":"h2","properties":{"id":"den-interaktiva-pythontolken","style":"position:relative;"},"children":[{"type":"text","value":"Den interaktiva Pythontolken"},{"type":"element","tagName":"a","properties":{"href":"#den-interaktiva-pythontolken","ariaLabel":"den interaktiva pythontolken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett av de viktigaste verktygen för en Python-programmerare är den interaktiva Python-tolken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur du får tolken igång beror eventuellt på den miljö där du kör Python i. På Linux och Mac kan du skriva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python3"}]},{"type":"text","value":" i terminalen. På Windows kan kommandot heta "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python"}]},{"type":"text","value":". Så här ser tolken ut på Mac:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/0f246/4_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 17.82608695652174%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAArUlEQVQI13XLywqDMBCFYd//tbroMhiYZFppBG/YJAQTN+o4sVS77bc6HPiL2/Pe1E3ftc8HlmWJiFprKWVVVQAgpUT8/gCglBJCGGPatm1ORV2Zruve49s5F2O01nrvnXMppb7vQwjHfwU+cBxHRBRCAIB5GaUUIg7DoLUGgJRSjDGEME2T995aO8/zLyainDMzExEzH8ex7zsz55yvvW0bEW2ndV2XZSGiK/4AItbceE5Q2z8AAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/a0b58/4_1_1.webp 230w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/bc10c/4_1_1.webp 460w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/966d8/4_1_1.webp 920w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/34186/4_1_1.webp 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/81c8e/4_1_1.png 230w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/08a84/4_1_1.png 460w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/c0255/4_1_1.png 920w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/0f246/4_1_1.png 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/c0255/4_1_1.png","alt":"4 1 1","title":"4 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att starta tolken i Visual Studio Code. Kör först ett program genom att klicka på triangeln. Nu borde en \"Terminal\"-del öppnas på din skärm. Där kan du skriva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python3"}]},{"type":"text","value":" (eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"):"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/895b071970fc467ecad71d69e6d70d5a/e431d/4_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 49.130434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAABYlAAAWJQFJUiTwAAABF0lEQVQoz52RWW7DIBCGuUcLw2I2Ox42O7hZrtCXqve/SgVOokh5aNVPCM32s8wQjDHFlHIOIRhjnfP78r7ttgVuEWedMuZo58mNNak4e+JHnxYsBXPBcfbaaGOsbSKntZZqMM4111qplGfySyYL4h2AASeUUiG4EJxzAAD6BOs8uwr4G6MUGPQU6Wl2K/wNym4y9hD/GwIAfyyFO7vdxLajtUbEaZqsMcZZLsUDKSXnfBzHdV0Ph8M8z62kt5PUWtd1RcRSSs4ZQ9hy+UjLmkuKMYSwx2OMy7KkNtOcOqUUcr1eL5dLrXXbtvPpVOrxO54/h4jSDEoJIeD+yNdfkG3bEHE/MvarQgiD1koPzjml1GNmrw37AVG5Vxy3ozSFAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/895b071970fc467ecad71d69e6d70d5a/a0b58/4_1_2.webp 230w","/static/895b071970fc467ecad71d69e6d70d5a/bc10c/4_1_2.webp 460w","/static/895b071970fc467ecad71d69e6d70d5a/966d8/4_1_2.webp 920w","/static/895b071970fc467ecad71d69e6d70d5a/445df/4_1_2.webp 1380w","/static/895b071970fc467ecad71d69e6d70d5a/78de1/4_1_2.webp 1840w","/static/895b071970fc467ecad71d69e6d70d5a/609ff/4_1_2.webp 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/895b071970fc467ecad71d69e6d70d5a/81c8e/4_1_2.png 230w","/static/895b071970fc467ecad71d69e6d70d5a/08a84/4_1_2.png 460w","/static/895b071970fc467ecad71d69e6d70d5a/c0255/4_1_2.png 920w","/static/895b071970fc467ecad71d69e6d70d5a/b1001/4_1_2.png 1380w","/static/895b071970fc467ecad71d69e6d70d5a/161ec/4_1_2.png 1840w","/static/895b071970fc467ecad71d69e6d70d5a/e431d/4_1_2.png 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/895b071970fc467ecad71d69e6d70d5a/c0255/4_1_2.png","alt":"4 1 2","title":"4 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också testa på en webbaserad Pythontolk, till exempel: "},{"type":"element","tagName":"a","properties":{"href":"https://www.python.org/shell/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://www.python.org/shell/"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med hjälp av tolken kan du köra Python-kod rad för rad, i realtid. När du skriver en rad kod och trycker på Enter, kommer tolken att köra den omedelbart och visa dess resultat:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7977922a8e37fd253a7fd34e364e6d58/0f246/4_1_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 19.565217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAkElEQVQI143LwQqDMAwAUP//53qQRq0zKmJALe0wB3HRZtDtOti7v6Kua0QchmEcRwCoqso5BwDOub7vjTFEpKoi8srO8xQRzYppmh4ZESHiuq7HcXjvY4zMHELw3jPzM4sxLsvCzN88z3PTNGVZtm2773tKSUTu+9Y/FETUdZ21FgAQcdu2kF3Xparph09+Az024L8JLoKAAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7977922a8e37fd253a7fd34e364e6d58/a0b58/4_1_3.webp 230w","/static/7977922a8e37fd253a7fd34e364e6d58/bc10c/4_1_3.webp 460w","/static/7977922a8e37fd253a7fd34e364e6d58/966d8/4_1_3.webp 920w","/static/7977922a8e37fd253a7fd34e364e6d58/34186/4_1_3.webp 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7977922a8e37fd253a7fd34e364e6d58/81c8e/4_1_3.png 230w","/static/7977922a8e37fd253a7fd34e364e6d58/08a84/4_1_3.png 460w","/static/7977922a8e37fd253a7fd34e364e6d58/c0255/4_1_3.png 920w","/static/7977922a8e37fd253a7fd34e364e6d58/0f246/4_1_3.png 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7977922a8e37fd253a7fd34e364e6d58/c0255/4_1_3.png","alt":"4 1 3","title":"4 1 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All Python-kod som skrivs i en fil kan också skrivas i tolken. Du kan till och med tilldela variabler och definiera metoder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" t "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"absolutbelopp"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"siffra\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffra\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolutbelopp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nTraceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call last"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n File "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\nNameError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'siffra'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" defined\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolutbelopp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolutbelopp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tolken kan framför allt användas för att göra små kontroller. Du kan till exempel testa funktioner eller metoder – eller kolla om de existerar över huvud taget:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"teXt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"toupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nTraceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call last"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n File "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\nAttributeError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'str'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"object"}]},{"type":"text","value":" has no attribute "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'toupper'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"teXt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'TEXT'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det finns en metod som du behöver, och du minns ungefär vad dess namn är, kan det ibland vara snabbare att skippa Google och istället använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":"-funktionen i tolken. Funktionen berättar vilka metoder kan användas hos ett specifikt objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"text\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__add__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__class__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__contains__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__delattr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__dir__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__doc__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__eq__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__format__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__ge__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getattribute__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getitem__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getnewargs__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__gt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__hash__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__init__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__init_subclass__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__iter__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__le__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__len__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__lt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__mod__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__mul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__ne__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__new__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reduce__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reduce_ex__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__repr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__rmod__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__rmul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__setattr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__sizeof__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__str__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__subclasshook__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'capitalize'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'casefold'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'center'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'count'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'encode'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'endswith'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'expandtabs'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'find'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'format'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'format_map'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'index'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isalnum'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isalpha'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isascii'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isdecimal'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isdigit'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isidentifier'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'islower'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isnumeric'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isprintable'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isspace'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'istitle'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isupper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'join'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'ljust'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'lower'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'lstrip'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'maketrans'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'partition'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'replace'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rfind'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rindex'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rjust'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rpartition'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rsplit'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rstrip'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'split'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'splitlines'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'startswith'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'strip'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'swapcase'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'title'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'translate'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'upper'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'zfill'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du ser ovan, har strängar i Python har en hel del metoder. För tillfället lönar det sig att ignorera metoder som har understreck i sina namn, men andra metoder kan visa sig vara nyttiga. Vissa får du kanske att fungera på egen hand, andra kan du söka mera information om på nätet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listor i Python verkar inte ha så många metoder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__add__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__class__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__contains__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__delattr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__delitem__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__dir__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__doc__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__eq__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__format__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__ge__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getattribute__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__getitem__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__gt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__hash__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__iadd__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__imul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__init__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__init_subclass__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__iter__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__le__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__len__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__lt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__mul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__ne__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__new__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reduce__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reduce_ex__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__repr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reversed__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__rmul__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__setattr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__setitem__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__sizeof__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__str__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__subclasshook__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'append'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'clear'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'copy'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'count'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'extend'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'index'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'insert'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'pop'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'remove'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'reverse'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'sort'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi testar på några av dem – "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"clear"}]},{"type":"text","value":" verkar lovande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reverse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" siffror\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan notera, gör dessa metoder just det man skulle kunna anta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att tolken inte skriver ut någonting när du kör instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror.reverse()"}]},{"type":"text","value":". Detta beror på att tolken endast skriver ut något då kodraden har ett värde. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse()"}]},{"type":"text","value":" här returnerar inget värde."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det ovanstående exemplet skrev vi ut värdet på listan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffor"}]},{"type":"text","value":" med att skriva namnet på variabeln. I tolken behöver man sällan använda sig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen. Det är däremot inte förbjudet att använda dem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att stänga tolken när du är klar. Kommandona "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"quit()"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit()"}]},{"type":"text","value":" fungerar bra för det här ändamålet. Även tangentkombinationerna Control + D (Linux/Mac) och Control + Z (Windows) har samma funktion. Speciellt i Visual Studio Code är det här viktigt. Om du nämligen försöker köra ett annat Python-program medan tolken är igång, kommer du att få ett ganska konstigt felmeddelande:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/030410f1be5c4a9d02209afb96fe30e4/966ce/4_1_4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA7klEQVQY023G0W6EIBCFYd9jBQZQgWEERdx1FTdbTd//lZqYthdNT76T/FXOkyMa0phzTikhekRy1nXGInrvyVqrlP4DVBNJVwbtOA8u9tA2QggAkFJKgO+4kv+MMc6uc8YAZKV0E4YQxwRaMyFqzm91fWOXK2rGfoGSoJSQwEEwwatnTFtM52MtcVrD+KA4O8rWz67PjiaDFz9Znwy+0v2dl1ea15BWjNVxnp/HeX4ceynPZdme615KWbfXvpdtC9QTInnfe+o92c5YYwjRIS4uVHcfHxgWGobGRt1FbaI2wyXqLsj2H6oj1S7KfgH1/DpLnHkC8gAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/030410f1be5c4a9d02209afb96fe30e4/a0b58/4_1_4.webp 230w","/static/030410f1be5c4a9d02209afb96fe30e4/bc10c/4_1_4.webp 460w","/static/030410f1be5c4a9d02209afb96fe30e4/966d8/4_1_4.webp 920w","/static/030410f1be5c4a9d02209afb96fe30e4/445df/4_1_4.webp 1380w","/static/030410f1be5c4a9d02209afb96fe30e4/78de1/4_1_4.webp 1840w","/static/030410f1be5c4a9d02209afb96fe30e4/df82e/4_1_4.webp 2050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/030410f1be5c4a9d02209afb96fe30e4/81c8e/4_1_4.png 230w","/static/030410f1be5c4a9d02209afb96fe30e4/08a84/4_1_4.png 460w","/static/030410f1be5c4a9d02209afb96fe30e4/c0255/4_1_4.png 920w","/static/030410f1be5c4a9d02209afb96fe30e4/b1001/4_1_4.png 1380w","/static/030410f1be5c4a9d02209afb96fe30e4/161ec/4_1_4.png 1840w","/static/030410f1be5c4a9d02209afb96fe30e4/966ce/4_1_4.png 2050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/030410f1be5c4a9d02209afb96fe30e4/c0255/4_1_4.png","alt":"4 1 4","title":"4 1 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"det-inbyggda-debuggningsverktyget","style":"position:relative;"},"children":[{"type":"text","value":"Det inbyggda debuggningsverktyget"},{"type":"element","tagName":"a","properties":{"href":"#det-inbyggda-debuggningsverktyget","ariaLabel":"det inbyggda debuggningsverktyget permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan övat en hel del på att debugga – främst med hjälp av print-satser. Visual Studio Code erbjuder dig ett ytterligare verktyg för ändamålet: en inbyggd visuell debuggare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att börja debugga måste du lägga till en breakpoint – ett ställe där debuggaren stannar körandet av koden. Du kan lägga till en breakpoint genom att klicka på det vänstra hörnet av vilken som helst kodrad i ditt program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande exemplet innehåller en icke-fungerande lösning på övningen Summan av på varandra följande tal från den förra modulen. På rad fem finns en breakpoint:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b2aa8f4cae6fd0db736f2cf1d62607f9/6e29b/4_1_5.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 39.130434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAAAv0lEQVQY042NW66DMAwFWQiO47wdY7glJEXd/8Iq+nelVmJ0/o5GM6WUZN3O8zyOo/euuubCXEVk0UVFFpGFuX7dpLrlwr33R2vtHM/R/oRrrarKzKWUlBIAmG9MsqwAUHLWbQUENEAWLV7fPM8A8y/zkpPzAbGyvJ6vozWugmjxHlMM0dEV2x/7GCPmQuSN+Vn7V/YhOu+ttTnmMUZlRgAD9+QQovc+xhBT3PemqvnDXdkYJLJEZD+Qc0R0R34DaJdB2A9pXSUAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b2aa8f4cae6fd0db736f2cf1d62607f9/a0b58/4_1_5.webp 230w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/bc10c/4_1_5.webp 460w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/966d8/4_1_5.webp 920w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/445df/4_1_5.webp 1380w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/a9e74/4_1_5.webp 1610w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b2aa8f4cae6fd0db736f2cf1d62607f9/81c8e/4_1_5.png 230w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/08a84/4_1_5.png 460w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/c0255/4_1_5.png 920w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/b1001/4_1_5.png 1380w","/static/b2aa8f4cae6fd0db736f2cf1d62607f9/6e29b/4_1_5.png 1610w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b2aa8f4cae6fd0db736f2cf1d62607f9/c0255/4_1_5.png","alt":"4 1 5","title":"4 1 5","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När breakpointen har skapats ska du välja Start debugging från Run-menyn. Det här öppnar en lista med alternativ av vilka du bör välja Python File:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/4ab241b5d1246646156624044be64914/adef7/4_1_6.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 36.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/klEQVQY043PyW6DMBSFYT9HEyZPYBsINpMxeGjSpqCmUt//aaoIRUpX7a9vec/igrxgSspgFzebYJdz8NvH9XNbb9v6/XVb398uwbvZWDO52aim4ZwLIUQpyroCVVnjUsmwnexVTBfSWdq7fPC4tVAtmVwyOaPW3nUuwiw6HqJHQHBR1Kp/3U7mzAcrRi9GX2kP6+FQNLuXfCePkCRJnDwCKINS8ODcYiajR933Y9fpYagFpxjlBD+DaZo8BSDEZVVZ67quH7WWbdtIJVVbMH6/J7+kaRY/BTAmCCFKKUKYMQ4hiuN4fyn+K0AITdMMoYxQUjBGKS0YRxj/Z/wD++1E6Al0/nYAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/4ab241b5d1246646156624044be64914/a0b58/4_1_6.webp 230w","/static/4ab241b5d1246646156624044be64914/bc10c/4_1_6.webp 460w","/static/4ab241b5d1246646156624044be64914/966d8/4_1_6.webp 920w","/static/4ab241b5d1246646156624044be64914/445df/4_1_6.webp 1380w","/static/4ab241b5d1246646156624044be64914/211fa/4_1_6.webp 1716w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/4ab241b5d1246646156624044be64914/81c8e/4_1_6.png 230w","/static/4ab241b5d1246646156624044be64914/08a84/4_1_6.png 460w","/static/4ab241b5d1246646156624044be64914/c0255/4_1_6.png 920w","/static/4ab241b5d1246646156624044be64914/b1001/4_1_6.png 1380w","/static/4ab241b5d1246646156624044be64914/adef7/4_1_6.png 1716w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/4ab241b5d1246646156624044be64914/c0255/4_1_6.png","alt":"4 1 6","title":"4 1 6","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här startar debuggaren som kör din kod normalt tills man när breakpointen: här stannar körandet upp. Om koden ber om indata ska du minnas att skriva det in i terminalen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8a01f8e6fba73266b2dd512d79dfb681/3145a/4_1_7.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 46.95652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABUElEQVQoz2WP4Y6kIBCEfYvbyyAigjoobdOgpwaGGe/93+myzGVzuf1SP6p/VLqq8j7ElPZ9n6fZGKOUBgBH5Dwtdp5hASQAmMxdiLZt5b+qHDq0ON/t2I9KaSmlUsoui1RStaJtOOd1zeq6rvk3KnSEM9rJKqW1HrTqei1/P93jMaPjFn5Y+AD4CfBhv6miM3v0k5lLuFdKCSGMMUQkpeS8bpr/HzYNbwoVEbWl6ied6rpOylZpZQG01iX/DvyFc84Yu91ujLHKr3vY431GY53FVY9Tmd11neqH4Sv/3swYE0JM0wQAxphq3X7tR9qPeMYc02tBH0Ig8tu2necZQti2jYi890QUCjnn67pSShUiNo2QUpbCn7uv63o+nyml1+v1eDxyzsdxvM3XeRxHjLFyzoWwLguO473vh74fUko55xjjuq6I6JxbClh4e1f4A1GXWrWtxlxFAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8a01f8e6fba73266b2dd512d79dfb681/a0b58/4_1_7.webp 230w","/static/8a01f8e6fba73266b2dd512d79dfb681/bc10c/4_1_7.webp 460w","/static/8a01f8e6fba73266b2dd512d79dfb681/966d8/4_1_7.webp 920w","/static/8a01f8e6fba73266b2dd512d79dfb681/445df/4_1_7.webp 1380w","/static/8a01f8e6fba73266b2dd512d79dfb681/78de1/4_1_7.webp 1840w","/static/8a01f8e6fba73266b2dd512d79dfb681/407bd/4_1_7.webp 2112w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8a01f8e6fba73266b2dd512d79dfb681/81c8e/4_1_7.png 230w","/static/8a01f8e6fba73266b2dd512d79dfb681/08a84/4_1_7.png 460w","/static/8a01f8e6fba73266b2dd512d79dfb681/c0255/4_1_7.png 920w","/static/8a01f8e6fba73266b2dd512d79dfb681/b1001/4_1_7.png 1380w","/static/8a01f8e6fba73266b2dd512d79dfb681/161ec/4_1_7.png 1840w","/static/8a01f8e6fba73266b2dd512d79dfb681/3145a/4_1_7.png 2112w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8a01f8e6fba73266b2dd512d79dfb681/c0255/4_1_7.png","alt":"4 1 7","title":"4 1 7","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till vänster finns nu en Variables-vy som innehåller de nuvarande värdena på alla aktiva variabler i koden. Du kan fortsätta att köra koden rad för rad genom att klicka på pilen som riktar neråt – Step into."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den följande bilden har loopen i koden upprepat några gånger:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/748cafbc68941c7eaaebec0f9b422806/cc418/4_1_8.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 51.73913043478261%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAABYlAAAWJQFJUiTwAAABbElEQVQoz3WP7Y6jMAxFeY62gUmcLyctJAETKKCWoWjf/41WVNpqd6U5Orp/ri3LxTB20zKv6zcRpRCNxZjaIR8QUUtE40x5HMecQmMRjbEfiybEcbjffG2UUUoJAVqbJkTjvbNGAwhQnENVccaq0+lyPrOPRVPXjWsa36BBpbSWEvgXdYG6G6JQkkk4w1spL/9ZhH7sQ3fzN3VgjJJSVCnq16tLrfT+7P3pJ4tIg0M02mqljdIAEqT8qngI0TlfspKx6ieLEKKQGpSGd3JQXHClQBt7fGEM55yxC2OsLNnflCUrunx/bL8i3euUh3mtU2+MtcYgYgjBe3+9XhFRvlFKAQAiTtO0LEvRtl1ThxhSjG3b0tVfu66bpomIcs79m6Mg6vv+kznnY7mua865ECAEAEghxLLM+74/n89931+v17Zt67ouy/L9h23b5vmYKVJKj8eDqHfOaW2U0p/LMUb8F+ccIlprtdaI+Bvm42YuwG95FAAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/748cafbc68941c7eaaebec0f9b422806/a0b58/4_1_8.webp 230w","/static/748cafbc68941c7eaaebec0f9b422806/bc10c/4_1_8.webp 460w","/static/748cafbc68941c7eaaebec0f9b422806/966d8/4_1_8.webp 920w","/static/748cafbc68941c7eaaebec0f9b422806/445df/4_1_8.webp 1380w","/static/748cafbc68941c7eaaebec0f9b422806/78de1/4_1_8.webp 1840w","/static/748cafbc68941c7eaaebec0f9b422806/75f48/4_1_8.webp 1992w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/748cafbc68941c7eaaebec0f9b422806/81c8e/4_1_8.png 230w","/static/748cafbc68941c7eaaebec0f9b422806/08a84/4_1_8.png 460w","/static/748cafbc68941c7eaaebec0f9b422806/c0255/4_1_8.png 920w","/static/748cafbc68941c7eaaebec0f9b422806/b1001/4_1_8.png 1380w","/static/748cafbc68941c7eaaebec0f9b422806/161ec/4_1_8.png 1840w","/static/748cafbc68941c7eaaebec0f9b422806/cc418/4_1_8.png 1992w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/748cafbc68941c7eaaebec0f9b422806/c0255/4_1_8.png","alt":"4 1 8","title":"4 1 8","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Debuggaren har en Debug console -flik som låter dig köra uttryck med de nuvarande variabelvärdena. Du kan till exempel kolla värdet på Boolean-uttrycket som finns i loopens villkor:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cb417e312ed6b45c9910c5a50d7fa175/cb1ac/4_1_9.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 43.913043478260875%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABLUlEQVQoz2WQ207EIBRF+Qs1GW7n1tJSyghMp3WeNPr/n6TTiUmjKysEEnbOBnXdrp9fX+u6nnMehxFZSrtTW4sxxpTnesmvpbQyhgAAeECVc63nGjgwCxEDABGmNDlnrTXGGGuNd87Zx1kfVUM/zDGFPnTSCQsTCMPnR3l/n2uDNL+k9Pzr02F/V01jarn1nYSuYxZEdM5OcSj1zILOa+e039f/qpTmaUpEzCx7mBABAV9LGcfRe2+tM8Zo/VdjrGIWACTiPUbeg3OWCIkZkFjEWqv3u0e01qfTSW3b2+WyxDjV2mpt3b08E9HjY4koxigix7zWGhFzzqrWy7Jsy3Ldttu63sIwxpRDCHthCwAiAgB/JjvnREQ93vmoTcTeQ993BGB+R+kd84+f2t90809R3YMDEQAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cb417e312ed6b45c9910c5a50d7fa175/a0b58/4_1_9.webp 230w","/static/cb417e312ed6b45c9910c5a50d7fa175/bc10c/4_1_9.webp 460w","/static/cb417e312ed6b45c9910c5a50d7fa175/966d8/4_1_9.webp 920w","/static/cb417e312ed6b45c9910c5a50d7fa175/445df/4_1_9.webp 1380w","/static/cb417e312ed6b45c9910c5a50d7fa175/78de1/4_1_9.webp 1840w","/static/cb417e312ed6b45c9910c5a50d7fa175/cb61f/4_1_9.webp 1936w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cb417e312ed6b45c9910c5a50d7fa175/81c8e/4_1_9.png 230w","/static/cb417e312ed6b45c9910c5a50d7fa175/08a84/4_1_9.png 460w","/static/cb417e312ed6b45c9910c5a50d7fa175/c0255/4_1_9.png 920w","/static/cb417e312ed6b45c9910c5a50d7fa175/b1001/4_1_9.png 1380w","/static/cb417e312ed6b45c9910c5a50d7fa175/161ec/4_1_9.png 1840w","/static/cb417e312ed6b45c9910c5a50d7fa175/cb1ac/4_1_9.png 1936w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cb417e312ed6b45c9910c5a50d7fa175/c0255/4_1_9.png","alt":"4 1 9","title":"4 1 9","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan ha flera breakpoints i din kod. När körandet av koden har stannat upp kan du starta det genom att klicka på den blå triangeln. Körandet fortsätter tills nästa breakpoint nås."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den inbyggda visuella debuggaren är ett bra alternativ till debuggning med print-satser. Det är upp till dig själv vilken metod du använder dig av i fortsättningen. Alla programmerare har sina egna preferenser, men det är alltid en bra idé att testa på olika alternativ förrän man bestämmer sig för att göra på ett visst sätt."}]}]},"html":"

Efter den här delen

    \n
  • kommer du att vara förberedd på att använda Visual Studio Code för att göra den här kursens övningar
  • \n
  • kommer du att vara bekant med den interaktiva Pythontolken, som du kan använda för att köra kod.
  • \n

Hittills har alla övningar i den här kursen gjorts direkt på kurssidorna i de inbäddade kodeditorerna. Att programmera i webbläddraren fungerar bra när man börjar lära sig att programmering. Nu är det ändå dags att börja använda en separat kodeditor som är anpassad just för att redigera kod.

Det finns en hel del olika editorer som är skräddarsydda för programmering. På den här kursen kommer vi att använda Visual Studio Code – en editor som under de senaste åren fått allt större fotfäste på marknaden.

Din uppgift är att nu installera Visual Studio Code på din dator. Det kan hända att du också måste installera Python och Python-tillägget (plugin) i Visual Studio Code. Du behöver också TMC-tillägget som tar hand om att köra de tester som finns i samband med övningarna. I TMC:s inställningar ska du välja MOOC som organisation och Python-programmering 2024 som kurs.

Här hittar du en guide som berättar hur du kan installera allt som nämnts ovan. Läs instruktionerna och gör sedan uppgiften nedan:

Skapa ett program som frågar vilken kodeditor användaren har. Programmet fortsätter köra tills användaren svarar Visual Studio Code.

Exempel:

Editor: Emacs\ninte bra\nEditor: Vim\ninte bra\nEditor: Word\nusel\nEditor: Atom\ninte bra\nEditor: Visual Studio Code\nperfekt val!

Om användaren svarar Word eller Notepad ger programmet kommentaren \"usel\". I övriga icke-giltiga fall \"inte bra\".

Programmet ska fungera oberoende hur namnet skrivs, dvs. bruket av gemener/versaler ska inte påverka:

Editor: NOTEPAD\nusel\nEditor: viSUal STudiO cODe\nperfekt val!

Kirjainten koon voi jättää huomiotta esim. muuttamalla kirjaimet pieniksi merkkijonojen metodilla lower, jota voi käyttää seuraavasti:

Det här kan åstadkomma t.ex. genom att ändra bokstavsstorleken med metoden lower, och bara använda gemener:

strang = \"Visual Studio CODE\"\nif \"visual studio code\" == strang.lower():\n    print(\"strängen hittades\")

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i dessa uppgifter, om du inte ombeds göra det.

Köra kod

I Visual Studio Code är det enklaste sättet att köra kod att klicka på triangeln uppe i hörnet till höger. Ibland kan det hända att något program blir och köra i bakgrunden – det kanske väntar på att användaren ger någon data eller är fast i en oändlig loop – utan att du märker det. Du märker det eventuellt först när du försöker köra nästa program som helt enkelt inte kommer att köra eftersom det föregående programmet tar upp alla resurser. En snabb lösning på det här är att trycka på tangenterna Control + C samtidigt. Det här avslutar den pågående processen. Nästa program borde nu kunna köra normalt.

Den interaktiva Pythontolken

Ett av de viktigaste verktygen för en Python-programmerare är den interaktiva Python-tolken.

Hur du får tolken igång beror eventuellt på den miljö där du kör Python i. På Linux och Mac kan du skriva python3 i terminalen. På Windows kan kommandot heta python. Så här ser tolken ut på Mac:

\n \n \n \n \n \n \"4\n \n \n

Du kan också testa på en webbaserad Pythontolk, till exempel: https://www.python.org/shell/.

Med hjälp av tolken kan du köra Python-kod rad för rad, i realtid. När du skriver en rad kod och trycker på Enter, kommer tolken att köra den omedelbart och visa dess resultat:

\n \n \n \n \n \n \"4\n \n \n

All Python-kod som skrivs i en fil kan också skrivas i tolken. Du kan till och med tilldela variabler och definiera metoder:

>>> t = [1,2,3,4,5]\n>>> for siffra in t:\n...   print(siffra)\n...\n1\n2\n3\n4\n5\n>>> def absolutbelopp(siffra):\n...   if siffra<0:\n...      siffra = -siffra\n...   return siffra\n...\n>>> x = 10\n>>> y = -7\n>>> absolutbelopp(siffra)\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nNameError: name 'siffra' is not defined\n>>> absolutbelopp(x)\n10\n>>> absolutbelopp(y)\n7\n>>>

Tolken kan framför allt användas för att göra små kontroller. Du kan till exempel testa funktioner eller metoder – eller kolla om de existerar över huvud taget:

>>> \"teXt\".toupper()\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nAttributeError: 'str' object has no attribute 'toupper'\n>>> \"teXt\".upper()\n'TEXT'\n>>>

Om det finns en metod som du behöver, och du minns ungefär vad dess namn är, kan det ibland vara snabbare att skippa Google och istället använda dir-funktionen i tolken. Funktionen berättar vilka metoder kan användas hos ett specifikt objekt:

>>> dir(\"text\")\n['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',\n'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',\n'__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',\n'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',\n'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',\n'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',\n'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit',\n'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join',\n'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust','rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',\n'title', 'translate', 'upper', 'zfill']

Som du ser ovan, har strängar i Python har en hel del metoder. För tillfället lönar det sig att ignorera metoder som har understreck i sina namn, men andra metoder kan visa sig vara nyttiga. Vissa får du kanske att fungera på egen hand, andra kan du söka mera information om på nätet.

Listor i Python verkar inte ha så många metoder:

>>> dir([])\n['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__',\n'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__',\n'__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',\n'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',\n'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',\n'__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop',\n'remove', 'reverse', 'sort']\n>>>

Vi testar på några av dem – reverse och clear verkar lovande:

>>> siffror = [1,2,3,4,5]\n>>> siffror.reverse()\n>>> siffror\n[5, 4, 3, 2, 1]\n>>> siffror.clear()\n>>> siffror\n[]

Som du kan notera, gör dessa metoder just det man skulle kunna anta.

Märk att tolken inte skriver ut någonting när du kör instruktionen siffror.reverse(). Detta beror på att tolken endast skriver ut något då kodraden har ett värde. Metoden reverse() här returnerar inget värde.

I det ovanstående exemplet skrev vi ut värdet på listan siffor med att skriva namnet på variabeln. I tolken behöver man sällan använda sig av print-instruktionen. Det är däremot inte förbjudet att använda dem.

Kom ihåg att stänga tolken när du är klar. Kommandona quit() och exit() fungerar bra för det här ändamålet. Även tangentkombinationerna Control + D (Linux/Mac) och Control + Z (Windows) har samma funktion. Speciellt i Visual Studio Code är det här viktigt. Om du nämligen försöker köra ett annat Python-program medan tolken är igång, kommer du att få ett ganska konstigt felmeddelande:

\n \n \n \n \n \n \"4\n \n \n

Det inbyggda debuggningsverktyget

Vi har redan övat en hel del på att debugga – främst med hjälp av print-satser. Visual Studio Code erbjuder dig ett ytterligare verktyg för ändamålet: en inbyggd visuell debuggare.

För att börja debugga måste du lägga till en breakpoint – ett ställe där debuggaren stannar körandet av koden. Du kan lägga till en breakpoint genom att klicka på det vänstra hörnet av vilken som helst kodrad i ditt program.

Det följande exemplet innehåller en icke-fungerande lösning på övningen Summan av på varandra följande tal från den förra modulen. På rad fem finns en breakpoint:

\n \n \n \n \n \n \"4\n \n \n

Till vänster finns nu en Variables-vy som innehåller de nuvarande värdena på alla aktiva variabler i koden. Du kan fortsätta att köra koden rad för rad genom att klicka på pilen som riktar neråt – Step into.

I den följande bilden har loopen i koden upprepat några gånger:

\n \n \n \n \n \n \"4\n \n \n

Debuggaren har en Debug console -flik som låter dig köra uttryck med de nuvarande variabelvärdena. Du kan till exempel kolla värdet på Boolean-uttrycket som finns i loopens villkor:

\n \n \n \n \n \n \"4\n \n \n

Du kan ha flera breakpoints i din kod. När körandet av koden har stannat upp kan du starta det genom att klicka på den blå triangeln. Körandet fortsätter tills nästa breakpoint nås.

Den inbyggda visuella debuggaren är ett bra alternativ till debuggning med print-satser. Det är upp till dig själv vilken metod du använder dig av i fortsättningen. Alla programmerare har sina egna preferenser, men det är alltid en bra idé att testa på olika alternativ förrän man bestämmer sig för att göra på ett visst sätt.

","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/1-vscode.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-4/2-mer-om-funktioner/page-data.json b/page-data/osa-4/2-mer-om-funktioner/page-data.json index cdac058c1..da17e98e6 100644 --- a/page-data/osa-4/2-mer-om-funktioner/page-data.json +++ b/page-data/osa-4/2-mer-om-funktioner/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-4/2-mer-om-funktioner", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du mera om argument och parametrar hos funktioner"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du returnera värden från funktioner och använda dessa värden i koden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du ge typledtrådar för parametrar och värden som returneras."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu är det dags för en snabbrepetition av funktioner i Python. Funktioner definieras med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"meddelande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här kommer från en funktion\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen kan anropas i koden på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"meddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet skulle programmet skriva ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kommer från en funktion"}]}]},{"type":"element","tagName":"h2","properties":{"id":"parametrar-och-argument-hos-en-funktion","style":"position:relative;"},"children":[{"type":"text","value":"Parametrar och argument hos en funktion"},{"type":"element","tagName":"a","properties":{"href":"#parametrar-och-argument-hos-en-funktion","ariaLabel":"parametrar och argument hos en funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion kan motta en eller flera argument. När funktionen anropas tilldelas argumenten till variabler som är definierade i funktionsdefinitionen. Dessa variabler kallas parametrar och de listas inom parenteserna som följer funktionens namn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den följande koden har funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa"}]},{"type":"text","value":" en definierad parameter, medan funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" har två:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summan av parametrarna är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"halsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej, Emilia\nSumman av parametrarna är 5"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Formell och riktig, parameter och argument"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Terminologin kring data som ges till en funktion kan kännas förvirrande. För att göra situationen svårare, använder en del källor uttryck som formella och riktiga parametrar eller formella och riktiga argument. Pythons dokumentation nämner endast termerna argument och parameter. Därför använder vi också dessa termer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad händer det egentligen när funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa(\"Emilia\")"}]},{"type":"text","value":" körs?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I funktionsdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa(namn)"}]},{"type":"text","value":" beter sig parametern namn som en normal variabel. Vi kan använda den inom funktionen på samma sätt som vi har använt variabler i huvudfunktionen i flera program hittills."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa(\"Emilia\")"}]},{"type":"text","value":" är argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"text","value":" som vilken som helst annan sträng vi stött på tidigare. Vi kan till exempel tilldela den till en variabel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alltså, när funktionsanropet körs kommer värdet på argumentet – "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"text","value":" – att tilldelas till variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":". Under den här körningen av funktionen kommer "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn = \"Emilia\""}]},{"type":"text","value":". När funktionen anropas med ett annat argument kommer värdet på namn att vara olikt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Terminologin kan kännas överflödig, men datavetenskapen strävar att vara så exakt vetenskap som möjligt. Att använda noga definierad terminologi hjälper."}]}]},{"type":"element","tagName":"h2","properties":{"id":"felmeddelanden-som-uppstar-i-testen","style":"position:relative;"},"children":[{"type":"text","value":"Felmeddelanden som uppstår i testen"},{"type":"element","tagName":"a","properties":{"href":"#felmeddelanden-som-uppstar-i-testen","ariaLabel":"felmeddelanden som uppstar i testen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De flesta uppgifter under den här kursen inkluderar automatiska test. Om programmet inte fungerar på det sätt som förutsätts av uppgiften, kommer testet att visa ett felmeddelande. Det här meddelandet kan vara till nytta – eller sen inte. Det kan vara värt att läsa meddelandet noga."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I vissa fall berättar felmeddelandet inte egentligen så mycket. I nästa övning kan du stöta på det här felmeddelandet:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/fd084faad6510920cd24630f3868f652/76435/4_2_0a.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 41.73913043478261%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAABTUlEQVQY02WQy04CQRBF+Qk16a5Hv+ienhlmcBQQEQxi1MQHoILRvStj4r+5deHar1JA4+vkrCq5dStV896HENIY8yxbGZMk+JBluVL6P8uhsdZpbWqIKKXUxpxNZmeT+elkNr6+HU+vxuMLAMDfECEt0VojYo2IGQX4TXH/zI+v9PACT2/Qu2NYV9Yare0PFX+CyyU1IkIEbVy1d1X156485vzI2rJO2pFhbYQz8kvtrJQSAFb9i/DiFsN8nKgTL4bMB1YM3cbQrR26TuFnIlximIK/odhV9bws0hiZ+auZyCkqCnC9kIyajcOtbNRMR6UfNQatcp5W07R5nVWnKmwCp408Jsl3WDJHIc59HO7uD7r9TtXubnX22r3edrfV3mkN+qEsbJYKqxHh79lAFACa1hhfV0wIEhYKAIlSkhAsJUupEQ0t/ruq/Qi/A2YuTKFDvDu7AAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/fd084faad6510920cd24630f3868f652/a0b58/4_2_0a.webp 230w","/static/fd084faad6510920cd24630f3868f652/bc10c/4_2_0a.webp 460w","/static/fd084faad6510920cd24630f3868f652/966d8/4_2_0a.webp 920w","/static/fd084faad6510920cd24630f3868f652/445df/4_2_0a.webp 1380w","/static/fd084faad6510920cd24630f3868f652/97499/4_2_0a.webp 1742w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/fd084faad6510920cd24630f3868f652/81c8e/4_2_0a.png 230w","/static/fd084faad6510920cd24630f3868f652/08a84/4_2_0a.png 460w","/static/fd084faad6510920cd24630f3868f652/c0255/4_2_0a.png 920w","/static/fd084faad6510920cd24630f3868f652/b1001/4_2_0a.png 1380w","/static/fd084faad6510920cd24630f3868f652/76435/4_2_0a.png 1742w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/fd084faad6510920cd24630f3868f652/c0255/4_2_0a.png","alt":"4 2 0a","title":"4 2 0a","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Meddelandet berättar att man borde kunna köra funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" med de specificerade argumenten:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"streck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det riktiga problemet får vi reda på när vi kör det funktionsanrop som stod i felmeddelandet. Du kan göra det genom att kopiera funktionsanropet till ditt program och klicka på triangeln:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5a22494efd4d5af2a80255c7ee6431f3/701e9/4_2_0b.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 42.173913043478265%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAABWElEQVQY023P246bMBAGYJ4jWsDGDoNPGAPmFINN0KarFVLTXqeqmvd/iCpZbaWV+um/+DWai5no+4+fUAA90iMAKQBR8iH7l4xknwyB+7EvKUiOS55H+77nkDengRu5VaYtioSkmCCUpYggfERplsZPSRxDglGcxC8vmNAEZ9HlcjkcDgAFIviM+DckREqS/0nTNP46ia7XKwCwgkkpZaWBMyEEAHwcT+njDYyxMWae57qum6bhT4yx6P7n7p9CCGsI3vtlWbz327Y558Zx7LrOWts0jbW2ruvySSlVlmV0u92WeT6v6xpC8ME599hu277r+65v29a2bVPXutTGmEprJaXgTEklOI9+ve4Xt+x+ezv5rZ9eB3e2Y2iH1Y7nbnK6GYTuhbZMdUwNspqUGbieStMLHf1+vy7jtJ3mzS22rKzSigLDlGeUYZrHOE9xnjzzKOgzj/4XGdNWXD7/XR0AAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a22494efd4d5af2a80255c7ee6431f3/a0b58/4_2_0b.webp 230w","/static/5a22494efd4d5af2a80255c7ee6431f3/bc10c/4_2_0b.webp 460w","/static/5a22494efd4d5af2a80255c7ee6431f3/966d8/4_2_0b.webp 920w","/static/5a22494efd4d5af2a80255c7ee6431f3/445df/4_2_0b.webp 1380w","/static/5a22494efd4d5af2a80255c7ee6431f3/e11e5/4_2_0b.webp 1780w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a22494efd4d5af2a80255c7ee6431f3/81c8e/4_2_0b.png 230w","/static/5a22494efd4d5af2a80255c7ee6431f3/08a84/4_2_0b.png 460w","/static/5a22494efd4d5af2a80255c7ee6431f3/c0255/4_2_0b.png 920w","/static/5a22494efd4d5af2a80255c7ee6431f3/b1001/4_2_0b.png 1380w","/static/5a22494efd4d5af2a80255c7ee6431f3/701e9/4_2_0b.png 1780w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5a22494efd4d5af2a80255c7ee6431f3/c0255/4_2_0b.png","alt":"4 2 0b","title":"4 2 0b","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De sista raderna som uppstår när programmet körs (markerade i bilden ovan) berättar att rad fyra i koden orsakar felet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IndexError"}]},{"type":"text","value":". I den förra modulen fanns det ett liknande exempel, där vi försökte använda ett index som inte var en del av en sträng. Den här gången orsakas problemet av att vi försöker hämta den första bokstaven hos en tom sträng – dvs. en sträng med längden noll."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Streck","tmcname":"osa04-02_streck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" som får två argument (bredd, sträng). Funktionen skapar ett steck genom att skriva ut det första tecknet i den angivna strängen så många gånger som angetts i den första parametern. Om den andra parametern är tom, används asterisker."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"streck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstreck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"LOL\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstreck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"%%%%%%%\nLLLLLLLLLL\n***\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"funktionsanrop-inom-funktionsanrop","style":"position:relative;"},"children":[{"type":"text","value":"Funktionsanrop inom funktionsanrop"},{"type":"element","tagName":"a","properties":{"href":"#funktionsanrop-inom-funktionsanrop","ariaLabel":"funktionsanrop inom funktionsanrop permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anropa en funktion från en annan funktion. Vi har faktiskt gjort det här flera gånger – vi har anropat "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-funktionen inom våra egna funktioner i den förra modulen. Våra egna funktioner fungerar på samma sätt. I det följande exemplet anropar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa_flera_ganger"}]},{"type":"text","value":" funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa"}]},{"type":"text","value":" så många gånger som specificerats i argumentet ganger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa_flera_ganger"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n halsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\nhalsa_flera_ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej, Emilia\nHej, Emilia\nHej, Emilia"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fyrkant (igen)","tmcname":"osa04-02a_fyrkant"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fyrkant"}]},{"type":"text","value":" som skriver ut en tio tecken bred fyrkant med den höjd som getts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fyrkant"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfyrkant"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"##########\n##########\n##########\n##########\n##########\n\n##########\n##########\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kvadrat","tmcname":"osa04-02b_kvadrat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kvadrat"}]},{"type":"text","value":", som skriver ut en kvadrat med den storlek som angetts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"#####\n#####\n#####\n#####\n#####\n\n###\n###\n###\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Speciell kvadrat","tmcname":"osa04-02c_speciell_kvadrat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"speciell_kvadrat"}]},{"type":"text","value":" som tar emot två argument – storleken samt det tecken som används för att rita kvadraten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"speciell_kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nspeciell_kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"o\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*****\n*****\n*****\n*****\n*****\n\nooo\nooo\nooo\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Triangel","tmcname":"osa04-02d_triangel"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"triangel"}]},{"type":"text","value":" som skriver ut en triangel med den höjd/bredd som angetts via argumentet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"triangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntriangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"#\n##\n###\n####\n#####\n######\n\n#\n##\n###\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Figur","tmcname":"osa04-03_figur"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"figur"}]},{"type":"text","value":" som tar emot fyra argument. Funktionen skapar en figur som består av en triangel och en fyrkant enligt de givna argumenten. Se exemplen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"figur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"X\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfigur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"o\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfigur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"X\nXX\nXXX\nXXXX\nXXXXX\n*****\n*****\n*****\n\no\noo\n++\n++\n++\n++\n\n.\n..\n...\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Försök inte lösa allt på en gång. Fokusera på en sak åt gången – börja t.ex. med att få triangeln utritad på korrekt sätt och fortsätt sedan med fyrkanten. Det här gäller vilket som helst problem inom programmering. Ta små steg och kolla att allt fungerar före du fortsätter."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Julgran","tmcname":"osa04-04_julgran"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"julgran"}]},{"type":"text","value":" som tar emot ett argument. Funktionen ska skriva ut texten \"julgran!\" samt en julgran med den angivna höjden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempelvis "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"julgran(3)"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"julgran!\n *\n ***\n*****\n *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eller till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"julgran(5)"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"julgran!\n *\n ***\n *****\n *******\n*********\n *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att antalet mellanslag till vänster om granen ska vara exakt korrekt. Även om granens form är korrekt, accepteras granen inte om den nedersta \"grenen\" inte är helt fast i det vänstra hörnet."}]}]},{"type":"element","tagName":"h2","properties":{"id":"returvardet-hos-en-funktion","style":"position:relative;"},"children":[{"type":"text","value":"Returvärdet hos en funktion"},{"type":"element","tagName":"a","properties":{"href":"#returvardet-hos-en-funktion","ariaLabel":"returvardet hos en funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner kan också returnera värden. Till exempel Pythons inbyggda funktion "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" returnerar en sträng som användaren angett. Värdet som returneras av en funktion kan lagras i en variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ett ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du vill ha ett heltalsvärde från användaren måste indatat från användaren konverteras till ett heltal. För det använder vi "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":"-funktionen som också returnerar ett värde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ett heltal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" tar den sträng som returneras av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":"-funktionen som argument och returnerar värdet i heltalsform om möjligt."}]},{"type":"element","tagName":"h2","properties":{"id":"return-satsen","style":"position:relative;"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen"},{"type":"element","tagName":"a","properties":{"href":"#return-satsen","ariaLabel":"return satsen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner som du själv definierar kan också returnera värden. För det här ändamålet behöver du "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen. Till exempel funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" nedan returnerar summan av dess parametrar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summa:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Summa: 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett annat exempel på ett returnerat värde. Funktionen ber om användarens namn och returnerar den sträng som användaren anger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fraga_namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är ditt namn? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" namn\n\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fraga_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad är ditt namn? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Anna"}]},{"type":"text","value":"\nHej, Anna"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen avslutar körandet av funktionen genast. Det här är ett sätt att göra en jämförelsefunktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"minst"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén är att om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" är mindre än "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" så kommer funktionen att returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och avslutas direkt. Annars fortsätter man till nästa rad som returnerar värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". En return-sats kan inte köras flera gånger i samma funktion under samma funktionsanrop."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda dig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen även om funktionen inte returnerar något värde. Då är dess uppgift helt enkelt att avsluta körandet av funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"???\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mårten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om argumentet som sparas i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" är en tom sträng, kommer texten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"???"}]},{"type":"text","value":" att skrivas ut och funktionen avslutas:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej, Emilia\n???\nHej, Mårten"}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-anvanda-returvarden-fran-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Att använda returvärden från funktioner"},{"type":"element","tagName":"a","properties":{"href":"#att-anvanda-returvarden-fran-funktioner","ariaLabel":"att anvanda returvarden fran funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi känner redan till att värden som returneras från funktioner kan lagras i variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summan är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Summan är 10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Returvärdet hos en funktion kan jämföras med vilket som helst annat värde. Det är inte nödvändigt att lagra värdet i en variabel för att ge det som argument till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summan är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Returvärdet hos en funktion kan vara ett argument för en funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"differens"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"b\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" differens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Svaret är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svaret är 2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet körs de inre funktionsanropen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa(5, 2)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa(2, 3)"}]},{"type":"text","value":" först. Värdena som returneras (sju och fem) används som argument för det yttre funktionsanropet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det yttre funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"differens(7, 5)"}]},{"type":"text","value":" returnerar värdet 2, som lagras i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":" och skrivs ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att sammanfatta: värden som returneras av funktioner fungerar som alla andra värden i Python. De kan skrivas ut, lagras i variabler och användas i uttryck och som argument i funktionsanrop."}]},{"type":"element","tagName":"h2","properties":{"id":"skillnaden-mellan-return-och-print","style":"position:relative;"},"children":[{"type":"text","value":"Skillnaden mellan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"a","properties":{"href":"#skillnaden-mellan-return-och-print","ariaLabel":"skillnaden mellan return och print permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland kan skillnaden mellan att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" inom en funktion vara oklara. Vi undersöker två olika sätt att göra en funktion som berättar vilket av två värden är större:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"max1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"max2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" max1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmax2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n7"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Båda versionerna verkar fungera i och med att det större av värdena skrivs ut korrekt. Det finns ändå en central skillnad mellan dessa två funktioner. Den första, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max1"}]},{"type":"text","value":", skriver inte ut något. Den använder sig istället av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen. Om vi kör den följande kodraden…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"max1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…verkar ingenting hända. Funktionens returvärde måste användas på något sätt i den kod som anropar funktionen. Det kan till exempel lagras i en variabel och skrivas ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"svar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" max1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den andra versionen, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max2"}]},{"type":"text","value":", använder sig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen inom funktionen. Om vi vill se värdet, kan vi helt enkelt anropa funktionen…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"max2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…och det större värdet kommer att skrivas ut. Det dåliga med den här funktionen är att värdet som funktionen räknar ut inte kan användas av själva programmet. Därför är funktioner som returnerar ett värde ofta ett bättre alternativ."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Störst av talen","tmcname":"osa04-05_storst"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storst"}]},{"type":"text","value":" som returnerar den största siffran av de tre givna argumenten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Käyttöesimerkki"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 99"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 0"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lika tecken","tmcname":"osa04-06_lika"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lika"}]},{"type":"text","value":" som får som argument en sträng och två heltal som syftar till index. Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" beroende på om tecknen vid dessa index i strängen är lika eller inte. Om något av indexen inte finns i strängen ska funktionen returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Några exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# e och e är lika"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kodexpert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# k och x är olika"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kodexpert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det andra indexet finns inte i strängen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kodexpert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Första, andra och sista","tmcname":"osa04-07_forsta_andra_sista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa tre funktioner: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord_ett"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord_tva"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sista_ordet"}]},{"type":"text","value":". Alla funktioner får som argument en sträng som består av en mening."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionerna returnerar meningens första, andra eller sista ord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att strängen alltid innehåller minst två ord och exakt ett mellanslag mellan orden. Det finns inga mellanslag i början eller slutet av strängen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"mening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jag gillar blåbärspaj så länge den inte innehåller ägg\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# jag"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_tva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# gillar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ägg"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"jag\ngillar\nägg"}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"mening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"la-la gunilla\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_tva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# gunilla"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# gunilla"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"typen-av-ett-argument","style":"position:relative;"},"children":[{"type":"text","value":"Typen av ett argument"},{"type":"element","tagName":"a","properties":{"href":"#typen-av-ett-argument","ariaLabel":"typen av ett argument permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är en kort repetition av de datatyper vi bekantat oss med hittills:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Tyyppi"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"I Python"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Exempel"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Heltal"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"23"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Flyttal"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-0.45"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Sträng"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Petra Python\""}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Sanningsvärde"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du anropar en funktion, kommer den bara att fungera korrekt då argumenten du ger åt den är av korrekt typ. Ta en titt på det här exemplet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"meddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"meddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen fungerar korrekt om vi anropar den på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hejsan\nHejsan\nHejsan\nHejsan\nHejsan"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi däremot ger funktionen ett argument av fel typ så kommer funktionen inte att fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: '>' not supported between instances of 'str' and 'int'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problemet här är att den andra parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ganger"}]},{"type":"text","value":" jämförs med ett heltal (0) på den andra raden av funktionsdefinitionen. Det givna argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"text","value":" är en sträng och inte ett heltal. Strängar och heltal kan inte jämföras så här enkelt – därmed felmeddelandet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att undvika problem som dessa kan du inkludera typledtrådar (type hints) när du definierar funktioner. Typledtråden berättar vilken typ av argument funktionen förväntar sig motta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"meddelande "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"meddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här berättar för alla användare av funktionen att argumentet som lagras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"meddelande"}]},{"type":"text","value":" ska vara en sträng medan argumentet som lagras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ganger"}]},{"type":"text","value":" ska vara ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Också typen av returvärdet kan specificeras när funktionen definieras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fraga_namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är ditt namn? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" namn"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här berättar för användaren att funktionen borde returnera en sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Typledtrådar är bokstavligen ledtrådar. Det är inte en garanti och kan inte säkerställa att felaktiga datatyper inte ges till eller returneras av en funktion. Om det här sker kommer funktionen ändå att köras, men den fungerar inte nödvändigtvis korrekt."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du mera om argument och parametrar hos funktioner
  • \n
  • kan du returnera värden från funktioner och använda dessa värden i koden
  • \n
  • kan du ge typledtrådar för parametrar och värden som returneras.
  • \n

Nu är det dags för en snabbrepetition av funktioner i Python. Funktioner definieras med nyckelordet def:

def meddelande():\n    print(\"Det här kommer från en funktion\")

Funktionen kan anropas i koden på följande sätt:

meddelande()

I det här fallet skulle programmet skriva ut följande:

Det här kommer från en funktion

Parametrar och argument hos en funktion

En funktion kan motta en eller flera argument. När funktionen anropas tilldelas argumenten till variabler som är definierade i funktionsdefinitionen. Dessa variabler kallas parametrar och de listas inom parenteserna som följer funktionens namn.

I den följande koden har funktionen halsa en definierad parameter, medan funktionen summa har två:

def halsa(namn):\n    print(\"Hej,\", namn)\n\ndef summa(a, b):\n    print(\"Summan av parametrarna är\", a + b)
halsa(\"Emilia\")\nsumma(2, 3)

Hej, Emilia\nSumman av parametrarna är 5

Terminologin kring data som ges till en funktion kan kännas förvirrande. För att göra situationen svårare, använder en del källor uttryck som formella och riktiga parametrar eller formella och riktiga argument. Pythons dokumentation nämner endast termerna argument och parameter. Därför använder vi också dessa termer.

Vad händer det egentligen när funktionsanropet halsa(\"Emilia\") körs?

I funktionsdefinitionen halsa(namn) beter sig parametern namn som en normal variabel. Vi kan använda den inom funktionen på samma sätt som vi har använt variabler i huvudfunktionen i flera program hittills.

I funktionsanropet halsa(\"Emilia\") är argumentet \"Emilia\" som vilken som helst annan sträng vi stött på tidigare. Vi kan till exempel tilldela den till en variabel.

Alltså, när funktionsanropet körs kommer värdet på argumentet – \"Emilia\" – att tilldelas till variabeln namn. Under den här körningen av funktionen kommer namn = \"Emilia\". När funktionen anropas med ett annat argument kommer värdet på namn att vara olikt.

Terminologin kan kännas överflödig, men datavetenskapen strävar att vara så exakt vetenskap som möjligt. Att använda noga definierad terminologi hjälper.

Felmeddelanden som uppstår i testen

De flesta uppgifter under den här kursen inkluderar automatiska test. Om programmet inte fungerar på det sätt som förutsätts av uppgiften, kommer testet att visa ett felmeddelande. Det här meddelandet kan vara till nytta – eller sen inte. Det kan vara värt att läsa meddelandet noga.

I vissa fall berättar felmeddelandet inte egentligen så mycket. I nästa övning kan du stöta på det här felmeddelandet:

\n \n \n \n \n \n \"4\n \n \n

Meddelandet berättar att man borde kunna köra funktionen streck med de specificerade argumenten:

streck(5, \"\")

Det riktiga problemet får vi reda på när vi kör det funktionsanrop som stod i felmeddelandet. Du kan göra det genom att kopiera funktionsanropet till ditt program och klicka på triangeln:

\n \n

Skapa funktionen streck som får två argument (bredd, sträng). Funktionen skapar ett steck genom att skriva ut det första tecknet i den angivna strängen så många gånger som angetts i den första parametern. Om den andra parametern är tom, används asterisker.

Exempel:

streck(7, \"%\")\nstreck(10, \"LOL\")\nstreck(3, \"\")
\n%%%%%%%\nLLLLLLLLLL\n***\n

Funktionsanrop inom funktionsanrop

Du kan anropa en funktion från en annan funktion. Vi har faktiskt gjort det här flera gånger – vi har anropat print-funktionen inom våra egna funktioner i den förra modulen. Våra egna funktioner fungerar på samma sätt. I det följande exemplet anropar funktionen halsa_flera_ganger funktionen halsa så många gånger som specificerats i argumentet ganger:

def halsa(namn):\n    print(\"Hej,\", namn)\n\ndef halsa_flera_ganger(namn, ganger):\n    while ganger > 0:\n        halsa(namn)\n        ganger -= 1\n\nhalsa_flera_ganger(\"Emilia\", 3)

Hej, Emilia\nHej, Emilia\nHej, Emilia

Skapa funktionen fyrkant som skriver ut en tio tecken bred fyrkant med den höjd som getts som argument.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

fyrkant(5)\nprint()\nfyrkant(2)
\n##########\n##########\n##########\n##########\n##########\n\n##########\n##########\n

Skapa funktionen kvadrat, som skriver ut en kvadrat med den storlek som angetts som argument.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

kvadrat(5)\nprint()\nkvadrat(3)
\n#####\n#####\n#####\n#####\n#####\n\n###\n###\n###\n

Skapa funktionen speciell_kvadrat som tar emot två argument – storleken samt det tecken som används för att rita kvadraten.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

speciell_kvadrat(5, \"*\")\nprint()\nspeciell_kvadrat(3, \"o\")
\n*****\n*****\n*****\n*****\n*****\n\nooo\nooo\nooo\n

Skapa funktionen triangel som skriver ut en triangel med den höjd/bredd som angetts via argumentet.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

triangel(6)\nprint()\ntriangel(3)
\n#\n##\n###\n####\n#####\n######\n\n#\n##\n###\n

Skapa funktionen figur som tar emot fyra argument. Funktionen skapar en figur som består av en triangel och en fyrkant enligt de givna argumenten. Se exemplen.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

figur(5, \"X\", 3, \"*\")\nprint()\nfigur(2, \"o\", 4, \"+\")\nprint()\nfigur(3, \".\", 0, \",\")
\nX\nXX\nXXX\nXXXX\nXXXXX\n*****\n*****\n*****\n\no\noo\n++\n++\n++\n++\n\n.\n..\n...\n

Tips: Försök inte lösa allt på en gång. Fokusera på en sak åt gången – börja t.ex. med att få triangeln utritad på korrekt sätt och fortsätt sedan med fyrkanten. Det här gäller vilket som helst problem inom programmering. Ta små steg och kolla att allt fungerar före du fortsätter.

Skapa funktionen julgran som tar emot ett argument. Funktionen ska skriva ut texten \"julgran!\" samt en julgran med den angivna höjden.

Exempelvis julgran(3):

\njulgran!\n  *\n ***\n*****\n  *\n

Eller till exempel julgran(5):

\njulgran!\n    *\n   ***\n  *****\n *******\n*********\n    *\n

Observera att antalet mellanslag till vänster om granen ska vara exakt korrekt. Även om granens form är korrekt, accepteras granen inte om den nedersta \"grenen\" inte är helt fast i det vänstra hörnet.

Returvärdet hos en funktion

Funktioner kan också returnera värden. Till exempel Pythons inbyggda funktion input returnerar en sträng som användaren angett. Värdet som returneras av en funktion kan lagras i en variabel:

ord = input(\"Ange ett ord: \")

När du vill ha ett heltalsvärde från användaren måste indatat från användaren konverteras till ett heltal. För det använder vi int-funktionen som också returnerar ett värde:

siffra = int(input(\"Ange ett heltal: \"))

Funktionen int tar den sträng som returneras av input-funktionen som argument och returnerar värdet i heltalsform om möjligt.

return-satsen

Funktioner som du själv definierar kan också returnera värden. För det här ändamålet behöver du return-satsen. Till exempel funktionen summa nedan returnerar summan av dess parametrar:

def summa(a, b):\n    return a + b\n\nsvar = summa(2, 3)\n\nprint(\"Summa:\", svar)

Summa: 5

Här är ett annat exempel på ett returnerat värde. Funktionen ber om användarens namn och returnerar den sträng som användaren anger:

def fraga_namn():\n    namn = input(\"Vad är ditt namn? \")\n    return namn\n\nnamn = fraga_namn()\nprint(\"Hej,\", namn)

Vad är ditt namn? Anna\nHej, Anna

return-satsen avslutar körandet av funktionen genast. Det här är ett sätt att göra en jämförelsefunktion:

def minst(a,b):\n    if a < b:\n        return a\n    return b\n\nprint(minst(3, 7))\nprint(minst(5, 2))

Idén är att om a är mindre än b så kommer funktionen att returnera a och avslutas direkt. Annars fortsätter man till nästa rad som returnerar värdet b. En return-sats kan inte köras flera gånger i samma funktion under samma funktionsanrop.

3\n2

Du kan använda dig av return-satsen även om funktionen inte returnerar något värde. Då är dess uppgift helt enkelt att avsluta körandet av funktionen:

def halsa(namn):\n    if namn == \"\":\n        print(\"???\")\n        return\n    print(\"Hej,\", namn)\n\nhalsa(\"Emilia\")\nhalsa(\"\")\nhalsa(\"Mårten\")

Om argumentet som sparas i variabeln namn är en tom sträng, kommer texten ??? att skrivas ut och funktionen avslutas:

Hej, Emilia\n???\nHej, Mårten

Att använda returvärden från funktioner

Vi känner redan till att värden som returneras från funktioner kan lagras i variabler:

def summa(a, b):\n    return a + b\n\nresultat = summa(4, 6)\nprint(\"Summan är\", resultat)

Summan är 10

Returvärdet hos en funktion kan jämföras med vilket som helst annat värde. Det är inte nödvändigt att lagra värdet i en variabel för att ge det som argument till print-instruktionen:

print(\"Summan är\", summa(4, 6))

Returvärdet hos en funktion kan vara ett argument för en funktion:

def summa(a, b):\n    return a+b\n\ndef differens(a, b):\n    return a-b\n\nresultat = differens(summa(5, 2), summa(2, 3))\nprint(\"Svaret är\", resultat)

Svaret är 2

I det här fallet körs de inre funktionsanropen summa(5, 2) och summa(2, 3) först. Värdena som returneras (sju och fem) används som argument för det yttre funktionsanropet.

Det yttre funktionsanropet differens(7, 5) returnerar värdet 2, som lagras i variabeln resultat och skrivs ut.

För att sammanfatta: värden som returneras av funktioner fungerar som alla andra värden i Python. De kan skrivas ut, lagras i variabler och användas i uttryck och som argument i funktionsanrop.

Skillnaden mellan return och print

Ibland kan skillnaden mellan att använda return och print inom en funktion vara oklara. Vi undersöker två olika sätt att göra en funktion som berättar vilket av två värden är större:

def max1(a, b):\n    if a > b:\n        return a\n    else:\n        return b\n\ndef max2(a, b):\n    if a > b:\n        print(a)\n    else:\n        print(b)\n\nsvar = max1(3, 5)\nprint(svar)\n\nmax2(7, 2)

5\n7

Båda versionerna verkar fungera i och med att det större av värdena skrivs ut korrekt. Det finns ändå en central skillnad mellan dessa två funktioner. Den första, max1, skriver inte ut något. Den använder sig istället av return-satsen. Om vi kör den följande kodraden…

max1(3, 5)

…verkar ingenting hända. Funktionens returvärde måste användas på något sätt i den kod som anropar funktionen. Det kan till exempel lagras i en variabel och skrivas ut:

svar = max1(3, 5)\nprint(svar)

Den andra versionen, max2, använder sig av print-instruktionen inom funktionen. Om vi vill se värdet, kan vi helt enkelt anropa funktionen…

max2(7, 5)

…och det större värdet kommer att skrivas ut. Det dåliga med den här funktionen är att värdet som funktionen räknar ut inte kan användas av själva programmet. Därför är funktioner som returnerar ett värde ofta ett bättre alternativ.

Skapa funktionen storst som returnerar den största siffran av de tre givna argumenten.

Käyttöesimerkki

print(storst(3, 4, 1)) # 4\nprint(storst(99, -4, 7)) # 99\nprint(storst(0, 0, 0)) # 0

Skapa funktionen lika som får som argument en sträng och två heltal som syftar till index. Funktionen ska returnera True eller False beroende på om tecknen vid dessa index i strängen är lika eller inte. Om något av indexen inte finns i strängen ska funktionen returnera False.

Några exempel:

# e och e är lika\nprint(lika(\"kodexpert\", 3, 6)) # True\n\n# k och x är olika\nprint(lika(\"kodexpert\", 0, 4)) # False\n\n# det andra indexet finns inte i strängen\nprint(lika(\"kodexpert\", 0, 20)) # False

Skapa tre funktioner: ord_ett, ord_tva och sista_ordet. Alla funktioner får som argument en sträng som består av en mening.

Funktionerna returnerar meningens första, andra eller sista ord.

Du kan anta att strängen alltid innehåller minst två ord och exakt ett mellanslag mellan orden. Det finns inga mellanslag i början eller slutet av strängen.

mening = \"jag gillar blåbärspaj så länge den inte innehåller ägg\"\n\nprint(ord_ett(mening)) # jag\nprint(ord_tva(mening)) # gillar\nprint(sista_ordet(mening)) # ägg

jag\ngillar\nägg

mening = \"la-la gunilla\"\n\nprint(ord_tva(mening)) # gunilla\nprint(sista_ordet(mening)) # gunilla

Typen av ett argument

Här är en kort repetition av de datatyper vi bekantat oss med hittills:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
TyyppiI PythonExempel
Heltalint23
Flyttalfloat-0.45
Strängstr\"Petra Python\"
SanningsvärdeboolTrue

När du anropar en funktion, kommer den bara att fungera korrekt då argumenten du ger åt den är av korrekt typ. Ta en titt på det här exemplet:

def skriv_ut_flera_ganger(meddelande, ganger):\n    while ganger > 0:\n        print(meddelande)\n        ganger -= 1

Funktionen fungerar korrekt om vi anropar den på följande sätt:

skriv_ut_flera_ganger(\"Hejsan\", 5)

Hejsan\nHejsan\nHejsan\nHejsan\nHejsan

Om vi däremot ger funktionen ett argument av fel typ så kommer funktionen inte att fungera:

skriv_ut_flera_ganger(\"Hejsan\", \"Emilia\")

TypeError: '>' not supported between instances of 'str' and 'int'

Problemet här är att den andra parametern ganger jämförs med ett heltal (0) på den andra raden av funktionsdefinitionen. Det givna argumentet \"Emilia\" är en sträng och inte ett heltal. Strängar och heltal kan inte jämföras så här enkelt – därmed felmeddelandet.

För att undvika problem som dessa kan du inkludera typledtrådar (type hints) när du definierar funktioner. Typledtråden berättar vilken typ av argument funktionen förväntar sig motta:

def skriv_ut_flera_ganger(meddelande : str, ganger : int):\n    while ganger > 0:\n        print(meddelande)\n        ganger -= 1

Det här berättar för alla användare av funktionen att argumentet som lagras i meddelande ska vara en sträng medan argumentet som lagras i ganger ska vara ett heltal.

Också typen av returvärdet kan specificeras när funktionen definieras:

def fraga_namn() -> str:\n    namn = input(\"Vad är ditt namn? \")\n    return namn

Det här berättar för användaren att funktionen borde returnera en sträng.

Obs! Typledtrådar är bokstavligen ledtrådar. Det är inte en garanti och kan inte säkerställa att felaktiga datatyper inte ges till eller returneras av en funktion. Om det här sker kommer funktionen ändå att köras, men den fungerar inte nödvändigtvis korrekt.

","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/2-mer-om-funktioner.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du mera om argument och parametrar hos funktioner"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du returnera värden från funktioner och använda dessa värden i koden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du ge typledtrådar för parametrar och värden som returneras."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu är det dags för en snabbrepetition av funktioner i Python. Funktioner definieras med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"meddelande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här kommer från en funktion\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen kan anropas i koden på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"meddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet skulle programmet skriva ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här kommer från en funktion"}]}]},{"type":"element","tagName":"h2","properties":{"id":"parametrar-och-argument-hos-en-funktion","style":"position:relative;"},"children":[{"type":"text","value":"Parametrar och argument hos en funktion"},{"type":"element","tagName":"a","properties":{"href":"#parametrar-och-argument-hos-en-funktion","ariaLabel":"parametrar och argument hos en funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion kan motta en eller flera argument. När funktionen anropas tilldelas argumenten till variabler som är definierade i funktionsdefinitionen. Dessa variabler kallas parametrar och de listas inom parenteserna som följer funktionens namn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den följande koden har funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa"}]},{"type":"text","value":" en definierad parameter, medan funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" har två:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summan av parametrarna är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"halsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej, Emilia\nSumman av parametrarna är 5"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Formell och riktig, parameter och argument"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Terminologin kring data som ges till en funktion kan kännas förvirrande. För att göra situationen svårare, använder en del källor uttryck som formella och riktiga parametrar eller formella och riktiga argument. Pythons dokumentation nämner endast termerna argument och parameter. Därför använder vi också dessa termer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad händer det egentligen när funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa(\"Emilia\")"}]},{"type":"text","value":" körs?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I funktionsdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa(namn)"}]},{"type":"text","value":" beter sig parametern namn som en normal variabel. Vi kan använda den inom funktionen på samma sätt som vi har använt variabler i huvudfunktionen i flera program hittills."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa(\"Emilia\")"}]},{"type":"text","value":" är argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"text","value":" som vilken som helst annan sträng vi stött på tidigare. Vi kan till exempel tilldela den till en variabel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alltså, när funktionsanropet körs kommer värdet på argumentet – "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"text","value":" – att tilldelas till variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":". Under den här körningen av funktionen kommer "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn = \"Emilia\""}]},{"type":"text","value":". När funktionen anropas med ett annat argument kommer värdet på namn att vara olikt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Terminologin kan kännas överflödig, men datavetenskapen strävar att vara så exakt vetenskap som möjligt. Att använda noga definierad terminologi hjälper."}]}]},{"type":"element","tagName":"h2","properties":{"id":"felmeddelanden-som-uppstar-i-testen","style":"position:relative;"},"children":[{"type":"text","value":"Felmeddelanden som uppstår i testen"},{"type":"element","tagName":"a","properties":{"href":"#felmeddelanden-som-uppstar-i-testen","ariaLabel":"felmeddelanden som uppstar i testen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De flesta uppgifter under den här kursen inkluderar automatiska test. Om programmet inte fungerar på det sätt som förutsätts av uppgiften, kommer testet att visa ett felmeddelande. Det här meddelandet kan vara till nytta – eller sen inte. Det kan vara värt att läsa meddelandet noga."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I vissa fall berättar felmeddelandet inte egentligen så mycket. I nästa övning kan du stöta på det här felmeddelandet:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/fd084faad6510920cd24630f3868f652/76435/4_2_0a.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 41.73913043478261%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAABTUlEQVQY02WQy04CQRBF+Qk16a5Hv+ienhlmcBQQEQxi1MQHoILRvStj4r+5deHar1JA4+vkrCq5dStV896HENIY8yxbGZMk+JBluVL6P8uhsdZpbWqIKKXUxpxNZmeT+elkNr6+HU+vxuMLAMDfECEt0VojYo2IGQX4TXH/zI+v9PACT2/Qu2NYV9Yare0PFX+CyyU1IkIEbVy1d1X156485vzI2rJO2pFhbYQz8kvtrJQSAFb9i/DiFsN8nKgTL4bMB1YM3cbQrR26TuFnIlximIK/odhV9bws0hiZ+auZyCkqCnC9kIyajcOtbNRMR6UfNQatcp5W07R5nVWnKmwCp408Jsl3WDJHIc59HO7uD7r9TtXubnX22r3edrfV3mkN+qEsbJYKqxHh79lAFACa1hhfV0wIEhYKAIlSkhAsJUupEQ0t/ruq/Qi/A2YuTKFDvDu7AAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/fd084faad6510920cd24630f3868f652/a0b58/4_2_0a.webp 230w","/static/fd084faad6510920cd24630f3868f652/bc10c/4_2_0a.webp 460w","/static/fd084faad6510920cd24630f3868f652/966d8/4_2_0a.webp 920w","/static/fd084faad6510920cd24630f3868f652/445df/4_2_0a.webp 1380w","/static/fd084faad6510920cd24630f3868f652/97499/4_2_0a.webp 1742w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/fd084faad6510920cd24630f3868f652/81c8e/4_2_0a.png 230w","/static/fd084faad6510920cd24630f3868f652/08a84/4_2_0a.png 460w","/static/fd084faad6510920cd24630f3868f652/c0255/4_2_0a.png 920w","/static/fd084faad6510920cd24630f3868f652/b1001/4_2_0a.png 1380w","/static/fd084faad6510920cd24630f3868f652/76435/4_2_0a.png 1742w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/fd084faad6510920cd24630f3868f652/c0255/4_2_0a.png","alt":"4 2 0a","title":"4 2 0a","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Meddelandet berättar att man borde kunna köra funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" med de specificerade argumenten:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"streck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det riktiga problemet får vi reda på när vi kör det funktionsanrop som stod i felmeddelandet. Du kan göra det genom att kopiera funktionsanropet till ditt program och klicka på triangeln:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5a22494efd4d5af2a80255c7ee6431f3/701e9/4_2_0b.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 42.173913043478265%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAABWElEQVQY023P246bMBAGYJ4jWsDGDoNPGAPmFINN0KarFVLTXqeqmvd/iCpZbaWV+um/+DWai5no+4+fUAA90iMAKQBR8iH7l4xknwyB+7EvKUiOS55H+77nkDengRu5VaYtioSkmCCUpYggfERplsZPSRxDglGcxC8vmNAEZ9HlcjkcDgAFIviM+DckREqS/0nTNP46ia7XKwCwgkkpZaWBMyEEAHwcT+njDYyxMWae57qum6bhT4yx6P7n7p9CCGsI3vtlWbz327Y558Zx7LrOWts0jbW2ruvySSlVlmV0u92WeT6v6xpC8ME599hu277r+65v29a2bVPXutTGmEprJaXgTEklOI9+ve4Xt+x+ezv5rZ9eB3e2Y2iH1Y7nbnK6GYTuhbZMdUwNspqUGbieStMLHf1+vy7jtJ3mzS22rKzSigLDlGeUYZrHOE9xnjzzKOgzj/4XGdNWXD7/XR0AAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a22494efd4d5af2a80255c7ee6431f3/a0b58/4_2_0b.webp 230w","/static/5a22494efd4d5af2a80255c7ee6431f3/bc10c/4_2_0b.webp 460w","/static/5a22494efd4d5af2a80255c7ee6431f3/966d8/4_2_0b.webp 920w","/static/5a22494efd4d5af2a80255c7ee6431f3/445df/4_2_0b.webp 1380w","/static/5a22494efd4d5af2a80255c7ee6431f3/e11e5/4_2_0b.webp 1780w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a22494efd4d5af2a80255c7ee6431f3/81c8e/4_2_0b.png 230w","/static/5a22494efd4d5af2a80255c7ee6431f3/08a84/4_2_0b.png 460w","/static/5a22494efd4d5af2a80255c7ee6431f3/c0255/4_2_0b.png 920w","/static/5a22494efd4d5af2a80255c7ee6431f3/b1001/4_2_0b.png 1380w","/static/5a22494efd4d5af2a80255c7ee6431f3/701e9/4_2_0b.png 1780w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5a22494efd4d5af2a80255c7ee6431f3/c0255/4_2_0b.png","alt":"4 2 0b","title":"4 2 0b","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De sista raderna som uppstår när programmet körs (markerade i bilden ovan) berättar att rad fyra i koden orsakar felet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IndexError"}]},{"type":"text","value":". I den förra modulen fanns det ett liknande exempel, där vi försökte använda ett index som inte var en del av en sträng. Den här gången orsakas problemet av att vi försöker hämta den första bokstaven hos en tom sträng – dvs. en sträng med längden noll."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Streck","tmcname":"osa04-02_streck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" som får två argument (bredd, sträng). Funktionen skapar ett steck genom att skriva ut det första tecknet i den angivna strängen så många gånger som angetts i den första parametern. Om den andra parametern är tom, används asterisker."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"streck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstreck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"LOL\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstreck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"%%%%%%%\nLLLLLLLLLL\n***\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"funktionsanrop-inom-funktionsanrop","style":"position:relative;"},"children":[{"type":"text","value":"Funktionsanrop inom funktionsanrop"},{"type":"element","tagName":"a","properties":{"href":"#funktionsanrop-inom-funktionsanrop","ariaLabel":"funktionsanrop inom funktionsanrop permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anropa en funktion från en annan funktion. Vi har faktiskt gjort det här flera gånger – vi har anropat "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-funktionen inom våra egna funktioner i den förra modulen. Våra egna funktioner fungerar på samma sätt. I det följande exemplet anropar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa_flera_ganger"}]},{"type":"text","value":" funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halsa"}]},{"type":"text","value":" så många gånger som specificerats i argumentet ganger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa_flera_ganger"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n halsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\nhalsa_flera_ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej, Emilia\nHej, Emilia\nHej, Emilia"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fyrkant (igen)","tmcname":"osa04-02a_fyrkant"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fyrkant"}]},{"type":"text","value":" som skriver ut en tio tecken bred fyrkant med den höjd som getts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"fyrkant"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfyrkant"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"##########\n##########\n##########\n##########\n##########\n\n##########\n##########\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kvadrat","tmcname":"osa04-02b_kvadrat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kvadrat"}]},{"type":"text","value":", som skriver ut en kvadrat med den storlek som angetts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"#####\n#####\n#####\n#####\n#####\n\n###\n###\n###\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Speciell kvadrat","tmcname":"osa04-02c_speciell_kvadrat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"speciell_kvadrat"}]},{"type":"text","value":" som tar emot två argument – storleken samt det tecken som används för att rita kvadraten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"speciell_kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nspeciell_kvadrat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"o\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*****\n*****\n*****\n*****\n*****\n\nooo\nooo\nooo\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Triangel","tmcname":"osa04-02d_triangel"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"triangel"}]},{"type":"text","value":" som skriver ut en triangel med den höjd/bredd som angetts via argumentet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"triangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntriangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"#\n##\n###\n####\n#####\n######\n\n#\n##\n###\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Figur","tmcname":"osa04-03_figur"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"figur"}]},{"type":"text","value":" som tar emot fyra argument. Funktionen skapar en figur som består av en triangel och en fyrkant enligt de givna argumenten. Se exemplen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska anropa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":" från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"streck"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"figur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"X\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfigur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"o\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nfigur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"X\nXX\nXXX\nXXXX\nXXXXX\n*****\n*****\n*****\n\no\noo\n++\n++\n++\n++\n\n.\n..\n...\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Försök inte lösa allt på en gång. Fokusera på en sak åt gången – börja t.ex. med att få triangeln utritad på korrekt sätt och fortsätt sedan med fyrkanten. Det här gäller vilket som helst problem inom programmering. Ta små steg och kolla att allt fungerar före du fortsätter."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Julgran","tmcname":"osa04-04_julgran"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"julgran"}]},{"type":"text","value":" som tar emot ett argument. Funktionen ska skriva ut texten \"julgran!\" samt en julgran med den angivna höjden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempelvis "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"julgran(3)"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"julgran!\n *\n ***\n*****\n *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eller till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"julgran(5)"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"julgran!\n *\n ***\n *****\n *******\n*********\n *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att antalet mellanslag till vänster om granen ska vara exakt korrekt. Även om granens form är korrekt, accepteras granen inte om den nedersta \"grenen\" inte är helt fast i det vänstra hörnet."}]}]},{"type":"element","tagName":"h2","properties":{"id":"returvardet-hos-en-funktion","style":"position:relative;"},"children":[{"type":"text","value":"Returvärdet hos en funktion"},{"type":"element","tagName":"a","properties":{"href":"#returvardet-hos-en-funktion","ariaLabel":"returvardet hos en funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner kan också returnera värden. Till exempel Pythons inbyggda funktion "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" returnerar en sträng som användaren angett. Värdet som returneras av en funktion kan lagras i en variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ett ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du vill ha ett heltalsvärde från användaren måste indatat från användaren konverteras till ett heltal. För det använder vi "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":"-funktionen som också returnerar ett värde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ett heltal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" tar den sträng som returneras av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":"-funktionen som argument och returnerar värdet i heltalsform om möjligt."}]},{"type":"element","tagName":"h2","properties":{"id":"return-satsen","style":"position:relative;"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen"},{"type":"element","tagName":"a","properties":{"href":"#return-satsen","ariaLabel":"return satsen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner som du själv definierar kan också returnera värden. För det här ändamålet behöver du "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen. Till exempel funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" nedan returnerar summan av dess parametrar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summa:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Summa: 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett annat exempel på ett returnerat värde. Funktionen ber om användarens namn och returnerar den sträng som användaren anger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fraga_namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är ditt namn? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" namn\n\nnamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fraga_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad är ditt namn? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Anna"}]},{"type":"text","value":"\nHej, Anna"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen avslutar körandet av funktionen genast. Det här är ett sätt att göra en jämförelsefunktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"minst"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén är att om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" är mindre än "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" så kommer funktionen att returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och avslutas direkt. Annars fortsätter man till nästa rad som returnerar värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". En return-sats kan inte köras flera gånger i samma funktion under samma funktionsanrop."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda dig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen även om funktionen inte returnerar något värde. Då är dess uppgift helt enkelt att avsluta körandet av funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"halsa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"???\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhalsa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mårten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om argumentet som sparas i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" är en tom sträng, kommer texten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"???"}]},{"type":"text","value":" att skrivas ut och funktionen avslutas:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej, Emilia\n???\nHej, Mårten"}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-anvanda-returvarden-fran-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Att använda returvärden från funktioner"},{"type":"element","tagName":"a","properties":{"href":"#att-anvanda-returvarden-fran-funktioner","ariaLabel":"att anvanda returvarden fran funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi känner redan till att värden som returneras från funktioner kan lagras i variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summan är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Summan är 10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Returvärdet hos en funktion kan jämföras med vilket som helst annat värde. Det är inte nödvändigt att lagra värdet i en variabel för att ge det som argument till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summan är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Returvärdet hos en funktion kan vara ett argument för en funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"differens"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"b\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" differens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Svaret är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svaret är 2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet körs de inre funktionsanropen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa(5, 2)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa(2, 3)"}]},{"type":"text","value":" först. Värdena som returneras (sju och fem) används som argument för det yttre funktionsanropet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det yttre funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"differens(7, 5)"}]},{"type":"text","value":" returnerar värdet 2, som lagras i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat"}]},{"type":"text","value":" och skrivs ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att sammanfatta: värden som returneras av funktioner fungerar som alla andra värden i Python. De kan skrivas ut, lagras i variabler och användas i uttryck och som argument i funktionsanrop."}]},{"type":"element","tagName":"h2","properties":{"id":"skillnaden-mellan-return-och-print","style":"position:relative;"},"children":[{"type":"text","value":"Skillnaden mellan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"a","properties":{"href":"#skillnaden-mellan-return-och-print","ariaLabel":"skillnaden mellan return och print permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland kan skillnaden mellan att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" inom en funktion vara oklara. Vi undersöker två olika sätt att göra en funktion som berättar vilket av två värden är större:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"max1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"max2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" max1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmax2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n7"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Båda versionerna verkar fungera i och med att det större av värdena skrivs ut korrekt. Det finns ändå en central skillnad mellan dessa två funktioner. Den första, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max1"}]},{"type":"text","value":", skriver inte ut något. Den använder sig istället av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"-satsen. Om vi kör den följande kodraden…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"max1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…verkar ingenting hända. Funktionens returvärde måste användas på något sätt i den kod som anropar funktionen. Det kan till exempel lagras i en variabel och skrivas ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"svar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" max1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den andra versionen, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max2"}]},{"type":"text","value":", använder sig av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen inom funktionen. Om vi vill se värdet, kan vi helt enkelt anropa funktionen…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"max2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…och det större värdet kommer att skrivas ut. Det dåliga med den här funktionen är att värdet som funktionen räknar ut inte kan användas av själva programmet. Därför är funktioner som returnerar ett värde ofta ett bättre alternativ."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Störst av talen","tmcname":"osa04-05_storst"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storst"}]},{"type":"text","value":" som returnerar den största siffran av de tre givna argumenten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Käyttöesimerkki"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 99"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 0"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lika tecken","tmcname":"osa04-06_lika"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lika"}]},{"type":"text","value":" som får som argument en sträng och två heltal som syftar till index. Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" beroende på om tecknen vid dessa index i strängen är lika eller inte. Om något av indexen inte finns i strängen ska funktionen returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Några exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# e och e är lika"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kodexpert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# k och x är olika"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kodexpert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det andra indexet finns inte i strängen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kodexpert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Första, andra och sista","tmcname":"osa04-07_forsta_andra_sista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa tre funktioner: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord_ett"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord_tva"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sista_ordet"}]},{"type":"text","value":". Alla funktioner får som argument en sträng som består av en mening."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionerna returnerar meningens första, andra eller sista ord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att strängen alltid innehåller minst två ord och exakt ett mellanslag mellan orden. Det finns inga mellanslag i början eller slutet av strängen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"mening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jag gillar blåbärspaj så länge den inte innehåller ägg\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# jag"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_tva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# gillar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ägg"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"jag\ngillar\nägg"}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"mening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"la-la gunilla\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_tva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# gunilla"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# gunilla"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"typen-av-ett-argument","style":"position:relative;"},"children":[{"type":"text","value":"Typen av ett argument"},{"type":"element","tagName":"a","properties":{"href":"#typen-av-ett-argument","ariaLabel":"typen av ett argument permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är en kort repetition av de datatyper vi bekantat oss med hittills:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Tyyppi"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"I Python"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Exempel"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Heltal"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"23"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Flyttal"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-0.45"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Sträng"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Petra Python\""}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Sanningsvärde"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du anropar en funktion, kommer den bara att fungera korrekt då argumenten du ger åt den är av korrekt typ. Ta en titt på det här exemplet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"meddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"meddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen fungerar korrekt om vi anropar den på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hejsan\nHejsan\nHejsan\nHejsan\nHejsan"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi däremot ger funktionen ett argument av fel typ så kommer funktionen inte att fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: '>' not supported between instances of 'str' and 'int'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problemet här är att den andra parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ganger"}]},{"type":"text","value":" jämförs med ett heltal (0) på den andra raden av funktionsdefinitionen. Det givna argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"text","value":" är en sträng och inte ett heltal. Strängar och heltal kan inte jämföras så här enkelt – därmed felmeddelandet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att undvika problem som dessa kan du inkludera typledtrådar (type hints) när du definierar funktioner. Typledtråden berättar vilken typ av argument funktionen förväntar sig motta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut_flera_ganger"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"meddelande "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"meddelande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n ganger "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här berättar för alla användare av funktionen att argumentet som lagras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"meddelande"}]},{"type":"text","value":" ska vara en sträng medan argumentet som lagras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ganger"}]},{"type":"text","value":" ska vara ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Också typen av returvärdet kan specificeras när funktionen definieras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fraga_namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vad är ditt namn? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" namn"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här berättar för användaren att funktionen borde returnera en sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Typledtrådar är bokstavligen ledtrådar. Det är inte en garanti och kan inte säkerställa att felaktiga datatyper inte ges till eller returneras av en funktion. Om det här sker kommer funktionen ändå att köras, men den fungerar inte nödvändigtvis korrekt."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du mera om argument och parametrar hos funktioner
  • \n
  • kan du returnera värden från funktioner och använda dessa värden i koden
  • \n
  • kan du ge typledtrådar för parametrar och värden som returneras.
  • \n

Nu är det dags för en snabbrepetition av funktioner i Python. Funktioner definieras med nyckelordet def:

def meddelande():\n    print(\"Det här kommer från en funktion\")

Funktionen kan anropas i koden på följande sätt:

meddelande()

I det här fallet skulle programmet skriva ut följande:

Det här kommer från en funktion

Parametrar och argument hos en funktion

En funktion kan motta en eller flera argument. När funktionen anropas tilldelas argumenten till variabler som är definierade i funktionsdefinitionen. Dessa variabler kallas parametrar och de listas inom parenteserna som följer funktionens namn.

I den följande koden har funktionen halsa en definierad parameter, medan funktionen summa har två:

def halsa(namn):\n    print(\"Hej,\", namn)\n\ndef summa(a, b):\n    print(\"Summan av parametrarna är\", a + b)
halsa(\"Emilia\")\nsumma(2, 3)

Hej, Emilia\nSumman av parametrarna är 5

Terminologin kring data som ges till en funktion kan kännas förvirrande. För att göra situationen svårare, använder en del källor uttryck som formella och riktiga parametrar eller formella och riktiga argument. Pythons dokumentation nämner endast termerna argument och parameter. Därför använder vi också dessa termer.

Vad händer det egentligen när funktionsanropet halsa(\"Emilia\") körs?

I funktionsdefinitionen halsa(namn) beter sig parametern namn som en normal variabel. Vi kan använda den inom funktionen på samma sätt som vi har använt variabler i huvudfunktionen i flera program hittills.

I funktionsanropet halsa(\"Emilia\") är argumentet \"Emilia\" som vilken som helst annan sträng vi stött på tidigare. Vi kan till exempel tilldela den till en variabel.

Alltså, när funktionsanropet körs kommer värdet på argumentet – \"Emilia\" – att tilldelas till variabeln namn. Under den här körningen av funktionen kommer namn = \"Emilia\". När funktionen anropas med ett annat argument kommer värdet på namn att vara olikt.

Terminologin kan kännas överflödig, men datavetenskapen strävar att vara så exakt vetenskap som möjligt. Att använda noga definierad terminologi hjälper.

Felmeddelanden som uppstår i testen

De flesta uppgifter under den här kursen inkluderar automatiska test. Om programmet inte fungerar på det sätt som förutsätts av uppgiften, kommer testet att visa ett felmeddelande. Det här meddelandet kan vara till nytta – eller sen inte. Det kan vara värt att läsa meddelandet noga.

I vissa fall berättar felmeddelandet inte egentligen så mycket. I nästa övning kan du stöta på det här felmeddelandet:

\n \n \n \n \n \n \"4\n \n \n

Meddelandet berättar att man borde kunna köra funktionen streck med de specificerade argumenten:

streck(5, \"\")

Det riktiga problemet får vi reda på när vi kör det funktionsanrop som stod i felmeddelandet. Du kan göra det genom att kopiera funktionsanropet till ditt program och klicka på triangeln:

\n \n

Skapa funktionen streck som får två argument (bredd, sträng). Funktionen skapar ett steck genom att skriva ut det första tecknet i den angivna strängen så många gånger som angetts i den första parametern. Om den andra parametern är tom, används asterisker.

Exempel:

streck(7, \"%\")\nstreck(10, \"LOL\")\nstreck(3, \"\")
\n%%%%%%%\nLLLLLLLLLL\n***\n

Funktionsanrop inom funktionsanrop

Du kan anropa en funktion från en annan funktion. Vi har faktiskt gjort det här flera gånger – vi har anropat print-funktionen inom våra egna funktioner i den förra modulen. Våra egna funktioner fungerar på samma sätt. I det följande exemplet anropar funktionen halsa_flera_ganger funktionen halsa så många gånger som specificerats i argumentet ganger:

def halsa(namn):\n    print(\"Hej,\", namn)\n\ndef halsa_flera_ganger(namn, ganger):\n    while ganger > 0:\n        halsa(namn)\n        ganger -= 1\n\nhalsa_flera_ganger(\"Emilia\", 3)

Hej, Emilia\nHej, Emilia\nHej, Emilia

Skapa funktionen fyrkant som skriver ut en tio tecken bred fyrkant med den höjd som getts som argument.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

fyrkant(5)\nprint()\nfyrkant(2)
\n##########\n##########\n##########\n##########\n##########\n\n##########\n##########\n

Skapa funktionen kvadrat, som skriver ut en kvadrat med den storlek som angetts som argument.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

kvadrat(5)\nprint()\nkvadrat(3)
\n#####\n#####\n#####\n#####\n#####\n\n###\n###\n###\n

Skapa funktionen speciell_kvadrat som tar emot två argument – storleken samt det tecken som används för att rita kvadraten.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

speciell_kvadrat(5, \"*\")\nprint()\nspeciell_kvadrat(3, \"o\")
\n*****\n*****\n*****\n*****\n*****\n\nooo\nooo\nooo\n

Skapa funktionen triangel som skriver ut en triangel med den höjd/bredd som angetts via argumentet.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

triangel(6)\nprint()\ntriangel(3)
\n#\n##\n###\n####\n#####\n######\n\n#\n##\n###\n

Skapa funktionen figur som tar emot fyra argument. Funktionen skapar en figur som består av en triangel och en fyrkant enligt de givna argumenten. Se exemplen.

Funktionen ska anropa streck från den föregående uppgiften. Kopiera alltså funktionen från den föregående uppgiften till den här uppgiften och skriv den nya funktionen under. Redigera inte funktionen streck!

Exempel:

figur(5, \"X\", 3, \"*\")\nprint()\nfigur(2, \"o\", 4, \"+\")\nprint()\nfigur(3, \".\", 0, \",\")
\nX\nXX\nXXX\nXXXX\nXXXXX\n*****\n*****\n*****\n\no\noo\n++\n++\n++\n++\n\n.\n..\n...\n

Tips: Försök inte lösa allt på en gång. Fokusera på en sak åt gången – börja t.ex. med att få triangeln utritad på korrekt sätt och fortsätt sedan med fyrkanten. Det här gäller vilket som helst problem inom programmering. Ta små steg och kolla att allt fungerar före du fortsätter.

Skapa funktionen julgran som tar emot ett argument. Funktionen ska skriva ut texten \"julgran!\" samt en julgran med den angivna höjden.

Exempelvis julgran(3):

\njulgran!\n  *\n ***\n*****\n  *\n

Eller till exempel julgran(5):

\njulgran!\n    *\n   ***\n  *****\n *******\n*********\n    *\n

Observera att antalet mellanslag till vänster om granen ska vara exakt korrekt. Även om granens form är korrekt, accepteras granen inte om den nedersta \"grenen\" inte är helt fast i det vänstra hörnet.

Returvärdet hos en funktion

Funktioner kan också returnera värden. Till exempel Pythons inbyggda funktion input returnerar en sträng som användaren angett. Värdet som returneras av en funktion kan lagras i en variabel:

ord = input(\"Ange ett ord: \")

När du vill ha ett heltalsvärde från användaren måste indatat från användaren konverteras till ett heltal. För det använder vi int-funktionen som också returnerar ett värde:

siffra = int(input(\"Ange ett heltal: \"))

Funktionen int tar den sträng som returneras av input-funktionen som argument och returnerar värdet i heltalsform om möjligt.

return-satsen

Funktioner som du själv definierar kan också returnera värden. För det här ändamålet behöver du return-satsen. Till exempel funktionen summa nedan returnerar summan av dess parametrar:

def summa(a, b):\n    return a + b\n\nsvar = summa(2, 3)\n\nprint(\"Summa:\", svar)

Summa: 5

Här är ett annat exempel på ett returnerat värde. Funktionen ber om användarens namn och returnerar den sträng som användaren anger:

def fraga_namn():\n    namn = input(\"Vad är ditt namn? \")\n    return namn\n\nnamn = fraga_namn()\nprint(\"Hej,\", namn)

Vad är ditt namn? Anna\nHej, Anna

return-satsen avslutar körandet av funktionen genast. Det här är ett sätt att göra en jämförelsefunktion:

def minst(a,b):\n    if a < b:\n        return a\n    return b\n\nprint(minst(3, 7))\nprint(minst(5, 2))

Idén är att om a är mindre än b så kommer funktionen att returnera a och avslutas direkt. Annars fortsätter man till nästa rad som returnerar värdet b. En return-sats kan inte köras flera gånger i samma funktion under samma funktionsanrop.

3\n2

Du kan använda dig av return-satsen även om funktionen inte returnerar något värde. Då är dess uppgift helt enkelt att avsluta körandet av funktionen:

def halsa(namn):\n    if namn == \"\":\n        print(\"???\")\n        return\n    print(\"Hej,\", namn)\n\nhalsa(\"Emilia\")\nhalsa(\"\")\nhalsa(\"Mårten\")

Om argumentet som sparas i variabeln namn är en tom sträng, kommer texten ??? att skrivas ut och funktionen avslutas:

Hej, Emilia\n???\nHej, Mårten

Att använda returvärden från funktioner

Vi känner redan till att värden som returneras från funktioner kan lagras i variabler:

def summa(a, b):\n    return a + b\n\nresultat = summa(4, 6)\nprint(\"Summan är\", resultat)

Summan är 10

Returvärdet hos en funktion kan jämföras med vilket som helst annat värde. Det är inte nödvändigt att lagra värdet i en variabel för att ge det som argument till print-instruktionen:

print(\"Summan är\", summa(4, 6))

Returvärdet hos en funktion kan vara ett argument för en funktion:

def summa(a, b):\n    return a+b\n\ndef differens(a, b):\n    return a-b\n\nresultat = differens(summa(5, 2), summa(2, 3))\nprint(\"Svaret är\", resultat)

Svaret är 2

I det här fallet körs de inre funktionsanropen summa(5, 2) och summa(2, 3) först. Värdena som returneras (sju och fem) används som argument för det yttre funktionsanropet.

Det yttre funktionsanropet differens(7, 5) returnerar värdet 2, som lagras i variabeln resultat och skrivs ut.

För att sammanfatta: värden som returneras av funktioner fungerar som alla andra värden i Python. De kan skrivas ut, lagras i variabler och användas i uttryck och som argument i funktionsanrop.

Skillnaden mellan return och print

Ibland kan skillnaden mellan att använda return och print inom en funktion vara oklara. Vi undersöker två olika sätt att göra en funktion som berättar vilket av två värden är större:

def max1(a, b):\n    if a > b:\n        return a\n    else:\n        return b\n\ndef max2(a, b):\n    if a > b:\n        print(a)\n    else:\n        print(b)\n\nsvar = max1(3, 5)\nprint(svar)\n\nmax2(7, 2)

5\n7

Båda versionerna verkar fungera i och med att det större av värdena skrivs ut korrekt. Det finns ändå en central skillnad mellan dessa två funktioner. Den första, max1, skriver inte ut något. Den använder sig istället av return-satsen. Om vi kör den följande kodraden…

max1(3, 5)

…verkar ingenting hända. Funktionens returvärde måste användas på något sätt i den kod som anropar funktionen. Det kan till exempel lagras i en variabel och skrivas ut:

svar = max1(3, 5)\nprint(svar)

Den andra versionen, max2, använder sig av print-instruktionen inom funktionen. Om vi vill se värdet, kan vi helt enkelt anropa funktionen…

max2(7, 5)

…och det större värdet kommer att skrivas ut. Det dåliga med den här funktionen är att värdet som funktionen räknar ut inte kan användas av själva programmet. Därför är funktioner som returnerar ett värde ofta ett bättre alternativ.

Skapa funktionen storst som returnerar den största siffran av de tre givna argumenten.

Käyttöesimerkki

print(storst(3, 4, 1)) # 4\nprint(storst(99, -4, 7)) # 99\nprint(storst(0, 0, 0)) # 0

Skapa funktionen lika som får som argument en sträng och två heltal som syftar till index. Funktionen ska returnera True eller False beroende på om tecknen vid dessa index i strängen är lika eller inte. Om något av indexen inte finns i strängen ska funktionen returnera False.

Några exempel:

# e och e är lika\nprint(lika(\"kodexpert\", 3, 6)) # True\n\n# k och x är olika\nprint(lika(\"kodexpert\", 0, 4)) # False\n\n# det andra indexet finns inte i strängen\nprint(lika(\"kodexpert\", 0, 20)) # False

Skapa tre funktioner: ord_ett, ord_tva och sista_ordet. Alla funktioner får som argument en sträng som består av en mening.

Funktionerna returnerar meningens första, andra eller sista ord.

Du kan anta att strängen alltid innehåller minst två ord och exakt ett mellanslag mellan orden. Det finns inga mellanslag i början eller slutet av strängen.

mening = \"jag gillar blåbärspaj så länge den inte innehåller ägg\"\n\nprint(ord_ett(mening)) # jag\nprint(ord_tva(mening)) # gillar\nprint(sista_ordet(mening)) # ägg

jag\ngillar\nägg

mening = \"la-la gunilla\"\n\nprint(ord_tva(mening)) # gunilla\nprint(sista_ordet(mening)) # gunilla

Typen av ett argument

Här är en kort repetition av de datatyper vi bekantat oss med hittills:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
TyyppiI PythonExempel
Heltalint23
Flyttalfloat-0.45
Strängstr\"Petra Python\"
SanningsvärdeboolTrue

När du anropar en funktion, kommer den bara att fungera korrekt då argumenten du ger åt den är av korrekt typ. Ta en titt på det här exemplet:

def skriv_ut_flera_ganger(meddelande, ganger):\n    while ganger > 0:\n        print(meddelande)\n        ganger -= 1

Funktionen fungerar korrekt om vi anropar den på följande sätt:

skriv_ut_flera_ganger(\"Hejsan\", 5)

Hejsan\nHejsan\nHejsan\nHejsan\nHejsan

Om vi däremot ger funktionen ett argument av fel typ så kommer funktionen inte att fungera:

skriv_ut_flera_ganger(\"Hejsan\", \"Emilia\")

TypeError: '>' not supported between instances of 'str' and 'int'

Problemet här är att den andra parametern ganger jämförs med ett heltal (0) på den andra raden av funktionsdefinitionen. Det givna argumentet \"Emilia\" är en sträng och inte ett heltal. Strängar och heltal kan inte jämföras så här enkelt – därmed felmeddelandet.

För att undvika problem som dessa kan du inkludera typledtrådar (type hints) när du definierar funktioner. Typledtråden berättar vilken typ av argument funktionen förväntar sig motta:

def skriv_ut_flera_ganger(meddelande : str, ganger : int):\n    while ganger > 0:\n        print(meddelande)\n        ganger -= 1

Det här berättar för alla användare av funktionen att argumentet som lagras i meddelande ska vara en sträng medan argumentet som lagras i ganger ska vara ett heltal.

Också typen av returvärdet kan specificeras när funktionen definieras:

def fraga_namn() -> str:\n    namn = input(\"Vad är ditt namn? \")\n    return namn

Det här berättar för användaren att funktionen borde returnera en sträng.

Obs! Typledtrådar är bokstavligen ledtrådar. Det är inte en garanti och kan inte säkerställa att felaktiga datatyper inte ges till eller returneras av en funktion. Om det här sker kommer funktionen ändå att köras, men den fungerar inte nödvändigtvis korrekt.

","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/2-mer-om-funktioner.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-4/3-listor/page-data.json b/page-data/osa-4/3-listor/page-data.json index da378daab..a73951c85 100644 --- a/page-data/osa-4/3-listor/page-data.json +++ b/page-data/osa-4/3-listor/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-4/3-listor", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad listor är i Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur du kommer åt ett visst element i en lista"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du lägga till och avlägsna ett element från en lista"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till inbyggda funktioner och metoder för listor."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tills nu har vi lagrat data med variabler i våra program, så att varje sak har lagrats i sin egen variabel. Det här har sina begränsningar – det kan bli arbetsdrygt att tilldela variabler för allt då det finns mycket data att behandla."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python är en lista en samling av värden som finns lagrade under samma variabelnamn. Listans element skrivs mellan hakparenteser. Listans värden kallas element."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande instruktion skapar en ny tom lista…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…medan det här instruktionen skapar en lista med fem element:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"komma-at-element-i-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Komma åt element i en lista"},{"type":"element","tagName":"a","properties":{"href":"#komma-at-element-i-en-lista","ariaLabel":"komma at element i en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Elementen i en lista är indexerade på samma sätt som tecken i en sträng. Indexeringen börjar från noll och det sista indexet är listans längd minus ett:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 581px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cfea0276df6deb4e5dd7f97ef7d30e0e/92d15/4_2_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 26.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsSAAALEgHS3X78AAAAuElEQVQY032QWQqEQBBD+/7X8SyCggv6Jy6475rhFSjMfExDkUoqnV6cJA3DoKqqtG2b9eCyLFb0+75rXdeX/1vuvu8vAU7AeZ7WU9d1WbHAR3s8x3G8Hvd7AoamaTSOo23AzAHcDA6iPa8gZJ5n4+x1DPu+twCQ4vmY6EFmXddpmqaX0/MNfFHbthbs+74cIgHcqq5rlWWpJEmUZZnCMFSapoqiSEEQmE7FcWyY57nNCCqKQp7n6QPgCoGcLj0znwAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cfea0276df6deb4e5dd7f97ef7d30e0e/a0b58/4_2_1.webp 230w","/static/cfea0276df6deb4e5dd7f97ef7d30e0e/bc10c/4_2_1.webp 460w","/static/cfea0276df6deb4e5dd7f97ef7d30e0e/39daa/4_2_1.webp 581w"],"sizes":"(max-width: 581px) 100vw, 581px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cfea0276df6deb4e5dd7f97ef7d30e0e/81c8e/4_2_1.png 230w","/static/cfea0276df6deb4e5dd7f97ef7d30e0e/08a84/4_2_1.png 460w","/static/cfea0276df6deb4e5dd7f97ef7d30e0e/92d15/4_2_1.png 581w"],"sizes":"(max-width: 581px) 100vw, 581px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cfea0276df6deb4e5dd7f97ef7d30e0e/92d15/4_2_1.png","alt":"Lista indeksoidaan nollasta alkaen","title":"Lista indeksoidaan nollasta alkaen","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett specifikt element kan kommas åt på samma sätt som ett specifikt tecken i en sträng, med hakparenteser:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summan av de två första:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n2\n5\nSumman av de två första: 9"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hela listans innehåll kan också skrivas ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[7, 2, 2, 5, 2]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till skillnad från strängar är listor föränderliga, vilket betyder att deras innehåll kan ändra. Du kan tilldela ett nytt värde till ett element i en lista på samma sätt som du kan tilldela ett nytt värde till en variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[7, 2, 2, 5, 2]\n[7, 3, 2, 5, 2]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" berättar antalet element i en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ändra på elementens värden","tmcname":"osa04-07a_andra_varden"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som initierar en lista med värdena "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1, 2, 3, 4, 5]"}]},{"type":"text","value":". Programmet ber därefter användaren ange ett index och ett nytt värde som programmet lagrar, och listan skrivs ut på nytt. Programmet avslutas då använder anger -1 som index."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nAnge värde: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n[10, 2, 3, 4, 5]\nAnge index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge värde: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"250"}]},{"type":"text","value":"\n[10, 2, 250, 4, 5]\nAnge index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nAnge värde: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-45"}]},{"type":"text","value":"\n[10, 2, 250, 4, -45]\nAnge index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lagga-till-element-i-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till element i en lista"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-element-i-en-lista","ariaLabel":"lagga till element i en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" lägger till element i slutet av en lista. Metoden fungerar så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[5, 10, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel använder sig av två listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nskonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nskonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"37"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffror:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Skonummer:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Elementet läggs till i slutet av den lista som metoden anropas på:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffror:\n[5, 10, 3]\nSkonummer:\n[37, 44, 40, 28]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lägg till element i en lista","tmcname":"osa04-07b_nya_element"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge antalet siffror. Därefter ber programmet användaren ange det här antalet siffror, som läggs till i en lista i den givna ordningen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan ska till slut skrivas ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många siffror: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nGe siffra 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nGe siffra 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"250"}]},{"type":"text","value":"\nGe siffra 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\n[10, 250, 34]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lagga-till-ett-element-pa-ett-specifikt-stalle","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till ett element på ett specifikt ställe"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-ett-element-pa-ett-specifikt-stalle","ariaLabel":"lagga till ett element pa ett specifikt stalle permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill specificera på vilket ställe i en lista ett värde ska läggas till, kan du använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insert"}]},{"type":"text","value":"-metoden. Metoden lägger till ett element vid ett specifikt index. Alla element vid det här eller senare index flyttar ett index framåt, \"mot höger\":"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 632px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6bc807508a898afcc180481b54c507a4/084e2/4_2_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 65.21739130434783%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsSAAALEgHS3X78AAABWElEQVQ4y62TS27DMAxEfYteuZteqqfoKkAQoMjPtiT/HdsTPAUUDMfoqgQGEmlpSI7oTJLO53NEVVUJIQS1bau+79U0TYyxx5Zl2QWW2eY/DK5snRF7PB7K8zxWRwx/HMcE86kW4IO3Cud51jRN8bD3XmVZqq7r2Co+QAaTg9U5l+ImRyIsikKXy0XH4zHqeb/fY6UQc9FWI+y6Lt4BxOgoEu5pQbXDMETwIJCtAcnWT4RUSNlkJ7NltxYgh5QLttIBZywGSJ4I+WgkfNgKvTWkoE2S7b7yX2OwnoC1JDwYXW0t2xvOLaE0qe9bOfd6bcjQ7XA4vE3BW4WMDoeR4OUvGkepaSbVdZV0s9ExXQEPlEFg+rFyyMYhhEbe/8r7L4XwLe8HhfCqBCKqWs8lsQyi6/Uahb7dbjqdTnEey7KQc51C+FEIH3LuU0UheZ+nQTZi7rLnp3gCiHjy2zC3accAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bc807508a898afcc180481b54c507a4/a0b58/4_2_2.webp 230w","/static/6bc807508a898afcc180481b54c507a4/bc10c/4_2_2.webp 460w","/static/6bc807508a898afcc180481b54c507a4/59680/4_2_2.webp 632w"],"sizes":"(max-width: 632px) 100vw, 632px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bc807508a898afcc180481b54c507a4/81c8e/4_2_2.png 230w","/static/6bc807508a898afcc180481b54c507a4/08a84/4_2_2.png 460w","/static/6bc807508a898afcc180481b54c507a4/084e2/4_2_2.png 632w"],"sizes":"(max-width: 632px) 100vw, 632px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6bc807508a898afcc180481b54c507a4/084e2/4_2_2.png","alt":"Alkion lisäys listaan","title":"Alkion lisäys listaan","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel det här programmet…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skriver ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[10, 1, 2, 3, 4, 5, 6]\n[10, 1, 20, 2, 3, 4, 5, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ta-bort-element-fran-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Ta bort element från en lista"},{"type":"element","tagName":"a","properties":{"href":"#ta-bort-element-fran-en-lista","ariaLabel":"ta bort element fran en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns två sätt för att ta bort ett element från en lista:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om man vet indexet för elementet kan man använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"-metoden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om man vet innehållet i elementet kan man använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":"-metoden."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"-metoden tar indexet för elementet som ska avlägsnas som argument. Följande program tar bort elementen vid indexen 2 och 3 i listan. Märk hur indexen för de resterande elementen ändrar när ett element avlägsnas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 4, 5, 6]\n[1, 2, 4, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är bra att minnas att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"-metoden också returnerar det avlägsnade elementet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n[4, 2, 2, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":"-metoden tar värdet för det element som ska avlägsnas som argument. Till exempel följande program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skriver ut detta:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 4, 5, 6]\n[1, 3, 4, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden tar bort det första värdet som motsvarar det givna argumentet från listan – på samma sätt som funktionen find hos strängar returnerar den första delsträngen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 1, 2]\n[2, 2]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lägg till, ta bort","tmcname":"osa04-07c_lagg_till_ta_bort"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som låter användaren lägga till eller avlägsna ett värde. Varje operation görs i slutet av listan. När ett element läggs till är dess värde alltid ett större än det föregående värdet (1 om inga element finns i listan)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mellan varje operation skrivs listan ut. Här följer ett exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan är nu []\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nListan är nu [1]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nListan är nu [1, 2]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nListan är nu [1, 2, 3]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nListan är nu [1, 2]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nListan är nu [1, 2, 3]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"v"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att man inte försöker avlägsna element då listan är tom."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det givna elementet inte hittas i listan, kommer remove-funktionen att ge ett fel. På samma sätt som med strängar kan vi kolla om ett element finns i en lista med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":"-operatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Listan innehåller värdet 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Listan innehåller värdet 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan innehåller värdet 1"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Samma ord två gånger","tmcname":"osa04-08_samma_tva_ganger"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren mata in ord. När användaren anger ett ord som hon gett tidigare, avslutas programmet och antalet ord skrivs ut."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"det"}]},{"type":"text","value":"\nord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"var"}]},{"type":"text","value":"\nord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"en"}]},{"type":"text","value":"\nord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"gång"}]},{"type":"text","value":"\nord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"en"}]},{"type":"text","value":"\nDu gav 4 olika ord"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"ordna-listor","style":"position:relative;"},"children":[{"type":"text","value":"Ordna listor"},{"type":"element","tagName":"a","properties":{"href":"#ordna-listor","ariaLabel":"ordna listor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Elementen i en lista kan ordnas från minsta till största med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notera att den här metoden modifierar själva listan. Alltid vill vi inte ändra på den ursprungliga listan, och då kan vi istället använda funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":". Den returnerar en ordnad lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg skillnaden mellan dessa: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" ändrar på ordningen i den ursprungliga listan medan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" skapar en ny, ordnad kopia av listan. Med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" kan vi behålla den ursprungliga ordningen hos listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ursprunglig "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nordnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ursprunglig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ursprunglig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 5, 1, 2, 4]\n[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"En lista, två varianter","tmcname":"osa04-08b_en_lista_tva_varianter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange siffror, som läggs till i en lista. Efter varje tillägg skrivs listan ut på två sätt:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"elementen i den ordning de lagts till"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"elementen i storleksordning från det minsta till det största"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet avslutas då användaren anger siffran 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nLista: [3]\nOrdnat: [3]\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nLista: [3, 1]\nOrdnat: [1, 3]\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nLista: [3, 1, 9]\nOrdnat: [1, 3, 9]\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nLista: [3, 1, 9, 5]\nOrdnat: [1, 3, 5, 9]\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"maximi--och-minimivarde-samt-summa","style":"position:relative;"},"children":[{"type":"text","value":"Maximi- och minimivärde samt summa"},{"type":"element","tagName":"a","properties":{"href":"#maximi--och-minimivarde-samt-summa","ariaLabel":"maximi och minimivarde samt summa permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":" returnerar det största respektive minsta värdet i en lista. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" returnerar summan av listans värden."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nstorst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nminst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minst:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Störst:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summa:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Minst: 1\nStörst: 5\nSumma: 15"}]}]},{"type":"element","tagName":"h2","properties":{"id":"metoder-och-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Metoder och funktioner"},{"type":"element","tagName":"a","properties":{"href":"#metoder-och-funktioner","ariaLabel":"metoder och funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns två sätt att behandla listor i Python, och det här kan ibland orsaka huvudbry. För det mesta kommer du att använda metoder hos listor – till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":". De används med punktoperatorn hos listvariabeln:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metodanrop"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metodanrop"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En del funktioner kan ta emot listor som argument. De nyss presenterade funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" är goda exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktionsanrop, lista som argument"}]},{"type":"text","value":"\nstorst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nminst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npituus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minst:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Störst:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Listan pituus:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pituus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktionsanrop, lista som argument, ordnad lista returneras"}]},{"type":"text","value":"\njarjestyksessa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jarjestyksessa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Minst: 1\nStörst: 7\nListan pituus: 4\n[1, 2, 3, 7]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lista-som-argument-eller-returvarde","style":"position:relative;"},"children":[{"type":"text","value":"Lista som argument eller returvärde"},{"type":"element","tagName":"a","properties":{"href":"#lista-som-argument-eller-returvarde","ariaLabel":"lista som argument eller returvarde permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som de inbyggda funktionerna ovan kan också våra egna funktioner ta listor som argument och returnera listor. Den följande funktionen tar reda på det mellersta – median – värdet i en ordnad lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"median"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ordnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n mellersta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"mellersta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen skapar en ordnad version av listan som gavs som argument och returnerar elementet i mitten. Märk heltalsdivisionsoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" som används. Indexet i en lista måste vara ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen fungerar på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"skonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"36"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"39"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Skonumrens median är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\niat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"56"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"34"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"77"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medianåldern är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"iat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skonumrens median är 40\nMedianåldern är 22"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion kan också returnera en lista. Den följande funktionen ber användaren ge heltal, som sedan returneras som en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_in_siffror"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge siffra (tomt avslutar programmet): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffror"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen använder sig av hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror"}]},{"type":"text","value":", som är en lista. Alla siffror som användaren skriver läggs till i listan. När loopen avslutas returnerar funktionen listan med satsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return siffror"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När funktionen anropas så här…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_in_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Största siffran är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medianvärdet är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…kan utskriften se ut på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-22"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"35"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet):\nStörsta siffran är 35\nMedianvärdet är 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här lilla exemplet demonstrerar ett av de viktigaste användningsområdena för funktioner: de hjälper dig att dela din kod i mindre helheter som enkelt kan förstås."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma funktionalitet kan förstås fås till stånd utan några som helst egna funktioner:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge siffra (tomt avslutar programmet): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nordnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmellersta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nmedian "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"mellersta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Största siffran är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medianvärdet är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här versionen är det svårare att uppfatta logiken bakom programmet, eftersom det inte mera är enkelt att se vilka instruktioner som hör till vilken funktionalitet. Koden uppnår samma mål – ta emot data, räkna medianvärde o.s.v. – men strukturen är mycket mindre tydlig."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att dela din kod i flera funktioner kommer att förbättra läsbarheten av koden och hjälper att uppfatta logiska helheter. Det här är till nytta när du ska verifiera att programmet fungerar som det ska, eftersom varje funktion kan testas separat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En annan viktig orsak till att använda funktioner är återanvändbarhet av koden. Om du behöver samma funktionalitet på flera ställen i ditt program är det en bra idé att skapa en funktion och namnge den väl:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Skonummer:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_in_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Painot:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvikter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_in_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pituudet:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlangder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_in_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Listans längd","tmcname":"osa04-09_listans_langd"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langd"}]},{"type":"text","value":" som returnerar längden på den lista som getts som argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# observera att du också kan anropa funktionen genom att direkt ge en lista som argument till funktionen"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"svar 5\nsvar 4"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Medeltal","tmcname":"osa04-10_medeltal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"medeltal"}]},{"type":"text","value":" som returnerar medelvärdet av värdena i en lista bestående av heltal som getts som argument till funktionen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"svar 3.0"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Variationsbredd","tmcname":"osa04-11_variationsbredd"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"variationsbredd"}]},{"type":"text","value":" som returnerar diffrensen av det största och minsta värdet i en lista med heltal som getts som argument till funktionen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" variationsbredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"svar 4"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"mer-om-att-behandla-listor","style":"position:relative;"},"children":[{"type":"text","value":"Mer om att behandla listor"},{"type":"element","tagName":"a","properties":{"href":"#mer-om-att-behandla-listor","ariaLabel":"mer om att behandla listor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns flera sätt till att använda listor i Python. Om du vill läsa mera är "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/tutorial/datastructures.html#more-on-lists","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pythons dokumentation"}]},{"type":"text","value":" ett bra ställe att börja med."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad listor är i Python
  • \n
  • vet du hur du kommer åt ett visst element i en lista
  • \n
  • kan du lägga till och avlägsna ett element från en lista
  • \n
  • känner du till inbyggda funktioner och metoder för listor.
  • \n

Tills nu har vi lagrat data med variabler i våra program, så att varje sak har lagrats i sin egen variabel. Det här har sina begränsningar – det kan bli arbetsdrygt att tilldela variabler för allt då det finns mycket data att behandla.

I Python är en lista en samling av värden som finns lagrade under samma variabelnamn. Listans element skrivs mellan hakparenteser. Listans värden kallas element.

Följande instruktion skapar en ny tom lista…

lista = []

…medan det här instruktionen skapar en lista med fem element:

lista = [7, 2, 2, 5, 2]

Komma åt element i en lista

Elementen i en lista är indexerade på samma sätt som tecken i en sträng. Indexeringen börjar från noll och det sista indexet är listans längd minus ett:

\n \n \n \n \n \n \"Lista\n \n \n

Ett specifikt element kan kommas åt på samma sätt som ett specifikt tecken i en sträng, med hakparenteser:

lista = [7, 2, 2, 5, 2]\n\nprint(lista[0])\nprint(lista[1])\nprint(lista[3])\n\nprint(\"Summan av de två första:\", lista[0] + lista[1])

7\n2\n5\nSumman av de två första: 9

Hela listans innehåll kan också skrivas ut:

lista = [7, 2, 2, 5, 2]\nprint(lista)

[7, 2, 2, 5, 2]

Till skillnad från strängar är listor föränderliga, vilket betyder att deras innehåll kan ändra. Du kan tilldela ett nytt värde till ett element i en lista på samma sätt som du kan tilldela ett nytt värde till en variabel:

lista = [7, 2, 2, 5, 2]\nprint(lista)\nlista[1] = 3\nprint(lista)

[7, 2, 2, 5, 2]\n[7, 3, 2, 5, 2]

Funktionen len berättar antalet element i en lista:

lista = [7, 2, 2, 5, 2]\nprint(len(lista))

5

Skapa ett program som initierar en lista med värdena [1, 2, 3, 4, 5]. Programmet ber därefter användaren ange ett index och ett nytt värde som programmet lagrar, och listan skrivs ut på nytt. Programmet avslutas då använder anger -1 som index.

Exempel:

Ange index: 0\nAnge värde: 10\n[10, 2, 3, 4, 5]\nAnge index: 2\nAnge värde: 250\n[10, 2, 250, 4, 5]\nAnge index: 4\nAnge värde: -45\n[10, 2, 250, 4, -45]\nAnge index: -1

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Lägga till element i en lista

Metoden append lägger till element i slutet av en lista. Metoden fungerar så här:

siffror = []\nsiffror.append(5)\nsiffror.append(10)\nsiffror.append(3)\nprint(siffror)

[5, 10, 3]

Följande exempel använder sig av två listor:

siffror = []\nskonummer = []\n\nsiffror.append(5)\nsiffror.append(10)\nsiffror.append(3)\n\nskonummer.append(37)\nskonummer.append(44)\nskonummer.append(40)\nskonummer.append(28)\n\nprint(\"Siffror:\")\nprint(siffror)\n\nprint(\"Skonummer:\")\nprint(skonummer)

Elementet läggs till i slutet av den lista som metoden anropas på:

Siffror:\n[5, 10, 3]\nSkonummer:\n[37, 44, 40, 28]

Skapa ett program som ber användaren ge antalet siffror. Därefter ber programmet användaren ange det här antalet siffror, som läggs till i en lista i den givna ordningen.

Listan ska till slut skrivas ut:

Hur många siffror: 3\nGe siffra 1: 10\nGe siffra 2: 250\nGe siffra 3: 34\n[10, 250, 34]

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Lägga till ett element på ett specifikt ställe

Om du vill specificera på vilket ställe i en lista ett värde ska läggas till, kan du använda insert-metoden. Metoden lägger till ett element vid ett specifikt index. Alla element vid det här eller senare index flyttar ett index framåt, \"mot höger\":

\n \n

Skapa ett program som låter användaren lägga till eller avlägsna ett värde. Varje operation görs i slutet av listan. När ett element läggs till är dess värde alltid ett större än det föregående värdet (1 om inga element finns i listan).

Mellan varje operation skrivs listan ut. Här följer ett exempel:

Listan är nu []\n(l)ägg till, (a)vlägsna eller a(v)sluta: l\nListan är nu [1]\n(l)ägg till, (a)vlägsna eller a(v)sluta: l\nListan är nu [1, 2]\n(l)ägg till, (a)vlägsna eller a(v)sluta: l\nListan är nu [1, 2, 3]\n(l)ägg till, (a)vlägsna eller a(v)sluta: a\nListan är nu [1, 2]\n(l)ägg till, (a)vlägsna eller a(v)sluta: l\nListan är nu [1, 2, 3]\n(l)ägg till, (a)vlägsna eller a(v)sluta: v\nHejdå!

Du kan anta att man inte försöker avlägsna element då listan är tom.

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Om det givna elementet inte hittas i listan, kommer remove-funktionen att ge ett fel. På samma sätt som med strängar kan vi kolla om ett element finns i en lista med hjälp av in-operatorn:

lista = [1, 3, 4]\n\nif 1 in lista:\n    print(\"Listan innehåller värdet 1\")\n\nif 2 in lista:\n    print(\"Listan innehåller värdet 2\")

Listan innehåller värdet 1

Skapa ett program som ber användaren mata in ord. När användaren anger ett ord som hon gett tidigare, avslutas programmet och antalet ord skrivs ut.

ord: det\nord: var\nord: en\nord: gång\nord: en\nDu gav 4 olika ord

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Ordna listor

Elementen i en lista kan ordnas från minsta till största med metoden sort.

lista = [2,5,1,2,4]\nlista.sort()\nprint(lista)

[1, 2, 2, 4, 5]

Notera att den här metoden modifierar själva listan. Alltid vill vi inte ändra på den ursprungliga listan, och då kan vi istället använda funktionen sorted. Den returnerar en ordnad lista:

lista = [2,5,1,2,4]\nprint(sorted(lista))

[1, 2, 2, 4, 5]

Kom ihåg skillnaden mellan dessa: sort ändrar på ordningen i den ursprungliga listan medan sorted skapar en ny, ordnad kopia av listan. Med sorted kan vi behålla den ursprungliga ordningen hos listan:

ursprunglig = [2, 5, 1, 2, 4]\nordnad = sorted(ursprunglig)\nprint(ursprunglig)\nprint(ordnad)

[2, 5, 1, 2, 4]\n[1, 2, 2, 4, 5]

Skapa ett program som ber användaren ange siffror, som läggs till i en lista. Efter varje tillägg skrivs listan ut på två sätt:

    \n
  • elementen i den ordning de lagts till
  • \n
  • elementen i storleksordning från det minsta till det största
  • \n

Programmet avslutas då användaren anger siffran 0.

Exempel:

Ange siffra: 3\nLista: [3]\nOrdnat: [3]\nAnge siffra: 1\nLista: [3, 1]\nOrdnat: [1, 3]\nAnge siffra: 9\nLista: [3, 1, 9]\nOrdnat: [1, 3, 9]\nAnge siffra: 5\nLista: [3, 1, 9, 5]\nOrdnat: [1, 3, 5, 9]\nAnge siffra: 0\nHejdå!

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Maximi- och minimivärde samt summa

Funktionerna max och min returnerar det största respektive minsta värdet i en lista. Funktionen sum returnerar summan av listans värden.

lista = [5, 2, 3, 1, 4]\n\nstorst = max(lista)\nminst = min(lista)\nsumma = sum(lista)\n\nprint(\"Minst:\", minst)\nprint(\"Störst:\", storst)\nprint(\"Summa:\", summa)

Minst: 1\nStörst: 5\nSumma: 15

Metoder och funktioner

Det finns två sätt att behandla listor i Python, och det här kan ibland orsaka huvudbry. För det mesta kommer du att använda metoder hos listor – till exempel append och sort. De används med punktoperatorn hos listvariabeln:

lista = []\n\n# metodanrop\nlista.append(3)\nlista.append(1)\nlista.append(7)\nlista.append(2)\n\n# metodanrop\nlista.sort()

En del funktioner kan ta emot listor som argument. De nyss presenterade funktionerna max, min, len och sorted är goda exempel:

lista = [3, 2, 7, 1]\n\n# funktionsanrop, lista som argument\nstorst = max(lista)\nminst = min(lista)\npituus = len(lista)\n\nprint(\"Minst:\", minst)\nprint(\"Störst:\", storst)\nprint(\"Listan pituus:\", pituus)\n\n# funktionsanrop, lista som argument, ordnad lista returneras\njarjestyksessa = sorted(lista)\nprint(jarjestyksessa)

Minst: 1\nStörst: 7\nListan pituus: 4\n[1, 2, 3, 7]

Lista som argument eller returvärde

Som de inbyggda funktionerna ovan kan också våra egna funktioner ta listor som argument och returnera listor. Den följande funktionen tar reda på det mellersta – median – värdet i en ordnad lista:

def median(lista: list):\n    ordnad = sorted(lista)\n    mellersta = len(ordnad) // 2\n    return ordnad[mellersta]

Funktionen skapar en ordnad version av listan som gavs som argument och returnerar elementet i mitten. Märk heltalsdivisionsoperatorn // som används. Indexet i en lista måste vara ett heltal.

Funktionen fungerar på följande sätt:

skonummer = [45, 44, 36, 39, 40]\nprint(\"Skonumrens median är\", median(skonummer))\n\niat = [1, 56, 34, 22, 5, 77, 5]\nprint(\"Medianåldern är\", median(iat))

Skonumrens median är 40\nMedianåldern är 22

En funktion kan också returnera en lista. Den följande funktionen ber användaren ge heltal, som sedan returneras som en lista:

def las_in_siffror():\n    siffror = []\n    while True:\n        indata = input(\"Ge siffra (tomt avslutar programmet): \")\n        if len(indata) == 0:\n            break\n        siffror.append(int(indata))\n    return siffror

Funktionen använder sig av hjälpvariabeln siffror, som är en lista. Alla siffror som användaren skriver läggs till i listan. När loopen avslutas returnerar funktionen listan med satsen return siffror.

När funktionen anropas så här…

siffror = las_in_siffror()\n\nprint(\"Största siffran är\", max(siffror))\nprint(\"Medianvärdet är\", median(siffror))

…kan utskriften se ut på följande sätt:

Ge siffra (tomt avslutar programmet): 5\nGe siffra (tomt avslutar programmet): -22\nGe siffra (tomt avslutar programmet): 4\nGe siffra (tomt avslutar programmet): 35\nGe siffra (tomt avslutar programmet): 1\nGe siffra (tomt avslutar programmet):\nStörsta siffran är 35\nMedianvärdet är 4

Det här lilla exemplet demonstrerar ett av de viktigaste användningsområdena för funktioner: de hjälper dig att dela din kod i mindre helheter som enkelt kan förstås.

Samma funktionalitet kan förstås fås till stånd utan några som helst egna funktioner:

siffror = []\nwhile True:\n    indata = input(\"Ge siffra (tomt avslutar programmet): \")\n    if len(indata) == 0:\n        break\n    siffror.append(int(indata))\n\nordnad = sorted(siffror)\nmellersta = len(ordnad) // 2\nmedian = ordnad[mellersta]\n\nprint(\"Största siffran är\", max(siffror))\nprint(\"Medianvärdet är\", median)

I den här versionen är det svårare att uppfatta logiken bakom programmet, eftersom det inte mera är enkelt att se vilka instruktioner som hör till vilken funktionalitet. Koden uppnår samma mål – ta emot data, räkna medianvärde o.s.v. – men strukturen är mycket mindre tydlig.

Att dela din kod i flera funktioner kommer att förbättra läsbarheten av koden och hjälper att uppfatta logiska helheter. Det här är till nytta när du ska verifiera att programmet fungerar som det ska, eftersom varje funktion kan testas separat.

En annan viktig orsak till att använda funktioner är återanvändbarhet av koden. Om du behöver samma funktionalitet på flera ställen i ditt program är det en bra idé att skapa en funktion och namnge den väl:

print(\"Skonummer:\")\nskor = las_in_siffror()\n\nprint(\"Painot:\")\nvikter = las_in_siffror()\n\nprint(\"Pituudet:\")\nlangder = las_in_siffror()

Skapa funktionen langd som returnerar längden på den lista som getts som argument.

lista = [1, 2, 3, 4, 5]\nsvar = langd(lista)\nprint(\"svar\", svar)\n\n# observera att du också kan anropa funktionen genom att direkt ge en lista som argument till funktionen\nsvar = langd([1, 1, 1, 1])\nprint(\"svar\", svar)

svar 5\nsvar 4

Skapa funktionen medeltal som returnerar medelvärdet av värdena i en lista bestående av heltal som getts som argument till funktionen.

lista = [1, 2, 3, 4, 5]\nsvar = medeltal(lista)\nprint(\"svar\", svar)

svar 3.0

Skapa funktionen variationsbredd som returnerar diffrensen av det största och minsta värdet i en lista med heltal som getts som argument till funktionen.

lista = [1, 2, 3, 4, 5]\nsvar = variationsbredd(lista)\nprint(\"svar\", svar)

svar 4

Mer om att behandla listor

Det finns flera sätt till att använda listor i Python. Om du vill läsa mera är Pythons dokumentation ett bra ställe att börja med.

","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/3-listor.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad listor är i Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur du kommer åt ett visst element i en lista"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du lägga till och avlägsna ett element från en lista"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till inbyggda funktioner och metoder för listor."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tills nu har vi lagrat data med variabler i våra program, så att varje sak har lagrats i sin egen variabel. Det här har sina begränsningar – det kan bli arbetsdrygt att tilldela variabler för allt då det finns mycket data att behandla."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python är en lista en samling av värden som finns lagrade under samma variabelnamn. Listans element skrivs mellan hakparenteser. Listans värden kallas element."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande instruktion skapar en ny tom lista…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…medan det här instruktionen skapar en lista med fem element:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"komma-at-element-i-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Komma åt element i en lista"},{"type":"element","tagName":"a","properties":{"href":"#komma-at-element-i-en-lista","ariaLabel":"komma at element i en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Elementen i en lista är indexerade på samma sätt som tecken i en sträng. Indexeringen börjar från noll och det sista indexet är listans längd minus ett:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 581px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cfea0276df6deb4e5dd7f97ef7d30e0e/92d15/4_2_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 26.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsSAAALEgHS3X78AAAAuElEQVQY032QWQqEQBBD+/7X8SyCggv6Jy6475rhFSjMfExDkUoqnV6cJA3DoKqqtG2b9eCyLFb0+75rXdeX/1vuvu8vAU7AeZ7WU9d1WbHAR3s8x3G8Hvd7AoamaTSOo23AzAHcDA6iPa8gZJ5n4+x1DPu+twCQ4vmY6EFmXddpmqaX0/MNfFHbthbs+74cIgHcqq5rlWWpJEmUZZnCMFSapoqiSEEQmE7FcWyY57nNCCqKQp7n6QPgCoGcLj0znwAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cfea0276df6deb4e5dd7f97ef7d30e0e/a0b58/4_2_1.webp 230w","/static/cfea0276df6deb4e5dd7f97ef7d30e0e/bc10c/4_2_1.webp 460w","/static/cfea0276df6deb4e5dd7f97ef7d30e0e/39daa/4_2_1.webp 581w"],"sizes":"(max-width: 581px) 100vw, 581px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cfea0276df6deb4e5dd7f97ef7d30e0e/81c8e/4_2_1.png 230w","/static/cfea0276df6deb4e5dd7f97ef7d30e0e/08a84/4_2_1.png 460w","/static/cfea0276df6deb4e5dd7f97ef7d30e0e/92d15/4_2_1.png 581w"],"sizes":"(max-width: 581px) 100vw, 581px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cfea0276df6deb4e5dd7f97ef7d30e0e/92d15/4_2_1.png","alt":"Lista indeksoidaan nollasta alkaen","title":"Lista indeksoidaan nollasta alkaen","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett specifikt element kan kommas åt på samma sätt som ett specifikt tecken i en sträng, med hakparenteser:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summan av de två första:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n2\n5\nSumman av de två första: 9"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hela listans innehåll kan också skrivas ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[7, 2, 2, 5, 2]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till skillnad från strängar är listor föränderliga, vilket betyder att deras innehåll kan ändra. Du kan tilldela ett nytt värde till ett element i en lista på samma sätt som du kan tilldela ett nytt värde till en variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[7, 2, 2, 5, 2]\n[7, 3, 2, 5, 2]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" berättar antalet element i en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ändra på elementens värden","tmcname":"osa04-07a_andra_varden"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som initierar en lista med värdena "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1, 2, 3, 4, 5]"}]},{"type":"text","value":". Programmet ber därefter användaren ange ett index och ett nytt värde som programmet lagrar, och listan skrivs ut på nytt. Programmet avslutas då använder anger -1 som index."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nAnge värde: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n[10, 2, 3, 4, 5]\nAnge index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge värde: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"250"}]},{"type":"text","value":"\n[10, 2, 250, 4, 5]\nAnge index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nAnge värde: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-45"}]},{"type":"text","value":"\n[10, 2, 250, 4, -45]\nAnge index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lagga-till-element-i-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till element i en lista"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-element-i-en-lista","ariaLabel":"lagga till element i en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" lägger till element i slutet av en lista. Metoden fungerar så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[5, 10, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel använder sig av två listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nskonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nskonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"37"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffror:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Skonummer:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Elementet läggs till i slutet av den lista som metoden anropas på:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffror:\n[5, 10, 3]\nSkonummer:\n[37, 44, 40, 28]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lägg till element i en lista","tmcname":"osa04-07b_nya_element"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge antalet siffror. Därefter ber programmet användaren ange det här antalet siffror, som läggs till i en lista i den givna ordningen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan ska till slut skrivas ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur många siffror: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nGe siffra 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nGe siffra 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"250"}]},{"type":"text","value":"\nGe siffra 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\n[10, 250, 34]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lagga-till-ett-element-pa-ett-specifikt-stalle","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till ett element på ett specifikt ställe"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-ett-element-pa-ett-specifikt-stalle","ariaLabel":"lagga till ett element pa ett specifikt stalle permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill specificera på vilket ställe i en lista ett värde ska läggas till, kan du använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insert"}]},{"type":"text","value":"-metoden. Metoden lägger till ett element vid ett specifikt index. Alla element vid det här eller senare index flyttar ett index framåt, \"mot höger\":"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 632px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6bc807508a898afcc180481b54c507a4/084e2/4_2_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 65.21739130434783%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsSAAALEgHS3X78AAABWElEQVQ4y62TS27DMAxEfYteuZteqqfoKkAQoMjPtiT/HdsTPAUUDMfoqgQGEmlpSI7oTJLO53NEVVUJIQS1bau+79U0TYyxx5Zl2QWW2eY/DK5snRF7PB7K8zxWRwx/HMcE86kW4IO3Cud51jRN8bD3XmVZqq7r2Co+QAaTg9U5l+ImRyIsikKXy0XH4zHqeb/fY6UQc9FWI+y6Lt4BxOgoEu5pQbXDMETwIJCtAcnWT4RUSNlkJ7NltxYgh5QLttIBZywGSJ4I+WgkfNgKvTWkoE2S7b7yX2OwnoC1JDwYXW0t2xvOLaE0qe9bOfd6bcjQ7XA4vE3BW4WMDoeR4OUvGkepaSbVdZV0s9ExXQEPlEFg+rFyyMYhhEbe/8r7L4XwLe8HhfCqBCKqWs8lsQyi6/Uahb7dbjqdTnEey7KQc51C+FEIH3LuU0UheZ+nQTZi7rLnp3gCiHjy2zC3accAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bc807508a898afcc180481b54c507a4/a0b58/4_2_2.webp 230w","/static/6bc807508a898afcc180481b54c507a4/bc10c/4_2_2.webp 460w","/static/6bc807508a898afcc180481b54c507a4/59680/4_2_2.webp 632w"],"sizes":"(max-width: 632px) 100vw, 632px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bc807508a898afcc180481b54c507a4/81c8e/4_2_2.png 230w","/static/6bc807508a898afcc180481b54c507a4/08a84/4_2_2.png 460w","/static/6bc807508a898afcc180481b54c507a4/084e2/4_2_2.png 632w"],"sizes":"(max-width: 632px) 100vw, 632px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6bc807508a898afcc180481b54c507a4/084e2/4_2_2.png","alt":"Alkion lisäys listaan","title":"Alkion lisäys listaan","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel det här programmet…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skriver ut följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[10, 1, 2, 3, 4, 5, 6]\n[10, 1, 20, 2, 3, 4, 5, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ta-bort-element-fran-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Ta bort element från en lista"},{"type":"element","tagName":"a","properties":{"href":"#ta-bort-element-fran-en-lista","ariaLabel":"ta bort element fran en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns två sätt för att ta bort ett element från en lista:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om man vet indexet för elementet kan man använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"-metoden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om man vet innehållet i elementet kan man använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":"-metoden."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"-metoden tar indexet för elementet som ska avlägsnas som argument. Följande program tar bort elementen vid indexen 2 och 3 i listan. Märk hur indexen för de resterande elementen ändrar när ett element avlägsnas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 4, 5, 6]\n[1, 2, 4, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är bra att minnas att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"-metoden också returnerar det avlägsnade elementet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n[4, 2, 2, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":"-metoden tar värdet för det element som ska avlägsnas som argument. Till exempel följande program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skriver ut detta:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 4, 5, 6]\n[1, 3, 4, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden tar bort det första värdet som motsvarar det givna argumentet från listan – på samma sätt som funktionen find hos strängar returnerar den första delsträngen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 1, 2]\n[2, 2]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lägg till, ta bort","tmcname":"osa04-07c_lagg_till_ta_bort"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som låter användaren lägga till eller avlägsna ett värde. Varje operation görs i slutet av listan. När ett element läggs till är dess värde alltid ett större än det föregående värdet (1 om inga element finns i listan)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mellan varje operation skrivs listan ut. Här följer ett exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan är nu []\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nListan är nu [1]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nListan är nu [1, 2]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nListan är nu [1, 2, 3]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nListan är nu [1, 2]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"l"}]},{"type":"text","value":"\nListan är nu [1, 2, 3]\n(l)ägg till, (a)vlägsna eller a(v)sluta: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"v"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att man inte försöker avlägsna element då listan är tom."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det givna elementet inte hittas i listan, kommer remove-funktionen att ge ett fel. På samma sätt som med strängar kan vi kolla om ett element finns i en lista med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":"-operatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Listan innehåller värdet 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Listan innehåller värdet 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan innehåller värdet 1"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Samma ord två gånger","tmcname":"osa04-08_samma_tva_ganger"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren mata in ord. När användaren anger ett ord som hon gett tidigare, avslutas programmet och antalet ord skrivs ut."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"det"}]},{"type":"text","value":"\nord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"var"}]},{"type":"text","value":"\nord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"en"}]},{"type":"text","value":"\nord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"gång"}]},{"type":"text","value":"\nord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"en"}]},{"type":"text","value":"\nDu gav 4 olika ord"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"ordna-listor","style":"position:relative;"},"children":[{"type":"text","value":"Ordna listor"},{"type":"element","tagName":"a","properties":{"href":"#ordna-listor","ariaLabel":"ordna listor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Elementen i en lista kan ordnas från minsta till största med metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notera att den här metoden modifierar själva listan. Alltid vill vi inte ändra på den ursprungliga listan, och då kan vi istället använda funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":". Den returnerar en ordnad lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg skillnaden mellan dessa: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" ändrar på ordningen i den ursprungliga listan medan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" skapar en ny, ordnad kopia av listan. Med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" kan vi behålla den ursprungliga ordningen hos listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ursprunglig "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nordnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ursprunglig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ursprunglig"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 5, 1, 2, 4]\n[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"En lista, två varianter","tmcname":"osa04-08b_en_lista_tva_varianter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange siffror, som läggs till i en lista. Efter varje tillägg skrivs listan ut på två sätt:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"elementen i den ordning de lagts till"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"elementen i storleksordning från det minsta till det största"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet avslutas då användaren anger siffran 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nLista: [3]\nOrdnat: [3]\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nLista: [3, 1]\nOrdnat: [1, 3]\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nLista: [3, 1, 9]\nOrdnat: [1, 3, 9]\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nLista: [3, 1, 9, 5]\nOrdnat: [1, 3, 5, 9]\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i någon av dessa uppgifter, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"maximi--och-minimivarde-samt-summa","style":"position:relative;"},"children":[{"type":"text","value":"Maximi- och minimivärde samt summa"},{"type":"element","tagName":"a","properties":{"href":"#maximi--och-minimivarde-samt-summa","ariaLabel":"maximi och minimivarde samt summa permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":" returnerar det största respektive minsta värdet i en lista. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" returnerar summan av listans värden."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nstorst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nminst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minst:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Störst:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summa:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Minst: 1\nStörst: 5\nSumma: 15"}]}]},{"type":"element","tagName":"h2","properties":{"id":"metoder-och-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Metoder och funktioner"},{"type":"element","tagName":"a","properties":{"href":"#metoder-och-funktioner","ariaLabel":"metoder och funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns två sätt att behandla listor i Python, och det här kan ibland orsaka huvudbry. För det mesta kommer du att använda metoder hos listor – till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":". De används med punktoperatorn hos listvariabeln:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metodanrop"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metodanrop"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En del funktioner kan ta emot listor som argument. De nyss presenterade funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" är goda exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktionsanrop, lista som argument"}]},{"type":"text","value":"\nstorst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nminst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npituus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minst:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Störst:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Listan pituus:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pituus"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktionsanrop, lista som argument, ordnad lista returneras"}]},{"type":"text","value":"\njarjestyksessa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jarjestyksessa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Minst: 1\nStörst: 7\nListan pituus: 4\n[1, 2, 3, 7]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lista-som-argument-eller-returvarde","style":"position:relative;"},"children":[{"type":"text","value":"Lista som argument eller returvärde"},{"type":"element","tagName":"a","properties":{"href":"#lista-som-argument-eller-returvarde","ariaLabel":"lista som argument eller returvarde permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som de inbyggda funktionerna ovan kan också våra egna funktioner ta listor som argument och returnera listor. Den följande funktionen tar reda på det mellersta – median – värdet i en ordnad lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"median"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ordnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n mellersta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"mellersta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen skapar en ordnad version av listan som gavs som argument och returnerar elementet i mitten. Märk heltalsdivisionsoperatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" som används. Indexet i en lista måste vara ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen fungerar på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"skonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"36"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"39"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Skonumrens median är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\niat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"56"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"34"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"77"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medianåldern är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"iat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skonumrens median är 40\nMedianåldern är 22"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En funktion kan också returnera en lista. Den följande funktionen ber användaren ge heltal, som sedan returneras som en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_in_siffror"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge siffra (tomt avslutar programmet): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffror"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen använder sig av hjälpvariabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror"}]},{"type":"text","value":", som är en lista. Alla siffror som användaren skriver läggs till i listan. När loopen avslutas returnerar funktionen listan med satsen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return siffror"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När funktionen anropas så här…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_in_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Största siffran är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medianvärdet är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…kan utskriften se ut på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ge siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-22"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"35"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nGe siffra (tomt avslutar programmet):\nStörsta siffran är 35\nMedianvärdet är 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här lilla exemplet demonstrerar ett av de viktigaste användningsområdena för funktioner: de hjälper dig att dela din kod i mindre helheter som enkelt kan förstås."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma funktionalitet kan förstås fås till stånd utan några som helst egna funktioner:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ge siffra (tomt avslutar programmet): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nordnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmellersta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nmedian "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ordnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"mellersta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Största siffran är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medianvärdet är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här versionen är det svårare att uppfatta logiken bakom programmet, eftersom det inte mera är enkelt att se vilka instruktioner som hör till vilken funktionalitet. Koden uppnår samma mål – ta emot data, räkna medianvärde o.s.v. – men strukturen är mycket mindre tydlig."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att dela din kod i flera funktioner kommer att förbättra läsbarheten av koden och hjälper att uppfatta logiska helheter. Det här är till nytta när du ska verifiera att programmet fungerar som det ska, eftersom varje funktion kan testas separat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En annan viktig orsak till att använda funktioner är återanvändbarhet av koden. Om du behöver samma funktionalitet på flera ställen i ditt program är det en bra idé att skapa en funktion och namnge den väl:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Skonummer:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_in_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Painot:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvikter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_in_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pituudet:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlangder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_in_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Listans längd","tmcname":"osa04-09_listans_langd"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langd"}]},{"type":"text","value":" som returnerar längden på den lista som getts som argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# observera att du också kan anropa funktionen genom att direkt ge en lista som argument till funktionen"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"svar 5\nsvar 4"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Medeltal","tmcname":"osa04-10_medeltal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"medeltal"}]},{"type":"text","value":" som returnerar medelvärdet av värdena i en lista bestående av heltal som getts som argument till funktionen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"svar 3.0"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Variationsbredd","tmcname":"osa04-11_variationsbredd"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"variationsbredd"}]},{"type":"text","value":" som returnerar diffrensen av det största och minsta värdet i en lista med heltal som getts som argument till funktionen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" variationsbredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"svar 4"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"mer-om-att-behandla-listor","style":"position:relative;"},"children":[{"type":"text","value":"Mer om att behandla listor"},{"type":"element","tagName":"a","properties":{"href":"#mer-om-att-behandla-listor","ariaLabel":"mer om att behandla listor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns flera sätt till att använda listor i Python. Om du vill läsa mera är "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/tutorial/datastructures.html#more-on-lists","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pythons dokumentation"}]},{"type":"text","value":" ett bra ställe att börja med."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad listor är i Python
  • \n
  • vet du hur du kommer åt ett visst element i en lista
  • \n
  • kan du lägga till och avlägsna ett element från en lista
  • \n
  • känner du till inbyggda funktioner och metoder för listor.
  • \n

Tills nu har vi lagrat data med variabler i våra program, så att varje sak har lagrats i sin egen variabel. Det här har sina begränsningar – det kan bli arbetsdrygt att tilldela variabler för allt då det finns mycket data att behandla.

I Python är en lista en samling av värden som finns lagrade under samma variabelnamn. Listans element skrivs mellan hakparenteser. Listans värden kallas element.

Följande instruktion skapar en ny tom lista…

lista = []

…medan det här instruktionen skapar en lista med fem element:

lista = [7, 2, 2, 5, 2]

Komma åt element i en lista

Elementen i en lista är indexerade på samma sätt som tecken i en sträng. Indexeringen börjar från noll och det sista indexet är listans längd minus ett:

\n \n \n \n \n \n \"Lista\n \n \n

Ett specifikt element kan kommas åt på samma sätt som ett specifikt tecken i en sträng, med hakparenteser:

lista = [7, 2, 2, 5, 2]\n\nprint(lista[0])\nprint(lista[1])\nprint(lista[3])\n\nprint(\"Summan av de två första:\", lista[0] + lista[1])

7\n2\n5\nSumman av de två första: 9

Hela listans innehåll kan också skrivas ut:

lista = [7, 2, 2, 5, 2]\nprint(lista)

[7, 2, 2, 5, 2]

Till skillnad från strängar är listor föränderliga, vilket betyder att deras innehåll kan ändra. Du kan tilldela ett nytt värde till ett element i en lista på samma sätt som du kan tilldela ett nytt värde till en variabel:

lista = [7, 2, 2, 5, 2]\nprint(lista)\nlista[1] = 3\nprint(lista)

[7, 2, 2, 5, 2]\n[7, 3, 2, 5, 2]

Funktionen len berättar antalet element i en lista:

lista = [7, 2, 2, 5, 2]\nprint(len(lista))

5

Skapa ett program som initierar en lista med värdena [1, 2, 3, 4, 5]. Programmet ber därefter användaren ange ett index och ett nytt värde som programmet lagrar, och listan skrivs ut på nytt. Programmet avslutas då använder anger -1 som index.

Exempel:

Ange index: 0\nAnge värde: 10\n[10, 2, 3, 4, 5]\nAnge index: 2\nAnge värde: 250\n[10, 2, 250, 4, 5]\nAnge index: 4\nAnge värde: -45\n[10, 2, 250, 4, -45]\nAnge index: -1

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Lägga till element i en lista

Metoden append lägger till element i slutet av en lista. Metoden fungerar så här:

siffror = []\nsiffror.append(5)\nsiffror.append(10)\nsiffror.append(3)\nprint(siffror)

[5, 10, 3]

Följande exempel använder sig av två listor:

siffror = []\nskonummer = []\n\nsiffror.append(5)\nsiffror.append(10)\nsiffror.append(3)\n\nskonummer.append(37)\nskonummer.append(44)\nskonummer.append(40)\nskonummer.append(28)\n\nprint(\"Siffror:\")\nprint(siffror)\n\nprint(\"Skonummer:\")\nprint(skonummer)

Elementet läggs till i slutet av den lista som metoden anropas på:

Siffror:\n[5, 10, 3]\nSkonummer:\n[37, 44, 40, 28]

Skapa ett program som ber användaren ge antalet siffror. Därefter ber programmet användaren ange det här antalet siffror, som läggs till i en lista i den givna ordningen.

Listan ska till slut skrivas ut:

Hur många siffror: 3\nGe siffra 1: 10\nGe siffra 2: 250\nGe siffra 3: 34\n[10, 250, 34]

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Lägga till ett element på ett specifikt ställe

Om du vill specificera på vilket ställe i en lista ett värde ska läggas till, kan du använda insert-metoden. Metoden lägger till ett element vid ett specifikt index. Alla element vid det här eller senare index flyttar ett index framåt, \"mot höger\":

\n \n

Skapa ett program som låter användaren lägga till eller avlägsna ett värde. Varje operation görs i slutet av listan. När ett element läggs till är dess värde alltid ett större än det föregående värdet (1 om inga element finns i listan).

Mellan varje operation skrivs listan ut. Här följer ett exempel:

Listan är nu []\n(l)ägg till, (a)vlägsna eller a(v)sluta: l\nListan är nu [1]\n(l)ägg till, (a)vlägsna eller a(v)sluta: l\nListan är nu [1, 2]\n(l)ägg till, (a)vlägsna eller a(v)sluta: l\nListan är nu [1, 2, 3]\n(l)ägg till, (a)vlägsna eller a(v)sluta: a\nListan är nu [1, 2]\n(l)ägg till, (a)vlägsna eller a(v)sluta: l\nListan är nu [1, 2, 3]\n(l)ägg till, (a)vlägsna eller a(v)sluta: v\nHejdå!

Du kan anta att man inte försöker avlägsna element då listan är tom.

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Om det givna elementet inte hittas i listan, kommer remove-funktionen att ge ett fel. På samma sätt som med strängar kan vi kolla om ett element finns i en lista med hjälp av in-operatorn:

lista = [1, 3, 4]\n\nif 1 in lista:\n    print(\"Listan innehåller värdet 1\")\n\nif 2 in lista:\n    print(\"Listan innehåller värdet 2\")

Listan innehåller värdet 1

Skapa ett program som ber användaren mata in ord. När användaren anger ett ord som hon gett tidigare, avslutas programmet och antalet ord skrivs ut.

ord: det\nord: var\nord: en\nord: gång\nord: en\nDu gav 4 olika ord

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Ordna listor

Elementen i en lista kan ordnas från minsta till största med metoden sort.

lista = [2,5,1,2,4]\nlista.sort()\nprint(lista)

[1, 2, 2, 4, 5]

Notera att den här metoden modifierar själva listan. Alltid vill vi inte ändra på den ursprungliga listan, och då kan vi istället använda funktionen sorted. Den returnerar en ordnad lista:

lista = [2,5,1,2,4]\nprint(sorted(lista))

[1, 2, 2, 4, 5]

Kom ihåg skillnaden mellan dessa: sort ändrar på ordningen i den ursprungliga listan medan sorted skapar en ny, ordnad kopia av listan. Med sorted kan vi behålla den ursprungliga ordningen hos listan:

ursprunglig = [2, 5, 1, 2, 4]\nordnad = sorted(ursprunglig)\nprint(ursprunglig)\nprint(ordnad)

[2, 5, 1, 2, 4]\n[1, 2, 2, 4, 5]

Skapa ett program som ber användaren ange siffror, som läggs till i en lista. Efter varje tillägg skrivs listan ut på två sätt:

    \n
  • elementen i den ordning de lagts till
  • \n
  • elementen i storleksordning från det minsta till det största
  • \n

Programmet avslutas då användaren anger siffran 0.

Exempel:

Ange siffra: 3\nLista: [3]\nOrdnat: [3]\nAnge siffra: 1\nLista: [3, 1]\nOrdnat: [1, 3]\nAnge siffra: 9\nLista: [3, 1, 9]\nOrdnat: [1, 3, 9]\nAnge siffra: 5\nLista: [3, 1, 9, 5]\nOrdnat: [1, 3, 5, 9]\nAnge siffra: 0\nHejdå!

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i någon av dessa uppgifter, om du inte ombeds göra det.

Maximi- och minimivärde samt summa

Funktionerna max och min returnerar det största respektive minsta värdet i en lista. Funktionen sum returnerar summan av listans värden.

lista = [5, 2, 3, 1, 4]\n\nstorst = max(lista)\nminst = min(lista)\nsumma = sum(lista)\n\nprint(\"Minst:\", minst)\nprint(\"Störst:\", storst)\nprint(\"Summa:\", summa)

Minst: 1\nStörst: 5\nSumma: 15

Metoder och funktioner

Det finns två sätt att behandla listor i Python, och det här kan ibland orsaka huvudbry. För det mesta kommer du att använda metoder hos listor – till exempel append och sort. De används med punktoperatorn hos listvariabeln:

lista = []\n\n# metodanrop\nlista.append(3)\nlista.append(1)\nlista.append(7)\nlista.append(2)\n\n# metodanrop\nlista.sort()

En del funktioner kan ta emot listor som argument. De nyss presenterade funktionerna max, min, len och sorted är goda exempel:

lista = [3, 2, 7, 1]\n\n# funktionsanrop, lista som argument\nstorst = max(lista)\nminst = min(lista)\npituus = len(lista)\n\nprint(\"Minst:\", minst)\nprint(\"Störst:\", storst)\nprint(\"Listan pituus:\", pituus)\n\n# funktionsanrop, lista som argument, ordnad lista returneras\njarjestyksessa = sorted(lista)\nprint(jarjestyksessa)

Minst: 1\nStörst: 7\nListan pituus: 4\n[1, 2, 3, 7]

Lista som argument eller returvärde

Som de inbyggda funktionerna ovan kan också våra egna funktioner ta listor som argument och returnera listor. Den följande funktionen tar reda på det mellersta – median – värdet i en ordnad lista:

def median(lista: list):\n    ordnad = sorted(lista)\n    mellersta = len(ordnad) // 2\n    return ordnad[mellersta]

Funktionen skapar en ordnad version av listan som gavs som argument och returnerar elementet i mitten. Märk heltalsdivisionsoperatorn // som används. Indexet i en lista måste vara ett heltal.

Funktionen fungerar på följande sätt:

skonummer = [45, 44, 36, 39, 40]\nprint(\"Skonumrens median är\", median(skonummer))\n\niat = [1, 56, 34, 22, 5, 77, 5]\nprint(\"Medianåldern är\", median(iat))

Skonumrens median är 40\nMedianåldern är 22

En funktion kan också returnera en lista. Den följande funktionen ber användaren ge heltal, som sedan returneras som en lista:

def las_in_siffror():\n    siffror = []\n    while True:\n        indata = input(\"Ge siffra (tomt avslutar programmet): \")\n        if len(indata) == 0:\n            break\n        siffror.append(int(indata))\n    return siffror

Funktionen använder sig av hjälpvariabeln siffror, som är en lista. Alla siffror som användaren skriver läggs till i listan. När loopen avslutas returnerar funktionen listan med satsen return siffror.

När funktionen anropas så här…

siffror = las_in_siffror()\n\nprint(\"Största siffran är\", max(siffror))\nprint(\"Medianvärdet är\", median(siffror))

…kan utskriften se ut på följande sätt:

Ge siffra (tomt avslutar programmet): 5\nGe siffra (tomt avslutar programmet): -22\nGe siffra (tomt avslutar programmet): 4\nGe siffra (tomt avslutar programmet): 35\nGe siffra (tomt avslutar programmet): 1\nGe siffra (tomt avslutar programmet):\nStörsta siffran är 35\nMedianvärdet är 4

Det här lilla exemplet demonstrerar ett av de viktigaste användningsområdena för funktioner: de hjälper dig att dela din kod i mindre helheter som enkelt kan förstås.

Samma funktionalitet kan förstås fås till stånd utan några som helst egna funktioner:

siffror = []\nwhile True:\n    indata = input(\"Ge siffra (tomt avslutar programmet): \")\n    if len(indata) == 0:\n        break\n    siffror.append(int(indata))\n\nordnad = sorted(siffror)\nmellersta = len(ordnad) // 2\nmedian = ordnad[mellersta]\n\nprint(\"Största siffran är\", max(siffror))\nprint(\"Medianvärdet är\", median)

I den här versionen är det svårare att uppfatta logiken bakom programmet, eftersom det inte mera är enkelt att se vilka instruktioner som hör till vilken funktionalitet. Koden uppnår samma mål – ta emot data, räkna medianvärde o.s.v. – men strukturen är mycket mindre tydlig.

Att dela din kod i flera funktioner kommer att förbättra läsbarheten av koden och hjälper att uppfatta logiska helheter. Det här är till nytta när du ska verifiera att programmet fungerar som det ska, eftersom varje funktion kan testas separat.

En annan viktig orsak till att använda funktioner är återanvändbarhet av koden. Om du behöver samma funktionalitet på flera ställen i ditt program är det en bra idé att skapa en funktion och namnge den väl:

print(\"Skonummer:\")\nskor = las_in_siffror()\n\nprint(\"Painot:\")\nvikter = las_in_siffror()\n\nprint(\"Pituudet:\")\nlangder = las_in_siffror()

Skapa funktionen langd som returnerar längden på den lista som getts som argument.

lista = [1, 2, 3, 4, 5]\nsvar = langd(lista)\nprint(\"svar\", svar)\n\n# observera att du också kan anropa funktionen genom att direkt ge en lista som argument till funktionen\nsvar = langd([1, 1, 1, 1])\nprint(\"svar\", svar)

svar 5\nsvar 4

Skapa funktionen medeltal som returnerar medelvärdet av värdena i en lista bestående av heltal som getts som argument till funktionen.

lista = [1, 2, 3, 4, 5]\nsvar = medeltal(lista)\nprint(\"svar\", svar)

svar 3.0

Skapa funktionen variationsbredd som returnerar diffrensen av det största och minsta värdet i en lista med heltal som getts som argument till funktionen.

lista = [1, 2, 3, 4, 5]\nsvar = variationsbredd(lista)\nprint(\"svar\", svar)

svar 4

Mer om att behandla listor

Det finns flera sätt till att använda listor i Python. Om du vill läsa mera är Pythons dokumentation ett bra ställe att börja med.

","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/3-listor.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-4/4-iteration/page-data.json b/page-data/osa-4/4-iteration/page-data.json index 3e639489c..456acdbf9 100644 --- a/page-data/osa-4/4-iteration/page-data.json +++ b/page-data/osa-4/4-iteration/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-4/4-iteration", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad som menas med iteration"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen i Python fungerar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop för att gå igenom listor och strängar."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda en while-loop för att gå igenom elementen i en lista på samma sätt som vi använde oss av while-loopar för att gå igenom strängar. Det följande programmet skriver ut elementen i en lista på varsin rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nplats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" plats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"plats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n plats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2\n4\n5\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här fungerar helt bra, men det är ett komplicerat sätt att gå igenom en lista eftersom att det krävs en hjälpvariabel, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"index"}]},{"type":"text","value":", för att minnas vid vilket element man är vid. Till all lycka erbjuder Python ett enklare sätt att gå igenom listor, strängar och liknande strukturer."}]},{"type":"element","tagName":"h2","properties":{"id":"for-loopen","style":"position:relative;"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen"},{"type":"element","tagName":"a","properties":{"href":"#for-loopen","ariaLabel":"for loopen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du vill gå igenom en färdig samling av element i Python, kan du använda dig av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop. Loopen kan till exempel gå igenom alla element i en lista – från det första till det sista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man använder en while-loop, vet programmet inte på förhand hur många iterationer (\"varv\") loopen kommer att gå igenom. Loopen kommer att fortsätta tills villkoret inte längre är sant eller loopen avslutas på ett annat sätt. I en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop vet man antalet iterationer på förhand."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen går igenom elementen i samlingen ett för ett och utför samma sak för varje element. Programmeraren behöver inte fundera på vilket element som behandlas och när det görs. Syntaxen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen är den följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"variabel"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"samling"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"block"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen tar ett element i samlingen, tilldelar det till en variabel, kör kodblocket och fortsätter till nästa element. När alla element har behandlats kommer programmet att fortsätta köras från och med kodraden som följer loopen."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 327px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d403f61ea9acc12fa15e837c0051c2eb/00e65/4_3_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 214.3478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAArCAYAAAB4pah1AAAACXBIWXMAAAsSAAALEgHS3X78AAADQklEQVRIx62X2U4qQRCG5/V5DR8AE72DG2LEC4EARgVFQJRNATfcUevkq5PqtHN6GCCnk84Uvfxdy1/VTSSB9vPzo9+vry9ZLBa/xtJatAxwNpvJ7e3t/wOcTqdyc3PjxlYBjUJgtnE+n8vd3d1mgLaYL76jHR4eSq1Wc/701yQCMvn9/a09vnBnZ0fy+XzQglB3gH77+PiQ0WgknU5HTX54eFB5PB67iCc1sNTkVqsl7XZbN/X7fbm6upKzszMZDodycXEh3W5Xfdnr9fQwDnl8fHTf+/t7eX19/ashg9lsVnZ3d+X09FQmk4mC1Ot1aTabcnx8rPLLy4tGvVKp6IHQCQXoyOwBNEKjz89PpzIaMMki8y+aYoFxMtRY8/T0JBECvvIbEYXUuOL8/FxN8iPsdw6kDQYDxYkQnp+f3WJbgIzPmA9xNP5NBLT29vamgalWq/L+/i4hvqYC0qCGAWxvb0sul1OZMSO8f3AQ0HxIFPEbNCGSaEemIF9eXkqj0VDHp2pogBB4WSNQHLoyIJqxAerAR2R4hgzH0N4vFqmA8K9cLsve3p4rDPRisaiEPjo60uxYy2TMIkjWiTZaIVMb19IQQKLplzAjOo2swvwkQM0UHxBziCYbySBy++TkRGXWcSA5Hc8YO8wB+jxEQ/u9tbWlhcOqt5/zcR4SPA77pSHJT6Svr69VIyoNWiKjAZkT4iHBJJAET4uDmQHYsvIOjZJ4iBIENELA0aQclcXMwp+22QoGUWaT70ObQzFcpQUWzRiAg1bm+V0qlbQO2iG4xAcMRtk3iZw1QKgCB3GH0SYNUHnoqw0tll1EAC7joQO0QS4cTISL8U6xtXtjKWDogYTP0NTvjPnVPLEehk5Lu3tTNfTNJggQGDPxKR0WxM1d2WQcH7ouSa0QqVMB2UQkCRAUQaZkWcFN8qErDvEJNjMJqQuFgt4rjNEzmYwcHBy4yoMr6LiHLONdFNQQk9HQ3jF2n+BHv+LES1jwfQiYmeY38pgXhJ/boWfeP4BoxnWJ2VxKdEzEJL90Jb4PQxwzDYisPUVCpF75Sey/sc3MEPdSAeMacm1yp2ykod9sM++a/f19N7bR34q4C9b50wPgH3De+nsX6dJiAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d403f61ea9acc12fa15e837c0051c2eb/a0b58/4_3_1.webp 230w","/static/d403f61ea9acc12fa15e837c0051c2eb/e25e9/4_3_1.webp 327w"],"sizes":"(max-width: 327px) 100vw, 327px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d403f61ea9acc12fa15e837c0051c2eb/81c8e/4_3_1.png 230w","/static/d403f61ea9acc12fa15e837c0051c2eb/00e65/4_3_1.png 327w"],"sizes":"(max-width: 327px) 100vw, 327px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d403f61ea9acc12fa15e837c0051c2eb/00e65/4_3_1.png","alt":"Listan iterointi","title":"Listan iterointi","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program skriver ut alla element i en lista med hjälp av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2\n4\n5\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jämfört med exemplet i början av den här delen är strukturen mycket enklare att förstå. En "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop gör det enkelt att gå igenom elementen i en samling från början till slut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma princip gäller också för strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tecken"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Peter"}]},{"type":"text","value":"\nP\ne\nt\ne\nr"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Utskrift med asterisker","tmcname":"osa04-11a_utskrift_asterisker"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng. Programmet ska sedan skriva ut strängen så att dess tecken kommer under varandra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter varje tecken skriver man också ut en asterisk på en ny rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange en sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python"}]},{"type":"text","value":"\nP\n*\ny\n*\nt\n*\nh\n*\no\n*\nn\n*"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I de här uppgifterna ska du inte placera kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"funktionen-range","style":"position:relative;"},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"a","properties":{"href":"#funktionen-range","ariaLabel":"funktionen range permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man vet ofta hur många gånger man vill upprepa en viss kodsnutt. Det kan till exempel hända att du vill gå igenom siffrorna ett till hundra. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" kan användas i samband med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop för att uppnå detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns några olika sätt att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-funktionen. Det enklaste sättet är att använda ett argument, som då hänvisar till slutpunkten. Själva slutpunkten inkluderas inte, likt extrahering av delsträngar (slice). Det betyder alltså att anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(n)"}]},{"type":"text","value":" ger oss en loop som går igenom siffrorna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n - 1"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med två argument kommer funktionen att returnera ett intervall mellan två siffror. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(a, b)"}]},{"type":"text","value":" kommer att ge ett intervall som startar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och slutar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b - 1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n4\n5\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till sist: vi kan ge ett tredje argument som specificerar avståndet mellan värdena. Anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(a, b, c)"}]},{"type":"text","value":" kommer att ge ett intervall som börjar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och slutar vid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b - 1"}]},{"type":"text","value":", och ökar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":" för varje steg."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n3\n5\n7"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Avståndet eller steget kan också vara negativt. Då kommer intervallet att vara ordnat i fallande ordning. Observera att de två första argumenten har bytt plats här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"6\n5\n4\n3"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Från negativ till positiv","tmcname":"osa04-11b_negativ_till_positiv"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge ett positivt heltal "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":". Programmet ska därefter skriva ut siffrorna i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-n ... n"}]},{"type":"text","value":", exklusive noll. Varje siffra skrivs ut på en skild rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n-4\n-3\n-2\n-1\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I de här uppgifterna ska du inte placera kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"fran-intervall-till-lista","style":"position:relative;"},"children":[{"type":"text","value":"Från intervall till lista"},{"type":"element","tagName":"a","properties":{"href":"#fran-intervall-till-lista","ariaLabel":"fran intervall till lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" returnerar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-objekt som på flera sätt fungerar som en lista, men i verkligheten inte är det. Om du försöker skriva ut värdet som funktionen returnerar kommer du bara att se en beskrivning av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-objektet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"range(2, 7)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" konverterar ett intervall till en lista. Listan kommer att innehålla de värden som finns i intervallet. I fortsättningskursen i Python som följer den här kursen kommer vi att gå djupare in på det här."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 3, 4, 5, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-paminnelse-om-de-automatiska-testen","style":"position:relative;"},"children":[{"type":"text","value":"En påminnelse om de automatiska testen"},{"type":"element","tagName":"a","properties":{"href":"#en-paminnelse-om-de-automatiska-testen","ariaLabel":"en paminnelse om de automatiska testen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tills nu har de övningar som krävt att du skriver en funktion haft färdiga mallar som sett ut på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# din lösning ska skrivas här"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det lönar sig att testa på funktionen här, på följande sätt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jag gillar blåbärspaj så länge den inte innehåller ägg\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_tva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Från och med nu kommer det inte längre att finnas påminnelser om att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket. De automatiska testen kommer fortsättningsvis ändå att kräva att de används, så du måste själv lägga till blocket i din kod när du testar dina funktioner inom programmets huvudfunktion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Vissa övningar, som Palindrom i den här delen, förutsätter att du skriver kod som använder sig av den funktionen du gjort. Den här koden bör inte läggas i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket. De automatiska testen kör ingen kod inom dessa block, så din lösning kommer inte att vara fullständig om du placerar dina funktionsanrop där."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Asterisker","tmcname":"osa04-12_asterisker"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_som_asterisker"}]},{"type":"text","value":" som får som argument en lista med heltal. Funktionen ska skriva ut rader med asterisker så att siffrorna i listan indikerar antalet asterisker på en rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"T.ex. med anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_som_asterisker([3, 7, 1, 1, 2])"}]},{"type":"text","value":" ska resultatet vara:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"***\n*******\n*\n*\n**\n"}]}]},{"type":"comment","value":" **Huomaa** että tällä hetkellä Windowsissa on ongelmia joidenkin tehtävien testien suorittamisessa. Jos törmäät seuraavaan virheilmoitukseen\n\n\"Listan\n\nvoit suorittaa testit lähettämällä ne palvelimelle valitsemalla testien suoritusnapin oikealla puolella olevasta symbolista avautuvasta TMC-valikosta _Submit solutions_.\n\nOngelman saa korjattua menemällä laajennuksen asennusvalikkoon ja muuttamalla \"TMC Data\" -kohdassa tehtävien sijainnin johonkin toiseen sijaintiin, jonka tiedostopolku on lyhempi, allaolevassa kuvassa nappi _change path_. Siirrossa saattaa kestää hetken, joten odotathan operaation päättymistä.\n\n\"Listan\n\nOngelmaan pyritään saamaan parempi ratkaisu lähipäivinä. "}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Anagram","tmcname":"osa04-13_anagram"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"anagram"}]},{"type":"text","value":" som får två strängar som argument. Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om strängarna är anagram – dvs. de bildas av exakt samma bokstäver."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"stol\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lost\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"burk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bruk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"anagram\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"magarna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lykta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lycka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" fungerar även för strängar."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Palindrom","tmcname":"osa04-14_palindrom"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"palindrom"}]},{"type":"text","value":" som får en sträng som argument. Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om strängen är ett palindrom – dvs. den är den samma oberoende om man börjar läsa från vänster eller höger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett huvudprogram som ber användaren ange ord tills ett palindrom ges:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ett palindrom: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\ndet var inte ett palindrom\nAnge ett palindrom: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"java"}]},{"type":"text","value":"\ndet var inte ett palindrom\nAnge ett palindrom: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"snöhöna"}]},{"type":"text","value":"\ndet var inte ett palindrom\nAnge ett palindrom: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"snöhöns"}]},{"type":"text","value":"\nsnöhöns är ett palindrom!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Huvudprogrammet ska inte vara i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Summan av de positiva","tmcname":"osa04-15_positivas_summa"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"positiv_summa"}]},{"type":"text","value":" som tar emot en lista med heltal som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera summan av de positiva talen i listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" positiv_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"svar 9"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I dessa uppgifter kommer vi att använda listor som argument och returvärden. Det här såg vi på i den förra delen."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Jämna","tmcname":"osa04-16_jamna"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"jamna"}]},{"type":"text","value":" som får som argument en lista med heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en ny lista som innehåller de jämna talen som förekommer i den ursprungliga listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nny_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ursprunglig\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ursprunglig [1, 2, 3, 4, 5]\nny [2, 4]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Summalista","tmcname":"osa04-17_summalista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" som får två listor som argument. Båda listorna har samma antal element, som består av heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en ny lista vars element består av summorna av elementen i de urpsrungliga listorna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# [8, 10, 12]"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Unika","tmcname":"osa04-18_unika"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"unika"}]},{"type":"text","value":" som får som argument en lista med heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en lista som innehåller den ursprungliga listans siffror i storleksordning. Varje siffra ska förekomma bara en gång."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"unika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# [1, 2, 3]"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"hitta-det-basta-eller-samsta-vardet-i-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Hitta det bästa eller sämsta värdet i en lista"},{"type":"element","tagName":"a","properties":{"href":"#hitta-det-basta-eller-samsta-vardet-i-en-lista","ariaLabel":"hitta det basta eller samsta vardet i en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En vanlig programmeringsuppgift är att hitta det bästa eller sämsta värdet i en lista enligt något visst kriterium. En enkel lösning är att använda en hjälpvariabel för att komma ihåg vilket av elementen tills vidare är det mest \"optimala\". Det här mest \"optimala\" jämförs med varje element och när loopen är klar kommer hjälpvariabeln att innehålla det värdet man söker efter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett utkast som inte ännu fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bast "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" start "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det passliga startvärdet beror på situationen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" element bättre än bast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bast "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" element\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi vet nu det bästa värdet"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detaljerna kring den slutliga koden beror på typen av elementen i listan och kriteriet för väljandet av det bästa (eller sämsta) elementet. Ibland kan du behöva fler än en hjälpvariabel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss öva på den här metoden."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Längden av den längsta","tmcname":"osa04-18a_langsta_langden"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta_langden"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska returnera längden på den längsta strängen i listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sjuttionde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta_langden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"venla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eva\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"antonia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"julia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta_langden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"10\n7"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Listans kortaste","tmcname":"osa04-18b_listans_kortaste"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kortast"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska returnera listans kortaste sträng. Om det finns flera strängar med samma längd kan man returnera vilken som helst av dessa. Man kan anta at det inte finns tomma strängar (längd noll) i listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sjuttionde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"venla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eva\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"antonia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"julia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"andra\neva"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Listans längsta","tmcname":"osa04-19_listans_langsta"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska returnera en lista som innehåller den längsta strängen i listan. Om de finns flera strängar med samma längd skrivs de alla ut i listan, i den ordning som de förekommer i den ursprungliga listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sjuttionde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ['sjuttionde']"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"venla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eva\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"antonia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"julia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"saharah\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ['antonia', 'saharah']"}]}]}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad som menas med iteration
  • \n
  • vet du hur for-loopen i Python fungerar
  • \n
  • kan du använda dig av en for-loop för att gå igenom listor och strängar.
  • \n

Du kan använda en while-loop för att gå igenom elementen i en lista på samma sätt som vi använde oss av while-loopar för att gå igenom strängar. Det följande programmet skriver ut elementen i en lista på varsin rad:

lista = [3, 2, 4, 5, 2]\n\nplats = 0\nwhile plats < len(lista):\n    print(lista[plats])\n    plats += 1

3\n2\n4\n5\n2

Det här fungerar helt bra, men det är ett komplicerat sätt att gå igenom en lista eftersom att det krävs en hjälpvariabel, index, för att minnas vid vilket element man är vid. Till all lycka erbjuder Python ett enklare sätt att gå igenom listor, strängar och liknande strukturer.

for-loopen

När du vill gå igenom en färdig samling av element i Python, kan du använda dig av en for-loop. Loopen kan till exempel gå igenom alla element i en lista – från det första till det sista.

När man använder en while-loop, vet programmet inte på förhand hur många iterationer (\"varv\") loopen kommer att gå igenom. Loopen kommer att fortsätta tills villkoret inte längre är sant eller loopen avslutas på ett annat sätt. I en for-loop vet man antalet iterationer på förhand.

Idén är att for-loopen går igenom elementen i samlingen ett för ett och utför samma sak för varje element. Programmeraren behöver inte fundera på vilket element som behandlas och när det görs. Syntaxen för for-loopen är den följande:

for <variabel> in <samling>:\n    <block>

for-loopen tar ett element i samlingen, tilldelar det till en variabel, kör kodblocket och fortsätter till nästa element. När alla element har behandlats kommer programmet att fortsätta köras från och med kodraden som följer loopen.

\n \n \n \n \n \n \"Listan\n \n \n

Följande program skriver ut alla element i en lista med hjälp av en for-loop:

lista = [3, 2, 4, 5, 2]\n\nfor element in lista:\n    print(element)

3\n2\n4\n5\n2

Jämfört med exemplet i början av den här delen är strukturen mycket enklare att förstå. En for-loop gör det enkelt att gå igenom elementen i en samling från början till slut.

Samma princip gäller också för strängar:

namn = input(\"Ange ditt namn: \")\n\nfor tecken in namn:\n    print(tecken)

Ange ditt namn: Peter\nP\ne\nt\ne\nr

Skapa ett program som ber användaren ange en sträng. Programmet ska sedan skriva ut strängen så att dess tecken kommer under varandra.

Efter varje tecken skriver man också ut en asterisk på en ny rad.

Exempel:

Ange en sträng: Python\nP\n*\ny\n*\nt\n*\nh\n*\no\n*\nn\n*

Obs! I de här uppgifterna ska du inte placera kod i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Funktionen range

Man vet ofta hur många gånger man vill upprepa en viss kodsnutt. Det kan till exempel hända att du vill gå igenom siffrorna ett till hundra. Funktionen range kan användas i samband med en for-loop för att uppnå detta.

Det finns några olika sätt att använda range-funktionen. Det enklaste sättet är att använda ett argument, som då hänvisar till slutpunkten. Själva slutpunkten inkluderas inte, likt extrahering av delsträngar (slice). Det betyder alltså att anropet range(n) ger oss en loop som går igenom siffrorna 0 till n - 1:

for i in range(5):\n    print(i)

0\n1\n2\n3\n4

Med två argument kommer funktionen att returnera ett intervall mellan två siffror. Funktionen range(a, b) kommer att ge ett intervall som startar med a och slutar med b - 1.

for i in range(3, 7):\n    print(i)

3\n4\n5\n6

Till sist: vi kan ge ett tredje argument som specificerar avståndet mellan värdena. Anropet range(a, b, c) kommer att ge ett intervall som börjar med a och slutar vid b - 1, och ökar med c för varje steg.

for i in range(1, 9, 2):\n    print(i)

1\n3\n5\n7

Avståndet eller steget kan också vara negativt. Då kommer intervallet att vara ordnat i fallande ordning. Observera att de två första argumenten har bytt plats här:

for i in range(6, 2, -1):\n    print(i)

6\n5\n4\n3

Skapa ett program som ber användaren ge ett positivt heltal n. Programmet ska därefter skriva ut siffrorna i intervallet -n ... n, exklusive noll. Varje siffra skrivs ut på en skild rad.

Exempel:

Ange tal: 4\n-4\n-3\n-2\n-1\n1\n2\n3\n4

Obs! I de här uppgifterna ska du inte placera kod i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Från intervall till lista

Funktionen range returnerar ett range-objekt som på flera sätt fungerar som en lista, men i verkligheten inte är det. Om du försöker skriva ut värdet som funktionen returnerar kommer du bara att se en beskrivning av range-objektet:

siffror = range(2, 7)\nprint(siffror)

range(2, 7)

Funktionen list konverterar ett intervall till en lista. Listan kommer att innehålla de värden som finns i intervallet. I fortsättningskursen i Python som följer den här kursen kommer vi att gå djupare in på det här.

siffror = list(range(2, 7))\nprint(siffror)

[2, 3, 4, 5, 6]

En påminnelse om de automatiska testen

Tills nu har de övningar som krävt att du skriver en funktion haft färdiga mallar som sett ut på följande sätt:

# din lösning ska skrivas här\n\n# det lönar sig att testa på funktionen här, på följande sätt\nif __name__ == \"__main__\":\n    mening = \"jag gillar blåbärspaj så länge den inte innehåller ägg\"\n\n    print(ord_ett(mening))\n    print(ord_tva(mening))\n    print(sista_ordet(mening))

Från och med nu kommer det inte längre att finnas påminnelser om att använda if __name__ == \"__main__\" -blocket. De automatiska testen kommer fortsättningsvis ändå att kräva att de används, så du måste själv lägga till blocket i din kod när du testar dina funktioner inom programmets huvudfunktion.

Obs! Vissa övningar, som Palindrom i den här delen, förutsätter att du skriver kod som använder sig av den funktionen du gjort. Den här koden bör inte läggas i if __name__ == \"__main__\" -blocket. De automatiska testen kör ingen kod inom dessa block, så din lösning kommer inte att vara fullständig om du placerar dina funktionsanrop där.

Skapa funktionen lista_som_asterisker som får som argument en lista med heltal. Funktionen ska skriva ut rader med asterisker så att siffrorna i listan indikerar antalet asterisker på en rad.

T.ex. med anropet lista_som_asterisker([3, 7, 1, 1, 2]) ska resultatet vara:

\n***\n*******\n*\n*\n**\n

Skapa funktionen anagram som får två strängar som argument. Funktionen ska returnera True om strängarna är anagram – dvs. de bildas av exakt samma bokstäver.

Exempel:

print(anagram(\"stol\", \"lost\")) # True\nprint(anagram(\"burk\", \"bruk\")) # True\nprint(anagram(\"anagram\", \"magarna\")) # True\nprint(anagram(\"lykta\", \"lycka\")) # False\nprint(anagram(\"python\", \"java\")) # False

Tips: Funktionen sorted fungerar även för strängar.

Skapa funktionen palindrom som får en sträng som argument. Funktionen ska returnera True om strängen är ett palindrom – dvs. den är den samma oberoende om man börjar läsa från vänster eller höger.

Skapa ett huvudprogram som ber användaren ange ord tills ett palindrom ges:

Ange ett palindrom: python\ndet var inte ett palindrom\nAnge ett palindrom: java\ndet var inte ett palindrom\nAnge ett palindrom: snöhöna\ndet var inte ett palindrom\nAnge ett palindrom: snöhöns\nsnöhöns är ett palindrom!

Obs! Huvudprogrammet ska inte vara i if __name__ == \"__main__\" -blocket.

Skapa funktionen positiv_summa som tar emot en lista med heltal som argument.

Funktionen ska returnera summan av de positiva talen i listan.

lista = [1, -2, 3, -4, 5]\nsvar = positiv_summa(lista)\nprint(\"svar\", svar)

svar 9

I dessa uppgifter kommer vi att använda listor som argument och returvärden. Det här såg vi på i den förra delen.

Skapa funktionen jamna som får som argument en lista med heltal.

Funktionen ska returnera en ny lista som innehåller de jämna talen som förekommer i den ursprungliga listan.

lista = [1, 2, 3, 4, 5]\nny_lista = jamna(lista)\nprint(\"ursprunglig\", lista)\nprint(\"ny\", ny_lista)

ursprunglig [1, 2, 3, 4, 5]\nny [2, 4]

Skapa funktionen summa som får två listor som argument. Båda listorna har samma antal element, som består av heltal.

Funktionen ska returnera en ny lista vars element består av summorna av elementen i de urpsrungliga listorna.

Exempel:

a = [1, 2, 3]\nb = [7, 8, 9]\nprint(summa(a, b)) # [8, 10, 12]

Skapa funktionen unika som får som argument en lista med heltal.

Funktionen ska returnera en lista som innehåller den ursprungliga listans siffror i storleksordning. Varje siffra ska förekomma bara en gång.

lista = [3, 2, 2, 1, 3, 3, 1]\nprint(unika(lista)) # [1, 2, 3]

Hitta det bästa eller sämsta värdet i en lista

En vanlig programmeringsuppgift är att hitta det bästa eller sämsta värdet i en lista enligt något visst kriterium. En enkel lösning är att använda en hjälpvariabel för att komma ihåg vilket av elementen tills vidare är det mest \"optimala\". Det här mest \"optimala\" jämförs med varje element och när loopen är klar kommer hjälpvariabeln att innehålla det värdet man söker efter.

Här är ett utkast som inte ännu fungerar:

bast = start # det passliga startvärdet beror på situationen\nfor element in lista:\n    if element bättre än bast:\n        bast = element\n\n# vi vet nu det bästa värdet

Detaljerna kring den slutliga koden beror på typen av elementen i listan och kriteriet för väljandet av det bästa (eller sämsta) elementet. Ibland kan du behöva fler än en hjälpvariabel.

Låt oss öva på den här metoden.

Skapa funktionen langsta_langden som får som argument en lista med strängar. Funktionen ska returnera längden på den längsta strängen i listan.

lista = [\"första\", \"andra\", \"tredje\", \"sjuttionde\"]\n\nresultat = langsta_langden(lista)\nprint(resultat)
lista = [\"peter\", \"emilia\", \"venla\", \"eva\", \"antonia\", \"julia\"]\n\nresultat = langsta_langden(lista)\nprint(resultat)

10\n7

Skapa funktionen kortast som får som argument en lista med strängar. Funktionen ska returnera listans kortaste sträng. Om det finns flera strängar med samma längd kan man returnera vilken som helst av dessa. Man kan anta at det inte finns tomma strängar (längd noll) i listan.

lista = [\"första\", \"andra\", \"tredje\", \"sjuttionde\"]\n\nresultat = kortast(lista)\nprint(resultat)
lista = [\"peter\", \"emilia\", \"venla\", \"eva\", \"antonia\", \"julia\"]\n\nresultat = kortast(lista)\nprint(resultat)

andra\neva

Skapa funktionen langsta som får som argument en lista med strängar. Funktionen ska returnera en lista som innehåller den längsta strängen i listan. Om de finns flera strängar med samma längd skrivs de alla ut i listan, i den ordning som de förekommer i den ursprungliga listan.

lista = [\"första\", \"andra\", \"tredje\", \"sjuttionde\"]\n\nresultat = langsta(lista)\nprint(resultat) # ['sjuttionde']
lista = [\"peter\", \"emilia\", \"venla\", \"eva\", \"antonia\", \"julia\", \"saharah\"]\n\nresultat = langsta(lista)\nprint(resultat) # ['antonia', 'saharah']
","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/4-iteration.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad som menas med iteration"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen i Python fungerar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop för att gå igenom listor och strängar."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda en while-loop för att gå igenom elementen i en lista på samma sätt som vi använde oss av while-loopar för att gå igenom strängar. Det följande programmet skriver ut elementen i en lista på varsin rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nplats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" plats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"plats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n plats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2\n4\n5\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här fungerar helt bra, men det är ett komplicerat sätt att gå igenom en lista eftersom att det krävs en hjälpvariabel, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"index"}]},{"type":"text","value":", för att minnas vid vilket element man är vid. Till all lycka erbjuder Python ett enklare sätt att gå igenom listor, strängar och liknande strukturer."}]},{"type":"element","tagName":"h2","properties":{"id":"for-loopen","style":"position:relative;"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen"},{"type":"element","tagName":"a","properties":{"href":"#for-loopen","ariaLabel":"for loopen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du vill gå igenom en färdig samling av element i Python, kan du använda dig av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop. Loopen kan till exempel gå igenom alla element i en lista – från det första till det sista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man använder en while-loop, vet programmet inte på förhand hur många iterationer (\"varv\") loopen kommer att gå igenom. Loopen kommer att fortsätta tills villkoret inte längre är sant eller loopen avslutas på ett annat sätt. I en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop vet man antalet iterationer på förhand."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen går igenom elementen i samlingen ett för ett och utför samma sak för varje element. Programmeraren behöver inte fundera på vilket element som behandlas och när det görs. Syntaxen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen är den följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"variabel"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"samling"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"block"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loopen tar ett element i samlingen, tilldelar det till en variabel, kör kodblocket och fortsätter till nästa element. När alla element har behandlats kommer programmet att fortsätta köras från och med kodraden som följer loopen."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 327px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d403f61ea9acc12fa15e837c0051c2eb/00e65/4_3_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 214.3478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAArCAYAAAB4pah1AAAACXBIWXMAAAsSAAALEgHS3X78AAADQklEQVRIx62X2U4qQRCG5/V5DR8AE72DG2LEC4EARgVFQJRNATfcUevkq5PqtHN6GCCnk84Uvfxdy1/VTSSB9vPzo9+vry9ZLBa/xtJatAxwNpvJ7e3t/wOcTqdyc3PjxlYBjUJgtnE+n8vd3d1mgLaYL76jHR4eSq1Wc/701yQCMvn9/a09vnBnZ0fy+XzQglB3gH77+PiQ0WgknU5HTX54eFB5PB67iCc1sNTkVqsl7XZbN/X7fbm6upKzszMZDodycXEh3W5Xfdnr9fQwDnl8fHTf+/t7eX19/ashg9lsVnZ3d+X09FQmk4mC1Ot1aTabcnx8rPLLy4tGvVKp6IHQCQXoyOwBNEKjz89PpzIaMMki8y+aYoFxMtRY8/T0JBECvvIbEYXUuOL8/FxN8iPsdw6kDQYDxYkQnp+f3WJbgIzPmA9xNP5NBLT29vamgalWq/L+/i4hvqYC0qCGAWxvb0sul1OZMSO8f3AQ0HxIFPEbNCGSaEemIF9eXkqj0VDHp2pogBB4WSNQHLoyIJqxAerAR2R4hgzH0N4vFqmA8K9cLsve3p4rDPRisaiEPjo60uxYy2TMIkjWiTZaIVMb19IQQKLplzAjOo2swvwkQM0UHxBziCYbySBy++TkRGXWcSA5Hc8YO8wB+jxEQ/u9tbWlhcOqt5/zcR4SPA77pSHJT6Svr69VIyoNWiKjAZkT4iHBJJAET4uDmQHYsvIOjZJ4iBIENELA0aQclcXMwp+22QoGUWaT70ObQzFcpQUWzRiAg1bm+V0qlbQO2iG4xAcMRtk3iZw1QKgCB3GH0SYNUHnoqw0tll1EAC7joQO0QS4cTISL8U6xtXtjKWDogYTP0NTvjPnVPLEehk5Lu3tTNfTNJggQGDPxKR0WxM1d2WQcH7ouSa0QqVMB2UQkCRAUQaZkWcFN8qErDvEJNjMJqQuFgt4rjNEzmYwcHBy4yoMr6LiHLONdFNQQk9HQ3jF2n+BHv+LES1jwfQiYmeY38pgXhJ/boWfeP4BoxnWJ2VxKdEzEJL90Jb4PQxwzDYisPUVCpF75Sey/sc3MEPdSAeMacm1yp2ykod9sM++a/f19N7bR34q4C9b50wPgH3De+nsX6dJiAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d403f61ea9acc12fa15e837c0051c2eb/a0b58/4_3_1.webp 230w","/static/d403f61ea9acc12fa15e837c0051c2eb/e25e9/4_3_1.webp 327w"],"sizes":"(max-width: 327px) 100vw, 327px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d403f61ea9acc12fa15e837c0051c2eb/81c8e/4_3_1.png 230w","/static/d403f61ea9acc12fa15e837c0051c2eb/00e65/4_3_1.png 327w"],"sizes":"(max-width: 327px) 100vw, 327px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d403f61ea9acc12fa15e837c0051c2eb/00e65/4_3_1.png","alt":"Listan iterointi","title":"Listan iterointi","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program skriver ut alla element i en lista med hjälp av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2\n4\n5\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jämfört med exemplet i början av den här delen är strukturen mycket enklare att förstå. En "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop gör det enkelt att gå igenom elementen i en samling från början till slut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma princip gäller också för strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ditt namn: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" tecken "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tecken"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ditt namn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Peter"}]},{"type":"text","value":"\nP\ne\nt\ne\nr"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Utskrift med asterisker","tmcname":"osa04-11a_utskrift_asterisker"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange en sträng. Programmet ska sedan skriva ut strängen så att dess tecken kommer under varandra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter varje tecken skriver man också ut en asterisk på en ny rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange en sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python"}]},{"type":"text","value":"\nP\n*\ny\n*\nt\n*\nh\n*\no\n*\nn\n*"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I de här uppgifterna ska du inte placera kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"funktionen-range","style":"position:relative;"},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"a","properties":{"href":"#funktionen-range","ariaLabel":"funktionen range permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man vet ofta hur många gånger man vill upprepa en viss kodsnutt. Det kan till exempel hända att du vill gå igenom siffrorna ett till hundra. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" kan användas i samband med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":"-loop för att uppnå detta."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns några olika sätt att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-funktionen. Det enklaste sättet är att använda ett argument, som då hänvisar till slutpunkten. Själva slutpunkten inkluderas inte, likt extrahering av delsträngar (slice). Det betyder alltså att anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(n)"}]},{"type":"text","value":" ger oss en loop som går igenom siffrorna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n - 1"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med två argument kommer funktionen att returnera ett intervall mellan två siffror. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(a, b)"}]},{"type":"text","value":" kommer att ge ett intervall som startar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och slutar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b - 1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n4\n5\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till sist: vi kan ge ett tredje argument som specificerar avståndet mellan värdena. Anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(a, b, c)"}]},{"type":"text","value":" kommer att ge ett intervall som börjar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och slutar vid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b - 1"}]},{"type":"text","value":", och ökar med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":" för varje steg."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n3\n5\n7"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Avståndet eller steget kan också vara negativt. Då kommer intervallet att vara ordnat i fallande ordning. Observera att de två första argumenten har bytt plats här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"6\n5\n4\n3"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Från negativ till positiv","tmcname":"osa04-11b_negativ_till_positiv"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ge ett positivt heltal "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":". Programmet ska därefter skriva ut siffrorna i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-n ... n"}]},{"type":"text","value":", exklusive noll. Varje siffra skrivs ut på en skild rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n-4\n-3\n-2\n-1\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I de här uppgifterna ska du inte placera kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"fran-intervall-till-lista","style":"position:relative;"},"children":[{"type":"text","value":"Från intervall till lista"},{"type":"element","tagName":"a","properties":{"href":"#fran-intervall-till-lista","ariaLabel":"fran intervall till lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" returnerar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-objekt som på flera sätt fungerar som en lista, men i verkligheten inte är det. Om du försöker skriva ut värdet som funktionen returnerar kommer du bara att se en beskrivning av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-objektet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"range(2, 7)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" konverterar ett intervall till en lista. Listan kommer att innehålla de värden som finns i intervallet. I fortsättningskursen i Python som följer den här kursen kommer vi att gå djupare in på det här."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 3, 4, 5, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-paminnelse-om-de-automatiska-testen","style":"position:relative;"},"children":[{"type":"text","value":"En påminnelse om de automatiska testen"},{"type":"element","tagName":"a","properties":{"href":"#en-paminnelse-om-de-automatiska-testen","ariaLabel":"en paminnelse om de automatiska testen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tills nu har de övningar som krävt att du skriver en funktion haft färdiga mallar som sett ut på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# din lösning ska skrivas här"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det lönar sig att testa på funktionen här, på följande sätt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jag gillar blåbärspaj så länge den inte innehåller ägg\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_tva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Från och med nu kommer det inte längre att finnas påminnelser om att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket. De automatiska testen kommer fortsättningsvis ändå att kräva att de används, så du måste själv lägga till blocket i din kod när du testar dina funktioner inom programmets huvudfunktion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Vissa övningar, som Palindrom i den här delen, förutsätter att du skriver kod som använder sig av den funktionen du gjort. Den här koden bör inte läggas i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket. De automatiska testen kör ingen kod inom dessa block, så din lösning kommer inte att vara fullständig om du placerar dina funktionsanrop där."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Asterisker","tmcname":"osa04-12_asterisker"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_som_asterisker"}]},{"type":"text","value":" som får som argument en lista med heltal. Funktionen ska skriva ut rader med asterisker så att siffrorna i listan indikerar antalet asterisker på en rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"T.ex. med anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_som_asterisker([3, 7, 1, 1, 2])"}]},{"type":"text","value":" ska resultatet vara:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"***\n*******\n*\n*\n**\n"}]}]},{"type":"comment","value":" **Huomaa** että tällä hetkellä Windowsissa on ongelmia joidenkin tehtävien testien suorittamisessa. Jos törmäät seuraavaan virheilmoitukseen\n\n\"Listan\n\nvoit suorittaa testit lähettämällä ne palvelimelle valitsemalla testien suoritusnapin oikealla puolella olevasta symbolista avautuvasta TMC-valikosta _Submit solutions_.\n\nOngelman saa korjattua menemällä laajennuksen asennusvalikkoon ja muuttamalla \"TMC Data\" -kohdassa tehtävien sijainnin johonkin toiseen sijaintiin, jonka tiedostopolku on lyhempi, allaolevassa kuvassa nappi _change path_. Siirrossa saattaa kestää hetken, joten odotathan operaation päättymistä.\n\n\"Listan\n\nOngelmaan pyritään saamaan parempi ratkaisu lähipäivinä. "}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Anagram","tmcname":"osa04-13_anagram"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"anagram"}]},{"type":"text","value":" som får två strängar som argument. Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om strängarna är anagram – dvs. de bildas av exakt samma bokstäver."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"stol\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lost\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"burk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bruk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"anagram\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"magarna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lykta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lycka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"anagram"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" fungerar även för strängar."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Palindrom","tmcname":"osa04-14_palindrom"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"palindrom"}]},{"type":"text","value":" som får en sträng som argument. Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om strängen är ett palindrom – dvs. den är den samma oberoende om man börjar läsa från vänster eller höger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett huvudprogram som ber användaren ange ord tills ett palindrom ges:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ett palindrom: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\ndet var inte ett palindrom\nAnge ett palindrom: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"java"}]},{"type":"text","value":"\ndet var inte ett palindrom\nAnge ett palindrom: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"snöhöna"}]},{"type":"text","value":"\ndet var inte ett palindrom\nAnge ett palindrom: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"snöhöns"}]},{"type":"text","value":"\nsnöhöns är ett palindrom!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Huvudprogrammet ska inte vara i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Summan av de positiva","tmcname":"osa04-15_positivas_summa"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"positiv_summa"}]},{"type":"text","value":" som tar emot en lista med heltal som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera summan av de positiva talen i listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" positiv_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"svar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" svar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"svar 9"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I dessa uppgifter kommer vi att använda listor som argument och returvärden. Det här såg vi på i den förra delen."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Jämna","tmcname":"osa04-16_jamna"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"jamna"}]},{"type":"text","value":" som får som argument en lista med heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en ny lista som innehåller de jämna talen som förekommer i den ursprungliga listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nny_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" jamna"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ursprunglig\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ursprunglig [1, 2, 3, 4, 5]\nny [2, 4]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Summalista","tmcname":"osa04-17_summalista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" som får två listor som argument. Båda listorna har samma antal element, som består av heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en ny lista vars element består av summorna av elementen i de urpsrungliga listorna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# [8, 10, 12]"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Unika","tmcname":"osa04-18_unika"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"unika"}]},{"type":"text","value":" som får som argument en lista med heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en lista som innehåller den ursprungliga listans siffror i storleksordning. Varje siffra ska förekomma bara en gång."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"unika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# [1, 2, 3]"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"hitta-det-basta-eller-samsta-vardet-i-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Hitta det bästa eller sämsta värdet i en lista"},{"type":"element","tagName":"a","properties":{"href":"#hitta-det-basta-eller-samsta-vardet-i-en-lista","ariaLabel":"hitta det basta eller samsta vardet i en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En vanlig programmeringsuppgift är att hitta det bästa eller sämsta värdet i en lista enligt något visst kriterium. En enkel lösning är att använda en hjälpvariabel för att komma ihåg vilket av elementen tills vidare är det mest \"optimala\". Det här mest \"optimala\" jämförs med varje element och när loopen är klar kommer hjälpvariabeln att innehålla det värdet man söker efter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett utkast som inte ännu fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bast "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" start "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det passliga startvärdet beror på situationen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" element bättre än bast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bast "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" element\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi vet nu det bästa värdet"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detaljerna kring den slutliga koden beror på typen av elementen i listan och kriteriet för väljandet av det bästa (eller sämsta) elementet. Ibland kan du behöva fler än en hjälpvariabel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss öva på den här metoden."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Längden av den längsta","tmcname":"osa04-18a_langsta_langden"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta_langden"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska returnera längden på den längsta strängen i listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sjuttionde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta_langden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"venla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eva\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"antonia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"julia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta_langden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"10\n7"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Listans kortaste","tmcname":"osa04-18b_listans_kortaste"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kortast"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska returnera listans kortaste sträng. Om det finns flera strängar med samma längd kan man returnera vilken som helst av dessa. Man kan anta at det inte finns tomma strängar (längd noll) i listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sjuttionde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"venla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eva\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"antonia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"julia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"andra\neva"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Listans längsta","tmcname":"osa04-19_listans_langsta"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska returnera en lista som innehåller den längsta strängen i listan. Om de finns flera strängar med samma längd skrivs de alla ut i listan, i den ordning som de förekommer i den ursprungliga listan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sjuttionde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ['sjuttionde']"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"venla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eva\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"antonia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"julia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"saharah\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ['antonia', 'saharah']"}]}]}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad som menas med iteration
  • \n
  • vet du hur for-loopen i Python fungerar
  • \n
  • kan du använda dig av en for-loop för att gå igenom listor och strängar.
  • \n

Du kan använda en while-loop för att gå igenom elementen i en lista på samma sätt som vi använde oss av while-loopar för att gå igenom strängar. Det följande programmet skriver ut elementen i en lista på varsin rad:

lista = [3, 2, 4, 5, 2]\n\nplats = 0\nwhile plats < len(lista):\n    print(lista[plats])\n    plats += 1

3\n2\n4\n5\n2

Det här fungerar helt bra, men det är ett komplicerat sätt att gå igenom en lista eftersom att det krävs en hjälpvariabel, index, för att minnas vid vilket element man är vid. Till all lycka erbjuder Python ett enklare sätt att gå igenom listor, strängar och liknande strukturer.

for-loopen

När du vill gå igenom en färdig samling av element i Python, kan du använda dig av en for-loop. Loopen kan till exempel gå igenom alla element i en lista – från det första till det sista.

När man använder en while-loop, vet programmet inte på förhand hur många iterationer (\"varv\") loopen kommer att gå igenom. Loopen kommer att fortsätta tills villkoret inte längre är sant eller loopen avslutas på ett annat sätt. I en for-loop vet man antalet iterationer på förhand.

Idén är att for-loopen går igenom elementen i samlingen ett för ett och utför samma sak för varje element. Programmeraren behöver inte fundera på vilket element som behandlas och när det görs. Syntaxen för for-loopen är den följande:

for <variabel> in <samling>:\n    <block>

for-loopen tar ett element i samlingen, tilldelar det till en variabel, kör kodblocket och fortsätter till nästa element. När alla element har behandlats kommer programmet att fortsätta köras från och med kodraden som följer loopen.

\n \n \n \n \n \n \"Listan\n \n \n

Följande program skriver ut alla element i en lista med hjälp av en for-loop:

lista = [3, 2, 4, 5, 2]\n\nfor element in lista:\n    print(element)

3\n2\n4\n5\n2

Jämfört med exemplet i början av den här delen är strukturen mycket enklare att förstå. En for-loop gör det enkelt att gå igenom elementen i en samling från början till slut.

Samma princip gäller också för strängar:

namn = input(\"Ange ditt namn: \")\n\nfor tecken in namn:\n    print(tecken)

Ange ditt namn: Peter\nP\ne\nt\ne\nr

Skapa ett program som ber användaren ange en sträng. Programmet ska sedan skriva ut strängen så att dess tecken kommer under varandra.

Efter varje tecken skriver man också ut en asterisk på en ny rad.

Exempel:

Ange en sträng: Python\nP\n*\ny\n*\nt\n*\nh\n*\no\n*\nn\n*

Obs! I de här uppgifterna ska du inte placera kod i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Funktionen range

Man vet ofta hur många gånger man vill upprepa en viss kodsnutt. Det kan till exempel hända att du vill gå igenom siffrorna ett till hundra. Funktionen range kan användas i samband med en for-loop för att uppnå detta.

Det finns några olika sätt att använda range-funktionen. Det enklaste sättet är att använda ett argument, som då hänvisar till slutpunkten. Själva slutpunkten inkluderas inte, likt extrahering av delsträngar (slice). Det betyder alltså att anropet range(n) ger oss en loop som går igenom siffrorna 0 till n - 1:

for i in range(5):\n    print(i)

0\n1\n2\n3\n4

Med två argument kommer funktionen att returnera ett intervall mellan två siffror. Funktionen range(a, b) kommer att ge ett intervall som startar med a och slutar med b - 1.

for i in range(3, 7):\n    print(i)

3\n4\n5\n6

Till sist: vi kan ge ett tredje argument som specificerar avståndet mellan värdena. Anropet range(a, b, c) kommer att ge ett intervall som börjar med a och slutar vid b - 1, och ökar med c för varje steg.

for i in range(1, 9, 2):\n    print(i)

1\n3\n5\n7

Avståndet eller steget kan också vara negativt. Då kommer intervallet att vara ordnat i fallande ordning. Observera att de två första argumenten har bytt plats här:

for i in range(6, 2, -1):\n    print(i)

6\n5\n4\n3

Skapa ett program som ber användaren ge ett positivt heltal n. Programmet ska därefter skriva ut siffrorna i intervallet -n ... n, exklusive noll. Varje siffra skrivs ut på en skild rad.

Exempel:

Ange tal: 4\n-4\n-3\n-2\n-1\n1\n2\n3\n4

Obs! I de här uppgifterna ska du inte placera kod i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Från intervall till lista

Funktionen range returnerar ett range-objekt som på flera sätt fungerar som en lista, men i verkligheten inte är det. Om du försöker skriva ut värdet som funktionen returnerar kommer du bara att se en beskrivning av range-objektet:

siffror = range(2, 7)\nprint(siffror)

range(2, 7)

Funktionen list konverterar ett intervall till en lista. Listan kommer att innehålla de värden som finns i intervallet. I fortsättningskursen i Python som följer den här kursen kommer vi att gå djupare in på det här.

siffror = list(range(2, 7))\nprint(siffror)

[2, 3, 4, 5, 6]

En påminnelse om de automatiska testen

Tills nu har de övningar som krävt att du skriver en funktion haft färdiga mallar som sett ut på följande sätt:

# din lösning ska skrivas här\n\n# det lönar sig att testa på funktionen här, på följande sätt\nif __name__ == \"__main__\":\n    mening = \"jag gillar blåbärspaj så länge den inte innehåller ägg\"\n\n    print(ord_ett(mening))\n    print(ord_tva(mening))\n    print(sista_ordet(mening))

Från och med nu kommer det inte längre att finnas påminnelser om att använda if __name__ == \"__main__\" -blocket. De automatiska testen kommer fortsättningsvis ändå att kräva att de används, så du måste själv lägga till blocket i din kod när du testar dina funktioner inom programmets huvudfunktion.

Obs! Vissa övningar, som Palindrom i den här delen, förutsätter att du skriver kod som använder sig av den funktionen du gjort. Den här koden bör inte läggas i if __name__ == \"__main__\" -blocket. De automatiska testen kör ingen kod inom dessa block, så din lösning kommer inte att vara fullständig om du placerar dina funktionsanrop där.

Skapa funktionen lista_som_asterisker som får som argument en lista med heltal. Funktionen ska skriva ut rader med asterisker så att siffrorna i listan indikerar antalet asterisker på en rad.

T.ex. med anropet lista_som_asterisker([3, 7, 1, 1, 2]) ska resultatet vara:

\n***\n*******\n*\n*\n**\n

Skapa funktionen anagram som får två strängar som argument. Funktionen ska returnera True om strängarna är anagram – dvs. de bildas av exakt samma bokstäver.

Exempel:

print(anagram(\"stol\", \"lost\")) # True\nprint(anagram(\"burk\", \"bruk\")) # True\nprint(anagram(\"anagram\", \"magarna\")) # True\nprint(anagram(\"lykta\", \"lycka\")) # False\nprint(anagram(\"python\", \"java\")) # False

Tips: Funktionen sorted fungerar även för strängar.

Skapa funktionen palindrom som får en sträng som argument. Funktionen ska returnera True om strängen är ett palindrom – dvs. den är den samma oberoende om man börjar läsa från vänster eller höger.

Skapa ett huvudprogram som ber användaren ange ord tills ett palindrom ges:

Ange ett palindrom: python\ndet var inte ett palindrom\nAnge ett palindrom: java\ndet var inte ett palindrom\nAnge ett palindrom: snöhöna\ndet var inte ett palindrom\nAnge ett palindrom: snöhöns\nsnöhöns är ett palindrom!

Obs! Huvudprogrammet ska inte vara i if __name__ == \"__main__\" -blocket.

Skapa funktionen positiv_summa som tar emot en lista med heltal som argument.

Funktionen ska returnera summan av de positiva talen i listan.

lista = [1, -2, 3, -4, 5]\nsvar = positiv_summa(lista)\nprint(\"svar\", svar)

svar 9

I dessa uppgifter kommer vi att använda listor som argument och returvärden. Det här såg vi på i den förra delen.

Skapa funktionen jamna som får som argument en lista med heltal.

Funktionen ska returnera en ny lista som innehåller de jämna talen som förekommer i den ursprungliga listan.

lista = [1, 2, 3, 4, 5]\nny_lista = jamna(lista)\nprint(\"ursprunglig\", lista)\nprint(\"ny\", ny_lista)

ursprunglig [1, 2, 3, 4, 5]\nny [2, 4]

Skapa funktionen summa som får två listor som argument. Båda listorna har samma antal element, som består av heltal.

Funktionen ska returnera en ny lista vars element består av summorna av elementen i de urpsrungliga listorna.

Exempel:

a = [1, 2, 3]\nb = [7, 8, 9]\nprint(summa(a, b)) # [8, 10, 12]

Skapa funktionen unika som får som argument en lista med heltal.

Funktionen ska returnera en lista som innehåller den ursprungliga listans siffror i storleksordning. Varje siffra ska förekomma bara en gång.

lista = [3, 2, 2, 1, 3, 3, 1]\nprint(unika(lista)) # [1, 2, 3]

Hitta det bästa eller sämsta värdet i en lista

En vanlig programmeringsuppgift är att hitta det bästa eller sämsta värdet i en lista enligt något visst kriterium. En enkel lösning är att använda en hjälpvariabel för att komma ihåg vilket av elementen tills vidare är det mest \"optimala\". Det här mest \"optimala\" jämförs med varje element och när loopen är klar kommer hjälpvariabeln att innehålla det värdet man söker efter.

Här är ett utkast som inte ännu fungerar:

bast = start # det passliga startvärdet beror på situationen\nfor element in lista:\n    if element bättre än bast:\n        bast = element\n\n# vi vet nu det bästa värdet

Detaljerna kring den slutliga koden beror på typen av elementen i listan och kriteriet för väljandet av det bästa (eller sämsta) elementet. Ibland kan du behöva fler än en hjälpvariabel.

Låt oss öva på den här metoden.

Skapa funktionen langsta_langden som får som argument en lista med strängar. Funktionen ska returnera längden på den längsta strängen i listan.

lista = [\"första\", \"andra\", \"tredje\", \"sjuttionde\"]\n\nresultat = langsta_langden(lista)\nprint(resultat)
lista = [\"peter\", \"emilia\", \"venla\", \"eva\", \"antonia\", \"julia\"]\n\nresultat = langsta_langden(lista)\nprint(resultat)

10\n7

Skapa funktionen kortast som får som argument en lista med strängar. Funktionen ska returnera listans kortaste sträng. Om det finns flera strängar med samma längd kan man returnera vilken som helst av dessa. Man kan anta at det inte finns tomma strängar (längd noll) i listan.

lista = [\"första\", \"andra\", \"tredje\", \"sjuttionde\"]\n\nresultat = kortast(lista)\nprint(resultat)
lista = [\"peter\", \"emilia\", \"venla\", \"eva\", \"antonia\", \"julia\"]\n\nresultat = kortast(lista)\nprint(resultat)

andra\neva

Skapa funktionen langsta som får som argument en lista med strängar. Funktionen ska returnera en lista som innehåller den längsta strängen i listan. Om de finns flera strängar med samma längd skrivs de alla ut i listan, i den ordning som de förekommer i den ursprungliga listan.

lista = [\"första\", \"andra\", \"tredje\", \"sjuttionde\"]\n\nresultat = langsta(lista)\nprint(resultat) # ['sjuttionde']
lista = [\"peter\", \"emilia\", \"venla\", \"eva\", \"antonia\", \"julia\", \"saharah\"]\n\nresultat = langsta(lista)\nprint(resultat) # ['antonia', 'saharah']
","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/4-iteration.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-4/5-utskrift/page-data.json b/page-data/osa-4/5-utskrift/page-data.json index 8288b0006..8d85fce9d 100644 --- a/page-data/osa-4/5-utskrift/page-data.json +++ b/page-data/osa-4/5-utskrift/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-4/5-utskrift", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda argument för att påverka formatet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionens resultat"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av f-strängar för att formatera utskrift."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan lärt oss tre metoder för att ge argument till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det första är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-operatorn för strängar. Den tillåter enkel kombination av strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Elin\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" med din ålder på \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" år\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här metoden fungerar inte då något av segmenten inte är strängar. I exemplet ovan har variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alder"}]},{"type":"text","value":" konverterats till en sträng med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":", i och med att variabeln är ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den andra metoden är att ge alla delar av strängen som skilda argument, skiljandes dem med komma:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"med din ålder på\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här koden ger likadant resultat som den föregående versionen. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen lägger vanligtvis ett mellanslag mellan varje argument. Det positiva är att olika segment kan ha olika datatyper, så man behöver inte konvertera någonting till en sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill avlägsna mellanslagen som läggs till automatiskt, kan du ge ett argument med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sep"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"med din ålder på\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sep"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"HejElinmed din ålder på22år"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sep=\"\""}]},{"type":"text","value":" är ett namngivet argument. Det specificerar att alla argument ska avgränsas med en tom sträng. Du kan använda vilken som helst sträng som avgränsare. Till exempel om du vill ha varje argument på en skild rad kan du använda avgränsaren "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"text","value":" som är en radbrytning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"med din ålder på\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sep"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej\nElin\nmed din ålder på\n22\når"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vanligtvis avslutas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen med en radbrytning, men du kan också ändra på det här. Det namngivna argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end"}]},{"type":"text","value":" bestämmer vad som ska finnas i slutet av en rad. Om end är en tom sträng, kommer ingen radbrytning att skrivas ut i slutet av utskriften:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"allihopa!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej allihopa!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"f-strangar","style":"position:relative;"},"children":[{"type":"text","value":"F-strängar"},{"type":"element","tagName":"a","properties":{"href":"#f-strangar","ariaLabel":"f strangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det tredje sättet att förbereda strängar för utskrift är f-strängar. Det föregående exemplet med namnet och åldern skulle se så här ut med f-strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Elin\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Hej "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" med din ålder på "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi bara använt oss av enkla f-strängar, men de är väldigt flexibla när det kommer till formatering av innehållet i strängen. Ett vanligt användningsområde är att specificera antalet decimaler hos ett flyttal. Normalt är antalet ganska stort:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Siffran är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffran är 0.333333333333333"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan ändra på detta med hjälp av att inom klammerparenteser ge variabelnamnet och formateringsinstruktioner:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Siffran är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":".2f"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"Siffran är "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.33"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".2f"}]},{"type":"text","value":" berättar att vi vill visa två decimaler. Bokstaven f betyder att vi vill visa variabeln som ett flyttal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett annat exempel där vi ger ett visst mellanrum som är reserverat för en specifik variabel i utskriften. I båda fallen inkluderas variabeln namn i den resulterande strängen, med 15 tecken reserverat utrymme. Först är namnen vänsterjusterade, sedan högerjusterade:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antonia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Johan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" mittplats "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":">15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Antonia mittplats Antonia\nEmilia mittplats Emilia\nJohan mittplats Johan\nMaja mittplats Maja"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan också använda f-strängar utanför print-instruktioner. De kan tilldelas till variabler och kombineras med andra strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Leffe\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"59"}]},{"type":"text","value":"\nstad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Villmanstrand\""}]},{"type":"text","value":"\nhalsning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Hej "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", du är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år\""}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"halsning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\", du bor i "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"stad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Leffe, du är 59 år, du bor i Villmanstrand"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan tänka att f-strängen är en slags funktion som skapar en normal sträng baserat på \"argumenten\" mellan klammerparenteserna."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Från sifferlista till stränglista","tmcname":"osa04-20_sifferlista_till_stranglista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"formatera"}]},{"type":"text","value":" som får som argument en lista med flyttal. Funktionen ska returnera en ny lista som innehåller strängar med flyttalen avrundade till närmaste två decimaler. Ordningen av siffrorna ska vara den samma."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Använd f-strängar för att formatera flyttalen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.234"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.3333"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.11111"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.446"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" formatera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['1.23', '0.33', '0.11', '3.45']"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"c06c0359-d590-5b81-ba81-0faedf42b216"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du använda argument för att påverka formatet på print-instruktionens resultat
  • \n
  • kan du använda dig av f-strängar för att formatera utskrift.
  • \n

Vi har redan lärt oss tre metoder för att ge argument till print-instruktionen.

Det första är +-operatorn för strängar. Den tillåter enkel kombination av strängar:

namn = \"Elin\"\nalder = 22\nprint(\"Hej \" + namn + \" med din ålder på \" + str(alder) + \" år\" )

Den här metoden fungerar inte då något av segmenten inte är strängar. I exemplet ovan har variabeln alder konverterats till en sträng med funktionen str, i och med att variabeln är ett heltal.

Den andra metoden är att ge alla delar av strängen som skilda argument, skiljandes dem med komma:

print(\"Hej\", namn, \"med din ålder på\", alder, \"år\" )

Den här koden ger likadant resultat som den föregående versionen. print-instruktionen lägger vanligtvis ett mellanslag mellan varje argument. Det positiva är att olika segment kan ha olika datatyper, så man behöver inte konvertera någonting till en sträng.

Om du vill avlägsna mellanslagen som läggs till automatiskt, kan du ge ett argument med namnet sep:

print(\"Hej\", namn, \"med din ålder på\", alder, \"år\", sep=\"\")

Det här skriver ut:

HejElinmed din ålder på22år

Argumentet sep=\"\" är ett namngivet argument. Det specificerar att alla argument ska avgränsas med en tom sträng. Du kan använda vilken som helst sträng som avgränsare. Till exempel om du vill ha varje argument på en skild rad kan du använda avgränsaren \"\\n\" som är en radbrytning.

print(\"Hej\", namn, \"med din ålder på\", alder, \"år\", sep=\"\\n\")

Hej\nElin\nmed din ålder på\n22\når

Vanligtvis avslutas print-instruktionen med en radbrytning, men du kan också ändra på det här. Det namngivna argumentet end bestämmer vad som ska finnas i slutet av en rad. Om end är en tom sträng, kommer ingen radbrytning att skrivas ut i slutet av utskriften:

print(\"Hej \", end=\"\")\nprint(\"allihopa!\")

Hej allihopa!

F-strängar

Det tredje sättet att förbereda strängar för utskrift är f-strängar. Det föregående exemplet med namnet och åldern skulle se så här ut med f-strängar:

namn = \"Elin\"\nalder = 22\nprint(f\"Hej {namn} med din ålder på {alder} år\")

Hittills har vi bara använt oss av enkla f-strängar, men de är väldigt flexibla när det kommer till formatering av innehållet i strängen. Ett vanligt användningsområde är att specificera antalet decimaler hos ett flyttal. Normalt är antalet ganska stort:

siffra = 1/3\nprint(f\"Siffran är {siffra}\")

Siffran är 0.333333333333333

Vi kan ändra på detta med hjälp av att inom klammerparenteser ge variabelnamnet och formateringsinstruktioner:

siffra = 1/3\nprint(f\"Siffran är {siffra:.2f}\")
Siffran är 0.33

Instruktionen .2f berättar att vi vill visa två decimaler. Bokstaven f betyder att vi vill visa variabeln som ett flyttal.

Här är ett annat exempel där vi ger ett visst mellanrum som är reserverat för en specifik variabel i utskriften. I båda fallen inkluderas variabeln namn i den resulterande strängen, med 15 tecken reserverat utrymme. Först är namnen vänsterjusterade, sedan högerjusterade:

namn =  [\"Antonia\", \"Emilia\", \"Johan\", \"Maja\"]\nfor person in namn:\n  print(f\"{person:15} mittplats {person:>15}\")
Antonia         mittplats         Antonia\nEmilia          mittplats          Emilia\nJohan           mittplats           Johan\nMaja            mittplats            Maja

Man kan också använda f-strängar utanför print-instruktioner. De kan tilldelas till variabler och kombineras med andra strängar:

namn = \"Leffe\"\nalder = 59\nstad = \"Villmanstrand\"\nhalsning = f\"Hej {namn}, du är {alder} år\"\nprint(halsning + f\", du bor i {stad}\")

Hej Leffe, du är 59 år, du bor i Villmanstrand

Du kan tänka att f-strängen är en slags funktion som skapar en normal sträng baserat på \"argumenten\" mellan klammerparenteserna.

Skapa funktionen formatera som får som argument en lista med flyttal. Funktionen ska returnera en ny lista som innehåller strängar med flyttalen avrundade till närmaste två decimaler. Ordningen av siffrorna ska vara den samma.

Tips: Använd f-strängar för att formatera flyttalen.

Exempel:

lista = [1.234, 0.3333, 0.11111, 3.446]\nlista2 = formatera(lista)\nprint(lista2)

['1.23', '0.33', '0.11', '3.45']

","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/5-utskrift.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda argument för att påverka formatet på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionens resultat"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av f-strängar för att formatera utskrift."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan lärt oss tre metoder för att ge argument till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det första är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"-operatorn för strängar. Den tillåter enkel kombination av strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Elin\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" med din ålder på \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" år\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här metoden fungerar inte då något av segmenten inte är strängar. I exemplet ovan har variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alder"}]},{"type":"text","value":" konverterats till en sträng med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":", i och med att variabeln är ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den andra metoden är att ge alla delar av strängen som skilda argument, skiljandes dem med komma:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"med din ålder på\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här koden ger likadant resultat som den föregående versionen. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen lägger vanligtvis ett mellanslag mellan varje argument. Det positiva är att olika segment kan ha olika datatyper, så man behöver inte konvertera någonting till en sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill avlägsna mellanslagen som läggs till automatiskt, kan du ge ett argument med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sep"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"med din ålder på\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sep"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här skriver ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"HejElinmed din ålder på22år"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sep=\"\""}]},{"type":"text","value":" är ett namngivet argument. Det specificerar att alla argument ska avgränsas med en tom sträng. Du kan använda vilken som helst sträng som avgränsare. Till exempel om du vill ha varje argument på en skild rad kan du använda avgränsaren "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"text","value":" som är en radbrytning."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"med din ålder på\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sep"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej\nElin\nmed din ålder på\n22\når"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vanligtvis avslutas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen med en radbrytning, men du kan också ändra på det här. Det namngivna argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end"}]},{"type":"text","value":" bestämmer vad som ska finnas i slutet av en rad. Om end är en tom sträng, kommer ingen radbrytning att skrivas ut i slutet av utskriften:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"allihopa!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej allihopa!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"f-strangar","style":"position:relative;"},"children":[{"type":"text","value":"F-strängar"},{"type":"element","tagName":"a","properties":{"href":"#f-strangar","ariaLabel":"f strangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det tredje sättet att förbereda strängar för utskrift är f-strängar. Det föregående exemplet med namnet och åldern skulle se så här ut med f-strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Elin\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Hej "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" med din ålder på "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi bara använt oss av enkla f-strängar, men de är väldigt flexibla när det kommer till formatering av innehållet i strängen. Ett vanligt användningsområde är att specificera antalet decimaler hos ett flyttal. Normalt är antalet ganska stort:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Siffran är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffran är 0.333333333333333"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan ändra på detta med hjälp av att inom klammerparenteser ge variabelnamnet och formateringsinstruktioner:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Siffran är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":".2f"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"Siffran är "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.33"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".2f"}]},{"type":"text","value":" berättar att vi vill visa två decimaler. Bokstaven f betyder att vi vill visa variabeln som ett flyttal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett annat exempel där vi ger ett visst mellanrum som är reserverat för en specifik variabel i utskriften. I båda fallen inkluderas variabeln namn i den resulterande strängen, med 15 tecken reserverat utrymme. Först är namnen vänsterjusterade, sedan högerjusterade:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antonia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Johan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" mittplats "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":">15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Antonia mittplats Antonia\nEmilia mittplats Emilia\nJohan mittplats Johan\nMaja mittplats Maja"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan också använda f-strängar utanför print-instruktioner. De kan tilldelas till variabler och kombineras med andra strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Leffe\""}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"59"}]},{"type":"text","value":"\nstad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Villmanstrand\""}]},{"type":"text","value":"\nhalsning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Hej "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", du är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år\""}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"halsning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\", du bor i "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"stad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Leffe, du är 59 år, du bor i Villmanstrand"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan tänka att f-strängen är en slags funktion som skapar en normal sträng baserat på \"argumenten\" mellan klammerparenteserna."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Från sifferlista till stränglista","tmcname":"osa04-20_sifferlista_till_stranglista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"formatera"}]},{"type":"text","value":" som får som argument en lista med flyttal. Funktionen ska returnera en ny lista som innehåller strängar med flyttalen avrundade till närmaste två decimaler. Ordningen av siffrorna ska vara den samma."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Använd f-strängar för att formatera flyttalen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.234"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.3333"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.11111"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3.446"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" formatera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['1.23', '0.33', '0.11', '3.45']"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"c06c0359-d590-5b81-ba81-0faedf42b216"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du använda argument för att påverka formatet på print-instruktionens resultat
  • \n
  • kan du använda dig av f-strängar för att formatera utskrift.
  • \n

Vi har redan lärt oss tre metoder för att ge argument till print-instruktionen.

Det första är +-operatorn för strängar. Den tillåter enkel kombination av strängar:

namn = \"Elin\"\nalder = 22\nprint(\"Hej \" + namn + \" med din ålder på \" + str(alder) + \" år\" )

Den här metoden fungerar inte då något av segmenten inte är strängar. I exemplet ovan har variabeln alder konverterats till en sträng med funktionen str, i och med att variabeln är ett heltal.

Den andra metoden är att ge alla delar av strängen som skilda argument, skiljandes dem med komma:

print(\"Hej\", namn, \"med din ålder på\", alder, \"år\" )

Den här koden ger likadant resultat som den föregående versionen. print-instruktionen lägger vanligtvis ett mellanslag mellan varje argument. Det positiva är att olika segment kan ha olika datatyper, så man behöver inte konvertera någonting till en sträng.

Om du vill avlägsna mellanslagen som läggs till automatiskt, kan du ge ett argument med namnet sep:

print(\"Hej\", namn, \"med din ålder på\", alder, \"år\", sep=\"\")

Det här skriver ut:

HejElinmed din ålder på22år

Argumentet sep=\"\" är ett namngivet argument. Det specificerar att alla argument ska avgränsas med en tom sträng. Du kan använda vilken som helst sträng som avgränsare. Till exempel om du vill ha varje argument på en skild rad kan du använda avgränsaren \"\\n\" som är en radbrytning.

print(\"Hej\", namn, \"med din ålder på\", alder, \"år\", sep=\"\\n\")

Hej\nElin\nmed din ålder på\n22\når

Vanligtvis avslutas print-instruktionen med en radbrytning, men du kan också ändra på det här. Det namngivna argumentet end bestämmer vad som ska finnas i slutet av en rad. Om end är en tom sträng, kommer ingen radbrytning att skrivas ut i slutet av utskriften:

print(\"Hej \", end=\"\")\nprint(\"allihopa!\")

Hej allihopa!

F-strängar

Det tredje sättet att förbereda strängar för utskrift är f-strängar. Det föregående exemplet med namnet och åldern skulle se så här ut med f-strängar:

namn = \"Elin\"\nalder = 22\nprint(f\"Hej {namn} med din ålder på {alder} år\")

Hittills har vi bara använt oss av enkla f-strängar, men de är väldigt flexibla när det kommer till formatering av innehållet i strängen. Ett vanligt användningsområde är att specificera antalet decimaler hos ett flyttal. Normalt är antalet ganska stort:

siffra = 1/3\nprint(f\"Siffran är {siffra}\")

Siffran är 0.333333333333333

Vi kan ändra på detta med hjälp av att inom klammerparenteser ge variabelnamnet och formateringsinstruktioner:

siffra = 1/3\nprint(f\"Siffran är {siffra:.2f}\")
Siffran är 0.33

Instruktionen .2f berättar att vi vill visa två decimaler. Bokstaven f betyder att vi vill visa variabeln som ett flyttal.

Här är ett annat exempel där vi ger ett visst mellanrum som är reserverat för en specifik variabel i utskriften. I båda fallen inkluderas variabeln namn i den resulterande strängen, med 15 tecken reserverat utrymme. Först är namnen vänsterjusterade, sedan högerjusterade:

namn =  [\"Antonia\", \"Emilia\", \"Johan\", \"Maja\"]\nfor person in namn:\n  print(f\"{person:15} mittplats {person:>15}\")
Antonia         mittplats         Antonia\nEmilia          mittplats          Emilia\nJohan           mittplats           Johan\nMaja            mittplats            Maja

Man kan också använda f-strängar utanför print-instruktioner. De kan tilldelas till variabler och kombineras med andra strängar:

namn = \"Leffe\"\nalder = 59\nstad = \"Villmanstrand\"\nhalsning = f\"Hej {namn}, du är {alder} år\"\nprint(halsning + f\", du bor i {stad}\")

Hej Leffe, du är 59 år, du bor i Villmanstrand

Du kan tänka att f-strängen är en slags funktion som skapar en normal sträng baserat på \"argumenten\" mellan klammerparenteserna.

Skapa funktionen formatera som får som argument en lista med flyttal. Funktionen ska returnera en ny lista som innehåller strängar med flyttalen avrundade till närmaste två decimaler. Ordningen av siffrorna ska vara den samma.

Tips: Använd f-strängar för att formatera flyttalen.

Exempel:

lista = [1.234, 0.3333, 0.11111, 3.446]\nlista2 = formatera(lista)\nprint(lista2)

['1.23', '0.33', '0.11', '3.45']

","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/5-utskrift.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-4/6-strangar-listor/page-data.json b/page-data/osa-4/6-strangar-listor/page-data.json index 06e9a6a61..fb6ac2cd7 100644 --- a/page-data/osa-4/6-strangar-listor/page-data.json +++ b/page-data/osa-4/6-strangar-listor/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-4/6-strangar-listor", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kommer du att vara bekant med flera metoder för att extrahera delar av strängar och listor"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"förstår du vad oföränderlighet hos strängar innebär"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du är redan bekant med syntaxen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" för att ta fram en delsträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"mpel"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma syntax fungerar med listor. Dellistor kan extraheras på samma sätt som delsträngar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[4, 6, 1, 2]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"mera-extrahering","style":"position:relative;"},"children":[{"type":"text","value":"Mera extrahering"},{"type":"element","tagName":"a","properties":{"href":"#mera-extrahering","ariaLabel":"mera extrahering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Syntaxen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" fungerar faktiskt mycket lika som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-funktionen, vilket innebär att vi också kan ge den ett steg:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"eepl\n[7, 6, 5, 4]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi lämnar bort något av indexen kommer operatorn att inkludera alla element. Tack vare detta kan vi till exempel skriva ett mycket kort program som vänder om en sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange en sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange en sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exempel"}]},{"type":"text","value":"\nlepmexe"}]}]},{"type":"comment","value":"vastaava varoitusteksti löytyy osioista 3-4, 4-6 ja 5-1, tsekkaa kaikki jos muokkaat"},{"type":"element","tagName":"h2","properties":{"id":"varning-globala-variabler-inom-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Varning: globala variabler inom funktioner"},{"type":"element","tagName":"a","properties":{"href":"#varning-globala-variabler-inom-funktioner","ariaLabel":"varning globala variabler inom funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har observerat att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel på en funktion som använder en global variabel \"av misstag\":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"svangd_utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använder av misstag den globala variabeln namnlista"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\nnamnlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erkki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Margaret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsvangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louise\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ophelia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Margaret\nErkki\nEmilia\nAntti"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Margaret\nErkki\nEmilia\nAntti"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om funktionen anropas korrekt skrivs alltid namnen i den globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnlista"}]},{"type":"text","value":" ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All kod som testar funktioner ska skrivas inom ett separat block så att TMC-testen accepterar koden. Föregående exempel ska alltså skrivas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"svangd_utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använder av misstag den globala variabeln namnlista"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod som testar funktionen placeras här"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\n namnlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erkki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Margaret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n svangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n svangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louise\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ophelia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu definieras också den globala variabeln i if-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TMC-testen körs alltid så att koden inom dessa if-block inte körs. Därför fungerar funktionen inte ens i teorin eftersom variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnlista"}]},{"type":"text","value":" inte finns då testen körs."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Allt omvänt","tmcname":"osa04-21_allt_omvant"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"allt_omvant"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska skapa och returnera en ny lista där alla strängar i den ursprungliga listan är omvända. Elementen ska också komma i omvänd ordning i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"alla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ett till\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" allt_omvant"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['llit tte', 'lepmexe', 'alla', 'jeH']"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"strangar-ar-oforanderliga","style":"position:relative;"},"children":[{"type":"text","value":"Strängar är oföränderliga"},{"type":"element","tagName":"a","properties":{"href":"#strangar-ar-oforanderliga","ariaLabel":"strangar ar oforanderliga permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar och listor har en hel del likheter, framför allt då det kommer till hur de fungerar med olika operatorer. En nyckelskillnad är att strängar är oföränderliga. Det betyder att de inte kan ändras."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel\""}]},{"type":"text","value":"\nstrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar kan inte ändras på, så det här programmet kommer att ge ett felmeddelande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: 'str' object does not support item assignment"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett liknande fel uppstår om du försöker ordna en sträng med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"-metoden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar är oföränderliga, men variablerna som lagrar dem är inte det. En sträng kan ersättas med en annan sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De följande exemplen är alltså till sin grund olika:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 271px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8b07a257ace229cc26a5eedf03a60986/5fce1/4_4_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 70.43478260869564%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeUlEQVQ4y5WT66rCQAyE9337w8eT/hEfQVCs4K2iWO+1VevdyDewBxE91EDYbHZ3Opmkbj6fWxzH1uv1bDab2Xa7tTzPLUkSW61WtlgslJ9Op4bd73d7PB5f3RVFIQB8v98rebvd7HA42G63U471eDxaGXP2g202G1WwXq/l7zHn7nK5iAFMKQd2+PV61Up+MBjY6XRS6TBFCkCIAVkul6qIveNStVq1Wq2mi/1+37rdrpy42WxapVKRzoB7KXgMkM/RC5XsNYPpu+AYTHH24/FYmtIw3APRMD5C2c4/xD517fUMVoDA6puXbsor+L9dRsPRaGTD4VDUKQtt/LiwZ6TKzKDmkId+HNI0tSzLBISmOGDn87n0aP1UMqMDOOu32NFB3+FPxgTAnBVZqAaJkIL8ZDLR3g+4o+1hGFq9XtccMn+dTkdO3Gg0LAgCgdFFAACCAHJABnZIppL5MgckiCmNy74BXIYVZ61WS38Na7vd1keiKPrLs38CMo80d8OuAIYAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8b07a257ace229cc26a5eedf03a60986/a0b58/4_4_1.webp 230w","/static/8b07a257ace229cc26a5eedf03a60986/cadab/4_4_1.webp 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8b07a257ace229cc26a5eedf03a60986/81c8e/4_4_1.png 230w","/static/8b07a257ace229cc26a5eedf03a60986/5fce1/4_4_1.png 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8b07a257ace229cc26a5eedf03a60986/5fce1/4_4_1.png","alt":"4 4 1","title":"4 4 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"text","value":"\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!\""}]}]}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 281px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/4_4_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 85.65217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsSAAALEgHS3X78AAABq0lEQVQ4y42U6YrCUAyF+9jiExZEtPrDBRdQXHCt+27ky3DkjjMtBkKTu+Qm5ySNns+nPR4Pu9/v/sVH8S+Xi91uN7cR7eVpxMHpdGqtVsvq9brVajXbbDa22+0sSRKrVquWpql9K1HokMn5fP51gKwlPLRarWy9Xvsj+Njocrm0w+HwExBju916sOv16j56Op18vdls2n6/t/l87vtAwTpBuIMCDZV6QMqN49jK5bINh0Prdruu/X7fGo2GFYtFt3lEQqbcIZBksVhYBJDfCOfG47GXRzBKli0Y3hlmMSbWdQaMKVllo6HNfiTgw4uhfpKmFstkGaZ6vZ7jWKlUHEMIwGedthF+EDGbzZzdrMoilUHK4HI8Hn1D5RAEzcsqsw9DAv4THqCV1CqUz5p89jwg1OtgGCzEFpszfFHujEYjh0eEvUmZTCZWKpVcwYv2CCeGCwT4nCL6lrN/Sg5/CCgTwHQMBgMvSQK+4Mmco+BMhlrjm9vYjFq73fap6XQ6ThprPIiquZkQfDogt7EltFOhUPBgIUGUTBW5LIeBQhv8yETrEIOvX5twfgFptCTy4L9bkwAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e866c943663bc4d8360df84d7bc1c4de/a0b58/4_4_2.webp 230w","/static/e866c943663bc4d8360df84d7bc1c4de/f44a8/4_4_2.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e866c943663bc4d8360df84d7bc1c4de/81c8e/4_4_2.png 230w","/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/4_4_2.png 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/4_4_2.png","alt":"4 4 2","title":"4 4 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det första exemplet ändrar på innehållet i den lista som man hänvisar till. I det andra exemplet ersätts hänvisningen till den ursprungliga strängen med en hänvisning till en ny sträng. Den ursprungliga strängen finns fortfarande någonstans i datorns minne, men det saknas en referens till strängen så den kan inte längre användas i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi återkommer till det här ämnet senare. Då utforskar vi hänvisningar till listor närmare."}]},{"type":"element","tagName":"h2","properties":{"id":"mera-metoder-hos-listor-och-strangar","style":"position:relative;"},"children":[{"type":"text","value":"Mera metoder hos listor och strängar"},{"type":"element","tagName":"a","properties":{"href":"#mera-metoder-hos-listor-och-strangar","ariaLabel":"mera metoder hos listor och strangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" räknar antalet gånger ett element eller en delsträng finns i en lista eller sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hon håsa på hussatimmen så lärar'n sa sakta: städa upp mjölet på golvet så att klassen int' e som en savann\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden räknar inte överlappande förekomster. Till exempel i strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aaaa"}]},{"type":"text","value":" räknar metoden upp till två förekomster av delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aa"}]},{"type":"text","value":", även om det finns tre stycken om överlappande förekomster skulle tillåtas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":" skapar en ny sträng där en specifik delsträng har ersatts med en annan sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla\""}]},{"type":"text","value":"\nuusi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"God eftermiddag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"uusi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"God eftermiddag alla"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden påverkar alla delsträngar som hittas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lause "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"de åtta potatissemlorna förvandlades till en stor potatisplåt i ugnen – läraren, ja hon suckade i sitt hörn av klassen\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lause"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"en\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"EN\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"de åtta potatissemlorna förvandlades till EN stor potatisplåt i ugnEN – lärarEN, ja hon suckade i sitt hörn av klassEN"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":"-metoden används, är ett vanligt misstag att man glömmer att strängar är oföränderliga:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jag gillar Python\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi ersätter en delsträng, men resultatet lagras ingenstans"}]},{"type":"text","value":"\nstrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jag gillar Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om den gamla strängen inte längre behövs kan man tilldela den nya strängen till samma variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jag gillar Python\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi ersätter en delsträng och lagrar resultatet"}]},{"type":"text","value":"\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jag gillar Java"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vanligaste bokstaven","tmcname":"osa04-22_vanligaste_bokstaven"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vanligaste_bokstaven"}]},{"type":"text","value":" som får som argument en sträng. Funktionen ska returnera den bokstav som förekommer mest i strängen. Om det finns flera kandidater ska man returnera den bokstav som förekommer först."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abba\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vanligaste_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstrang2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jästsurdegmedsaltosocker\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vanligaste_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"a\ns"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Utan vokaler","tmcname":"osa04-23_utan_vokaler"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"utan_vokaler"}]},{"type":"text","value":" som får som argument en sträng. Funktionen ska returnera en ny sträng där vokalerna i den ursprungliga strängen fattas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att strängen består av gemener i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a-ö"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"det här är ett exempel\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ilman_vokaaleja"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"dt hr r tt xmpl"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Versaler bort","tmcname":"osa04-24_versaler_bort"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons strängmetod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isupper()"}]},{"type":"text","value":" returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om strängen består av enbart versaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"XYZ\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbara_stora "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Abc\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bara_stora"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"versaler_bort"}]},{"type":"text","value":" som tar emot som argument en lista med strängar. Funktionen ska returnera en ny lista med de strängar som inte består av enbart versaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"def\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"STOR\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ANNANSTOR\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"liten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"annan liten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Delvis stoR\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nfiltrerad_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" versaler_bort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filtrerad_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['def', 'liten', 'annan liten', 'Delvis stoR']"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Grannar i en lista","tmcname":"osa04-25_grannar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi definierar att två element i en lista är grannar då skillnaden mellan deras värden är ett. Dvs. t.ex. elementen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" samt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"56"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"55"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta_grannstrackan"}]},{"type":"text","value":" som letar efter den längsta dellistan bestående av bredvidliggande grannar. Listans längd ska returneras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"T.ex. i listan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1, 2, 5, 4, 3, 4]"}]},{"type":"text","value":" skulle dellistan vara "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[5, 4, 3, 4]"}]},{"type":"text","value":" och längden som returneras därmed "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langsta_grannstrackan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"skapa-ett-storre-programmeringsprojekt","style":"position:relative;"},"children":[{"type":"text","value":"Skapa ett större programmeringsprojekt"},{"type":"element","tagName":"a","properties":{"href":"#skapa-ett-storre-programmeringsprojekt","ariaLabel":"skapa ett storre programmeringsprojekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här fjärde modulen avslutas med ett lite större programmeringsprojekt där du får utnyttja det du lärt dig hittills."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den viktigaste regeln när man börjar med ett programmeringsprojekt är att man inte ska försöka lösa alla problem samtidigt. Programmet ska bestå av mindre delar, till exempel hjälpfunktioner. Du ska testa att varje del fungerar före du fortsätter framåt. Om du försöker göra för mycket samtidigt kommer du högst antagligen hamna i en situation som präglas av kaos och mera kaos."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kommer att behöva ett sätt att testa dina funktioner utanför huvudfunktionen. Du kan uppnå det här genom att definiera en skild huvudfunktion som du anropar utanför alla andra funktioner i programmet. Det är enkelt att tillfälligt kommentera bort ett funktionsanrop när man testar programmet. De första stegen i utförandet av programmeringsprojektet skulle kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# programmets kod hit"}]},{"type":"text","value":"\n\nmain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu kan hjälpfunktionerna köras utan att huvudfunktionen körs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpfunktion som räknar vitsord baserat på givet poängantal"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"vitsord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# koo"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# programmets kod hit"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kommenterar huvudprogrammet bort"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"#main()"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testar hjälpfunktionen"}]},{"type":"text","value":"\npoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"skicka-data-fran-en-funktion-till-en-annan","style":"position:relative;"},"children":[{"type":"text","value":"Skicka data från en funktion till en annan"},{"type":"element","tagName":"a","properties":{"href":"#skicka-data-fran-en-funktion-till-en-annan","ariaLabel":"skicka data fran en funktion till en annan permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När ett program innehåller flera funktioner uppstår en fråga: hur skickar jag data från en funktion till en annan?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel frågar man efter några heltal från användaren. Programmet skriver sedan ut dessa värden och utför en \"analys\" på dem. Programmet är uppdelat i tre skilda funktioner:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_fran_anvandare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Ange "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" tal:\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffror\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talen är: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"analysera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n medeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Siffror tillsammans "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", medeltal "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", minsta "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och största "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# \"huvudprogram\" som använder funktionerna"}]},{"type":"text","value":"\nindata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_fran_anvandare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nanalysens_resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analysera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analysens_resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, skulle det kunna se ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange 5 tal:\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-32"}]},{"type":"text","value":"\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"99"}]},{"type":"text","value":"\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-53"}]},{"type":"text","value":"\nTalen är:\n10\n34\n-32\n99\n-53\nSiffror tillsammans 5, medeltal 11.6, minsta -53 och största 99"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén är att huvudfunktionen \"lagrar\" all data som behandlas av programmet. I det här fallet är det enda som vi behöver de värden som användaren gett, i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det här behövs i en funktion ges det som ett argument. Det här sker med funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_resultat"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analysera"}]},{"type":"text","value":". Om funktionen resulterar i data som behövs på annat håll i programmet, returnerar funktionen det. Det här sparas i en variabel i huvudfunktionen. Det här sker med funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"indata_fran_anvandare"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analysera"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kunde använda den globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror"}]},{"type":"text","value":" från huvudfunktionen direkt i hjälpfunktionerna. Vi har redan gått igenom varför det är en "},{"type":"element","tagName":"a","properties":{"href":"https://softwareengineering.stackexchange.com/q/148108","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"dålig idé"}]},{"type":"text","value":", men här följer ännu en annan förklaring. Om funktionerna kan ändra på den globala variabeln kan oförutsedda saker börja hända i programmet, framför allt då antalet funktioner ökar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skicka data ut och in från funktioner gör man alltså helst med hjälp av argument och returvärden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kunde också göra huvudfunktionen till sin egen funktion. Då skulle variabeln siffor inte längre vara en global variabel, utan en lokal variabel i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktion som representerar huvudfunktionen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_fran_anvandare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n skriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n analysens_resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analysera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analysens_resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# start av programmet"}]},{"type":"text","value":"\nmain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vitsordsstatistik","tmcname":"osa04-26_vitsordsstatistik"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi ett program vars uppgift är att skriva ut vitsordsstatistik för en kurs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till programmet ger man rader med information om en studerandes provpoäng samt antalet gjorda uppgifter under kursen. Programmet skriver ut statistik på basis av dessa uppgifter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Provpoängen är heltal mellan noll och 20. Antalet gjorda uppgifter under kursen är ett heltal mellan noll och 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ber om indata från användaren, tills hon matar in en tom rad. Du kan anta att alla rader är korrekt inmatade – de innehåller alltså antingen två heltal eller är tomma."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här anger man provpoängen och antalet gjorda uppgifter under kursen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Provpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15 87"}]},{"type":"text","value":"\nProvpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10 55"}]},{"type":"text","value":"\nProvpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11 40"}]},{"type":"text","value":"\nProvpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4 17"}]},{"type":"text","value":"\nProvpoäng samt antalet gjorda uppgifter under kursen:\nStatistik:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När användaren matat in en tom rad ska programmet skriva ut statistik."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Statistiken består av de följande:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftspoäng: Ett poäng för 10 % gjorda uppgifter. Detta går ända upp till 100 % (100 uppgifter) —> 10 poäng. Uppgiftspoängen är ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kursvitsord: Räknas på basis av prov- och uppgiftspoängens summa, se följande tabell."}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Provpoäng + uppgiftspoäng"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Vitsord"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0–14"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0 (underkänt)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"15–17"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"18–20"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"21–23"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"24–27"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"28–30"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om provpoängen är under 10, kommer vitsordet dock alltid att vara 0, underkänt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande statistik skrivs ut med indatan från exemplet ovan:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Statistik:\nPoängmedeltal: 14.5\nGodkända (%): 75.0\nVitsordsfördelning:\n 5:\n 4:\n 3: *\n 2:\n 1: **\n 0: *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Decimaltal ska skrivas ut med en decimal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska du inte placera kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det. Om programmets funktionalitet t.ex. finns i funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":", ska funktionen anropas utanför det nämnda if-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Indatan ges som rader som består av två siffror i följande format."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Provpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15 87"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Raden som matas in ska först delas i två, varefter delarna ska konverteras till heltal med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":"-funktionen. Du kan dela en rad på samma sätt som i den här uppgiften. Du kan också använda strängmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":". Du kan söka efter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python string split"}]},{"type":"text","value":" på nätet för mera information."}]},{"type":"comment","value":" **Huomaa** että tällä hetkellä Windowsissa on ongelmia joidenkin tehtävien testien suorittamisessa. Jos törmäät seuraavaan virheilmoitukseen\n\n\"Listan\n\nvoit suorittaa testit lähettämällä ne palvelimelle valitsemalla testien suoritusnapin oikealla puolella olevasta symbolista avautuvasta TMC-valikosta _Submit solutions_.\n\nOngelman saa korjattua menemällä laajennuksen asennusvalikkoon ja muuttamalla \"TMC Data\" -kohdassa tehtävien sijainnin johonkin toiseen sijaintiin, jonka tiedostopolku on lyhempi, allaolevassa kuvassa nappi _change path_. Siirrossa saattaa kestää hetken, joten odotathan operaation päättymistä.\n\n\"Listan\n\nOngelmaan pyritään saamaan parempi ratkaisu lähipäivinä. "}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät om den här veckans material."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kommer du att vara bekant med flera metoder för att extrahera delar av strängar och listor
  • \n
  • förstår du vad oföränderlighet hos strängar innebär
  • \n
  • kan du använda dig av metoderna count och replace.
  • \n

Du är redan bekant med syntaxen [] för att ta fram en delsträng:

strang = \"exempel\"\nprint(strang[3:7])

mpel

Samma syntax fungerar med listor. Dellistor kan extraheras på samma sätt som delsträngar:

lista = [3,4,2,4,6,1,2,4,2]\nprint(lista[3:7])

[4, 6, 1, 2]

Mera extrahering

Syntaxen [] fungerar faktiskt mycket lika som range-funktionen, vilket innebär att vi också kan ge den ett steg:

strang = \"exempel\"\nprint(strang[0:7:2])\nlista = [1,2,3,4,5,6,7,8]\nprint(lista[6:2:-1])

eepl\n[7, 6, 5, 4]

Om vi lämnar bort något av indexen kommer operatorn att inkludera alla element. Tack vare detta kan vi till exempel skriva ett mycket kort program som vänder om en sträng:

strang = input(\"Ange en sträng: \")\nprint(strang[::-1])

Ange en sträng: exempel\nlepmexe

Varning: globala variabler inom funktioner

Vi har observerat att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler.

Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra.

Här är ett exempel på en funktion som använder en global variabel \"av misstag\":

def svangd_utskrift(namn: list):\n    # använder av misstag den globala variabeln namnlista\n    i = len(namnlista) - 1\n    while i >= 0:\n        print(namnlista[i])\n        i -= 1\n\n# global variabel\nnamnlista = [\"Antti\", \"Emilia\", \"Erkki\", \"Margaret\"]\nsvangd_utskrift(namnlista)\nprint()\nsvangd_utskrift([\"Louise\", \"Ophelia\", \"Lotta\"])

Margaret\nErkki\nEmilia\nAntti

Margaret\nErkki\nEmilia\nAntti

Även om funktionen anropas korrekt skrivs alltid namnen i den globala variabeln namnlista ut.

All kod som testar funktioner ska skrivas inom ett separat block så att TMC-testen accepterar koden. Föregående exempel ska alltså skrivas så här:

def svangd_utskrift(namn: list):\n    # använder av misstag den globala variabeln namnlista\n    i = len(namnlista) - 1\n    while i >= 0:\n        print(namnlista[i])\n        i -= 1\n\n# kod som testar funktionen placeras här\nif __name__ == \"__main__\":\n    # global variabel\n    namnlista = [\"Antti\", \"Emilia\", \"Erkki\", \"Margaret\"]\n    svangd_utskrift(namnlista)\n    print()\n    svangd_utskrift([\"Louise\", \"Ophelia\", \"Lotta\"])

Nu definieras också den globala variabeln i if-blocket.

TMC-testen körs alltid så att koden inom dessa if-block inte körs. Därför fungerar funktionen inte ens i teorin eftersom variabeln namnlista inte finns då testen körs.

Skapa funktionen allt_omvant som får som argument en lista med strängar. Funktionen ska skapa och returnera en ny lista där alla strängar i den ursprungliga listan är omvända. Elementen ska också komma i omvänd ordning i listan.

Exempel:

lista = [\"Hej\", \"alla\", \"exempel\", \"ett till\"]\nlista2 = allt_omvant(lista)\nprint(lista2)

['llit tte', 'lepmexe', 'alla', 'jeH']

Strängar är oföränderliga

Strängar och listor har en hel del likheter, framför allt då det kommer till hur de fungerar med olika operatorer. En nyckelskillnad är att strängar är oföränderliga. Det betyder att de inte kan ändras.

strang = \"exempel\"\nstrang[0] = \"a\"

Strängar kan inte ändras på, så det här programmet kommer att ge ett felmeddelande:

TypeError: 'str' object does not support item assignment

Ett liknande fel uppstår om du försöker ordna en sträng med sort-metoden.

Strängar är oföränderliga, men variablerna som lagrar dem är inte det. En sträng kan ersättas med en annan sträng.

De följande exemplen är alltså till sin grund olika:

lista = [1,2,3]\nlista[0] = 10
\n \n \n \n \n \n \"4\n \n \n

Det första exemplet ändrar på innehållet i den lista som man hänvisar till. I det andra exemplet ersätts hänvisningen till den ursprungliga strängen med en hänvisning till en ny sträng. Den ursprungliga strängen finns fortfarande någonstans i datorns minne, men det saknas en referens till strängen så den kan inte längre användas i programmet.

Vi återkommer till det här ämnet senare. Då utforskar vi hänvisningar till listor närmare.

Mera metoder hos listor och strängar

Metoden count räknar antalet gånger ett element eller en delsträng finns i en lista eller sträng:

strang = \"Hon håsa på hussatimmen så lärar'n sa sakta: städa upp mjölet på golvet så att klassen int' e som en savann\"\nprint(strang.count(\"sa\"))\n\nlista = [1,2,3,1,4,5,1,6]\nprint(lista.count(1))

5\n3

Metoden räknar inte överlappande förekomster. Till exempel i strängen aaaa räknar metoden upp till två förekomster av delsträngen aa, även om det finns tre stycken om överlappande förekomster skulle tillåtas.

Metoden replace skapar en ny sträng där en specifik delsträng har ersatts med en annan sträng:

strang = \"Hej alla\"\nuusi = strang.replace(\"Hej\", \"God eftermiddag\")\nprint(uusi)

God eftermiddag alla

Metoden påverkar alla delsträngar som hittas:

lause = \"de åtta potatissemlorna förvandlades till en stor potatisplåt i ugnen – läraren, ja hon suckade i sitt hörn av klassen\"\nprint(lause.replace(\"en\", \"EN\"))

de åtta potatissemlorna förvandlades till EN stor potatisplåt i ugnEN – lärarEN, ja hon suckade i sitt hörn av klassEN

När replace-metoden används, är ett vanligt misstag att man glömmer att strängar är oföränderliga:

strang = \"Jag gillar Python\"\n\n# vi ersätter en delsträng, men resultatet lagras ingenstans\nstrang.replace(\"Python\", \"Java\")\nprint(strang)

Jag gillar Python

Om den gamla strängen inte längre behövs kan man tilldela den nya strängen till samma variabel:

strang = \"Jag gillar Python\"\n\n# vi ersätter en delsträng och lagrar resultatet\nstrang = strang.replace(\"Python\", \"Java\")\nprint(strang)

Jag gillar Java

Skapa funktionen vanligaste_bokstaven som får som argument en sträng. Funktionen ska returnera den bokstav som förekommer mest i strängen. Om det finns flera kandidater ska man returnera den bokstav som förekommer först.

Exempel:

strang = \"abba\"\nprint(vanligaste_bokstaven(strang))\n\nstrang2 = \"jästsurdegmedsaltosocker\"\nprint(vanligaste_bokstaven(strang2))

a\ns

Skapa funktionen utan_vokaler som får som argument en sträng. Funktionen ska returnera en ny sträng där vokalerna i den ursprungliga strängen fattas.

Du kan anta att strängen består av gemener i intervallet a-ö.

Exempel:

strang = \"det här är ett exempel\"\nprint(ilman_vokaaleja(strang))

dt hr r tt xmpl

Pythons strängmetod isupper() returnerar True om strängen består av enbart versaler.

Exempel:

print(\"XYZ\".isupper())\n\nbara_stora = \"Abc\".isupper()\nprint(bara_stora)

True\nFalse

Skapa funktionen versaler_bort som tar emot som argument en lista med strängar. Funktionen ska returnera en ny lista med de strängar som inte består av enbart versaler.

Exempel:

lista = [\"ABC\", \"def\", \"STOR\", \"ANNANSTOR\", \"liten\", \"annan liten\", \"Delvis stoR\"]\nfiltrerad_lista = versaler_bort(lista)\nprint(filtrerad_lista)

['def', 'liten', 'annan liten', 'Delvis stoR']

Vi definierar att två element i en lista är grannar då skillnaden mellan deras värden är ett. Dvs. t.ex. elementen 1 och 2 samt 56 och 55.

Skapa funktionen langsta_grannstrackan som letar efter den längsta dellistan bestående av bredvidliggande grannar. Listans längd ska returneras.

T.ex. i listan [1, 2, 5, 4, 3, 4] skulle dellistan vara [5, 4, 3, 4] och längden som returneras därmed 4.

Exempel:

lista = [1, 2, 5, 7, 6, 5, 6, 3, 4, 1, 0]\nprint(langsta_grannstrackan(lista))

4

Skapa ett större programmeringsprojekt

Den här fjärde modulen avslutas med ett lite större programmeringsprojekt där du får utnyttja det du lärt dig hittills.

Den viktigaste regeln när man börjar med ett programmeringsprojekt är att man inte ska försöka lösa alla problem samtidigt. Programmet ska bestå av mindre delar, till exempel hjälpfunktioner. Du ska testa att varje del fungerar före du fortsätter framåt. Om du försöker göra för mycket samtidigt kommer du högst antagligen hamna i en situation som präglas av kaos och mera kaos.

Du kommer att behöva ett sätt att testa dina funktioner utanför huvudfunktionen. Du kan uppnå det här genom att definiera en skild huvudfunktion som du anropar utanför alla andra funktioner i programmet. Det är enkelt att tillfälligt kommentera bort ett funktionsanrop när man testar programmet. De första stegen i utförandet av programmeringsprojektet skulle kunna se ut så här:

def main():\n    poang = []\n    # programmets kod hit\n\nmain()

Nu kan hjälpfunktionerna köras utan att huvudfunktionen körs:

# hjälpfunktion som räknar vitsord baserat på givet poängantal\ndef vitsord(poang):\n    # koo\n\ndef main():\n    poang = []\n    # programmets kod hit\n\n# kommenterar huvudprogrammet bort\n#main()\n\n# testar hjälpfunktionen\npoang = 35\nresultat = vitsord(poang)\nprint(resultat)

Skicka data från en funktion till en annan

När ett program innehåller flera funktioner uppstår en fråga: hur skickar jag data från en funktion till en annan?

I följande exempel frågar man efter några heltal från användaren. Programmet skriver sedan ut dessa värden och utför en \"analys\" på dem. Programmet är uppdelat i tre skilda funktioner:

def las_fran_anvandare(antal: int):\n    print(f\"Ange {antal} tal:\")\n    siffror = []\n\n    for i in range(antal):\n        siffra = int(input(\"Ange tal: \"))\n        siffror.append(siffra)\n\n    return siffror\n\ndef skriv_ut(siffror: list):\n    print(\"Talen är: \")\n    for siffra in siffror:\n        print(siffra)\n\ndef analysera(siffror: list):\n    medeltal = sum(siffror) / len(siffror)\n    return f\"Siffror tillsammans {len(siffror)}, medeltal {medeltal}, minsta {min(siffror)} och största {max(siffror)}\"\n\n# \"huvudprogram\" som använder funktionerna\nindata = las_fran_anvandare(5)\nskriv_ut(indata)\nanalysens_resultat = analysera(indata)\nprint(analysens_resultat)

När programmet körs, skulle det kunna se ut så här:

Ange 5 tal:\nAnge tal: 10\nAnge tal: 34\nAnge tal: -32\nAnge tal: 99\nAnge tal: -53\nTalen är:\n10\n34\n-32\n99\n-53\nSiffror tillsammans 5, medeltal 11.6, minsta -53 och största 99

Idén är att huvudfunktionen \"lagrar\" all data som behandlas av programmet. I det här fallet är det enda som vi behöver de värden som användaren gett, i variabeln siffror.

Om det här behövs i en funktion ges det som ett argument. Det här sker med funktionerna skriv_ut_resultat och analysera. Om funktionen resulterar i data som behövs på annat håll i programmet, returnerar funktionen det. Det här sparas i en variabel i huvudfunktionen. Det här sker med funktionerna indata_fran_anvandare och analysera.

Du kunde använda den globala variabeln siffror från huvudfunktionen direkt i hjälpfunktionerna. Vi har redan gått igenom varför det är en dålig idé, men här följer ännu en annan förklaring. Om funktionerna kan ändra på den globala variabeln kan oförutsedda saker börja hända i programmet, framför allt då antalet funktioner ökar.

Att skicka data ut och in från funktioner gör man alltså helst med hjälp av argument och returvärden.

Du kunde också göra huvudfunktionen till sin egen funktion. Då skulle variabeln siffor inte längre vara en global variabel, utan en lokal variabel i main-funktionen:

# funktion som representerar huvudfunktionen\ndef main():\n    indata = las_fran_anvandare(5)\n    skriv_ut(indata)\n    analysens_resultat = analysera(indata)\n\n    print(analysens_resultat)\n\n# start av programmet\nmain()

I den här uppgiften skapar vi ett program vars uppgift är att skriva ut vitsordsstatistik för en kurs.

Till programmet ger man rader med information om en studerandes provpoäng samt antalet gjorda uppgifter under kursen. Programmet skriver ut statistik på basis av dessa uppgifter.

Provpoängen är heltal mellan noll och 20. Antalet gjorda uppgifter under kursen är ett heltal mellan noll och 100.

Programmet ber om indata från användaren, tills hon matar in en tom rad. Du kan anta att alla rader är korrekt inmatade – de innehåller alltså antingen två heltal eller är tomma.

Så här anger man provpoängen och antalet gjorda uppgifter under kursen:

Provpoäng samt antalet gjorda uppgifter under kursen: 15 87\nProvpoäng samt antalet gjorda uppgifter under kursen: 10 55\nProvpoäng samt antalet gjorda uppgifter under kursen: 11 40\nProvpoäng samt antalet gjorda uppgifter under kursen: 4 17\nProvpoäng samt antalet gjorda uppgifter under kursen:\nStatistik:

När användaren matat in en tom rad ska programmet skriva ut statistik.

Statistiken består av de följande:

Uppgiftspoäng: Ett poäng för 10 % gjorda uppgifter. Detta går ända upp till 100 % (100 uppgifter) —> 10 poäng. Uppgiftspoängen är ett heltal.

Kursvitsord: Räknas på basis av prov- och uppgiftspoängens summa, se följande tabell.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Provpoäng + uppgiftspoängVitsord
0–140 (underkänt)
15–171
18–202
21–233
24–274
28–305

Om provpoängen är under 10, kommer vitsordet dock alltid att vara 0, underkänt.

Följande statistik skrivs ut med indatan från exemplet ovan:

\nStatistik:\nPoängmedeltal: 14.5\nGodkända (%): 75.0\nVitsordsfördelning:\n  5:\n  4:\n  3: *\n  2:\n  1: **\n  0: *\n

Decimaltal ska skrivas ut med en decimal.

Obs! I dessa uppgifter ska du inte placera kod i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det. Om programmets funktionalitet t.ex. finns i funktionen main, ska funktionen anropas utanför det nämnda if-blocket.

Tips: Indatan ges som rader som består av två siffror i följande format.

Provpoäng samt antalet gjorda uppgifter under kursen: 15 87

Raden som matas in ska först delas i två, varefter delarna ska konverteras till heltal med int-funktionen. Du kan dela en rad på samma sätt som i den här uppgiften. Du kan också använda strängmetoden split. Du kan söka efter python string split på nätet för mera information.

Vänligen svara på en kort enkät om den här veckans material.

","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/6-strangar-listor.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kommer du att vara bekant med flera metoder för att extrahera delar av strängar och listor"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"förstår du vad oföränderlighet hos strängar innebär"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du är redan bekant med syntaxen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" för att ta fram en delsträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"mpel"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma syntax fungerar med listor. Dellistor kan extraheras på samma sätt som delsträngar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[4, 6, 1, 2]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"mera-extrahering","style":"position:relative;"},"children":[{"type":"text","value":"Mera extrahering"},{"type":"element","tagName":"a","properties":{"href":"#mera-extrahering","ariaLabel":"mera extrahering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Syntaxen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" fungerar faktiskt mycket lika som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-funktionen, vilket innebär att vi också kan ge den ett steg:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"eepl\n[7, 6, 5, 4]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi lämnar bort något av indexen kommer operatorn att inkludera alla element. Tack vare detta kan vi till exempel skriva ett mycket kort program som vänder om en sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange en sträng: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange en sträng: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exempel"}]},{"type":"text","value":"\nlepmexe"}]}]},{"type":"comment","value":"vastaava varoitusteksti löytyy osioista 3-4, 4-6 ja 5-1, tsekkaa kaikki jos muokkaat"},{"type":"element","tagName":"h2","properties":{"id":"varning-globala-variabler-inom-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Varning: globala variabler inom funktioner"},{"type":"element","tagName":"a","properties":{"href":"#varning-globala-variabler-inom-funktioner","ariaLabel":"varning globala variabler inom funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har observerat att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel på en funktion som använder en global variabel \"av misstag\":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"svangd_utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använder av misstag den globala variabeln namnlista"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\nnamnlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erkki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Margaret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsvangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louise\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ophelia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Margaret\nErkki\nEmilia\nAntti"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Margaret\nErkki\nEmilia\nAntti"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om funktionen anropas korrekt skrivs alltid namnen i den globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnlista"}]},{"type":"text","value":" ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All kod som testar funktioner ska skrivas inom ett separat block så att TMC-testen accepterar koden. Föregående exempel ska alltså skrivas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"svangd_utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använder av misstag den globala variabeln namnlista"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod som testar funktionen placeras här"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\n namnlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erkki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Margaret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n svangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n svangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louise\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ophelia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu definieras också den globala variabeln i if-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TMC-testen körs alltid så att koden inom dessa if-block inte körs. Därför fungerar funktionen inte ens i teorin eftersom variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnlista"}]},{"type":"text","value":" inte finns då testen körs."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Allt omvänt","tmcname":"osa04-21_allt_omvant"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"allt_omvant"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska skapa och returnera en ny lista där alla strängar i den ursprungliga listan är omvända. Elementen ska också komma i omvänd ordning i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"alla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ett till\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" allt_omvant"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['llit tte', 'lepmexe', 'alla', 'jeH']"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"strangar-ar-oforanderliga","style":"position:relative;"},"children":[{"type":"text","value":"Strängar är oföränderliga"},{"type":"element","tagName":"a","properties":{"href":"#strangar-ar-oforanderliga","ariaLabel":"strangar ar oforanderliga permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar och listor har en hel del likheter, framför allt då det kommer till hur de fungerar med olika operatorer. En nyckelskillnad är att strängar är oföränderliga. Det betyder att de inte kan ändras."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel\""}]},{"type":"text","value":"\nstrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar kan inte ändras på, så det här programmet kommer att ge ett felmeddelande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: 'str' object does not support item assignment"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett liknande fel uppstår om du försöker ordna en sträng med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"-metoden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängar är oföränderliga, men variablerna som lagrar dem är inte det. En sträng kan ersättas med en annan sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De följande exemplen är alltså till sin grund olika:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 271px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8b07a257ace229cc26a5eedf03a60986/5fce1/4_4_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 70.43478260869564%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeUlEQVQ4y5WT66rCQAyE9337w8eT/hEfQVCs4K2iWO+1VevdyDewBxE91EDYbHZ3Opmkbj6fWxzH1uv1bDab2Xa7tTzPLUkSW61WtlgslJ9Op4bd73d7PB5f3RVFIQB8v98rebvd7HA42G63U471eDxaGXP2g202G1WwXq/l7zHn7nK5iAFMKQd2+PV61Up+MBjY6XRS6TBFCkCIAVkul6qIveNStVq1Wq2mi/1+37rdrpy42WxapVKRzoB7KXgMkM/RC5XsNYPpu+AYTHH24/FYmtIw3APRMD5C2c4/xD517fUMVoDA6puXbsor+L9dRsPRaGTD4VDUKQtt/LiwZ6TKzKDmkId+HNI0tSzLBISmOGDn87n0aP1UMqMDOOu32NFB3+FPxgTAnBVZqAaJkIL8ZDLR3g+4o+1hGFq9XtccMn+dTkdO3Gg0LAgCgdFFAACCAHJABnZIppL5MgckiCmNy74BXIYVZ61WS38Na7vd1keiKPrLs38CMo80d8OuAIYAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8b07a257ace229cc26a5eedf03a60986/a0b58/4_4_1.webp 230w","/static/8b07a257ace229cc26a5eedf03a60986/cadab/4_4_1.webp 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8b07a257ace229cc26a5eedf03a60986/81c8e/4_4_1.png 230w","/static/8b07a257ace229cc26a5eedf03a60986/5fce1/4_4_1.png 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8b07a257ace229cc26a5eedf03a60986/5fce1/4_4_1.png","alt":"4 4 1","title":"4 4 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"text","value":"\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"!\""}]}]}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 281px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/4_4_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 85.65217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsSAAALEgHS3X78AAABq0lEQVQ4y42U6YrCUAyF+9jiExZEtPrDBRdQXHCt+27ky3DkjjMtBkKTu+Qm5ySNns+nPR4Pu9/v/sVH8S+Xi91uN7cR7eVpxMHpdGqtVsvq9brVajXbbDa22+0sSRKrVquWpql9K1HokMn5fP51gKwlPLRarWy9Xvsj+Njocrm0w+HwExBju916sOv16j56Op18vdls2n6/t/l87vtAwTpBuIMCDZV6QMqN49jK5bINh0Prdruu/X7fGo2GFYtFt3lEQqbcIZBksVhYBJDfCOfG47GXRzBKli0Y3hlmMSbWdQaMKVllo6HNfiTgw4uhfpKmFstkGaZ6vZ7jWKlUHEMIwGedthF+EDGbzZzdrMoilUHK4HI8Hn1D5RAEzcsqsw9DAv4THqCV1CqUz5p89jwg1OtgGCzEFpszfFHujEYjh0eEvUmZTCZWKpVcwYv2CCeGCwT4nCL6lrN/Sg5/CCgTwHQMBgMvSQK+4Mmco+BMhlrjm9vYjFq73fap6XQ6ThprPIiquZkQfDogt7EltFOhUPBgIUGUTBW5LIeBQhv8yETrEIOvX5twfgFptCTy4L9bkwAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e866c943663bc4d8360df84d7bc1c4de/a0b58/4_4_2.webp 230w","/static/e866c943663bc4d8360df84d7bc1c4de/f44a8/4_4_2.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e866c943663bc4d8360df84d7bc1c4de/81c8e/4_4_2.png 230w","/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/4_4_2.png 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/4_4_2.png","alt":"4 4 2","title":"4 4 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det första exemplet ändrar på innehållet i den lista som man hänvisar till. I det andra exemplet ersätts hänvisningen till den ursprungliga strängen med en hänvisning till en ny sträng. Den ursprungliga strängen finns fortfarande någonstans i datorns minne, men det saknas en referens till strängen så den kan inte längre användas i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi återkommer till det här ämnet senare. Då utforskar vi hänvisningar till listor närmare."}]},{"type":"element","tagName":"h2","properties":{"id":"mera-metoder-hos-listor-och-strangar","style":"position:relative;"},"children":[{"type":"text","value":"Mera metoder hos listor och strängar"},{"type":"element","tagName":"a","properties":{"href":"#mera-metoder-hos-listor-och-strangar","ariaLabel":"mera metoder hos listor och strangar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" räknar antalet gånger ett element eller en delsträng finns i en lista eller sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hon håsa på hussatimmen så lärar'n sa sakta: städa upp mjölet på golvet så att klassen int' e som en savann\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"sa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden räknar inte överlappande förekomster. Till exempel i strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aaaa"}]},{"type":"text","value":" räknar metoden upp till två förekomster av delsträngen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aa"}]},{"type":"text","value":", även om det finns tre stycken om överlappande förekomster skulle tillåtas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":" skapar en ny sträng där en specifik delsträng har ersatts med en annan sträng:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla\""}]},{"type":"text","value":"\nuusi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"God eftermiddag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"uusi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"God eftermiddag alla"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden påverkar alla delsträngar som hittas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lause "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"de åtta potatissemlorna förvandlades till en stor potatisplåt i ugnen – läraren, ja hon suckade i sitt hörn av klassen\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lause"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"en\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"EN\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"de åtta potatissemlorna förvandlades till EN stor potatisplåt i ugnEN – lärarEN, ja hon suckade i sitt hörn av klassEN"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":"-metoden används, är ett vanligt misstag att man glömmer att strängar är oföränderliga:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jag gillar Python\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi ersätter en delsträng, men resultatet lagras ingenstans"}]},{"type":"text","value":"\nstrang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jag gillar Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om den gamla strängen inte längre behövs kan man tilldela den nya strängen till samma variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jag gillar Python\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi ersätter en delsträng och lagrar resultatet"}]},{"type":"text","value":"\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jag gillar Java"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vanligaste bokstaven","tmcname":"osa04-22_vanligaste_bokstaven"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vanligaste_bokstaven"}]},{"type":"text","value":" som får som argument en sträng. Funktionen ska returnera den bokstav som förekommer mest i strängen. Om det finns flera kandidater ska man returnera den bokstav som förekommer först."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abba\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vanligaste_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstrang2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jästsurdegmedsaltosocker\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vanligaste_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"a\ns"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Utan vokaler","tmcname":"osa04-23_utan_vokaler"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"utan_vokaler"}]},{"type":"text","value":" som får som argument en sträng. Funktionen ska returnera en ny sträng där vokalerna i den ursprungliga strängen fattas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att strängen består av gemener i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a-ö"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"strang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"det här är ett exempel\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ilman_vokaaleja"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"dt hr r tt xmpl"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Versaler bort","tmcname":"osa04-24_versaler_bort"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons strängmetod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isupper()"}]},{"type":"text","value":" returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om strängen består av enbart versaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"XYZ\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbara_stora "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Abc\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bara_stora"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"versaler_bort"}]},{"type":"text","value":" som tar emot som argument en lista med strängar. Funktionen ska returnera en ny lista med de strängar som inte består av enbart versaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"def\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"STOR\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ANNANSTOR\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"liten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"annan liten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Delvis stoR\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nfiltrerad_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" versaler_bort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filtrerad_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['def', 'liten', 'annan liten', 'Delvis stoR']"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Grannar i en lista","tmcname":"osa04-25_grannar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi definierar att två element i en lista är grannar då skillnaden mellan deras värden är ett. Dvs. t.ex. elementen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" samt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"56"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"55"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta_grannstrackan"}]},{"type":"text","value":" som letar efter den längsta dellistan bestående av bredvidliggande grannar. Listans längd ska returneras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"T.ex. i listan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1, 2, 5, 4, 3, 4]"}]},{"type":"text","value":" skulle dellistan vara "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[5, 4, 3, 4]"}]},{"type":"text","value":" och längden som returneras därmed "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langsta_grannstrackan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"skapa-ett-storre-programmeringsprojekt","style":"position:relative;"},"children":[{"type":"text","value":"Skapa ett större programmeringsprojekt"},{"type":"element","tagName":"a","properties":{"href":"#skapa-ett-storre-programmeringsprojekt","ariaLabel":"skapa ett storre programmeringsprojekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här fjärde modulen avslutas med ett lite större programmeringsprojekt där du får utnyttja det du lärt dig hittills."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den viktigaste regeln när man börjar med ett programmeringsprojekt är att man inte ska försöka lösa alla problem samtidigt. Programmet ska bestå av mindre delar, till exempel hjälpfunktioner. Du ska testa att varje del fungerar före du fortsätter framåt. Om du försöker göra för mycket samtidigt kommer du högst antagligen hamna i en situation som präglas av kaos och mera kaos."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kommer att behöva ett sätt att testa dina funktioner utanför huvudfunktionen. Du kan uppnå det här genom att definiera en skild huvudfunktion som du anropar utanför alla andra funktioner i programmet. Det är enkelt att tillfälligt kommentera bort ett funktionsanrop när man testar programmet. De första stegen i utförandet av programmeringsprojektet skulle kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# programmets kod hit"}]},{"type":"text","value":"\n\nmain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu kan hjälpfunktionerna köras utan att huvudfunktionen körs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpfunktion som räknar vitsord baserat på givet poängantal"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"vitsord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# koo"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# programmets kod hit"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kommenterar huvudprogrammet bort"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"#main()"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testar hjälpfunktionen"}]},{"type":"text","value":"\npoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"skicka-data-fran-en-funktion-till-en-annan","style":"position:relative;"},"children":[{"type":"text","value":"Skicka data från en funktion till en annan"},{"type":"element","tagName":"a","properties":{"href":"#skicka-data-fran-en-funktion-till-en-annan","ariaLabel":"skicka data fran en funktion till en annan permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När ett program innehåller flera funktioner uppstår en fråga: hur skickar jag data från en funktion till en annan?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel frågar man efter några heltal från användaren. Programmet skriver sedan ut dessa värden och utför en \"analys\" på dem. Programmet är uppdelat i tre skilda funktioner:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_fran_anvandare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Ange "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" tal:\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange tal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffror\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Talen är: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"analysera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n medeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Siffror tillsammans "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", medeltal "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", minsta "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och största "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# \"huvudprogram\" som använder funktionerna"}]},{"type":"text","value":"\nindata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_fran_anvandare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nanalysens_resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analysera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analysens_resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, skulle det kunna se ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange 5 tal:\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-32"}]},{"type":"text","value":"\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"99"}]},{"type":"text","value":"\nAnge tal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-53"}]},{"type":"text","value":"\nTalen är:\n10\n34\n-32\n99\n-53\nSiffror tillsammans 5, medeltal 11.6, minsta -53 och största 99"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén är att huvudfunktionen \"lagrar\" all data som behandlas av programmet. I det här fallet är det enda som vi behöver de värden som användaren gett, i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det här behövs i en funktion ges det som ett argument. Det här sker med funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_resultat"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analysera"}]},{"type":"text","value":". Om funktionen resulterar i data som behövs på annat håll i programmet, returnerar funktionen det. Det här sparas i en variabel i huvudfunktionen. Det här sker med funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"indata_fran_anvandare"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analysera"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kunde använda den globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror"}]},{"type":"text","value":" från huvudfunktionen direkt i hjälpfunktionerna. Vi har redan gått igenom varför det är en "},{"type":"element","tagName":"a","properties":{"href":"https://softwareengineering.stackexchange.com/q/148108","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"dålig idé"}]},{"type":"text","value":", men här följer ännu en annan förklaring. Om funktionerna kan ändra på den globala variabeln kan oförutsedda saker börja hända i programmet, framför allt då antalet funktioner ökar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skicka data ut och in från funktioner gör man alltså helst med hjälp av argument och returvärden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kunde också göra huvudfunktionen till sin egen funktion. Då skulle variabeln siffor inte längre vara en global variabel, utan en lokal variabel i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktion som representerar huvudfunktionen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_fran_anvandare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n skriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n analysens_resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analysera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analysens_resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# start av programmet"}]},{"type":"text","value":"\nmain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vitsordsstatistik","tmcname":"osa04-26_vitsordsstatistik"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi ett program vars uppgift är att skriva ut vitsordsstatistik för en kurs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till programmet ger man rader med information om en studerandes provpoäng samt antalet gjorda uppgifter under kursen. Programmet skriver ut statistik på basis av dessa uppgifter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Provpoängen är heltal mellan noll och 20. Antalet gjorda uppgifter under kursen är ett heltal mellan noll och 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ber om indata från användaren, tills hon matar in en tom rad. Du kan anta att alla rader är korrekt inmatade – de innehåller alltså antingen två heltal eller är tomma."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här anger man provpoängen och antalet gjorda uppgifter under kursen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Provpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15 87"}]},{"type":"text","value":"\nProvpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10 55"}]},{"type":"text","value":"\nProvpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11 40"}]},{"type":"text","value":"\nProvpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4 17"}]},{"type":"text","value":"\nProvpoäng samt antalet gjorda uppgifter under kursen:\nStatistik:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När användaren matat in en tom rad ska programmet skriva ut statistik."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Statistiken består av de följande:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftspoäng: Ett poäng för 10 % gjorda uppgifter. Detta går ända upp till 100 % (100 uppgifter) —> 10 poäng. Uppgiftspoängen är ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kursvitsord: Räknas på basis av prov- och uppgiftspoängens summa, se följande tabell."}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Provpoäng + uppgiftspoäng"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Vitsord"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0–14"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0 (underkänt)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"15–17"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"18–20"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"21–23"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"24–27"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"28–30"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om provpoängen är under 10, kommer vitsordet dock alltid att vara 0, underkänt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande statistik skrivs ut med indatan från exemplet ovan:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Statistik:\nPoängmedeltal: 14.5\nGodkända (%): 75.0\nVitsordsfördelning:\n 5:\n 4:\n 3: *\n 2:\n 1: **\n 0: *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Decimaltal ska skrivas ut med en decimal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska du inte placera kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det. Om programmets funktionalitet t.ex. finns i funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":", ska funktionen anropas utanför det nämnda if-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Indatan ges som rader som består av två siffror i följande format."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Provpoäng samt antalet gjorda uppgifter under kursen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15 87"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Raden som matas in ska först delas i två, varefter delarna ska konverteras till heltal med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":"-funktionen. Du kan dela en rad på samma sätt som i den här uppgiften. Du kan också använda strängmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":". Du kan söka efter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python string split"}]},{"type":"text","value":" på nätet för mera information."}]},{"type":"comment","value":" **Huomaa** että tällä hetkellä Windowsissa on ongelmia joidenkin tehtävien testien suorittamisessa. Jos törmäät seuraavaan virheilmoitukseen\n\n\"Listan\n\nvoit suorittaa testit lähettämällä ne palvelimelle valitsemalla testien suoritusnapin oikealla puolella olevasta symbolista avautuvasta TMC-valikosta _Submit solutions_.\n\nOngelman saa korjattua menemällä laajennuksen asennusvalikkoon ja muuttamalla \"TMC Data\" -kohdassa tehtävien sijainnin johonkin toiseen sijaintiin, jonka tiedostopolku on lyhempi, allaolevassa kuvassa nappi _change path_. Siirrossa saattaa kestää hetken, joten odotathan operaation päättymistä.\n\n\"Listan\n\nOngelmaan pyritään saamaan parempi ratkaisu lähipäivinä. "}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät om den här veckans material."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kommer du att vara bekant med flera metoder för att extrahera delar av strängar och listor
  • \n
  • förstår du vad oföränderlighet hos strängar innebär
  • \n
  • kan du använda dig av metoderna count och replace.
  • \n

Du är redan bekant med syntaxen [] för att ta fram en delsträng:

strang = \"exempel\"\nprint(strang[3:7])

mpel

Samma syntax fungerar med listor. Dellistor kan extraheras på samma sätt som delsträngar:

lista = [3,4,2,4,6,1,2,4,2]\nprint(lista[3:7])

[4, 6, 1, 2]

Mera extrahering

Syntaxen [] fungerar faktiskt mycket lika som range-funktionen, vilket innebär att vi också kan ge den ett steg:

strang = \"exempel\"\nprint(strang[0:7:2])\nlista = [1,2,3,4,5,6,7,8]\nprint(lista[6:2:-1])

eepl\n[7, 6, 5, 4]

Om vi lämnar bort något av indexen kommer operatorn att inkludera alla element. Tack vare detta kan vi till exempel skriva ett mycket kort program som vänder om en sträng:

strang = input(\"Ange en sträng: \")\nprint(strang[::-1])

Ange en sträng: exempel\nlepmexe

Varning: globala variabler inom funktioner

Vi har observerat att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler.

Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra.

Här är ett exempel på en funktion som använder en global variabel \"av misstag\":

def svangd_utskrift(namn: list):\n    # använder av misstag den globala variabeln namnlista\n    i = len(namnlista) - 1\n    while i >= 0:\n        print(namnlista[i])\n        i -= 1\n\n# global variabel\nnamnlista = [\"Antti\", \"Emilia\", \"Erkki\", \"Margaret\"]\nsvangd_utskrift(namnlista)\nprint()\nsvangd_utskrift([\"Louise\", \"Ophelia\", \"Lotta\"])

Margaret\nErkki\nEmilia\nAntti

Margaret\nErkki\nEmilia\nAntti

Även om funktionen anropas korrekt skrivs alltid namnen i den globala variabeln namnlista ut.

All kod som testar funktioner ska skrivas inom ett separat block så att TMC-testen accepterar koden. Föregående exempel ska alltså skrivas så här:

def svangd_utskrift(namn: list):\n    # använder av misstag den globala variabeln namnlista\n    i = len(namnlista) - 1\n    while i >= 0:\n        print(namnlista[i])\n        i -= 1\n\n# kod som testar funktionen placeras här\nif __name__ == \"__main__\":\n    # global variabel\n    namnlista = [\"Antti\", \"Emilia\", \"Erkki\", \"Margaret\"]\n    svangd_utskrift(namnlista)\n    print()\n    svangd_utskrift([\"Louise\", \"Ophelia\", \"Lotta\"])

Nu definieras också den globala variabeln i if-blocket.

TMC-testen körs alltid så att koden inom dessa if-block inte körs. Därför fungerar funktionen inte ens i teorin eftersom variabeln namnlista inte finns då testen körs.

Skapa funktionen allt_omvant som får som argument en lista med strängar. Funktionen ska skapa och returnera en ny lista där alla strängar i den ursprungliga listan är omvända. Elementen ska också komma i omvänd ordning i listan.

Exempel:

lista = [\"Hej\", \"alla\", \"exempel\", \"ett till\"]\nlista2 = allt_omvant(lista)\nprint(lista2)

['llit tte', 'lepmexe', 'alla', 'jeH']

Strängar är oföränderliga

Strängar och listor har en hel del likheter, framför allt då det kommer till hur de fungerar med olika operatorer. En nyckelskillnad är att strängar är oföränderliga. Det betyder att de inte kan ändras.

strang = \"exempel\"\nstrang[0] = \"a\"

Strängar kan inte ändras på, så det här programmet kommer att ge ett felmeddelande:

TypeError: 'str' object does not support item assignment

Ett liknande fel uppstår om du försöker ordna en sträng med sort-metoden.

Strängar är oföränderliga, men variablerna som lagrar dem är inte det. En sträng kan ersättas med en annan sträng.

De följande exemplen är alltså till sin grund olika:

lista = [1,2,3]\nlista[0] = 10
\n \n \n \n \n \n \"4\n \n \n

Det första exemplet ändrar på innehållet i den lista som man hänvisar till. I det andra exemplet ersätts hänvisningen till den ursprungliga strängen med en hänvisning till en ny sträng. Den ursprungliga strängen finns fortfarande någonstans i datorns minne, men det saknas en referens till strängen så den kan inte längre användas i programmet.

Vi återkommer till det här ämnet senare. Då utforskar vi hänvisningar till listor närmare.

Mera metoder hos listor och strängar

Metoden count räknar antalet gånger ett element eller en delsträng finns i en lista eller sträng:

strang = \"Hon håsa på hussatimmen så lärar'n sa sakta: städa upp mjölet på golvet så att klassen int' e som en savann\"\nprint(strang.count(\"sa\"))\n\nlista = [1,2,3,1,4,5,1,6]\nprint(lista.count(1))

5\n3

Metoden räknar inte överlappande förekomster. Till exempel i strängen aaaa räknar metoden upp till två förekomster av delsträngen aa, även om det finns tre stycken om överlappande förekomster skulle tillåtas.

Metoden replace skapar en ny sträng där en specifik delsträng har ersatts med en annan sträng:

strang = \"Hej alla\"\nuusi = strang.replace(\"Hej\", \"God eftermiddag\")\nprint(uusi)

God eftermiddag alla

Metoden påverkar alla delsträngar som hittas:

lause = \"de åtta potatissemlorna förvandlades till en stor potatisplåt i ugnen – läraren, ja hon suckade i sitt hörn av klassen\"\nprint(lause.replace(\"en\", \"EN\"))

de åtta potatissemlorna förvandlades till EN stor potatisplåt i ugnEN – lärarEN, ja hon suckade i sitt hörn av klassEN

När replace-metoden används, är ett vanligt misstag att man glömmer att strängar är oföränderliga:

strang = \"Jag gillar Python\"\n\n# vi ersätter en delsträng, men resultatet lagras ingenstans\nstrang.replace(\"Python\", \"Java\")\nprint(strang)

Jag gillar Python

Om den gamla strängen inte längre behövs kan man tilldela den nya strängen till samma variabel:

strang = \"Jag gillar Python\"\n\n# vi ersätter en delsträng och lagrar resultatet\nstrang = strang.replace(\"Python\", \"Java\")\nprint(strang)

Jag gillar Java

Skapa funktionen vanligaste_bokstaven som får som argument en sträng. Funktionen ska returnera den bokstav som förekommer mest i strängen. Om det finns flera kandidater ska man returnera den bokstav som förekommer först.

Exempel:

strang = \"abba\"\nprint(vanligaste_bokstaven(strang))\n\nstrang2 = \"jästsurdegmedsaltosocker\"\nprint(vanligaste_bokstaven(strang2))

a\ns

Skapa funktionen utan_vokaler som får som argument en sträng. Funktionen ska returnera en ny sträng där vokalerna i den ursprungliga strängen fattas.

Du kan anta att strängen består av gemener i intervallet a-ö.

Exempel:

strang = \"det här är ett exempel\"\nprint(ilman_vokaaleja(strang))

dt hr r tt xmpl

Pythons strängmetod isupper() returnerar True om strängen består av enbart versaler.

Exempel:

print(\"XYZ\".isupper())\n\nbara_stora = \"Abc\".isupper()\nprint(bara_stora)

True\nFalse

Skapa funktionen versaler_bort som tar emot som argument en lista med strängar. Funktionen ska returnera en ny lista med de strängar som inte består av enbart versaler.

Exempel:

lista = [\"ABC\", \"def\", \"STOR\", \"ANNANSTOR\", \"liten\", \"annan liten\", \"Delvis stoR\"]\nfiltrerad_lista = versaler_bort(lista)\nprint(filtrerad_lista)

['def', 'liten', 'annan liten', 'Delvis stoR']

Vi definierar att två element i en lista är grannar då skillnaden mellan deras värden är ett. Dvs. t.ex. elementen 1 och 2 samt 56 och 55.

Skapa funktionen langsta_grannstrackan som letar efter den längsta dellistan bestående av bredvidliggande grannar. Listans längd ska returneras.

T.ex. i listan [1, 2, 5, 4, 3, 4] skulle dellistan vara [5, 4, 3, 4] och längden som returneras därmed 4.

Exempel:

lista = [1, 2, 5, 7, 6, 5, 6, 3, 4, 1, 0]\nprint(langsta_grannstrackan(lista))

4

Skapa ett större programmeringsprojekt

Den här fjärde modulen avslutas med ett lite större programmeringsprojekt där du får utnyttja det du lärt dig hittills.

Den viktigaste regeln när man börjar med ett programmeringsprojekt är att man inte ska försöka lösa alla problem samtidigt. Programmet ska bestå av mindre delar, till exempel hjälpfunktioner. Du ska testa att varje del fungerar före du fortsätter framåt. Om du försöker göra för mycket samtidigt kommer du högst antagligen hamna i en situation som präglas av kaos och mera kaos.

Du kommer att behöva ett sätt att testa dina funktioner utanför huvudfunktionen. Du kan uppnå det här genom att definiera en skild huvudfunktion som du anropar utanför alla andra funktioner i programmet. Det är enkelt att tillfälligt kommentera bort ett funktionsanrop när man testar programmet. De första stegen i utförandet av programmeringsprojektet skulle kunna se ut så här:

def main():\n    poang = []\n    # programmets kod hit\n\nmain()

Nu kan hjälpfunktionerna köras utan att huvudfunktionen körs:

# hjälpfunktion som räknar vitsord baserat på givet poängantal\ndef vitsord(poang):\n    # koo\n\ndef main():\n    poang = []\n    # programmets kod hit\n\n# kommenterar huvudprogrammet bort\n#main()\n\n# testar hjälpfunktionen\npoang = 35\nresultat = vitsord(poang)\nprint(resultat)

Skicka data från en funktion till en annan

När ett program innehåller flera funktioner uppstår en fråga: hur skickar jag data från en funktion till en annan?

I följande exempel frågar man efter några heltal från användaren. Programmet skriver sedan ut dessa värden och utför en \"analys\" på dem. Programmet är uppdelat i tre skilda funktioner:

def las_fran_anvandare(antal: int):\n    print(f\"Ange {antal} tal:\")\n    siffror = []\n\n    for i in range(antal):\n        siffra = int(input(\"Ange tal: \"))\n        siffror.append(siffra)\n\n    return siffror\n\ndef skriv_ut(siffror: list):\n    print(\"Talen är: \")\n    for siffra in siffror:\n        print(siffra)\n\ndef analysera(siffror: list):\n    medeltal = sum(siffror) / len(siffror)\n    return f\"Siffror tillsammans {len(siffror)}, medeltal {medeltal}, minsta {min(siffror)} och största {max(siffror)}\"\n\n# \"huvudprogram\" som använder funktionerna\nindata = las_fran_anvandare(5)\nskriv_ut(indata)\nanalysens_resultat = analysera(indata)\nprint(analysens_resultat)

När programmet körs, skulle det kunna se ut så här:

Ange 5 tal:\nAnge tal: 10\nAnge tal: 34\nAnge tal: -32\nAnge tal: 99\nAnge tal: -53\nTalen är:\n10\n34\n-32\n99\n-53\nSiffror tillsammans 5, medeltal 11.6, minsta -53 och största 99

Idén är att huvudfunktionen \"lagrar\" all data som behandlas av programmet. I det här fallet är det enda som vi behöver de värden som användaren gett, i variabeln siffror.

Om det här behövs i en funktion ges det som ett argument. Det här sker med funktionerna skriv_ut_resultat och analysera. Om funktionen resulterar i data som behövs på annat håll i programmet, returnerar funktionen det. Det här sparas i en variabel i huvudfunktionen. Det här sker med funktionerna indata_fran_anvandare och analysera.

Du kunde använda den globala variabeln siffror från huvudfunktionen direkt i hjälpfunktionerna. Vi har redan gått igenom varför det är en dålig idé, men här följer ännu en annan förklaring. Om funktionerna kan ändra på den globala variabeln kan oförutsedda saker börja hända i programmet, framför allt då antalet funktioner ökar.

Att skicka data ut och in från funktioner gör man alltså helst med hjälp av argument och returvärden.

Du kunde också göra huvudfunktionen till sin egen funktion. Då skulle variabeln siffor inte längre vara en global variabel, utan en lokal variabel i main-funktionen:

# funktion som representerar huvudfunktionen\ndef main():\n    indata = las_fran_anvandare(5)\n    skriv_ut(indata)\n    analysens_resultat = analysera(indata)\n\n    print(analysens_resultat)\n\n# start av programmet\nmain()

I den här uppgiften skapar vi ett program vars uppgift är att skriva ut vitsordsstatistik för en kurs.

Till programmet ger man rader med information om en studerandes provpoäng samt antalet gjorda uppgifter under kursen. Programmet skriver ut statistik på basis av dessa uppgifter.

Provpoängen är heltal mellan noll och 20. Antalet gjorda uppgifter under kursen är ett heltal mellan noll och 100.

Programmet ber om indata från användaren, tills hon matar in en tom rad. Du kan anta att alla rader är korrekt inmatade – de innehåller alltså antingen två heltal eller är tomma.

Så här anger man provpoängen och antalet gjorda uppgifter under kursen:

Provpoäng samt antalet gjorda uppgifter under kursen: 15 87\nProvpoäng samt antalet gjorda uppgifter under kursen: 10 55\nProvpoäng samt antalet gjorda uppgifter under kursen: 11 40\nProvpoäng samt antalet gjorda uppgifter under kursen: 4 17\nProvpoäng samt antalet gjorda uppgifter under kursen:\nStatistik:

När användaren matat in en tom rad ska programmet skriva ut statistik.

Statistiken består av de följande:

Uppgiftspoäng: Ett poäng för 10 % gjorda uppgifter. Detta går ända upp till 100 % (100 uppgifter) —> 10 poäng. Uppgiftspoängen är ett heltal.

Kursvitsord: Räknas på basis av prov- och uppgiftspoängens summa, se följande tabell.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Provpoäng + uppgiftspoängVitsord
0–140 (underkänt)
15–171
18–202
21–233
24–274
28–305

Om provpoängen är under 10, kommer vitsordet dock alltid att vara 0, underkänt.

Följande statistik skrivs ut med indatan från exemplet ovan:

\nStatistik:\nPoängmedeltal: 14.5\nGodkända (%): 75.0\nVitsordsfördelning:\n  5:\n  4:\n  3: *\n  2:\n  1: **\n  0: *\n

Decimaltal ska skrivas ut med en decimal.

Obs! I dessa uppgifter ska du inte placera kod i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det. Om programmets funktionalitet t.ex. finns i funktionen main, ska funktionen anropas utanför det nämnda if-blocket.

Tips: Indatan ges som rader som består av två siffror i följande format.

Provpoäng samt antalet gjorda uppgifter under kursen: 15 87

Raden som matas in ska först delas i två, varefter delarna ska konverteras till heltal med int-funktionen. Du kan dela en rad på samma sätt som i den här uppgiften. Du kan också använda strängmetoden split. Du kan söka efter python string split på nätet för mera information.

Vänligen svara på en kort enkät om den här veckans material.

","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/6-strangar-listor.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-4/page-data.json b/page-data/osa-4/page-data.json index c54c0cc16..f82900285 100644 --- a/page-data/osa-4/page-data.json +++ b/page-data/osa-4/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-4", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-4","title":"Modul 4"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-4","title":"Modul 4"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-4/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-5/1-mer-om-listor/page-data.json b/page-data/osa-5/1-mer-om-listor/page-data.json index 5e6046597..78c5cfb04 100644 --- a/page-data/osa-5/1-mer-om-listor/page-data.json +++ b/page-data/osa-5/1-mer-om-listor/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-5/1-mer-om-listor", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa listor med element av olika typer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man kan använda listor för att organisera data"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du lagra en matris som en tvådimensionell lista."}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"vastaava teksti löytyy osioista 3-1, 5-1 ja 6-1, tsekkaa kaikki jos muokkaat tätä"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Om uppgifterna i den här kursen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad."}]}]},{"type":"element","tagName":"h2","properties":{"id":"listor-med-olika-typer-av-data","style":"position:relative;"},"children":[{"type":"text","value":"Listor med olika typer av data"},{"type":"element","tagName":"a","properties":{"href":"#listor-med-olika-typer-av-data","ariaLabel":"listor med olika typer av data permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den förra modulen fokuserade vi främst på listor med element av heltalstyp, men listor kan innehålla vilka typer av data som helst. En lista bestående av strängar kunde exempelvis kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pontus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Konrad\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antal namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namnen i alfabetisk ordning:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['Maja', 'Lotta', 'Pontus']\n['Maja', 'Lotta', 'Pontus', 'Konrad']\nAntal namn: 4\nNamnen i alfabetisk ordning:\nKonrad\nLotta\nMaja\nPontus"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flyttal kan också lagras i listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14.6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"21.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19.2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" matning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" matningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmedeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medelvärde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"-2.5\n1.1\n7.5\n14.6\n21.0\n19.2\nMedelvärde: 10.15"}]}]},{"type":"comment","value":"vastaava varoitusteksti löytyy osioista 3-4, 4-6 ja 5-1, tsekkaa kaikki jos muokkaat tätä"},{"type":"element","tagName":"h2","properties":{"id":"varning-globala-variabler-inom-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Varning: globala variabler inom funktioner"},{"type":"element","tagName":"a","properties":{"href":"#varning-globala-variabler-inom-funktioner","ariaLabel":"varning globala variabler inom funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har observerat att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel på en funktion som använder en global variabel \"av misstag\":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"svangd_utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använder av misstag den globala variabeln namnlista"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\nnamnlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erkki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Margaret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsvangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louise\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ophelia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Margaret\nErkki\nEmilia\nAntti"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Margaret\nErkki\nEmilia\nAntti"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om funktionen anropas korrekt skrivs alltid namnen i den globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnlista"}]},{"type":"text","value":" ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All kod som testar funktioner ska skrivas inom ett separat block så att TMC-testen accepterar koden. Föregående exempel ska alltså skrivas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"svangd_utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använder av misstag den globala variabeln namnlista"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod som testar funktionen placeras här"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\n namnlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erkki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Margaret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n svangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n svangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louise\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ophelia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu definieras också den globala variabeln i if-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TMC-testen körs alltid så att koden inom dessa if-block inte körs. Därför fungerar funktionen inte ens i teorin eftersom variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnlista"}]},{"type":"text","value":" inte finns då testen körs."}]},{"type":"element","tagName":"h2","properties":{"id":"varning-att-skriva-over-en-parameter-och-returnera-for-tidigt","style":"position:relative;"},"children":[{"type":"text","value":"Varning: att skriva över en parameter och returnera för tidigt"},{"type":"element","tagName":"a","properties":{"href":"#varning-att-skriva-over-en-parameter-och-returnera-for-tidigt","ariaLabel":"varning att skriva over en parameter och returnera for tidigt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns ett par nya källor till buggar som vi borde ta en titt på före vi börjar med övningarna i den här modulen. Låt oss kika på en funktion som berättar om ett heltal hittas i en lista. Båda är definierade som parametrar i funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffra_i_listan"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här funktionen verkar alltid returnera värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". Orsaken till det är att for-loopen skriver över värdet som är lagrat i parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":". Därför är villkoret i if-satsen alltid sant."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att ändra på parameterns namn löser problemet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffra_i_listan"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra_som_soks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" siffra_som_soks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu verkar villkoret i if-satsen vara i bättre skick. Men det finns ett nytt problem, eftersom funktionen inte ännu heller fungerar som den ska. Om vi testar följande, märker vi en bugg:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffra_i_listan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problemet här är att funktionen returnerar ett värde för tidigt, utan att kolla igenom alla siffror i listan. Funktionen kollar faktiskt endast det första värdet i listan och returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" beroende på dess värde. Vi kan inte veta att en siffra inte finns i listan förrän vi har gått igenom hela listan. Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return False"}]},{"type":"text","value":" måste alltså placeras utanför for-loopen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffra_i_listan"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra_som_soks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" siffra_som_soks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en annan funktion som inte fungerar korrekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffrorna_olika"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpvariabel där vi lagrar de värden som kollats"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# har siffran redan förekommit?"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffrorna_olika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen borde kolla om alla siffor i en lista är olika, men värdet som returneras är alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet skriver funktionen igen över värdet som är lagrat i dess parameter. Funktionen försöker använda variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" för att lagra de siffror som redan har kontrollerats, men det här skriver över det ursprungliga argumentet. Att namnge hjälpvariabeln på nytt löser vårt problem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffrorna_olika"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpvariabel där de siffror som kollats lagras"}]},{"type":"text","value":"\n sedda_siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# har siffran redan förekommit?"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sedda_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n sedda_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffrorna_olika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problem som dessa kan hittas och korrigeras med hjälp av debuggaren eller "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyget"}]},{"type":"text","value":". Det lönar sig verkligen att lära sig använda dessa effektivt."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Längsta strängen","tmcname":"osa05-01a_langsta_strangen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"HUOM:"}]},{"type":"text","value":" tämä ja seuraava tehtävä ovat väärässä järjestyksessä VS Coden sivupalkissa"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langst(strangar: list)"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska hitta och returnera den längsta strängen. Du kan anta att det bara finns en längsta sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"morjens\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tjolahoppsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tjenare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"tjolahoppsan"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"listor-inom-listor","style":"position:relative;"},"children":[{"type":"text","value":"Listor inom listor"},{"type":"element","tagName":"a","properties":{"href":"#listor-inom-listor","ariaLabel":"listor inom listor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett element i en lista kan vara en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[5, 2, 3], [4, 1], [2, 2, 5, 1]]\n[4, 1]\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I hurdana fall skulle det här kunna vara nyttigt?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att listor kan innehålla element av olika typer. Du kan till exempel lagra information om en person i en lista. Det första elementet kan till exempel vara personens namn, det andra dess ålder och det tredje dess höjd:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nelly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En databas bestående av flera personer skulle då kunna vara en lista, vars element skulle vara listor som innehåller information om en person:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nelly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PO\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"37"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"August\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"39"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n sko "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år, skostorlek "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"sko"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nelly: 10 år, skostorlek 26\nPO: 7 år, skostorlek 22\nEmilia: 32 år, skostorlek 37\nAugust: 39 år, skostorlek 44"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For-loopen går igenom elementen i den yttre listan en för en. Varje lista som innehåller info om en person tilldelas en för en till variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listor är inte alltid det bästa sättet att presentera data som information om en person. Vi kommer snart att se på lexikon (dictionary) i Python. Den är bättre anpassad för situationer som den ovan nämnda."}]},{"type":"element","tagName":"h2","properties":{"id":"matriser","style":"position:relative;"},"children":[{"type":"text","value":"Matriser"},{"type":"element","tagName":"a","properties":{"href":"#matriser","ariaLabel":"matriser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En tvådimensionell tabell – matris – är ett annat användningsområde för listor inom listor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel följande matris…"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 151px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_0.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAABw0lEQVQ4y62UyYoCQQyG+3EF38YHUPHoyYsLeFLw4smDO654UVBxAXfN8AVS1LSt4zATCJ1OUn+l8qcqKBQKMhwOpd1uq3Y6Hen3+1Iul6XZbEq323UxbHzEyCHX1pgGgEXJbDaTV/IuFrADst/vpdFouMBgMJDT6aR2r9fTyhB8xJDH4yH1el02m43+3+93CSgT2W63kkwmHeBoNHKAuVxOstmsA/RPlUgktBVPgPzsdjv9Iiw6n89qT6dTmc/nauMzwNvtpjHLo+KA5kbJZDJx4L7gI/ayhzBGk+kLO6MsqFQqyuR4PHYxbHzEyMFnMVqEBtZsyr5cLqpUwcLD4SDX61UVP198xMixfF8Dv6G+UA0AYcFHJS+PbKSs12tJpVIOxCelWq1KsViMZJkJYK0jxeZwtVpJOp2W4/HoAG1s8vm8lEqlSED6+Q3wt0dmVN4eOUwK3z+R8snYmP+jsfn3wTaW7T7TozDLy+VSSQtfPUhYLBZ61CeWuZPxeNyB+CzDfiaTeWKZFyoWi2krIl+bWq3mdvMr5PlqtVpPFQLAjNrzpRX6RzanPV8/3RTL9e1vzxf98wGtWmJGED4f0F9DhV9fOel+Sn/2BgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/57e8504bd70c2538405be5b5e1572347/8d139/5_1_0.webp 151w"],"sizes":"(max-width: 151px) 100vw, 151px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_0.png 151w"],"sizes":"(max-width: 151px) 100vw, 151px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_0.png","alt":"5 1 0","title":"5 1 0","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…kan presenteras som en tvådimensionell lista i Python på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom en matris är en lista som innehåller listor kan enskilda element inom matrisen kommas åt med hjälp av varandra påföljande hakparenteser. Det första indexet hänvisar till raden, medan den andra hänvisar till kolumnen. Indexeringen startar från noll så "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matris[0][1]"}]},{"type":"text","value":" hänvisar till det andra elementet på den första raden."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmatris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n[[1, 2, 3], [10, 2, 1], [4, 5, 6]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som med vilken som helst lista, kan raderna i matrisen gås igenom med en for-loop. Den följande koden skriver ut varje rad i matrisen på en skild rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt kan kapslade loopar användas för att komma åt enskilda element inom matrisen. Följande kodsnutten skriver ut varje element i matrisen på en skild rad med hjälp av två for-loopar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny rad\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ny rad\n1\n2\n3\nny rad\n4\n5\n6\nny rad\n7\n8\n9"}]}]},{"type":"element","tagName":"h2","properties":{"id":"visualisering-av-kod-som-innehaller-listor-inom-listor","style":"position:relative;"},"children":[{"type":"text","value":"Visualisering av kod som innehåller listor inom listor"},{"type":"element","tagName":"a","properties":{"href":"#visualisering-av-kod-som-innehaller-listor-inom-listor","ariaLabel":"visualisering av kod som innehaller listor inom listor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Program som består av listor inom listor kan vara svåra att greppa om i början. "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" av Python Tutor är ett bra sätt att bekanta sig med hur de fungerar. Det följande är en visualisering av exemplet ovan:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9450893ea3ce24ad2a607cde889d3e70/769f8/5_1_0a.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 42.173913043478265%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAABFElEQVQY03WQ207DMAyG+/7PtgtuBhMSDG1rx+jWJG0OtuMDamCIm32yLCv6fzt2Z2Yigoi10rzkY38dLq4UICIAUFUiSikRESJSQ0S00ZkZiyzLkn7JeNcBQOtLYZ6Z2RrNJSLMzKu5QiHEvu8Ph8P5PPzp/qm1lCzCpaxfbI9rrGYhNDMACGEZrz6mdWBrLmZGxK/78+6tB5IY/ZKOGW4IvUjoYnDOB+cc16pqBRhJ7jPXjMQv+6/N9iOC5jJdbk9uPqX0DHDqatsPEd3K1MJN05RS+lmOKvuIV5dYxNQy8Od1C2Wnpl3MtOSaYslz8CEMwzCOY4yRiOwBfh5ifDeDjkVZVETtMdrOdi+tgPd+Q3T8Bqyl0FSTvoR1AAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9450893ea3ce24ad2a607cde889d3e70/a0b58/5_1_0a.webp 230w","/static/9450893ea3ce24ad2a607cde889d3e70/bc10c/5_1_0a.webp 460w","/static/9450893ea3ce24ad2a607cde889d3e70/966d8/5_1_0a.webp 920w","/static/9450893ea3ce24ad2a607cde889d3e70/445df/5_1_0a.webp 1380w","/static/9450893ea3ce24ad2a607cde889d3e70/78de1/5_1_0a.webp 1840w","/static/9450893ea3ce24ad2a607cde889d3e70/7e82b/5_1_0a.webp 1924w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9450893ea3ce24ad2a607cde889d3e70/81c8e/5_1_0a.png 230w","/static/9450893ea3ce24ad2a607cde889d3e70/08a84/5_1_0a.png 460w","/static/9450893ea3ce24ad2a607cde889d3e70/c0255/5_1_0a.png 920w","/static/9450893ea3ce24ad2a607cde889d3e70/b1001/5_1_0a.png 1380w","/static/9450893ea3ce24ad2a607cde889d3e70/161ec/5_1_0a.png 1840w","/static/9450893ea3ce24ad2a607cde889d3e70/769f8/5_1_0a.png 1924w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9450893ea3ce24ad2a607cde889d3e70/c0255/5_1_0a.png","alt":"5 1 0a","title":"5 1 0a","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bilden ovan avslöjar att en 3 x 3 -matris tekniskt sett består av fyra listor. Den första listan representerar hela matrisen. De tre resterande listorna är element i den första listan, och de representerar rader i matrisen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom multidimensionella listor kan gås igenom med kapslade loopar skulle man kunna tänka sig att listorna är inuti varandra, men som bilden visar är detta inte sant. Istället hänvisar den \"stora listan\" som representerar matrisen till skilda listor som alla representerar en rad i matrisen. Det här är en referens – något vi kommer att se mera på i följande del."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I bilden ovan har programmet hunnit till den andra raden i matrisen och det är den listan som variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rad"}]},{"type":"text","value":" hänvisar till för tillfället. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"element"}]},{"type":"text","value":" innehåller det element där programmet är vid för tillfället. Värdet i den variabeln är det mellersta värdet i listan, dvs. 5."}]},{"type":"element","tagName":"h2","properties":{"id":"komma-at-element-i-en-matris","style":"position:relative;"},"children":[{"type":"text","value":"Komma åt element i en matris"},{"type":"element","tagName":"a","properties":{"href":"#komma-at-element-i-en-matris","ariaLabel":"komma at element i en matris permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att komma åt en viss rad i en matris är enkelt. Det är bara att välja den raden. Följande funktion räknar summan av elementen på en viss rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summan_av_radens_element"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" radnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi inspekterar bara en rad"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"radnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" element\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" summa\n\nm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summan_av_radens_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 33 (dvs. 9 + 1 + 12 + 11)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att arbeta med kolumner i en matris är lite mera komplicerat eftersom matrisen är lagrad som rader:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summan_av_kolumnens_element"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kolumnnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# till summan adderas för varje rad elementet på det önskade stället"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"kolumnnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" summa\n\nm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summan_av_kolumnens_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 39 (dvs. 3 + 12 + 9 + 15)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kolumnen som söks här består av elementen vid index 2 på varje rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" rekommenderas varmt för att bättre förstå hur de här funktionerna fungerar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att ändra på ett värde hos ett specifikt element inom en matris är enkelt. Välj en rad i matrisen och sedan en kolumn inom raden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"byt_varde"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" radnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kolumnnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# välj korrekt rad"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"radnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# och korrekt ställe i raden"}]},{"type":"text","value":"\n rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"kolumnnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" varde\n\nm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbyt_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 1000], [2, 9, 15, 1]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att vi ovan använde indexen för raden och kolumnen för att komma åt det element vi ville ändra på. Om vi vill ändra på innehållet i en matris måste vi komma åt elementen md hjälp av deras index. Vi kan inte bara använda oss av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for element in lista"}]},{"type":"text","value":" -loop för att gå igenom matrisen då vi vill ändra på innehållet i en matris."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Istället behöver vi hålla reda på indexen hos elementen: till exempel med en while-loop eller en for-loop med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-funktionen. Följande kod ökar på värdet hos varje element i en matris med ett:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"m "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" j "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"j"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[2, 3, 4], [5, 6, 7], [8, 9, 10]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den yttre loopen går igenom indexen från noll till matrisens längd, alltså antalet rader i matrisen. Den inre loopen går igenom indexen från noll till radernas längd."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Antalet element","tmcname":"osa05-01_antalet_element"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"HUOM:"}]},{"type":"text","value":" tämä ja edellinen tehtävä ovat väärässä järjestyksessä VS Coden sivupalkissa"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rakna_element(matris: list, element: int)"}]},{"type":"text","value":" som tar emot en matris samt ett heltal som argument. Funktionen ska returnera antalet gånger det valda elementet hittas i matrisen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"m "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-tvadimensionell-tabell-som-datastruktur-i-ett-spel","style":"position:relative;"},"children":[{"type":"text","value":"En tvådimensionell tabell som datastruktur i ett spel"},{"type":"element","tagName":"a","properties":{"href":"#en-tvadimensionell-tabell-som-datastruktur-i-ett-spel","ariaLabel":"en tvadimensionell tabell som datastruktur i ett spel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En matris kan fungera väl som datastruktur i flera olika spel. Till exempel rutorna i ett sudokuspel…"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 410px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100.43478260869566%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAABYlAAAWJQFJUiTwAAADmUlEQVQ4y0WUWXOqPACG/fX9B163vTg67dQuap2OYBfbutQFRSmQoDGQQBAQqsXd5hvLmfO915l5kndJStM0XdcBAJ7nfX5+uq5bKpUIIQghTdMAAISQfr//8vJCCNE0zbZtVVWn0ylCKAV+5bou59w0zcPhUCgUVqtVFEWKomCMV6uVYRjPz8/T6dS27c1mY1kW55wQktJ13XGc4XAYBIEsy9Pp9OLiAmMMITQMAwBgWVar1SqXy8kxx3EGg4Hv+xDClOu6+/3e933OOWOMcy6KIsY4DEP7V7vdbjKZNJvN+Xzued5+v0+OMcZSyd0kSbJtu9PpUEr//PkDIVRVFQCgqup4PK7Vag8PD4wxSZJM05QkiTGmadqRfDgcHMfxPI9SyjkXBIEQ4nnecrn8+vpar9fj8ViSpIR2OBwopaZpHt98f39fLBaz2WyhUMhkMqIonpyc5HK5q6ur29vb6+vrQqFwdnaWTqcFQchms6VSKZvN3t3dvby8pCilm80GITSZTAghEMJ8Pr/ZbGazWRRFQRB8f38DAFqtVuLwer3+322EUBiGSVoAANM0c7lcGIaEEMYYpdTzPFmWX19fk8zm8zmEcLlcHnPGGEdRhBDabreGYaxWq3w+DyG0LAv+KgxDRVFEUXRdV1VVCCFCaL1eY4z/kiGEcRxrmrZYLG5ubizLIoQYv/J9X5blSqUym80Gg0GSfxzHRzJCiFJq2/bhcMAYu66bz+fjOA6CACFECFksFrquV6vVIAgSgzDGPz8/lmWl2u12t9vtdDoY40ajIUnS6enpaDRSFKVSqdTrdcMw3t7eLi8vW61Wu92GEH58fGCMFUVJ1Wq1drstiqIsy+VyuVqtptPpcrn89PTUaDRqtdrj42Mulzs/P6/X66IottttQRAkSep0On/rGQQB59x13SiKyuVyUs/lchnHMWNsOBy+v7/PZjPf9//V03XdlKqqpmkmk5AkiVKayWRs206M0XXdNM1msykIQhAEvV6PECJJ0mQyAQCkHMfZbreO43DOKaWMsWKxuFqtwjCcz+dhGMZxbBhGUhJK6W63M03TdV1K6ZGMMR4Oh57n9Xo927az2WwS8mg0AgBgjBPydDqVZZlS2u/3Hcc5khFClmWFYcg5t22bc14qlXa73WKxiOM4GcZoNOp2u/+GkczGcZzUeDxOps8YSz6GarVqWVbSdoSQaZqKonQ6HcYYAIBSqut60qL/AD7hB6pgN21XAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a4d938ae5202e8db72b179ff4d76293/a0b58/5_1_1.webp 230w","/static/5a4d938ae5202e8db72b179ff4d76293/0cb6c/5_1_1.webp 410w"],"sizes":"(max-width: 410px) 100vw, 410px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a4d938ae5202e8db72b179ff4d76293/81c8e/5_1_1.png 230w","/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_1.png 410w"],"sizes":"(max-width: 410px) 100vw, 410px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_1.png","alt":"5 1 1","title":"5 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…kan representeras i en matris på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu representerar noll en tom ruta, eftersom noll inte är ett värde som kan användas i sudoku."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är en enkel funktion som skriver ut sudokurutor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" ruta "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" ruta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"ruta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" _\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":" 9 _ _ _ 8 _ 3 _ _\n _ _ _ 2 5 _ 7 _ _\n _ 2 _ 3 _ _ _ _ 4\n _ 9 4 _ _ _ _ _ _\n _ _ _ 7 3 _ 5 6 _\n 7 _ 5 _ 6 _ 4 _ _\n _ _ 7 8 _ 3 9 _ _\n _ _ 1 _ _ _ _ _ 3\n 3 _ _ _ _ _ _ _ 2"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flera andra spel kan också representeras på liknande sätt: till exempel schack, minröj, sänka skepp och Mastermind. I sudoku fungerar siffor väl för att representera spelets läge, medan för andra spel kan andra metoder vara bättre."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Go","tmcname":"osa05-02_go"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I spelet Go plaerar man turvis svarta och vita stenar på spelbrädet. Den spelare som har flera stenar på brädet vinner spelet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vem_vann(brade: list)"}]},{"type":"text","value":" som får som argument en matris som representerar spelbrädet. Matrisen består av heltalsvärden:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"0: tom ruta"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"1: sten, spelare 1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"2: sten, spelare 2"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelbrädet kan vara av vilken storlek som helst."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" om spelare ett har vunnit, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" om spelare två har vunnit och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" om båda spelarna har lika många stenar på brädet."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Rader korrekt","tmcname":"osa05-03_sudoku_rader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rad_korrekt(sudoku: list, radnummer: int)"}]},{"type":"text","value":" som får som argument en matris samt ett heltal. Radnumreringen börjar med noll. Funktionen ska returnera ett värde som beskriver om raden är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Kolumner korrekt","tmcname":"osa05-04_sudoku_kolumner"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kolumn_korrekt(sudoku: list, kolumnnummer: int)"}]},{"type":"text","value":" som får som argument en matris samt ett heltal. Funktionen ska returnera ett värde som beskriver om kolumnen är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kolumn_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kolumn_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Kvadrater korrekt","tmcname":"osa05-05_sudoku_kvadrater"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kvadrat_korrekt(sudoku: list, radnummer: int, kolumnnummer: int)"}]},{"type":"text","value":" som får som argument en matris samt två heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska berätta om 3 x 3 -kvadraten vid positionen (radnummer, kolumnnummer) är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är värt att notera att i man vanligtvis i sudoku enbart ser på kvadraterna vid positionerna (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) och (6, 6). Den här funktionen kommer dock inte att beakta det här."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kvadraten vid (0, 0) i det första funktionsanropet:"}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"9 0 0\n2 0 0\n0 2 0\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kvadraten vid (1, 2) i det andra funktionsanropet:"}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"0 2 5\n0 3 0\n4 0 0\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I ett riktigt sudokuspel skulle den här kvadraten inte alltså kollas."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Korrekt?","tmcname":"osa05-06_sudoku_korrekt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sudoku_korrekt(sudoku: list)"}]},{"type":"text","value":" som får som argument en matris. Den här funktionen ska använda sig av de tre föregående uppgifternas funktioner (kopiera dem) för att säkerställa att varje rad, kolumn och 3 x 3 -kvadrat innehåller högst en av siffrorna 1-9."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De 3 x 3 -kvadrater som ska kontrolleras nämndes ovan. De är alltså vid positionerna (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) och (6, 6)."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsudoku2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa listor med element av olika typer
  • \n
  • vet du hur man kan använda listor för att organisera data
  • \n
  • kan du lagra en matris som en tvådimensionell lista.
  • \n

Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen.

En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida.

Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter.

En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad.

Listor med olika typer av data

I den förra modulen fokuserade vi främst på listor med element av heltalstyp, men listor kan innehålla vilka typer av data som helst. En lista bestående av strängar kunde exempelvis kunna se ut så här:

namn = [\"Maja\", \"Lotta\", \"Pontus\"]\nprint(namn)\nnamn.append(\"Konrad\")\nprint(namn)\n\nprint(\"Antal namn:\", len(namn))\nprint(\"Namnen i alfabetisk ordning:\")\nnamn.sort()\nfor person in namn:\n  print(person)

['Maja', 'Lotta', 'Pontus']\n['Maja', 'Lotta', 'Pontus', 'Konrad']\nAntal namn: 4\nNamnen i alfabetisk ordning:\nKonrad\nLotta\nMaja\nPontus

Flyttal kan också lagras i listor:

matningar = [-2.5, 1.1, 7.5, 14.6, 21.0, 19.2]\n\nfor matning in matningar:\n    print(matning)\n\nmedeltal = sum(matningar) / len(matningar)\n\nprint(\"Medelvärde:\", medeltal)

-2.5\n1.1\n7.5\n14.6\n21.0\n19.2\nMedelvärde: 10.15

Varning: globala variabler inom funktioner

Vi har observerat att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler.

Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra.

Här är ett exempel på en funktion som använder en global variabel \"av misstag\":

def svangd_utskrift(namn: list):\n    # använder av misstag den globala variabeln namnlista\n    i = len(namnlista) - 1\n    while i >= 0:\n        print(namnlista[i])\n        i -= 1\n\n# global variabel\nnamnlista = [\"Antti\", \"Emilia\", \"Erkki\", \"Margaret\"]\nsvangd_utskrift(namnlista)\nprint()\nsvangd_utskrift([\"Louise\", \"Ophelia\", \"Lotta\"])

Margaret\nErkki\nEmilia\nAntti

Margaret\nErkki\nEmilia\nAntti

Även om funktionen anropas korrekt skrivs alltid namnen i den globala variabeln namnlista ut.

All kod som testar funktioner ska skrivas inom ett separat block så att TMC-testen accepterar koden. Föregående exempel ska alltså skrivas så här:

def svangd_utskrift(namn: list):\n    # använder av misstag den globala variabeln namnlista\n    i = len(namnlista) - 1\n    while i >= 0:\n        print(namnlista[i])\n        i -= 1\n\n# kod som testar funktionen placeras här\nif __name__ == \"__main__\":\n    # global variabel\n    namnlista = [\"Antti\", \"Emilia\", \"Erkki\", \"Margaret\"]\n    svangd_utskrift(namnlista)\n    print()\n    svangd_utskrift([\"Louise\", \"Ophelia\", \"Lotta\"])

Nu definieras också den globala variabeln i if-blocket.

TMC-testen körs alltid så att koden inom dessa if-block inte körs. Därför fungerar funktionen inte ens i teorin eftersom variabeln namnlista inte finns då testen körs.

Varning: att skriva över en parameter och returnera för tidigt

Det finns ett par nya källor till buggar som vi borde ta en titt på före vi börjar med övningarna i den här modulen. Låt oss kika på en funktion som berättar om ett heltal hittas i en lista. Båda är definierade som parametrar i funktionen:

def siffra_i_listan(siffror: list, siffra: int):\n    for siffra in siffror:\n        if siffra == siffra:\n            return True\n        else:\n            return False

Den här funktionen verkar alltid returnera värdet True. Orsaken till det är att for-loopen skriver över värdet som är lagrat i parametern siffra. Därför är villkoret i if-satsen alltid sant.

Att ändra på parameterns namn löser problemet:

def siffra_i_listan(siffror: list, siffra_som_soks: int):\n    for siffra in siffror:\n        if siffra == siffra_som_soks:\n            return True\n        else:\n            return False

Nu verkar villkoret i if-satsen vara i bättre skick. Men det finns ett nytt problem, eftersom funktionen inte ännu heller fungerar som den ska. Om vi testar följande, märker vi en bugg:

resultat = siffra_i_listan([1, 2, 3, 4], 3)\nprint(resultat) # False

Problemet här är att funktionen returnerar ett värde för tidigt, utan att kolla igenom alla siffror i listan. Funktionen kollar faktiskt endast det första värdet i listan och returnerar True eller False beroende på dess värde. Vi kan inte veta att en siffra inte finns i listan förrän vi har gått igenom hela listan. Instruktionen return False måste alltså placeras utanför for-loopen:

def siffra_i_listan(siffror: list, siffra_som_soks: int):\n    for siffra in siffror:\n        if siffra == siffra_som_soks:\n            return True\n\n    return False

Låt oss ta en titt på en annan funktion som inte fungerar korrekt:

def siffrorna_olika(siffror: list):\n    # hjälpvariabel där vi lagrar de värden som kollats\n    siffror = []\n    for siffra in siffror:\n        # har siffran redan förekommit?\n        if siffra in siffror:\n            return False\n        siffror.append(siffra)\n\n    return True\n\nresultat = siffrorna_olika([1, 2, 2])\nprint(resultat) # True

Funktionen borde kolla om alla siffor i en lista är olika, men värdet som returneras är alltid True.

I det här fallet skriver funktionen igen över värdet som är lagrat i dess parameter. Funktionen försöker använda variabeln siffra för att lagra de siffror som redan har kontrollerats, men det här skriver över det ursprungliga argumentet. Att namnge hjälpvariabeln på nytt löser vårt problem:

def siffrorna_olika(siffror: list):\n    # hjälpvariabel där de siffror som kollats lagras\n    sedda_siffror = []\n    for siffra in siffror:\n        # har siffran redan förekommit?\n        if siffra in sedda_siffror:\n            return False\n        sedda_siffror.append(siffra)\n\n    return True\n\nresultat = siffrorna_olika([1, 2, 2])\nprint(resultat) # False

Problem som dessa kan hittas och korrigeras med hjälp av debuggaren eller visualiseringsverktyget. Det lönar sig verkligen att lära sig använda dessa effektivt.

HUOM: tämä ja seuraava tehtävä ovat väärässä järjestyksessä VS Coden sivupalkissa

Skapa funktionen langst(strangar: list) som får som argument en lista med strängar. Funktionen ska hitta och returnera den längsta strängen. Du kan anta att det bara finns en längsta sträng.

Exempel:

if __name__ == \"__main__\":\n    lista = [\"hej\", \"hejsan\", \"morjens\", \"tjolahoppsan\", \"tjenare\"]\n    print(langst(lista))

tjolahoppsan

Listor inom listor

Ett element i en lista kan vara en lista:

lista = [[5, 2, 3], [4, 1], [2, 2, 5, 1]]\nprint(lista)\nprint(lista[1])\nprint(lista[1][0])

[[5, 2, 3], [4, 1], [2, 2, 5, 1]]\n[4, 1]\n4

I hurdana fall skulle det här kunna vara nyttigt?

Kom ihåg att listor kan innehålla element av olika typer. Du kan till exempel lagra information om en person i en lista. Det första elementet kan till exempel vara personens namn, det andra dess ålder och det tredje dess höjd:

[\"Nelly\", 10, 26]

En databas bestående av flera personer skulle då kunna vara en lista, vars element skulle vara listor som innehåller information om en person:

personer = [[\"Nelly\", 10, 26], [\"PO\", 7, 22], [\"Emilia\", 32, 37], [\"August\", 39, 44]]\n\nfor person in personer:\n  namn = person[0]\n  alder = person[1]\n  sko = person[2]\n  print(f\"{namn}: {alder} år, skostorlek {sko}\")

Nelly: 10 år, skostorlek 26\nPO: 7 år, skostorlek 22\nEmilia: 32 år, skostorlek 37\nAugust: 39 år, skostorlek 44

For-loopen går igenom elementen i den yttre listan en för en. Varje lista som innehåller info om en person tilldelas en för en till variabeln person.

Listor är inte alltid det bästa sättet att presentera data som information om en person. Vi kommer snart att se på lexikon (dictionary) i Python. Den är bättre anpassad för situationer som den ovan nämnda.

Matriser

En tvådimensionell tabell – matris – är ett annat användningsområde för listor inom listor.

Till exempel följande matris…

\n \n \n \n \n \n \"5\n \n \n

…kan presenteras som en tvådimensionell lista i Python på följande sätt:

matris = [[1, 2, 3], [3, 2, 1], [4, 5, 6]]

Eftersom en matris är en lista som innehåller listor kan enskilda element inom matrisen kommas åt med hjälp av varandra påföljande hakparenteser. Det första indexet hänvisar till raden, medan den andra hänvisar till kolumnen. Indexeringen startar från noll så matris[0][1] hänvisar till det andra elementet på den första raden.

matris = [[1, 2, 3], [3, 2, 1], [4, 5, 6]]\n\nprint(matris[0][1])\nmatris[1][0] = 10\nprint(matris)

2\n[[1, 2, 3], [10, 2, 1], [4, 5, 6]]

Som med vilken som helst lista, kan raderna i matrisen gås igenom med en for-loop. Den följande koden skriver ut varje rad i matrisen på en skild rad:

matris = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\nfor rad in matris:\n    print(rad)

[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]

På samma sätt kan kapslade loopar användas för att komma åt enskilda element inom matrisen. Följande kodsnutten skriver ut varje element i matrisen på en skild rad med hjälp av två for-loopar:

matris = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\nfor rad in matris:\n    print(\"ny rad\")\n    for element in rad:\n        print(element)

ny rad\n1\n2\n3\nny rad\n4\n5\n6\nny rad\n7\n8\n9

Visualisering av kod som innehåller listor inom listor

Program som består av listor inom listor kan vara svåra att greppa om i början. Visualiseringsverktyget av Python Tutor är ett bra sätt att bekanta sig med hur de fungerar. Det följande är en visualisering av exemplet ovan:

\n \n \n \n \n \n \"5\n \n \n

Bilden ovan avslöjar att en 3 x 3 -matris tekniskt sett består av fyra listor. Den första listan representerar hela matrisen. De tre resterande listorna är element i den första listan, och de representerar rader i matrisen.

Eftersom multidimensionella listor kan gås igenom med kapslade loopar skulle man kunna tänka sig att listorna är inuti varandra, men som bilden visar är detta inte sant. Istället hänvisar den \"stora listan\" som representerar matrisen till skilda listor som alla representerar en rad i matrisen. Det här är en referens – något vi kommer att se mera på i följande del.

I bilden ovan har programmet hunnit till den andra raden i matrisen och det är den listan som variabeln rad hänvisar till för tillfället. Variabeln element innehåller det element där programmet är vid för tillfället. Värdet i den variabeln är det mellersta värdet i listan, dvs. 5.

Komma åt element i en matris

Att komma åt en viss rad i en matris är enkelt. Det är bara att välja den raden. Följande funktion räknar summan av elementen på en viss rad:

def summan_av_radens_element(matris, radnummer: int):\n    # vi inspekterar bara en rad\n    rad = matris[radnummer]\n    summa = 0\n    for element in rad:\n        summa += element\n\n    return summa\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nsumma = summan_av_radens_element(m, 1)\nprint(summa) # 33 (dvs. 9 + 1 + 12 + 11)

Att arbeta med kolumner i en matris är lite mera komplicerat eftersom matrisen är lagrad som rader:

def summan_av_kolumnens_element(matris, kolumnnummer: int):\n    # till summan adderas för varje rad elementet på det önskade stället\n    summa = 0\n    for rad in matris:\n        summa += rad[kolumnnummer]\n\n    return summa\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nsumma = summan_av_kolumnens_element(m, 2)\nprint(summa) # 39 (dvs. 3 + 12 + 9 + 15)

Kolumnen som söks här består av elementen vid index 2 på varje rad.

Visualiseringsverktyget rekommenderas varmt för att bättre förstå hur de här funktionerna fungerar.

Att ändra på ett värde hos ett specifikt element inom en matris är enkelt. Välj en rad i matrisen och sedan en kolumn inom raden:

def byt_varde(matris, radnummer: int, kolumnnummer: int, varde: int):\n    # välj korrekt rad\n    rad = matris[radnummer]\n    # och korrekt ställe i raden\n    rad[kolumnnummer] = varde\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nprint(m)\nbyt_varde(m, 2, 3, 1000)\nprint(m)

[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 1000], [2, 9, 15, 1]]

Observera att vi ovan använde indexen för raden och kolumnen för att komma åt det element vi ville ändra på. Om vi vill ändra på innehållet i en matris måste vi komma åt elementen md hjälp av deras index. Vi kan inte bara använda oss av en for element in lista -loop för att gå igenom matrisen då vi vill ändra på innehållet i en matris.

Istället behöver vi hålla reda på indexen hos elementen: till exempel med en while-loop eller en for-loop med range-funktionen. Följande kod ökar på värdet hos varje element i en matris med ett:

m = [[1,2,3], [4,5,6], [7,8,9]]\n\nfor i in range(len(m)):\n    for j in range(len(m[i])):\n        m[i][j] += 1\n\nprint(m)

[[2, 3, 4], [5, 6, 7], [8, 9, 10]]

Den yttre loopen går igenom indexen från noll till matrisens längd, alltså antalet rader i matrisen. Den inre loopen går igenom indexen från noll till radernas längd.

HUOM: tämä ja edellinen tehtävä ovat väärässä järjestyksessä VS Coden sivupalkissa

Skapa funktionen rakna_element(matris: list, element: int) som tar emot en matris samt ett heltal som argument. Funktionen ska returnera antalet gånger det valda elementet hittas i matrisen.

Exempel:

m = [[1, 2, 1], [0, 3, 4], [1, 0, 0]]\nprint(rakna_element(m, 1))

3

En tvådimensionell tabell som datastruktur i ett spel

En matris kan fungera väl som datastruktur i flera olika spel. Till exempel rutorna i ett sudokuspel…

\n \n \n \n \n \n \"5\n \n \n

…kan representeras i en matris på följande sätt:

sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [0, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [0, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]

Nu representerar noll en tom ruta, eftersom noll inte är ett värde som kan användas i sudoku.

Här är en enkel funktion som skriver ut sudokurutor:

def skriv_ut(sudoku):\n    for rad in sudoku:\n        for ruta in rad:\n            if ruta > 0:\n                print(f\" {ruta}\", end=\"\")\n            else:\n                print(\" _\", end=\"\")\n        print()\n\nskriv_ut(sudoku)

Utskriften borde se ut så här:

 9 _ _ _ 8 _ 3 _ _\n _ _ _ 2 5 _ 7 _ _\n _ 2 _ 3 _ _ _ _ 4\n _ 9 4 _ _ _ _ _ _\n _ _ _ 7 3 _ 5 6 _\n 7 _ 5 _ 6 _ 4 _ _\n _ _ 7 8 _ 3 9 _ _\n _ _ 1 _ _ _ _ _ 3\n 3 _ _ _ _ _ _ _ 2

Flera andra spel kan också representeras på liknande sätt: till exempel schack, minröj, sänka skepp och Mastermind. I sudoku fungerar siffor väl för att representera spelets läge, medan för andra spel kan andra metoder vara bättre.

I spelet Go plaerar man turvis svarta och vita stenar på spelbrädet. Den spelare som har flera stenar på brädet vinner spelet.

Skapa funktionen vem_vann(brade: list) som får som argument en matris som representerar spelbrädet. Matrisen består av heltalsvärden:

    \n
  • 0: tom ruta
  • \n
  • 1: sten, spelare 1
  • \n
  • 2: sten, spelare 2
  • \n

Spelbrädet kan vara av vilken storlek som helst.

Funktionen ska returnera 1 om spelare ett har vunnit, 2 om spelare två har vunnit och 0 om båda spelarna har lika många stenar på brädet.

Skapa funktionen rad_korrekt(sudoku: list, radnummer: int) som får som argument en matris samt ett heltal. Radnumreringen börjar med noll. Funktionen ska returnera ett värde som beskriver om raden är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång.

sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(rad_korrekt(sudoku, 0))\nprint(rad_korrekt(sudoku, 1))

True\nFalse

Skapa funktionen kolumn_korrekt(sudoku: list, kolumnnummer: int) som får som argument en matris samt ett heltal. Funktionen ska returnera ett värde som beskriver om kolumnen är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång.

sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(kolumn_korrekt(sudoku, 0))\nprint(kolumn_korrekt(sudoku, 1))

False\nTrue

Skapa funktionen kvadrat_korrekt(sudoku: list, radnummer: int, kolumnnummer: int) som får som argument en matris samt två heltal.

Funktionen ska berätta om 3 x 3 -kvadraten vid positionen (radnummer, kolumnnummer) är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång.

Det är värt att notera att i man vanligtvis i sudoku enbart ser på kvadraterna vid positionerna (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) och (6, 6). Den här funktionen kommer dock inte att beakta det här.

sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(kvadrat_korrekt(sudoku, 0, 0))\nprint(kvadrat_korrekt(sudoku, 1, 2))

False\nTrue

Kvadraten vid (0, 0) i det första funktionsanropet:

\n9 0 0\n2 0 0\n0 2 0\n

Kvadraten vid (1, 2) i det andra funktionsanropet:

\n0 2 5\n0 3 0\n4 0 0\n

I ett riktigt sudokuspel skulle den här kvadraten inte alltså kollas.

Skapa funktionen sudoku_korrekt(sudoku: list) som får som argument en matris. Den här funktionen ska använda sig av de tre föregående uppgifternas funktioner (kopiera dem) för att säkerställa att varje rad, kolumn och 3 x 3 -kvadrat innehåller högst en av siffrorna 1-9.

De 3 x 3 -kvadrater som ska kontrolleras nämndes ovan. De är alltså vid positionerna (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) och (6, 6).

sudoku1 = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(sudoku_korrekt(sudoku1))\n\nsudoku2 = [\n  [2, 6, 7, 8, 3, 9, 5, 0, 4],\n  [9, 0, 3, 5, 1, 0, 6, 0, 0],\n  [0, 5, 1, 6, 0, 0, 8, 3, 9],\n  [5, 1, 9, 0, 4, 6, 3, 2, 8],\n  [8, 0, 2, 1, 0, 5, 7, 0, 6],\n  [6, 7, 4, 3, 2, 0, 0, 0, 5],\n  [0, 0, 0, 4, 5, 7, 2, 6, 3],\n  [3, 2, 0, 0, 8, 0, 0, 5, 7],\n  [7, 4, 5, 0, 0, 3, 9, 0, 1]\n]\n\nprint(sudoku_korrekt(sudoku2))

False\nTrue

","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/1-mer-om-listor.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa listor med element av olika typer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man kan använda listor för att organisera data"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du lagra en matris som en tvådimensionell lista."}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"vastaava teksti löytyy osioista 3-1, 5-1 ja 6-1, tsekkaa kaikki jos muokkaat tätä"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Om uppgifterna i den här kursen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad."}]}]},{"type":"element","tagName":"h2","properties":{"id":"listor-med-olika-typer-av-data","style":"position:relative;"},"children":[{"type":"text","value":"Listor med olika typer av data"},{"type":"element","tagName":"a","properties":{"href":"#listor-med-olika-typer-av-data","ariaLabel":"listor med olika typer av data permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den förra modulen fokuserade vi främst på listor med element av heltalstyp, men listor kan innehålla vilka typer av data som helst. En lista bestående av strängar kunde exempelvis kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pontus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Konrad\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antal namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namnen i alfabetisk ordning:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['Maja', 'Lotta', 'Pontus']\n['Maja', 'Lotta', 'Pontus', 'Konrad']\nAntal namn: 4\nNamnen i alfabetisk ordning:\nKonrad\nLotta\nMaja\nPontus"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flyttal kan också lagras i listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1.1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14.6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"21.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19.2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" matning "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" matningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmedeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medelvärde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"-2.5\n1.1\n7.5\n14.6\n21.0\n19.2\nMedelvärde: 10.15"}]}]},{"type":"comment","value":"vastaava varoitusteksti löytyy osioista 3-4, 4-6 ja 5-1, tsekkaa kaikki jos muokkaat tätä"},{"type":"element","tagName":"h2","properties":{"id":"varning-globala-variabler-inom-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Varning: globala variabler inom funktioner"},{"type":"element","tagName":"a","properties":{"href":"#varning-globala-variabler-inom-funktioner","ariaLabel":"varning globala variabler inom funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har observerat att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel på en funktion som använder en global variabel \"av misstag\":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"svangd_utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använder av misstag den globala variabeln namnlista"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\nnamnlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erkki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Margaret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsvangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsvangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louise\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ophelia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Margaret\nErkki\nEmilia\nAntti"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Margaret\nErkki\nEmilia\nAntti"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om funktionen anropas korrekt skrivs alltid namnen i den globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnlista"}]},{"type":"text","value":" ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All kod som testar funktioner ska skrivas inom ett separat block så att TMC-testen accepterar koden. Föregående exempel ska alltså skrivas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"svangd_utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# använder av misstag den globala variabeln namnlista"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod som testar funktionen placeras här"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# global variabel"}]},{"type":"text","value":"\n namnlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erkki\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Margaret\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n svangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namnlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n svangd_utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Louise\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ophelia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lotta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu definieras också den globala variabeln i if-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TMC-testen körs alltid så att koden inom dessa if-block inte körs. Därför fungerar funktionen inte ens i teorin eftersom variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnlista"}]},{"type":"text","value":" inte finns då testen körs."}]},{"type":"element","tagName":"h2","properties":{"id":"varning-att-skriva-over-en-parameter-och-returnera-for-tidigt","style":"position:relative;"},"children":[{"type":"text","value":"Varning: att skriva över en parameter och returnera för tidigt"},{"type":"element","tagName":"a","properties":{"href":"#varning-att-skriva-over-en-parameter-och-returnera-for-tidigt","ariaLabel":"varning att skriva over en parameter och returnera for tidigt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns ett par nya källor till buggar som vi borde ta en titt på före vi börjar med övningarna i den här modulen. Låt oss kika på en funktion som berättar om ett heltal hittas i en lista. Båda är definierade som parametrar i funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffra_i_listan"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här funktionen verkar alltid returnera värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". Orsaken till det är att for-loopen skriver över värdet som är lagrat i parametern "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":". Därför är villkoret i if-satsen alltid sant."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att ändra på parameterns namn löser problemet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffra_i_listan"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra_som_soks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" siffra_som_soks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu verkar villkoret i if-satsen vara i bättre skick. Men det finns ett nytt problem, eftersom funktionen inte ännu heller fungerar som den ska. Om vi testar följande, märker vi en bugg:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffra_i_listan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problemet här är att funktionen returnerar ett värde för tidigt, utan att kolla igenom alla siffror i listan. Funktionen kollar faktiskt endast det första värdet i listan och returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" beroende på dess värde. Vi kan inte veta att en siffra inte finns i listan förrän vi har gått igenom hela listan. Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return False"}]},{"type":"text","value":" måste alltså placeras utanför for-loopen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffra_i_listan"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra_som_soks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" siffra_som_soks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en annan funktion som inte fungerar korrekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffrorna_olika"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpvariabel där vi lagrar de värden som kollats"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# har siffran redan förekommit?"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffrorna_olika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen borde kolla om alla siffor i en lista är olika, men värdet som returneras är alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet skriver funktionen igen över värdet som är lagrat i dess parameter. Funktionen försöker använda variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" för att lagra de siffror som redan har kontrollerats, men det här skriver över det ursprungliga argumentet. Att namnge hjälpvariabeln på nytt löser vårt problem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"siffrorna_olika"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpvariabel där de siffror som kollats lagras"}]},{"type":"text","value":"\n sedda_siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# har siffran redan förekommit?"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sedda_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n sedda_siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffrorna_olika"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problem som dessa kan hittas och korrigeras med hjälp av debuggaren eller "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyget"}]},{"type":"text","value":". Det lönar sig verkligen att lära sig använda dessa effektivt."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Längsta strängen","tmcname":"osa05-01a_langsta_strangen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"HUOM:"}]},{"type":"text","value":" tämä ja seuraava tehtävä ovat väärässä järjestyksessä VS Coden sivupalkissa"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langst(strangar: list)"}]},{"type":"text","value":" som får som argument en lista med strängar. Funktionen ska hitta och returnera den längsta strängen. Du kan anta att det bara finns en längsta sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hejsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"morjens\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tjolahoppsan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tjenare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"langst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"tjolahoppsan"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"listor-inom-listor","style":"position:relative;"},"children":[{"type":"text","value":"Listor inom listor"},{"type":"element","tagName":"a","properties":{"href":"#listor-inom-listor","ariaLabel":"listor inom listor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett element i en lista kan vara en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[5, 2, 3], [4, 1], [2, 2, 5, 1]]\n[4, 1]\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I hurdana fall skulle det här kunna vara nyttigt?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att listor kan innehålla element av olika typer. Du kan till exempel lagra information om en person i en lista. Det första elementet kan till exempel vara personens namn, det andra dess ålder och det tredje dess höjd:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nelly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En databas bestående av flera personer skulle då kunna vara en lista, vars element skulle vara listor som innehåller information om en person:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nelly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PO\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"37"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"August\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"39"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n sko "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"alder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" år, skostorlek "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"sko"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nelly: 10 år, skostorlek 26\nPO: 7 år, skostorlek 22\nEmilia: 32 år, skostorlek 37\nAugust: 39 år, skostorlek 44"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For-loopen går igenom elementen i den yttre listan en för en. Varje lista som innehåller info om en person tilldelas en för en till variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listor är inte alltid det bästa sättet att presentera data som information om en person. Vi kommer snart att se på lexikon (dictionary) i Python. Den är bättre anpassad för situationer som den ovan nämnda."}]},{"type":"element","tagName":"h2","properties":{"id":"matriser","style":"position:relative;"},"children":[{"type":"text","value":"Matriser"},{"type":"element","tagName":"a","properties":{"href":"#matriser","ariaLabel":"matriser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En tvådimensionell tabell – matris – är ett annat användningsområde för listor inom listor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel följande matris…"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 151px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_0.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAABw0lEQVQ4y62UyYoCQQyG+3EF38YHUPHoyYsLeFLw4smDO654UVBxAXfN8AVS1LSt4zATCJ1OUn+l8qcqKBQKMhwOpd1uq3Y6Hen3+1Iul6XZbEq323UxbHzEyCHX1pgGgEXJbDaTV/IuFrADst/vpdFouMBgMJDT6aR2r9fTyhB8xJDH4yH1el02m43+3+93CSgT2W63kkwmHeBoNHKAuVxOstmsA/RPlUgktBVPgPzsdjv9Iiw6n89qT6dTmc/nauMzwNvtpjHLo+KA5kbJZDJx4L7gI/ayhzBGk+kLO6MsqFQqyuR4PHYxbHzEyMFnMVqEBtZsyr5cLqpUwcLD4SDX61UVP198xMixfF8Dv6G+UA0AYcFHJS+PbKSs12tJpVIOxCelWq1KsViMZJkJYK0jxeZwtVpJOp2W4/HoAG1s8vm8lEqlSED6+Q3wt0dmVN4eOUwK3z+R8snYmP+jsfn3wTaW7T7TozDLy+VSSQtfPUhYLBZ61CeWuZPxeNyB+CzDfiaTeWKZFyoWi2krIl+bWq3mdvMr5PlqtVpPFQLAjNrzpRX6RzanPV8/3RTL9e1vzxf98wGtWmJGED4f0F9DhV9fOel+Sn/2BgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/57e8504bd70c2538405be5b5e1572347/8d139/5_1_0.webp 151w"],"sizes":"(max-width: 151px) 100vw, 151px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_0.png 151w"],"sizes":"(max-width: 151px) 100vw, 151px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_0.png","alt":"5 1 0","title":"5 1 0","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…kan presenteras som en tvådimensionell lista i Python på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom en matris är en lista som innehåller listor kan enskilda element inom matrisen kommas åt med hjälp av varandra påföljande hakparenteser. Det första indexet hänvisar till raden, medan den andra hänvisar till kolumnen. Indexeringen startar från noll så "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matris[0][1]"}]},{"type":"text","value":" hänvisar till det andra elementet på den första raden."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmatris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n[[1, 2, 3], [10, 2, 1], [4, 5, 6]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som med vilken som helst lista, kan raderna i matrisen gås igenom med en for-loop. Den följande koden skriver ut varje rad i matrisen på en skild rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt kan kapslade loopar användas för att komma åt enskilda element inom matrisen. Följande kodsnutten skriver ut varje element i matrisen på en skild rad med hjälp av två for-loopar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny rad\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ny rad\n1\n2\n3\nny rad\n4\n5\n6\nny rad\n7\n8\n9"}]}]},{"type":"element","tagName":"h2","properties":{"id":"visualisering-av-kod-som-innehaller-listor-inom-listor","style":"position:relative;"},"children":[{"type":"text","value":"Visualisering av kod som innehåller listor inom listor"},{"type":"element","tagName":"a","properties":{"href":"#visualisering-av-kod-som-innehaller-listor-inom-listor","ariaLabel":"visualisering av kod som innehaller listor inom listor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Program som består av listor inom listor kan vara svåra att greppa om i början. "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" av Python Tutor är ett bra sätt att bekanta sig med hur de fungerar. Det följande är en visualisering av exemplet ovan:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9450893ea3ce24ad2a607cde889d3e70/769f8/5_1_0a.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 42.173913043478265%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAABFElEQVQY03WQ207DMAyG+/7PtgtuBhMSDG1rx+jWJG0OtuMDamCIm32yLCv6fzt2Z2Yigoi10rzkY38dLq4UICIAUFUiSikRESJSQ0S00ZkZiyzLkn7JeNcBQOtLYZ6Z2RrNJSLMzKu5QiHEvu8Ph8P5PPzp/qm1lCzCpaxfbI9rrGYhNDMACGEZrz6mdWBrLmZGxK/78+6tB5IY/ZKOGW4IvUjoYnDOB+cc16pqBRhJ7jPXjMQv+6/N9iOC5jJdbk9uPqX0DHDqatsPEd3K1MJN05RS+lmOKvuIV5dYxNQy8Od1C2Wnpl3MtOSaYslz8CEMwzCOY4yRiOwBfh5ifDeDjkVZVETtMdrOdi+tgPd+Q3T8Bqyl0FSTvoR1AAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9450893ea3ce24ad2a607cde889d3e70/a0b58/5_1_0a.webp 230w","/static/9450893ea3ce24ad2a607cde889d3e70/bc10c/5_1_0a.webp 460w","/static/9450893ea3ce24ad2a607cde889d3e70/966d8/5_1_0a.webp 920w","/static/9450893ea3ce24ad2a607cde889d3e70/445df/5_1_0a.webp 1380w","/static/9450893ea3ce24ad2a607cde889d3e70/78de1/5_1_0a.webp 1840w","/static/9450893ea3ce24ad2a607cde889d3e70/7e82b/5_1_0a.webp 1924w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9450893ea3ce24ad2a607cde889d3e70/81c8e/5_1_0a.png 230w","/static/9450893ea3ce24ad2a607cde889d3e70/08a84/5_1_0a.png 460w","/static/9450893ea3ce24ad2a607cde889d3e70/c0255/5_1_0a.png 920w","/static/9450893ea3ce24ad2a607cde889d3e70/b1001/5_1_0a.png 1380w","/static/9450893ea3ce24ad2a607cde889d3e70/161ec/5_1_0a.png 1840w","/static/9450893ea3ce24ad2a607cde889d3e70/769f8/5_1_0a.png 1924w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9450893ea3ce24ad2a607cde889d3e70/c0255/5_1_0a.png","alt":"5 1 0a","title":"5 1 0a","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bilden ovan avslöjar att en 3 x 3 -matris tekniskt sett består av fyra listor. Den första listan representerar hela matrisen. De tre resterande listorna är element i den första listan, och de representerar rader i matrisen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom multidimensionella listor kan gås igenom med kapslade loopar skulle man kunna tänka sig att listorna är inuti varandra, men som bilden visar är detta inte sant. Istället hänvisar den \"stora listan\" som representerar matrisen till skilda listor som alla representerar en rad i matrisen. Det här är en referens – något vi kommer att se mera på i följande del."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I bilden ovan har programmet hunnit till den andra raden i matrisen och det är den listan som variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rad"}]},{"type":"text","value":" hänvisar till för tillfället. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"element"}]},{"type":"text","value":" innehåller det element där programmet är vid för tillfället. Värdet i den variabeln är det mellersta värdet i listan, dvs. 5."}]},{"type":"element","tagName":"h2","properties":{"id":"komma-at-element-i-en-matris","style":"position:relative;"},"children":[{"type":"text","value":"Komma åt element i en matris"},{"type":"element","tagName":"a","properties":{"href":"#komma-at-element-i-en-matris","ariaLabel":"komma at element i en matris permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att komma åt en viss rad i en matris är enkelt. Det är bara att välja den raden. Följande funktion räknar summan av elementen på en viss rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summan_av_radens_element"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" radnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi inspekterar bara en rad"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"radnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" element\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" summa\n\nm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summan_av_radens_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 33 (dvs. 9 + 1 + 12 + 11)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att arbeta med kolumner i en matris är lite mera komplicerat eftersom matrisen är lagrad som rader:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"summan_av_kolumnens_element"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kolumnnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# till summan adderas för varje rad elementet på det önskade stället"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"kolumnnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" summa\n\nm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" summan_av_kolumnens_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 39 (dvs. 3 + 12 + 9 + 15)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kolumnen som söks här består av elementen vid index 2 på varje rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" rekommenderas varmt för att bättre förstå hur de här funktionerna fungerar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att ändra på ett värde hos ett specifikt element inom en matris är enkelt. Välj en rad i matrisen och sedan en kolumn inom raden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"byt_varde"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" radnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kolumnnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# välj korrekt rad"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"radnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# och korrekt ställe i raden"}]},{"type":"text","value":"\n rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"kolumnnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" varde\n\nm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbyt_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 1000], [2, 9, 15, 1]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att vi ovan använde indexen för raden och kolumnen för att komma åt det element vi ville ändra på. Om vi vill ändra på innehållet i en matris måste vi komma åt elementen md hjälp av deras index. Vi kan inte bara använda oss av en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for element in lista"}]},{"type":"text","value":" -loop för att gå igenom matrisen då vi vill ändra på innehållet i en matris."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Istället behöver vi hålla reda på indexen hos elementen: till exempel med en while-loop eller en for-loop med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-funktionen. Följande kod ökar på värdet hos varje element i en matris med ett:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"m "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" j "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"j"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[2, 3, 4], [5, 6, 7], [8, 9, 10]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den yttre loopen går igenom indexen från noll till matrisens längd, alltså antalet rader i matrisen. Den inre loopen går igenom indexen från noll till radernas längd."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Antalet element","tmcname":"osa05-01_antalet_element"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"HUOM:"}]},{"type":"text","value":" tämä ja edellinen tehtävä ovat väärässä järjestyksessä VS Coden sivupalkissa"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rakna_element(matris: list, element: int)"}]},{"type":"text","value":" som tar emot en matris samt ett heltal som argument. Funktionen ska returnera antalet gånger det valda elementet hittas i matrisen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"m "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-tvadimensionell-tabell-som-datastruktur-i-ett-spel","style":"position:relative;"},"children":[{"type":"text","value":"En tvådimensionell tabell som datastruktur i ett spel"},{"type":"element","tagName":"a","properties":{"href":"#en-tvadimensionell-tabell-som-datastruktur-i-ett-spel","ariaLabel":"en tvadimensionell tabell som datastruktur i ett spel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En matris kan fungera väl som datastruktur i flera olika spel. Till exempel rutorna i ett sudokuspel…"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 410px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 100.43478260869566%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAABYlAAAWJQFJUiTwAAADmUlEQVQ4y0WUWXOqPACG/fX9B163vTg67dQuap2OYBfbutQFRSmQoDGQQBAQqsXd5hvLmfO915l5kndJStM0XdcBAJ7nfX5+uq5bKpUIIQghTdMAAISQfr//8vJCCNE0zbZtVVWn0ylCKAV+5bou59w0zcPhUCgUVqtVFEWKomCMV6uVYRjPz8/T6dS27c1mY1kW55wQktJ13XGc4XAYBIEsy9Pp9OLiAmMMITQMAwBgWVar1SqXy8kxx3EGg4Hv+xDClOu6+/3e933OOWOMcy6KIsY4DEP7V7vdbjKZNJvN+Xzued5+v0+OMcZSyd0kSbJtu9PpUEr//PkDIVRVFQCgqup4PK7Vag8PD4wxSZJM05QkiTGmadqRfDgcHMfxPI9SyjkXBIEQ4nnecrn8+vpar9fj8ViSpIR2OBwopaZpHt98f39fLBaz2WyhUMhkMqIonpyc5HK5q6ur29vb6+vrQqFwdnaWTqcFQchms6VSKZvN3t3dvby8pCilm80GITSZTAghEMJ8Pr/ZbGazWRRFQRB8f38DAFqtVuLwer3+322EUBiGSVoAANM0c7lcGIaEEMYYpdTzPFmWX19fk8zm8zmEcLlcHnPGGEdRhBDabreGYaxWq3w+DyG0LAv+KgxDRVFEUXRdV1VVCCFCaL1eY4z/kiGEcRxrmrZYLG5ubizLIoQYv/J9X5blSqUym80Gg0GSfxzHRzJCiFJq2/bhcMAYu66bz+fjOA6CACFECFksFrquV6vVIAgSgzDGPz8/lmWl2u12t9vtdDoY40ajIUnS6enpaDRSFKVSqdTrdcMw3t7eLi8vW61Wu92GEH58fGCMFUVJ1Wq1drstiqIsy+VyuVqtptPpcrn89PTUaDRqtdrj42Mulzs/P6/X66IottttQRAkSep0On/rGQQB59x13SiKyuVyUs/lchnHMWNsOBy+v7/PZjPf9//V03XdlKqqpmkmk5AkiVKayWRs206M0XXdNM1msykIQhAEvV6PECJJ0mQyAQCkHMfZbreO43DOKaWMsWKxuFqtwjCcz+dhGMZxbBhGUhJK6W63M03TdV1K6ZGMMR4Oh57n9Xo927az2WwS8mg0AgBgjBPydDqVZZlS2u/3Hcc5khFClmWFYcg5t22bc14qlXa73WKxiOM4GcZoNOp2u/+GkczGcZzUeDxOps8YSz6GarVqWVbSdoSQaZqKonQ6HcYYAIBSqut60qL/AD7hB6pgN21XAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a4d938ae5202e8db72b179ff4d76293/a0b58/5_1_1.webp 230w","/static/5a4d938ae5202e8db72b179ff4d76293/0cb6c/5_1_1.webp 410w"],"sizes":"(max-width: 410px) 100vw, 410px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a4d938ae5202e8db72b179ff4d76293/81c8e/5_1_1.png 230w","/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_1.png 410w"],"sizes":"(max-width: 410px) 100vw, 410px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_1.png","alt":"5 1 1","title":"5 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…kan representeras i en matris på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu representerar noll en tom ruta, eftersom noll inte är ett värde som kan användas i sudoku."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är en enkel funktion som skriver ut sudokurutor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" ruta "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" ruta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"ruta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" _\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":" 9 _ _ _ 8 _ 3 _ _\n _ _ _ 2 5 _ 7 _ _\n _ 2 _ 3 _ _ _ _ 4\n _ 9 4 _ _ _ _ _ _\n _ _ _ 7 3 _ 5 6 _\n 7 _ 5 _ 6 _ 4 _ _\n _ _ 7 8 _ 3 9 _ _\n _ _ 1 _ _ _ _ _ 3\n 3 _ _ _ _ _ _ _ 2"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flera andra spel kan också representeras på liknande sätt: till exempel schack, minröj, sänka skepp och Mastermind. I sudoku fungerar siffor väl för att representera spelets läge, medan för andra spel kan andra metoder vara bättre."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Go","tmcname":"osa05-02_go"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I spelet Go plaerar man turvis svarta och vita stenar på spelbrädet. Den spelare som har flera stenar på brädet vinner spelet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vem_vann(brade: list)"}]},{"type":"text","value":" som får som argument en matris som representerar spelbrädet. Matrisen består av heltalsvärden:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"0: tom ruta"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"1: sten, spelare 1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"2: sten, spelare 2"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelbrädet kan vara av vilken storlek som helst."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" om spelare ett har vunnit, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" om spelare två har vunnit och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" om båda spelarna har lika många stenar på brädet."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Rader korrekt","tmcname":"osa05-03_sudoku_rader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rad_korrekt(sudoku: list, radnummer: int)"}]},{"type":"text","value":" som får som argument en matris samt ett heltal. Radnumreringen börjar med noll. Funktionen ska returnera ett värde som beskriver om raden är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Kolumner korrekt","tmcname":"osa05-04_sudoku_kolumner"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kolumn_korrekt(sudoku: list, kolumnnummer: int)"}]},{"type":"text","value":" som får som argument en matris samt ett heltal. Funktionen ska returnera ett värde som beskriver om kolumnen är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kolumn_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kolumn_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Kvadrater korrekt","tmcname":"osa05-05_sudoku_kvadrater"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kvadrat_korrekt(sudoku: list, radnummer: int, kolumnnummer: int)"}]},{"type":"text","value":" som får som argument en matris samt två heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska berätta om 3 x 3 -kvadraten vid positionen (radnummer, kolumnnummer) är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är värt att notera att i man vanligtvis i sudoku enbart ser på kvadraterna vid positionerna (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) och (6, 6). Den här funktionen kommer dock inte att beakta det här."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kvadrat_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kvadraten vid (0, 0) i det första funktionsanropet:"}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"9 0 0\n2 0 0\n0 2 0\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kvadraten vid (1, 2) i det andra funktionsanropet:"}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"0 2 5\n0 3 0\n4 0 0\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I ett riktigt sudokuspel skulle den här kvadraten inte alltså kollas."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Korrekt?","tmcname":"osa05-06_sudoku_korrekt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sudoku_korrekt(sudoku: list)"}]},{"type":"text","value":" som får som argument en matris. Den här funktionen ska använda sig av de tre föregående uppgifternas funktioner (kopiera dem) för att säkerställa att varje rad, kolumn och 3 x 3 -kvadrat innehåller högst en av siffrorna 1-9."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De 3 x 3 -kvadrater som ska kontrolleras nämndes ovan. De är alltså vid positionerna (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) och (6, 6)."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsudoku2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku_korrekt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa listor med element av olika typer
  • \n
  • vet du hur man kan använda listor för att organisera data
  • \n
  • kan du lagra en matris som en tvådimensionell lista.
  • \n

Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen.

En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida.

Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter.

En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad.

Listor med olika typer av data

I den förra modulen fokuserade vi främst på listor med element av heltalstyp, men listor kan innehålla vilka typer av data som helst. En lista bestående av strängar kunde exempelvis kunna se ut så här:

namn = [\"Maja\", \"Lotta\", \"Pontus\"]\nprint(namn)\nnamn.append(\"Konrad\")\nprint(namn)\n\nprint(\"Antal namn:\", len(namn))\nprint(\"Namnen i alfabetisk ordning:\")\nnamn.sort()\nfor person in namn:\n  print(person)

['Maja', 'Lotta', 'Pontus']\n['Maja', 'Lotta', 'Pontus', 'Konrad']\nAntal namn: 4\nNamnen i alfabetisk ordning:\nKonrad\nLotta\nMaja\nPontus

Flyttal kan också lagras i listor:

matningar = [-2.5, 1.1, 7.5, 14.6, 21.0, 19.2]\n\nfor matning in matningar:\n    print(matning)\n\nmedeltal = sum(matningar) / len(matningar)\n\nprint(\"Medelvärde:\", medeltal)

-2.5\n1.1\n7.5\n14.6\n21.0\n19.2\nMedelvärde: 10.15

Varning: globala variabler inom funktioner

Vi har observerat att det är möjligt att tilldela nya variabler i funktionsdefinitioner. Funktionen kan också se variabler utanför funktionen, i huvudfunktionen. Dessa variabler kallas globala variabler.

Att använda globala variabler från funktioner är oftast en dålig idé. Det kan orsaka en hel del problem, till exempel orsaka buggar som är svåra att spåra.

Här är ett exempel på en funktion som använder en global variabel \"av misstag\":

def svangd_utskrift(namn: list):\n    # använder av misstag den globala variabeln namnlista\n    i = len(namnlista) - 1\n    while i >= 0:\n        print(namnlista[i])\n        i -= 1\n\n# global variabel\nnamnlista = [\"Antti\", \"Emilia\", \"Erkki\", \"Margaret\"]\nsvangd_utskrift(namnlista)\nprint()\nsvangd_utskrift([\"Louise\", \"Ophelia\", \"Lotta\"])

Margaret\nErkki\nEmilia\nAntti

Margaret\nErkki\nEmilia\nAntti

Även om funktionen anropas korrekt skrivs alltid namnen i den globala variabeln namnlista ut.

All kod som testar funktioner ska skrivas inom ett separat block så att TMC-testen accepterar koden. Föregående exempel ska alltså skrivas så här:

def svangd_utskrift(namn: list):\n    # använder av misstag den globala variabeln namnlista\n    i = len(namnlista) - 1\n    while i >= 0:\n        print(namnlista[i])\n        i -= 1\n\n# kod som testar funktionen placeras här\nif __name__ == \"__main__\":\n    # global variabel\n    namnlista = [\"Antti\", \"Emilia\", \"Erkki\", \"Margaret\"]\n    svangd_utskrift(namnlista)\n    print()\n    svangd_utskrift([\"Louise\", \"Ophelia\", \"Lotta\"])

Nu definieras också den globala variabeln i if-blocket.

TMC-testen körs alltid så att koden inom dessa if-block inte körs. Därför fungerar funktionen inte ens i teorin eftersom variabeln namnlista inte finns då testen körs.

Varning: att skriva över en parameter och returnera för tidigt

Det finns ett par nya källor till buggar som vi borde ta en titt på före vi börjar med övningarna i den här modulen. Låt oss kika på en funktion som berättar om ett heltal hittas i en lista. Båda är definierade som parametrar i funktionen:

def siffra_i_listan(siffror: list, siffra: int):\n    for siffra in siffror:\n        if siffra == siffra:\n            return True\n        else:\n            return False

Den här funktionen verkar alltid returnera värdet True. Orsaken till det är att for-loopen skriver över värdet som är lagrat i parametern siffra. Därför är villkoret i if-satsen alltid sant.

Att ändra på parameterns namn löser problemet:

def siffra_i_listan(siffror: list, siffra_som_soks: int):\n    for siffra in siffror:\n        if siffra == siffra_som_soks:\n            return True\n        else:\n            return False

Nu verkar villkoret i if-satsen vara i bättre skick. Men det finns ett nytt problem, eftersom funktionen inte ännu heller fungerar som den ska. Om vi testar följande, märker vi en bugg:

resultat = siffra_i_listan([1, 2, 3, 4], 3)\nprint(resultat) # False

Problemet här är att funktionen returnerar ett värde för tidigt, utan att kolla igenom alla siffror i listan. Funktionen kollar faktiskt endast det första värdet i listan och returnerar True eller False beroende på dess värde. Vi kan inte veta att en siffra inte finns i listan förrän vi har gått igenom hela listan. Instruktionen return False måste alltså placeras utanför for-loopen:

def siffra_i_listan(siffror: list, siffra_som_soks: int):\n    for siffra in siffror:\n        if siffra == siffra_som_soks:\n            return True\n\n    return False

Låt oss ta en titt på en annan funktion som inte fungerar korrekt:

def siffrorna_olika(siffror: list):\n    # hjälpvariabel där vi lagrar de värden som kollats\n    siffror = []\n    for siffra in siffror:\n        # har siffran redan förekommit?\n        if siffra in siffror:\n            return False\n        siffror.append(siffra)\n\n    return True\n\nresultat = siffrorna_olika([1, 2, 2])\nprint(resultat) # True

Funktionen borde kolla om alla siffor i en lista är olika, men värdet som returneras är alltid True.

I det här fallet skriver funktionen igen över värdet som är lagrat i dess parameter. Funktionen försöker använda variabeln siffra för att lagra de siffror som redan har kontrollerats, men det här skriver över det ursprungliga argumentet. Att namnge hjälpvariabeln på nytt löser vårt problem:

def siffrorna_olika(siffror: list):\n    # hjälpvariabel där de siffror som kollats lagras\n    sedda_siffror = []\n    for siffra in siffror:\n        # har siffran redan förekommit?\n        if siffra in sedda_siffror:\n            return False\n        sedda_siffror.append(siffra)\n\n    return True\n\nresultat = siffrorna_olika([1, 2, 2])\nprint(resultat) # False

Problem som dessa kan hittas och korrigeras med hjälp av debuggaren eller visualiseringsverktyget. Det lönar sig verkligen att lära sig använda dessa effektivt.

HUOM: tämä ja seuraava tehtävä ovat väärässä järjestyksessä VS Coden sivupalkissa

Skapa funktionen langst(strangar: list) som får som argument en lista med strängar. Funktionen ska hitta och returnera den längsta strängen. Du kan anta att det bara finns en längsta sträng.

Exempel:

if __name__ == \"__main__\":\n    lista = [\"hej\", \"hejsan\", \"morjens\", \"tjolahoppsan\", \"tjenare\"]\n    print(langst(lista))

tjolahoppsan

Listor inom listor

Ett element i en lista kan vara en lista:

lista = [[5, 2, 3], [4, 1], [2, 2, 5, 1]]\nprint(lista)\nprint(lista[1])\nprint(lista[1][0])

[[5, 2, 3], [4, 1], [2, 2, 5, 1]]\n[4, 1]\n4

I hurdana fall skulle det här kunna vara nyttigt?

Kom ihåg att listor kan innehålla element av olika typer. Du kan till exempel lagra information om en person i en lista. Det första elementet kan till exempel vara personens namn, det andra dess ålder och det tredje dess höjd:

[\"Nelly\", 10, 26]

En databas bestående av flera personer skulle då kunna vara en lista, vars element skulle vara listor som innehåller information om en person:

personer = [[\"Nelly\", 10, 26], [\"PO\", 7, 22], [\"Emilia\", 32, 37], [\"August\", 39, 44]]\n\nfor person in personer:\n  namn = person[0]\n  alder = person[1]\n  sko = person[2]\n  print(f\"{namn}: {alder} år, skostorlek {sko}\")

Nelly: 10 år, skostorlek 26\nPO: 7 år, skostorlek 22\nEmilia: 32 år, skostorlek 37\nAugust: 39 år, skostorlek 44

For-loopen går igenom elementen i den yttre listan en för en. Varje lista som innehåller info om en person tilldelas en för en till variabeln person.

Listor är inte alltid det bästa sättet att presentera data som information om en person. Vi kommer snart att se på lexikon (dictionary) i Python. Den är bättre anpassad för situationer som den ovan nämnda.

Matriser

En tvådimensionell tabell – matris – är ett annat användningsområde för listor inom listor.

Till exempel följande matris…

\n \n \n \n \n \n \"5\n \n \n

…kan presenteras som en tvådimensionell lista i Python på följande sätt:

matris = [[1, 2, 3], [3, 2, 1], [4, 5, 6]]

Eftersom en matris är en lista som innehåller listor kan enskilda element inom matrisen kommas åt med hjälp av varandra påföljande hakparenteser. Det första indexet hänvisar till raden, medan den andra hänvisar till kolumnen. Indexeringen startar från noll så matris[0][1] hänvisar till det andra elementet på den första raden.

matris = [[1, 2, 3], [3, 2, 1], [4, 5, 6]]\n\nprint(matris[0][1])\nmatris[1][0] = 10\nprint(matris)

2\n[[1, 2, 3], [10, 2, 1], [4, 5, 6]]

Som med vilken som helst lista, kan raderna i matrisen gås igenom med en for-loop. Den följande koden skriver ut varje rad i matrisen på en skild rad:

matris = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\nfor rad in matris:\n    print(rad)

[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]

På samma sätt kan kapslade loopar användas för att komma åt enskilda element inom matrisen. Följande kodsnutten skriver ut varje element i matrisen på en skild rad med hjälp av två for-loopar:

matris = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\nfor rad in matris:\n    print(\"ny rad\")\n    for element in rad:\n        print(element)

ny rad\n1\n2\n3\nny rad\n4\n5\n6\nny rad\n7\n8\n9

Visualisering av kod som innehåller listor inom listor

Program som består av listor inom listor kan vara svåra att greppa om i början. Visualiseringsverktyget av Python Tutor är ett bra sätt att bekanta sig med hur de fungerar. Det följande är en visualisering av exemplet ovan:

\n \n \n \n \n \n \"5\n \n \n

Bilden ovan avslöjar att en 3 x 3 -matris tekniskt sett består av fyra listor. Den första listan representerar hela matrisen. De tre resterande listorna är element i den första listan, och de representerar rader i matrisen.

Eftersom multidimensionella listor kan gås igenom med kapslade loopar skulle man kunna tänka sig att listorna är inuti varandra, men som bilden visar är detta inte sant. Istället hänvisar den \"stora listan\" som representerar matrisen till skilda listor som alla representerar en rad i matrisen. Det här är en referens – något vi kommer att se mera på i följande del.

I bilden ovan har programmet hunnit till den andra raden i matrisen och det är den listan som variabeln rad hänvisar till för tillfället. Variabeln element innehåller det element där programmet är vid för tillfället. Värdet i den variabeln är det mellersta värdet i listan, dvs. 5.

Komma åt element i en matris

Att komma åt en viss rad i en matris är enkelt. Det är bara att välja den raden. Följande funktion räknar summan av elementen på en viss rad:

def summan_av_radens_element(matris, radnummer: int):\n    # vi inspekterar bara en rad\n    rad = matris[radnummer]\n    summa = 0\n    for element in rad:\n        summa += element\n\n    return summa\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nsumma = summan_av_radens_element(m, 1)\nprint(summa) # 33 (dvs. 9 + 1 + 12 + 11)

Att arbeta med kolumner i en matris är lite mera komplicerat eftersom matrisen är lagrad som rader:

def summan_av_kolumnens_element(matris, kolumnnummer: int):\n    # till summan adderas för varje rad elementet på det önskade stället\n    summa = 0\n    for rad in matris:\n        summa += rad[kolumnnummer]\n\n    return summa\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nsumma = summan_av_kolumnens_element(m, 2)\nprint(summa) # 39 (dvs. 3 + 12 + 9 + 15)

Kolumnen som söks här består av elementen vid index 2 på varje rad.

Visualiseringsverktyget rekommenderas varmt för att bättre förstå hur de här funktionerna fungerar.

Att ändra på ett värde hos ett specifikt element inom en matris är enkelt. Välj en rad i matrisen och sedan en kolumn inom raden:

def byt_varde(matris, radnummer: int, kolumnnummer: int, varde: int):\n    # välj korrekt rad\n    rad = matris[radnummer]\n    # och korrekt ställe i raden\n    rad[kolumnnummer] = varde\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nprint(m)\nbyt_varde(m, 2, 3, 1000)\nprint(m)

[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 1000], [2, 9, 15, 1]]

Observera att vi ovan använde indexen för raden och kolumnen för att komma åt det element vi ville ändra på. Om vi vill ändra på innehållet i en matris måste vi komma åt elementen md hjälp av deras index. Vi kan inte bara använda oss av en for element in lista -loop för att gå igenom matrisen då vi vill ändra på innehållet i en matris.

Istället behöver vi hålla reda på indexen hos elementen: till exempel med en while-loop eller en for-loop med range-funktionen. Följande kod ökar på värdet hos varje element i en matris med ett:

m = [[1,2,3], [4,5,6], [7,8,9]]\n\nfor i in range(len(m)):\n    for j in range(len(m[i])):\n        m[i][j] += 1\n\nprint(m)

[[2, 3, 4], [5, 6, 7], [8, 9, 10]]

Den yttre loopen går igenom indexen från noll till matrisens längd, alltså antalet rader i matrisen. Den inre loopen går igenom indexen från noll till radernas längd.

HUOM: tämä ja edellinen tehtävä ovat väärässä järjestyksessä VS Coden sivupalkissa

Skapa funktionen rakna_element(matris: list, element: int) som tar emot en matris samt ett heltal som argument. Funktionen ska returnera antalet gånger det valda elementet hittas i matrisen.

Exempel:

m = [[1, 2, 1], [0, 3, 4], [1, 0, 0]]\nprint(rakna_element(m, 1))

3

En tvådimensionell tabell som datastruktur i ett spel

En matris kan fungera väl som datastruktur i flera olika spel. Till exempel rutorna i ett sudokuspel…

\n \n \n \n \n \n \"5\n \n \n

…kan representeras i en matris på följande sätt:

sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [0, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [0, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]

Nu representerar noll en tom ruta, eftersom noll inte är ett värde som kan användas i sudoku.

Här är en enkel funktion som skriver ut sudokurutor:

def skriv_ut(sudoku):\n    for rad in sudoku:\n        for ruta in rad:\n            if ruta > 0:\n                print(f\" {ruta}\", end=\"\")\n            else:\n                print(\" _\", end=\"\")\n        print()\n\nskriv_ut(sudoku)

Utskriften borde se ut så här:

 9 _ _ _ 8 _ 3 _ _\n _ _ _ 2 5 _ 7 _ _\n _ 2 _ 3 _ _ _ _ 4\n _ 9 4 _ _ _ _ _ _\n _ _ _ 7 3 _ 5 6 _\n 7 _ 5 _ 6 _ 4 _ _\n _ _ 7 8 _ 3 9 _ _\n _ _ 1 _ _ _ _ _ 3\n 3 _ _ _ _ _ _ _ 2

Flera andra spel kan också representeras på liknande sätt: till exempel schack, minröj, sänka skepp och Mastermind. I sudoku fungerar siffor väl för att representera spelets läge, medan för andra spel kan andra metoder vara bättre.

I spelet Go plaerar man turvis svarta och vita stenar på spelbrädet. Den spelare som har flera stenar på brädet vinner spelet.

Skapa funktionen vem_vann(brade: list) som får som argument en matris som representerar spelbrädet. Matrisen består av heltalsvärden:

    \n
  • 0: tom ruta
  • \n
  • 1: sten, spelare 1
  • \n
  • 2: sten, spelare 2
  • \n

Spelbrädet kan vara av vilken storlek som helst.

Funktionen ska returnera 1 om spelare ett har vunnit, 2 om spelare två har vunnit och 0 om båda spelarna har lika många stenar på brädet.

Skapa funktionen rad_korrekt(sudoku: list, radnummer: int) som får som argument en matris samt ett heltal. Radnumreringen börjar med noll. Funktionen ska returnera ett värde som beskriver om raden är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång.

sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(rad_korrekt(sudoku, 0))\nprint(rad_korrekt(sudoku, 1))

True\nFalse

Skapa funktionen kolumn_korrekt(sudoku: list, kolumnnummer: int) som får som argument en matris samt ett heltal. Funktionen ska returnera ett värde som beskriver om kolumnen är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång.

sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(kolumn_korrekt(sudoku, 0))\nprint(kolumn_korrekt(sudoku, 1))

False\nTrue

Skapa funktionen kvadrat_korrekt(sudoku: list, radnummer: int, kolumnnummer: int) som får som argument en matris samt två heltal.

Funktionen ska berätta om 3 x 3 -kvadraten vid positionen (radnummer, kolumnnummer) är korrekt ifylld, dvs. talen 1-9 förekommer högst en gång.

Det är värt att notera att i man vanligtvis i sudoku enbart ser på kvadraterna vid positionerna (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) och (6, 6). Den här funktionen kommer dock inte att beakta det här.

sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(kvadrat_korrekt(sudoku, 0, 0))\nprint(kvadrat_korrekt(sudoku, 1, 2))

False\nTrue

Kvadraten vid (0, 0) i det första funktionsanropet:

\n9 0 0\n2 0 0\n0 2 0\n

Kvadraten vid (1, 2) i det andra funktionsanropet:

\n0 2 5\n0 3 0\n4 0 0\n

I ett riktigt sudokuspel skulle den här kvadraten inte alltså kollas.

Skapa funktionen sudoku_korrekt(sudoku: list) som får som argument en matris. Den här funktionen ska använda sig av de tre föregående uppgifternas funktioner (kopiera dem) för att säkerställa att varje rad, kolumn och 3 x 3 -kvadrat innehåller högst en av siffrorna 1-9.

De 3 x 3 -kvadrater som ska kontrolleras nämndes ovan. De är alltså vid positionerna (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) och (6, 6).

sudoku1 = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(sudoku_korrekt(sudoku1))\n\nsudoku2 = [\n  [2, 6, 7, 8, 3, 9, 5, 0, 4],\n  [9, 0, 3, 5, 1, 0, 6, 0, 0],\n  [0, 5, 1, 6, 0, 0, 8, 3, 9],\n  [5, 1, 9, 0, 4, 6, 3, 2, 8],\n  [8, 0, 2, 1, 0, 5, 7, 0, 6],\n  [6, 7, 4, 3, 2, 0, 0, 0, 5],\n  [0, 0, 0, 4, 5, 7, 2, 6, 3],\n  [3, 2, 0, 0, 8, 0, 0, 5, 7],\n  [7, 4, 5, 0, 0, 3, 9, 0, 1]\n]\n\nprint(sudoku_korrekt(sudoku2))

False\nTrue

","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/1-mer-om-listor.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-5/2-referenser/page-data.json b/page-data/osa-5/2-referenser/page-data.json index 48ccd18ee..94084dcb2 100644 --- a/page-data/osa-5/2-referenser/page-data.json +++ b/page-data/osa-5/2-referenser/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-5/2-referenser", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad som menas med en referens till en variabel"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du att det kan finnas flera referenser till ett och samma objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda listor som parameter i funktioner"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"förstår du vad som menas med sidoeffekt hos en funktion."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi tänkt att en variabel är en slags \"låda\" som innehåller variabelns värde. Från teknisk synvinkel stämmer detta inte i Python – variabler innehåller inte ett värde, utan en referens till ett objekt, som en siffra, sträng eller en lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I praktiken innebär det att man inte lagrar ett värde i en variabel, utan ett ställe varifrån variabelns värde hittas från."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En referens kan beskrivas som en pil från variabeln till dess riktiga värde:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 261px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 30.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA1ElEQVQY041ROQ6EQAyb7/MBfsA/aCgoEHQcDQ2XOMQlxI1XzgqKbdhIljzJ2JNk1DiOKIoCdV1jnmfEcYymaZBlmSBJEjDO88R1Xa9Qx3FgWRYRMUHTaZoE5Kwx/2+om7yJ+AA7b9tWprl5VVXCu66TiaRDgoY3v8FI0xR93z8idlyWpRhxAtaZZ5imCUXXKIpkj77vCyeCIECe59A0Dbquyw7XdX0m4e750L7vgm3b4Lrut0MeeJGiXwzDIAb8LM/zEIYhbNuG4zjCLcuSPOuGYeADgRnJTrwaxUwAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8687855d3e59d8151e4fce49ec9669b0/a0b58/5_2_1.webp 230w","/static/8687855d3e59d8151e4fce49ec9669b0/ad293/5_2_1.webp 261w"],"sizes":"(max-width: 261px) 100vw, 261px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8687855d3e59d8151e4fce49ec9669b0/81c8e/5_2_1.png 230w","/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png 261w"],"sizes":"(max-width: 261px) 100vw, 261px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png","alt":"5 2 1","title":"5 2 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Referensen berättar alltså var det riktiga värdet finns. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" berättar vart en variabel refererar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är också en referens\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4538357072\n4537788912"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Referensen, alltså variabelns id är ett heltal. Man kan tänka att talet är adressen för variabelns värde i datorns minne. Observera att om du kör koden ovan på din dator, kommer resultatet sannolikt vara olikt eftersom variablerna har olika referenser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som vi redan såg i förra delens exempel, visar Python Tutors visualiseringsverktyg referenser som \"pilar\" till det riktiga innehållet. Verktyget \"lurar\" ändå när det kommer till strängar, och visar dem som att strängens innehåll skulle vara lagrat i själva variabeln:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9d19433a6bc55bf386a8fbcc572dba74/94cea/5_2_1a.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 29.565217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA2klEQVQY00VQ207EIBDt//+ZzxoT48boBsEs2wKlMMBcMFLbPQ/nYeZcMjP1f0jvnYhDiBkKsxzzTkRhYNs2GCAiZhaR6RQx87yE67e18yryMCPiPM/OeQBgZiKSASI6zCKQkzFaqS+AlNJWaz02jyCAzEyttZyrSJ/2GEbEAj7Eq7J3F1trxLwbakNjw8999bHdfK7IUHLKtvcyRbcsi3POneqzkEdnyvXlYp7f9dunff24xUxQVx+eAC4TIRIRIhpjtNaDtFLKey/y9whmqZWgVCgtl7ZfsfMv95hb032qZjcAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9d19433a6bc55bf386a8fbcc572dba74/a0b58/5_2_1a.webp 230w","/static/9d19433a6bc55bf386a8fbcc572dba74/bc10c/5_2_1a.webp 460w","/static/9d19433a6bc55bf386a8fbcc572dba74/966d8/5_2_1a.webp 920w","/static/9d19433a6bc55bf386a8fbcc572dba74/445df/5_2_1a.webp 1380w","/static/9d19433a6bc55bf386a8fbcc572dba74/78de1/5_2_1a.webp 1840w","/static/9d19433a6bc55bf386a8fbcc572dba74/0dc73/5_2_1a.webp 1940w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9d19433a6bc55bf386a8fbcc572dba74/81c8e/5_2_1a.png 230w","/static/9d19433a6bc55bf386a8fbcc572dba74/08a84/5_2_1a.png 460w","/static/9d19433a6bc55bf386a8fbcc572dba74/c0255/5_2_1a.png 920w","/static/9d19433a6bc55bf386a8fbcc572dba74/b1001/5_2_1a.png 1380w","/static/9d19433a6bc55bf386a8fbcc572dba74/161ec/5_2_1a.png 1840w","/static/9d19433a6bc55bf386a8fbcc572dba74/94cea/5_2_1a.png 1940w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9d19433a6bc55bf386a8fbcc572dba74/c0255/5_2_1a.png","alt":"5 2 1a","title":"5 2 1a","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så är det ändå inte i verkligheten – strängar behandlas också internt av Python på samma sätt som listor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flera av Pythons inbyggda datatyper – som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" – är oföränderliga. Det betyder att värdet på objektet aldrig kan ändras. Däremot kan ett värde ersättas med ett nytt värde."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 281px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/5_2_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 85.65217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsSAAALEgHS3X78AAABq0lEQVQ4y42U6YrCUAyF+9jiExZEtPrDBRdQXHCt+27ky3DkjjMtBkKTu+Qm5ySNns+nPR4Pu9/v/sVH8S+Xi91uN7cR7eVpxMHpdGqtVsvq9brVajXbbDa22+0sSRKrVquWpql9K1HokMn5fP51gKwlPLRarWy9Xvsj+Njocrm0w+HwExBju916sOv16j56Op18vdls2n6/t/l87vtAwTpBuIMCDZV6QMqN49jK5bINh0Prdruu/X7fGo2GFYtFt3lEQqbcIZBksVhYBJDfCOfG47GXRzBKli0Y3hlmMSbWdQaMKVllo6HNfiTgw4uhfpKmFstkGaZ6vZ7jWKlUHEMIwGedthF+EDGbzZzdrMoilUHK4HI8Hn1D5RAEzcsqsw9DAv4THqCV1CqUz5p89jwg1OtgGCzEFpszfFHujEYjh0eEvUmZTCZWKpVcwYv2CCeGCwT4nCL6lrN/Sg5/CCgTwHQMBgMvSQK+4Mmco+BMhlrjm9vYjFq73fap6XQ6ThprPIiquZkQfDogt7EltFOhUPBgIUGUTBW5LIeBQhv8yETrEIOvX5twfgFptCTy4L9bkwAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e866c943663bc4d8360df84d7bc1c4de/a0b58/5_2_2.webp 230w","/static/e866c943663bc4d8360df84d7bc1c4de/f44a8/5_2_2.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e866c943663bc4d8360df84d7bc1c4de/81c8e/5_2_2.png 230w","/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/5_2_2.png 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/5_2_2.png","alt":"5 2 2","title":"5 2 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python finns också datatyper som är föränderliga. Till exempel innehållet på en lista kan förändras utan att man behöver skapa en ny lista:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 271px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8b07a257ace229cc26a5eedf03a60986/5fce1/5_2_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 70.43478260869564%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeUlEQVQ4y5WT66rCQAyE9337w8eT/hEfQVCs4K2iWO+1VevdyDewBxE91EDYbHZ3Opmkbj6fWxzH1uv1bDab2Xa7tTzPLUkSW61WtlgslJ9Op4bd73d7PB5f3RVFIQB8v98rebvd7HA42G63U471eDxaGXP2g202G1WwXq/l7zHn7nK5iAFMKQd2+PV61Up+MBjY6XRS6TBFCkCIAVkul6qIveNStVq1Wq2mi/1+37rdrpy42WxapVKRzoB7KXgMkM/RC5XsNYPpu+AYTHH24/FYmtIw3APRMD5C2c4/xD517fUMVoDA6puXbsor+L9dRsPRaGTD4VDUKQtt/LiwZ6TKzKDmkId+HNI0tSzLBISmOGDn87n0aP1UMqMDOOu32NFB3+FPxgTAnBVZqAaJkIL8ZDLR3g+4o+1hGFq9XtccMn+dTkdO3Gg0LAgCgdFFAACCAHJABnZIppL5MgckiCmNy74BXIYVZ61WS38Na7vd1keiKPrLs38CMo80d8OuAIYAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8b07a257ace229cc26a5eedf03a60986/a0b58/5_2_3.webp 230w","/static/8b07a257ace229cc26a5eedf03a60986/cadab/5_2_3.webp 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8b07a257ace229cc26a5eedf03a60986/81c8e/5_2_3.png 230w","/static/8b07a257ace229cc26a5eedf03a60986/5fce1/5_2_3.png 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8b07a257ace229cc26a5eedf03a60986/5fce1/5_2_3.png","alt":"5 2 3","title":"5 2 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Något förvånande är att också grundläggande datatyper för lagring av siffor och sanningsvärden, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":", är oföränderliga. Låt oss använda följande kod som exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det verkar som att koden ändrar på siffran, men från teknisk synvinkel är det inte så. Istället skapar varje instruktion en ny siffra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften från det här programmet är intressant:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4535856912\n4535856944\n4535856912"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I början refererar variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" till adressen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4535856912"}]},{"type":"text","value":" och när variabelns värde förändras refererar variabeln till adressen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4535856944"}]},{"type":"text","value":". När variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" definieras och får värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", kommer variabeln att referera till samma ställe som variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" när dess värde var "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det verkar som att Python har lagrat siffran 1 till adressen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4535856912"}]},{"type":"text","value":" och alltid då en variabels värde är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", refererar variabeln till det här specifika stället i \"datorns minne\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om de grundläggande datatyperna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" är referenser behöver man som programmerare inte egentligen fundera på det."}]},{"type":"element","tagName":"h2","properties":{"id":"flera-referenser-till-en-och-samma-lista","style":"position:relative;"},"children":[{"type":"text","value":"Flera referenser till en och samma lista"},{"type":"element","tagName":"a","properties":{"href":"#flera-referenser-till-en-och-samma-lista","ariaLabel":"flera referenser till en och samma lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi undersöker som ett exempel att kopiera värdet på en variabel med en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a\nb"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Deklarationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b = a"}]},{"type":"text","value":" kopierar variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":":s värde till variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". Det är ändå viktigt att observera att variabelns värde inte är en lista utan en referens till listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Deklarationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b = a"}]},{"type":"text","value":" kopierar alltså referensen, varpå det efter kopieringen finns två referenser till samma lista."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 271px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 129.99999999999997%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAAsTAAALEwEAmpwYAAACN0lEQVRIx52VV64qQQxEZ60sgC2wJvhhFXwhkMg555zx07FU3FHf0QVeS1aHoctlu9xE4/HYlsulbbdbm06nNpvNjDPm4XBoj8fDGM/n8yOLdrudX8KOx6MDbzYbOxwOvtc3fvzJiOyLsVqtbL1ee0SLxeLXmu8OeLvd7H6/OxNmGfvT6WSdTscul4ungv18PncQ1gCSHiJi74C1Ws0Gg4H1+32rVCq+xxqNhhWLRUun09Zutz0FXCRNXAZIZ5PJ5CdkMQwTzLher26wxSGXR6ORm4AoHk4IO4on+6/E801s/rIo6WI4f1phD5kw9vu9S4WchCAh8FsdojtydD6fHTQJ8CuGSSEnFYcZ6cggABGtdR594p0KogKkQyV7vZ4ba+SGTiVwZ1iv1730fCiXy67BZrNp1WrV8vm8pVIpB0N7qjSs6AydAfYKmQPoarBvtVoODjBag2G32/U8wwqDFXqksAC+Wk/NT4FgS7eo4qEOYSTNwVJrMXXAUqnkLPCEl/jQa/NVlWm9pEthF32kQ/UqplcGB9i7dkxkSEWpIFUmF0o6r/d/dUr8HdQIXx7tk9IQrr96sXlgiYACskbYrJEOxeThdUD9hyhkGbJAX9ls9vV4sGeme3hUkBpgzETigIgXj3hSPhF2oVCwXC5nmUzGv3OR/MIKzSFsgcLOcxhWkT2XKRYOVG0GOoUZ5zgFiDVOiOAVMh4Jl9ai3QCM61JF4Sz+hyb9xs9fIZMX6IddEj5j73T4DynC3t7bOJziAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73c7fae5118d6e5b03c576f1a9d14c3f/a0b58/5_2_4.webp 230w","/static/73c7fae5118d6e5b03c576f1a9d14c3f/cadab/5_2_4.webp 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73c7fae5118d6e5b03c576f1a9d14c3f/81c8e/5_2_4.png 230w","/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_4.png 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_4.png","alt":"5 2 4","title":"5 2 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan kan behandlas med båda referenserna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nlista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[10, 20, 3, 4]\n[10, 20, 3, 4]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en och samma lista har flera referenser kan den behandlas på samma sätt med vilken som helst av referenserna. Däremot återspeglas en förändring via en referens också till alla andra referenser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Visualiseringsverktyget klargör igen vad som sker i programmet:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cf84b632e91094d24c611bd9d800a737/c6720/5_2_4a.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 32.608695652173914%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA30lEQVQY03WQwW7DIAyG8/7vtcsm7bLTTu0ydQmFMMC4CWCbKUyjUaV9Bw6WP/4fhnogpew95FyOQxEBgBBCjPHWSClxY+hLRDR+qtM4T8qu68rMfT5Ns1JXACh/iMij7J1DjIhIRCLSk9u5R4lwzvk3lojuMjMbs2htELGb+6XMLuDXbCDeiOjb2Vq51n3hLqecrF2M0Yjx+OZC/D4uz2/niwbcNutevX+K8UUEB9nLCDGXUpRxp/G62NDb7o1EXEwfFx3X3DJg21zOQYQHa60xxnvfhEpcWerDb9d/+AGpwZhBrvAmDQAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cf84b632e91094d24c611bd9d800a737/a0b58/5_2_4a.webp 230w","/static/cf84b632e91094d24c611bd9d800a737/bc10c/5_2_4a.webp 460w","/static/cf84b632e91094d24c611bd9d800a737/966d8/5_2_4a.webp 920w","/static/cf84b632e91094d24c611bd9d800a737/445df/5_2_4a.webp 1380w","/static/cf84b632e91094d24c611bd9d800a737/78de1/5_2_4a.webp 1840w","/static/cf84b632e91094d24c611bd9d800a737/da98d/5_2_4a.webp 1922w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cf84b632e91094d24c611bd9d800a737/81c8e/5_2_4a.png 230w","/static/cf84b632e91094d24c611bd9d800a737/08a84/5_2_4a.png 460w","/static/cf84b632e91094d24c611bd9d800a737/c0255/5_2_4a.png 920w","/static/cf84b632e91094d24c611bd9d800a737/b1001/5_2_4a.png 1380w","/static/cf84b632e91094d24c611bd9d800a737/161ec/5_2_4a.png 1840w","/static/cf84b632e91094d24c611bd9d800a737/c6720/5_2_4a.png 1922w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cf84b632e91094d24c611bd9d800a737/c0255/5_2_4a.png","alt":"5 2 4a","title":"5 2 4a","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"att-kopiera-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Att kopiera en lista"},{"type":"element","tagName":"a","properties":{"href":"#att-kopiera-en-lista","ariaLabel":"att kopiera en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill skapa en verklig kopia av en lista kan du skapa en ny lista och lägga till alla element från den ursprungliga listan till den nya listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nkopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nkopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lista\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kopia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"lista [1, 2, 3, 3, 5]\nkopia [10, 2, 3, 3, 5, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så ser det ut från visualiseringsverktyget:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/66b9f72bbac2f55cd8fac4539ba92dba/e431d/5_2_4b.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 43.04347826086957%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABGElEQVQoz32RzW7DIBCE/f4v1kY99NBTFFWtZMcQ29iADRjvD5WhcXJpPyEOLLM7mq3SHeZkZzdOFpDSEwDg7vgMAFCmOj45H1qpWjlqs4Tgmbm8bxlALBpEJCJmBoCHeJ6ttQZgI0RAfHLEef7GzDFuMW5ETMSImMWcKK5uWW5dd+s6KW/GmEPsQ5S9/q4FIjm3OG9Sgny4OnpP09Q0zfV6XZalGCu+rYsfF/F+blwkF5S1J61frH1lXh+2lVJ13Qgh+r4fxzEb2gkRL3V//pIR2IXZhxpgINr2yQB7EiUGpb3SPqxwpFV8MTPRvoIQtDFv6/pZStU4TcMweO9TSnsD+hXyE+kPqlJTSrVtK6UstxDiWOY/4h8FSwu1d8EohwAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/66b9f72bbac2f55cd8fac4539ba92dba/a0b58/5_2_4b.webp 230w","/static/66b9f72bbac2f55cd8fac4539ba92dba/bc10c/5_2_4b.webp 460w","/static/66b9f72bbac2f55cd8fac4539ba92dba/966d8/5_2_4b.webp 920w","/static/66b9f72bbac2f55cd8fac4539ba92dba/445df/5_2_4b.webp 1380w","/static/66b9f72bbac2f55cd8fac4539ba92dba/78de1/5_2_4b.webp 1840w","/static/66b9f72bbac2f55cd8fac4539ba92dba/609ff/5_2_4b.webp 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/66b9f72bbac2f55cd8fac4539ba92dba/81c8e/5_2_4b.png 230w","/static/66b9f72bbac2f55cd8fac4539ba92dba/08a84/5_2_4b.png 460w","/static/66b9f72bbac2f55cd8fac4539ba92dba/c0255/5_2_4b.png 920w","/static/66b9f72bbac2f55cd8fac4539ba92dba/b1001/5_2_4b.png 1380w","/static/66b9f72bbac2f55cd8fac4539ba92dba/161ec/5_2_4b.png 1840w","/static/66b9f72bbac2f55cd8fac4539ba92dba/e431d/5_2_4b.png 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/66b9f72bbac2f55cd8fac4539ba92dba/c0255/5_2_4b.png","alt":"5 2 4b","title":"5 2 4b","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_lista"}]},{"type":"text","value":" refererar till en annan lista än "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min_lista"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett enklare sätt att kopiera en lista är att använda hakparenteser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":", som vi använt tidigare för att extrahera innehåll från strängar och listor. Notationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[:]"}]},{"type":"text","value":" väljer alla element i en samling. Därmed skapar det en kopia av en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nkopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nkopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[10, 2, 3, 4]\n[1, 20, 3, 4]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-lista-som-parameter-i-en-funktion","style":"position:relative;"},"children":[{"type":"text","value":"En lista som parameter i en funktion"},{"type":"element","tagName":"a","properties":{"href":"#en-lista-som-parameter-i-en-funktion","ariaLabel":"en lista som parameter i en funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När en lista ges som parameter till en funktion, förmedlas referensen till listan. Det här innebär att funktionen kan ändra på listan som getts som parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel följande funktion lägger till ett nytt element i en lista som getts som funktionens parameter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"lagg_till_element"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n nytt_element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nytt_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlagg_till_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n[1, 2, 3]\n[1, 2, 3, 10]\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lagg_till_element"}]},{"type":"text","value":" inte returnerar något, utan ändrar på den lista som getts som funktionens parameter. Visualiseringsverktyget presenterar situationen så här:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9be11dea0a28e2151e4f4f7101f82472/764d0/5_2_4c.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 35.65217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA+UlEQVQY01WO226DMBAF+f9/62P7UFVKq0BCMdgLOOs1e6sITdTO45FGZxp/oKZY6NwN5y6UUlXVzNzdzOZ5jjHmnG93aq1mJiLNYZrqVimm1Lbd5dqP47Qui6oespnpHXcXkWNh5ofMzERrXodhiDGWUojo+WxmtVZEVFUAEOFjbJ7ZiNh1bd9fc17dzf8AAG3bIqK7r+st54KIe7Y9ICKA+dJPMWVmOSKPZ2aBeaFaN1ZYPhDfVIu7NzhDShBjJCJ3Z/GNj95fzP3UTS+vX21YVK1QX8rJrO6ybBsziwgAjGOIcZqmMYSQUlJVkT3hOi7vn98hrf6fH4cdljeAKLvbAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9be11dea0a28e2151e4f4f7101f82472/a0b58/5_2_4c.webp 230w","/static/9be11dea0a28e2151e4f4f7101f82472/bc10c/5_2_4c.webp 460w","/static/9be11dea0a28e2151e4f4f7101f82472/966d8/5_2_4c.webp 920w","/static/9be11dea0a28e2151e4f4f7101f82472/445df/5_2_4c.webp 1380w","/static/9be11dea0a28e2151e4f4f7101f82472/78de1/5_2_4c.webp 1840w","/static/9be11dea0a28e2151e4f4f7101f82472/b4500/5_2_4c.webp 1898w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9be11dea0a28e2151e4f4f7101f82472/81c8e/5_2_4c.png 230w","/static/9be11dea0a28e2151e4f4f7101f82472/08a84/5_2_4c.png 460w","/static/9be11dea0a28e2151e4f4f7101f82472/c0255/5_2_4c.png 920w","/static/9be11dea0a28e2151e4f4f7101f82472/b1001/5_2_4c.png 1380w","/static/9be11dea0a28e2151e4f4f7101f82472/161ec/5_2_4c.png 1840w","/static/9be11dea0a28e2151e4f4f7101f82472/764d0/5_2_4c.png 1898w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9be11dea0a28e2151e4f4f7101f82472/c0255/5_2_4c.png","alt":"5 2 4c","title":"5 2 4c","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Global frame syftar på huvudprogrammets variabler, och den blå lådan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lagg_till_element"}]},{"type":"text","value":" på funktionens parametrar och variabler. Som visualiseringen visar, refererar funktionen till samma lista som huvudprogrammet, vilket betyder att ändringar som görs i listan inom funktionen också syns i huvudprogrammet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt är att skapa en ny lista och returnera den:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"lagg_till_element"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n nytt_element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n kopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nytt_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" kopia\n\nsiffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsiffror2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lagg_till_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ursprunglig lista:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ny lista:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffror2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ursprunglig lista: [1, 2, 3]\nNy lista: [1, 2, 3, 10]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du inte är helt säker på vad som händer i en kodsnutt, kan det löna sig att utnyttja visualiseringsverktyget."}]},{"type":"element","tagName":"h2","properties":{"id":"andra-pa-en-lista-som-getts-som-argument","style":"position:relative;"},"children":[{"type":"text","value":"Ändra på en lista som getts som argument"},{"type":"element","tagName":"a","properties":{"href":"#andra-pa-en-lista-som-getts-som-argument","ariaLabel":"andra pa en lista som getts som argument permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande är ett försök på att skapa en funktion som ökar på varje element med tio:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oka_pa_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ny_lista\n\nsiffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"start:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noka_pa_alla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"efter funktionen:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"start: [1, 2, 3]\nefter funktionen: [1, 2, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Av någon orsak fungerar funktionen inte. Varför så?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen tar emot en referens till en lista som argument. Det här är lagrat i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min_lista"}]},{"type":"text","value":". Tilldelningen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min_lista = ny_lista"}]},{"type":"text","value":" tilldelar ett nytt värde till den samma variabeln. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min_lista"}]},{"type":"text","value":" hänvisar nu till den nya listan som skapades i funktionen, vilket betyder att referensen till den ursprungliga listan inte mera är tillgänglig inom funktionen. Tilldelningen har inte dock någon påverkan utanför funktionen."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 442px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cd75af1816ee2f9b354a8fc7017fc05b/e03bf/5_2_6.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 157.82608695652175%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAgCAYAAAASYli2AAAACXBIWXMAAAsSAAALEgHS3X78AAACOklEQVRIx6WW54rDMBCE8/5vlj8hgUB6Ib333vb4FjbodIoicwYh25EmM7NFzolzvV4v91Eej4dMJhMZDodyvV4lttaunL/g+XzK+XzW5/v9Lr1eTyqVisznczkcDrLb7fSPPoH+ATydTlKtVnUj98fjUVarlSyXS2m1WjIajd5so4AxGSbfFNi6JIbMJncwGEin01FWSMbL6XSqoJkZcj8ej9VDmwFn8GdRQGNlkur1uiwWC92Ij3i43+814nh5u92ySSbKgAGOd5fLRVkie71epzG02fwByO5JF1gm56ErGY/y+bx0u10FBAy57rqvUXYXAYBX5B/SfTBfUVKUAYOh65U/khm697Hray37CymvRqMhzWZTaxnGlF6tVvuVNj5wUDIRBhD/NpvNu5Zns5kOcjMpD20BJQYbQAkMgOQfedjv97Vx/Ls5AMYgYEndxi09k2hMYAlDv9ZD0c6F/CsWi1IoFBQYMHxMjfZHyZhPDSPRLqTyTFAYXxm6MmhX9EMDhS29kHf8RnBCR0Gw23DhGZtgQt4x25nC4D7K8FuUM3voRhlm7XZb5TFzBCCTquGdyXYl2/4gIACAkuCUmpVfuVxWIPuDj4C+F/i13W71mU2kDoGhFAmOe+gHJfuAnCe0fILBZgJAkIgyrAlKcmLHzLdjwT9G/wDaC3IOn0qlkiZ2rD9GGdoLOjSGE0WrED/hM6eNXdQy4ADjGZ7iLR0H9syhL4hc6DOEhRxUVs9E19qXfd4xh0rvBwfxyIYimKgPAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cd75af1816ee2f9b354a8fc7017fc05b/a0b58/5_2_6.webp 230w","/static/cd75af1816ee2f9b354a8fc7017fc05b/3fa5c/5_2_6.webp 442w"],"sizes":"(max-width: 442px) 100vw, 442px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cd75af1816ee2f9b354a8fc7017fc05b/81c8e/5_2_6.png 230w","/static/cd75af1816ee2f9b354a8fc7017fc05b/e03bf/5_2_6.png 442w"],"sizes":"(max-width: 442px) 100vw, 442px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cd75af1816ee2f9b354a8fc7017fc05b/e03bf/5_2_6.png","alt":"5 2 6","title":"5 2 6","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom innehåller variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_lista"}]},{"type":"text","value":" nu de nya värdena, men de är inte tillgängliga utanför funktionen. Variabeln försvinner alltså när funktionen körts och programmet fortsätter tillbaka till huvudfunktionen. Variabeln siffror i huvudfunktionen hänvisar alltid till den ursprungliga listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Visualiseringsverktyget hjälper igen. När du går igenom stegen utförligt märker du att den ursprungliga listan inte påverkas på något sätt av funktionen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/19d3606e35d311c11edb9891cf40eeeb/75d3b/5_2_4d.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 41.30434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAABLklEQVQY01WQ227bMBBE/f/fFgRFAhQwEECuK9GWaZCWRHGXe20UJW1znvblDGb24B+oKCKkPJ9+j+GaEFtrzczcHRFzzsuyAAARMbOZiYiZHT5l1VLK+Xy+Xq8xxnVdAUBE3H2PEBEiMrM9VFW/ZFNuqGYppRhjCGGeZyJqrbk7MwNirRURVTWlTMTywS4bE6Wcp2kqpQCA/wc2jLfbJYTW0Mzm+bGu8571WZuZh2Houm4YhtPptNfeCzfiqUBMU1m30FKOtb66q7sfmBoiAoCqNJJ7Xqfyb5W7L5V+HPvnn79CqhVpKT1iZ1Y2uU4bj8cDAJi3McxMRNshYqoF5diNzy9v4V5EdV7epumptcsm/92Wcx7Hse/7EEKMsdZqGwokOa+3yz0v337xLv8BaHfNshxTjQQAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19d3606e35d311c11edb9891cf40eeeb/a0b58/5_2_4d.webp 230w","/static/19d3606e35d311c11edb9891cf40eeeb/bc10c/5_2_4d.webp 460w","/static/19d3606e35d311c11edb9891cf40eeeb/966d8/5_2_4d.webp 920w","/static/19d3606e35d311c11edb9891cf40eeeb/445df/5_2_4d.webp 1380w","/static/19d3606e35d311c11edb9891cf40eeeb/78de1/5_2_4d.webp 1840w","/static/19d3606e35d311c11edb9891cf40eeeb/ee81e/5_2_4d.webp 1966w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19d3606e35d311c11edb9891cf40eeeb/81c8e/5_2_4d.png 230w","/static/19d3606e35d311c11edb9891cf40eeeb/08a84/5_2_4d.png 460w","/static/19d3606e35d311c11edb9891cf40eeeb/c0255/5_2_4d.png 920w","/static/19d3606e35d311c11edb9891cf40eeeb/b1001/5_2_4d.png 1380w","/static/19d3606e35d311c11edb9891cf40eeeb/161ec/5_2_4d.png 1840w","/static/19d3606e35d311c11edb9891cf40eeeb/75d3b/5_2_4d.png 1966w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/19d3606e35d311c11edb9891cf40eeeb/c0255/5_2_4d.png","alt":"5 2 4d","title":"5 2 4d","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett enkelt sätt att korrigera problemet är att kopiera över alla element från den nya listan till den gamla:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oka_pa_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi kopierar de nya värdena till den gamla listan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eller lite enklare tack vare Python:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan ersätts en del av en lista med värden från en annan samling."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som vi vet, kan vi också göra detta för en hel samling:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"98"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"97"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"98"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"97"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Allt innehåll i den gamla listan ersätts. Inspirerat av det här har vi nu skapat en fungerande version av funktionen som ökar på elementens värden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oka_pa_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ny_lista"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Egentligen finns det ingen orsak att skapa en ny lista inom funktionen. Vi kan helt enkelt tilldela värdena direkt till den ursprungliga listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oka_pa_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Elementen fördubblade","tmcname":"osa05-06a_elementen_x2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elementen_fordubblade(siffror: list)"}]},{"type":"text","value":" som får som argument en lista med siffror."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en ny lista där alla siffror är multiplicerade med två. Funktionen får inte ändra på den ursprungliga listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fordubblade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" elementen_fordubblade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ursprunglig:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fördubblade:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fordubblade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ursprunglig: [2, 4, 5, 3, 11, -4]\nfördubblade: [4, 8, 10, 6, 22, -8]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bort med minsta","tmcname":"osa05-06b_minsta_bort"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"avlagsna_minsta(siffror: list)"}]},{"type":"text","value":" som får som argument en lista med siffror."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska ta bort den minsta siffran från listan. Du kan anta att den minsta siffran endast förekommer en gång."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska inte returnera något, den ska endast ändra på listan som getts som argument!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n avlagsna_minsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 4, 6, 3, 5]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Utskrift och att lägga till en siffra","tmcname":"osa05-07_sudoku_utskrift_ny_siffra"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi ännu två funktioner för ett sudoku: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lagg_till"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"text","value":" får som argument en matris och skriver den ut enligt exemplet nedan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lagg_till(sudoku: list, radnummer: int, kolumnnummer: int, siffra: int)"}]},{"type":"text","value":" tar emot som argument en matris, två siffror som indikerar positionen och ett tal (1-9) som ska lagras."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlagg_till"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlagg_till"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlagg_till"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tre siffror tillagda:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\nTre siffror tillagda:\n\n2 _ _ _ _ _ _ _ _\n_ _ 7 _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ 3 _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Du kan dra nytta av att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen kan användas så att radbyten inte görs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tecken \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"utan mellanrum\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"tecken utan mellanrum"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En radbrytning kan åstadkommas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Lägga till en siffra i en kopia av sudokut","tmcname":"osa05-08_sudoku_ny_siffra_kopia"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi en lite annorlunda version av funktionen som lägger till nya tal i ett sudoku."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kopiera_och_lagg_till(sudoku: list, radnummer: int, kolumnnummer: int, siffra: int)"}]},{"type":"text","value":" får som argument en matris, två siffror som indikerar en position samt en siffra (1-9) som ska lagras. Funktionen ska returnera en kopia av matrisen som gavs som argument, med den angivna siffran lagrad på korrekt ställe. Funktionen får inte ändra på matrisen som getts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här utnyttjar vi funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"text","value":" från den föregående uppgiften:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nkopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kopiera_och_lagg_till"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ursprunglig:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kopia:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Ursprunglig:\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\nKopia:\n2 _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: I den här uppgiften måste man vara noga med vad allt som behöver kopieras och vart man slutligen lagrar värdet som getts till funktionen. "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" kan hjälpa. P.g.a. storleken av sudokut kan vyn dock vara mer råddig än vanligtvis."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Tre i rad","tmcname":"osa05-09_tre_i_rad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tre i rad spelas med ett 3 x 3 -rutnät, där spelarna turvis markerar ett kryss eller en ring. Spelaren som får tre markeringar i rad, vågrätt, lodrätt eller diagonalt, vinner. Om ingendera av spelarna får det, är spelet oavgjort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tur(brade: list, x: int, y: int, markering: str)"}]},{"type":"text","value":" där den givna markeringen görs på stället som indikeras av koordinaterna (0-2)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" indikerar kolumn och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelbrädet består av följande strängar:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":": tom ruta"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"X\""}]},{"type":"text","value":": markeing, spelare 1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"O\""}]},{"type":"text","value":": markeing, spelare 2"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om markeringen lyckades (stället var tomt på brädet), och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" om stället var reserverat eller koordinaterna inte var i intervallet 0-2."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"brade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"X\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\n[['', '', 'X'], ['', '', ''], ['', '', '']]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Transponera","tmcname":"osa05-10_transponera"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"transponera(matris: lista)"}]},{"type":"text","value":" som får som argument en matris. Funktionen ska transponera matrisen, alltså byta om rader till kolumner och tvärtom."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att matrisen har lika många rader och kolumner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här matrisen..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...skulle se ut så här efter transponeringen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska inte returnera något. Den ska ändra på matrisen som den fått som argument."}]}]},{"type":"element","tagName":"h2","properties":{"id":"sidoeffekter-hos-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Sidoeffekter hos funktioner"},{"type":"element","tagName":"a","properties":{"href":"#sidoeffekter-hos-funktioner","ariaLabel":"sidoeffekter hos funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en funktion tar emot en referens till en lista som argument, kan funktionen ändra på listan. Om programmeraren inte har tagit i beaktande att listan kan ändras på direkt, kan ändringar i listan förorsaka problem på andra håll i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en funktion som borde hitta det näst minsta värdet i en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nast_minst"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# i en ordnad lista finns det näst minsta elementet vid index 1"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsiffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nast_minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n2\n[1, 2, 3, 4, 4, 5, 6, 7]\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen hittar det näst minsta värdet, men dessutom ordnar funktionen listan. Om ordningen av elementen har betydelse på andra håll i programmet kommer det här funktionsanropet eventuellt att orsaka fel. Oplanerade modifikationer som görs hos objekt som ges som referens till en funktion kallas sidoeffekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan förhindra den här sidoeffekten genom att göra en liten ändring i funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nast_minst"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsiffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nast_minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n[1, 4, 2, 5, 3, 6, 4, 7]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" returnerar en ny ordnad kopia av listan, så vi behöver inte mera \"sabotera\" den ursprungliga listan när vi söker efter det näst minsta värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är en bra vana att undvika sidoeffekter i funktioner. Sidoeffekter kan göra det svårare att säkerställa att programmet fungerar som det ska i alla situationer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner som saknar sidoeffekter kallas rena funktioner. Då man arbetar med funktionell programmering är rena funktioner speciellt viktiga. Vi dyker djupare i det här under fortsättningskursen i programmering."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"21ff9f90-0aed-591d-8ff0-60e790724739"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad som menas med en referens till en variabel
  • \n
  • vet du att det kan finnas flera referenser till ett och samma objekt
  • \n
  • kan du använda listor som parameter i funktioner
  • \n
  • förstår du vad som menas med sidoeffekt hos en funktion.
  • \n

Hittills har vi tänkt att en variabel är en slags \"låda\" som innehåller variabelns värde. Från teknisk synvinkel stämmer detta inte i Python – variabler innehåller inte ett värde, utan en referens till ett objekt, som en siffra, sträng eller en lista.

I praktiken innebär det att man inte lagrar ett värde i en variabel, utan ett ställe varifrån variabelns värde hittas från.

En referens kan beskrivas som en pil från variabeln till dess riktiga värde:

\n \n \n \n \n \n \"5\n \n \n

I Python finns också datatyper som är föränderliga. Till exempel innehållet på en lista kan förändras utan att man behöver skapa en ny lista:

\n \n \n \n \n \n \"5\n \n \n

Listan kan behandlas med båda referenserna:

lista = [1, 2, 3, 4]\nlista2 = lista\n\nlista[0] = 10\nlista2[1] = 20\n\nprint(lista)\nprint(lista2)

[10, 20, 3, 4]\n[10, 20, 3, 4]

Om en och samma lista har flera referenser kan den behandlas på samma sätt med vilken som helst av referenserna. Däremot återspeglas en förändring via en referens också till alla andra referenser.

Visualiseringsverktyget klargör igen vad som sker i programmet:

\n \n \n \n \n \n \"5\n \n \n

Att kopiera en lista

Om du vill skapa en verklig kopia av en lista kan du skapa en ny lista och lägga till alla element från den ursprungliga listan till den nya listan:

lista = [1, 2, 3, 3, 5]\n\nkopia = []\nfor element in lista:\n    kopia.append(element)\n\nkopia[0] = 10\nkopia.append(6)\nprint(\"lista\", lista)\nprint(\"kopia\", kopia)

lista [1, 2, 3, 3, 5]\nkopia [10, 2, 3, 3, 5, 6]

Så ser det ut från visualiseringsverktyget:

\n \n \n \n \n \n \"5\n \n \n

Variabeln ny_lista refererar till en annan lista än min_lista.

Ett enklare sätt att kopiera en lista är att använda hakparenteser [], som vi använt tidigare för att extrahera innehåll från strängar och listor. Notationen [:] väljer alla element i en samling. Därmed skapar det en kopia av en lista:

lista = [1,2,3,4]\nkopia = lista[:]\n\nlista[0] = 10\nkopia[1] = 20\n\nprint(lista)\nprint(kopia)

[10, 2, 3, 4]\n[1, 20, 3, 4]

En lista som parameter i en funktion

När en lista ges som parameter till en funktion, förmedlas referensen till listan. Det här innebär att funktionen kan ändra på listan som getts som parameter.

Till exempel följande funktion lägger till ett nytt element i en lista som getts som funktionens parameter:

def lagg_till_element(lista: list):\n    nytt_element = 10\n    lista.append(nytt_element)\n\nlista = [1,2,3]\nprint(lista)\nlagg_till_element(lista)\nprint(lista)
\n[1, 2, 3]\n[1, 2, 3, 10]\n

Märk att funktionen lagg_till_element inte returnerar något, utan ändrar på den lista som getts som funktionens parameter. Visualiseringsverktyget presenterar situationen så här:

\n \n \n \n \n \n \"5\n \n \n

Global frame syftar på huvudprogrammets variabler, och den blå lådan lagg_till_element på funktionens parametrar och variabler. Som visualiseringen visar, refererar funktionen till samma lista som huvudprogrammet, vilket betyder att ändringar som görs i listan inom funktionen också syns i huvudprogrammet.

Ett annat sätt är att skapa en ny lista och returnera den:

def lagg_till_element(lista: list) -> list:\n    nytt_element = 10\n    kopia = lista[:]\n    kopia.append(nytt_element)\n    return kopia\n\nsiffror = [1, 2, 3]\nsiffror2 = lagg_till_element(siffror)\n\nprint(\"Ursprunglig lista:\", siffror)\nprint(\"Ny lista:\", siffror2)

Ursprunglig lista: [1, 2, 3]\nNy lista: [1, 2, 3, 10]

Om du inte är helt säker på vad som händer i en kodsnutt, kan det löna sig att utnyttja visualiseringsverktyget.

Ändra på en lista som getts som argument

Det följande är ett försök på att skapa en funktion som ökar på varje element med tio:

def oka_pa_alla(lista: list):\n    ny_lista = []\n    for element in lista:\n        ny_lista.append(element + 10)\n    lista = ny_lista\n\nsiffror = [1, 2, 3]\nprint(\"start:\",siffror)\noka_pa_alla(siffror)\nprint(\"efter funktionen:\", siffror)

start: [1, 2, 3]\nefter funktionen: [1, 2, 3]

Av någon orsak fungerar funktionen inte. Varför så?

Funktionen tar emot en referens till en lista som argument. Det här är lagrat i variabeln min_lista. Tilldelningen min_lista = ny_lista tilldelar ett nytt värde till den samma variabeln. Variabeln min_lista hänvisar nu till den nya listan som skapades i funktionen, vilket betyder att referensen till den ursprungliga listan inte mera är tillgänglig inom funktionen. Tilldelningen har inte dock någon påverkan utanför funktionen.

\n \n \n \n \n \n \"5\n \n \n

Dessutom innehåller variabeln ny_lista nu de nya värdena, men de är inte tillgängliga utanför funktionen. Variabeln försvinner alltså när funktionen körts och programmet fortsätter tillbaka till huvudfunktionen. Variabeln siffror i huvudfunktionen hänvisar alltid till den ursprungliga listan.

Visualiseringsverktyget hjälper igen. När du går igenom stegen utförligt märker du att den ursprungliga listan inte påverkas på något sätt av funktionen:

\n \n

Skapa funktionen elementen_fordubblade(siffror: list) som får som argument en lista med siffror.

Funktionen ska returnera en ny lista där alla siffror är multiplicerade med två. Funktionen får inte ändra på den ursprungliga listan.

Exempel:

if __name__ == \"__main__\":\n    siffror = [2, 4, 5, 3, 11, -4]\n    fordubblade = elementen_fordubblade(siffror)\n    print(\"ursprunglig:\", siffror)\n    print(\"fördubblade:\", fordubblade)

ursprunglig: [2, 4, 5, 3, 11, -4]\nfördubblade: [4, 8, 10, 6, 22, -8]

Skapa funktionen avlagsna_minsta(siffror: list) som får som argument en lista med siffror.

Funktionen ska ta bort den minsta siffran från listan. Du kan anta att den minsta siffran endast förekommer en gång.

Funktionen ska inte returnera något, den ska endast ändra på listan som getts som argument!

Exempel:

if __name__ == \"__main__\":\n    siffror = [2, 4, 6, 1, 3, 5]\n    avlagsna_minsta(siffror)\n    print(siffror)

[2, 4, 6, 3, 5]

I den här uppgiften skapar vi ännu två funktioner för ett sudoku: skriv_ut och lagg_till.

Funktionen skriv_ut får som argument en matris och skriver den ut enligt exemplet nedan.

Funktionen lagg_till(sudoku: list, radnummer: int, kolumnnummer: int, siffra: int) tar emot som argument en matris, två siffror som indikerar positionen och ett tal (1-9) som ska lagras.

sudoku  = [\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n]\n\nskriv_ut(sudoku)\nlagg_till(sudoku, 0, 0, 2)\nlagg_till(sudoku, 1, 2, 7)\nlagg_till(sudoku, 5, 7, 3)\nprint()\nprint(\"Tre siffror tillagda:\")\nprint()\nskriv_ut(sudoku)
\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\nTre siffror tillagda:\n\n2 _ _  _ _ _  _ _ _\n_ _ 7  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ 3 _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n

Tips: Du kan dra nytta av att print-instruktionen kan användas så att radbyten inte görs:

print(\"tecken \", end=\"\")\nprint(\"utan mellanrum\", end=\"\")

tecken utan mellanrum

En radbrytning kan åstadkommas så här:

print()

I den här uppgiften skapar vi en lite annorlunda version av funktionen som lägger till nya tal i ett sudoku.

Funktionen kopiera_och_lagg_till(sudoku: list, radnummer: int, kolumnnummer: int, siffra: int) får som argument en matris, två siffror som indikerar en position samt en siffra (1-9) som ska lagras. Funktionen ska returnera en kopia av matrisen som gavs som argument, med den angivna siffran lagrad på korrekt ställe. Funktionen får inte ändra på matrisen som getts som argument.

Här utnyttjar vi funktionen skriv_ut från den föregående uppgiften:

sudoku  = [\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n]\n\nkopia = kopiera_och_lagg_till(sudoku, 0, 0, 2)\nprint(\"Ursprunglig:\")\nskriv_ut(sudoku)\nprint()\nprint(\"Kopia:\")\nskriv_ut(kopia)
\nUrsprunglig:\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\nKopia:\n2 _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n

Tips: I den här uppgiften måste man vara noga med vad allt som behöver kopieras och vart man slutligen lagrar värdet som getts till funktionen. Visualiseringsverktyget kan hjälpa. P.g.a. storleken av sudokut kan vyn dock vara mer råddig än vanligtvis.

Tre i rad spelas med ett 3 x 3 -rutnät, där spelarna turvis markerar ett kryss eller en ring. Spelaren som får tre markeringar i rad, vågrätt, lodrätt eller diagonalt, vinner. Om ingendera av spelarna får det, är spelet oavgjort.

Skapa funktionen tur(brade: list, x: int, y: int, markering: str) där den givna markeringen görs på stället som indikeras av koordinaterna (0-2).

Observera att x indikerar kolumn och y rad.

Spelbrädet består av följande strängar:

    \n
  • \"\": tom ruta
  • \n
  • \"X\": markeing, spelare 1
  • \n
  • \"O\": markeing, spelare 2
  • \n

Funktionen returnerar True om markeringen lyckades (stället var tomt på brädet), och False om stället var reserverat eller koordinaterna inte var i intervallet 0-2.

Exempel:

brade = [[\"\", \"\", \"\"], [\"\", \"\", \"\"], [\"\", \"\", \"\"]]\nprint(tur(brade, 2, 0, \"X\"))\nprint(brade)

True\n[['', '', 'X'], ['', '', ''], ['', '', '']]

Skapa funktionen transponera(matris: lista) som får som argument en matris. Funktionen ska transponera matrisen, alltså byta om rader till kolumner och tvärtom.

Du kan anta att matrisen har lika många rader och kolumner.

Den här matrisen...

1 2 3\n4 5 6\n7 8 9

...skulle se ut så här efter transponeringen:

1 4 7\n2 5 8\n3 6 9

Funktionen ska inte returnera något. Den ska ändra på matrisen som den fått som argument.

Sidoeffekter hos funktioner

Om en funktion tar emot en referens till en lista som argument, kan funktionen ändra på listan. Om programmeraren inte har tagit i beaktande att listan kan ändras på direkt, kan ändringar i listan förorsaka problem på andra håll i programmet.

Låt oss ta en titt på en funktion som borde hitta det näst minsta värdet i en lista:

def nast_minst(lista: list) -> int:\n    # i en ordnad lista finns det näst minsta elementet vid index 1\n    lista.sort()\n    return lista[1]\n\nsiffror = [1, 4, 2, 5, 3, 6, 4, 7]\nprint(nast_minst(siffror))\nprint(siffror)
\n2\n[1, 2, 3, 4, 4, 5, 6, 7]\n

Funktionen hittar det näst minsta värdet, men dessutom ordnar funktionen listan. Om ordningen av elementen har betydelse på andra håll i programmet kommer det här funktionsanropet eventuellt att orsaka fel. Oplanerade modifikationer som görs hos objekt som ges som referens till en funktion kallas sidoeffekt.

Vi kan förhindra den här sidoeffekten genom att göra en liten ändring i funktionen:

def nast_minst(lista: list) -> int:\n    kopia = sorted(lista)\n    return kopia[1]\n\nsiffror = [1, 4, 2, 5, 3, 6, 4, 7]\nprint(nast_minst(siffror))\nprint(siffror)

2\n[1, 4, 2, 5, 3, 6, 4, 7]

Funktionen sorted returnerar en ny ordnad kopia av listan, så vi behöver inte mera \"sabotera\" den ursprungliga listan när vi söker efter det näst minsta värdet.

Det är en bra vana att undvika sidoeffekter i funktioner. Sidoeffekter kan göra det svårare att säkerställa att programmet fungerar som det ska i alla situationer.

Funktioner som saknar sidoeffekter kallas rena funktioner. Då man arbetar med funktionell programmering är rena funktioner speciellt viktiga. Vi dyker djupare i det här under fortsättningskursen i programmering.

","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/2-referenser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad som menas med en referens till en variabel"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du att det kan finnas flera referenser till ett och samma objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda listor som parameter i funktioner"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"förstår du vad som menas med sidoeffekt hos en funktion."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi tänkt att en variabel är en slags \"låda\" som innehåller variabelns värde. Från teknisk synvinkel stämmer detta inte i Python – variabler innehåller inte ett värde, utan en referens till ett objekt, som en siffra, sträng eller en lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I praktiken innebär det att man inte lagrar ett värde i en variabel, utan ett ställe varifrån variabelns värde hittas från."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En referens kan beskrivas som en pil från variabeln till dess riktiga värde:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 261px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 30.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA1ElEQVQY041ROQ6EQAyb7/MBfsA/aCgoEHQcDQ2XOMQlxI1XzgqKbdhIljzJ2JNk1DiOKIoCdV1jnmfEcYymaZBlmSBJEjDO88R1Xa9Qx3FgWRYRMUHTaZoE5Kwx/2+om7yJ+AA7b9tWprl5VVXCu66TiaRDgoY3v8FI0xR93z8idlyWpRhxAtaZZ5imCUXXKIpkj77vCyeCIECe59A0Dbquyw7XdX0m4e750L7vgm3b4Lrut0MeeJGiXwzDIAb8LM/zEIYhbNuG4zjCLcuSPOuGYeADgRnJTrwaxUwAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8687855d3e59d8151e4fce49ec9669b0/a0b58/5_2_1.webp 230w","/static/8687855d3e59d8151e4fce49ec9669b0/ad293/5_2_1.webp 261w"],"sizes":"(max-width: 261px) 100vw, 261px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8687855d3e59d8151e4fce49ec9669b0/81c8e/5_2_1.png 230w","/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png 261w"],"sizes":"(max-width: 261px) 100vw, 261px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png","alt":"5 2 1","title":"5 2 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Referensen berättar alltså var det riktiga värdet finns. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" berättar vart en variabel refererar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är också en referens\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4538357072\n4537788912"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Referensen, alltså variabelns id är ett heltal. Man kan tänka att talet är adressen för variabelns värde i datorns minne. Observera att om du kör koden ovan på din dator, kommer resultatet sannolikt vara olikt eftersom variablerna har olika referenser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som vi redan såg i förra delens exempel, visar Python Tutors visualiseringsverktyg referenser som \"pilar\" till det riktiga innehållet. Verktyget \"lurar\" ändå när det kommer till strängar, och visar dem som att strängens innehåll skulle vara lagrat i själva variabeln:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9d19433a6bc55bf386a8fbcc572dba74/94cea/5_2_1a.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 29.565217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA2klEQVQY00VQ207EIBDt//+ZzxoT48boBsEs2wKlMMBcMFLbPQ/nYeZcMjP1f0jvnYhDiBkKsxzzTkRhYNs2GCAiZhaR6RQx87yE67e18yryMCPiPM/OeQBgZiKSASI6zCKQkzFaqS+AlNJWaz02jyCAzEyttZyrSJ/2GEbEAj7Eq7J3F1trxLwbakNjw8999bHdfK7IUHLKtvcyRbcsi3POneqzkEdnyvXlYp7f9dunff24xUxQVx+eAC4TIRIRIhpjtNaDtFLKey/y9whmqZWgVCgtl7ZfsfMv95hb032qZjcAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9d19433a6bc55bf386a8fbcc572dba74/a0b58/5_2_1a.webp 230w","/static/9d19433a6bc55bf386a8fbcc572dba74/bc10c/5_2_1a.webp 460w","/static/9d19433a6bc55bf386a8fbcc572dba74/966d8/5_2_1a.webp 920w","/static/9d19433a6bc55bf386a8fbcc572dba74/445df/5_2_1a.webp 1380w","/static/9d19433a6bc55bf386a8fbcc572dba74/78de1/5_2_1a.webp 1840w","/static/9d19433a6bc55bf386a8fbcc572dba74/0dc73/5_2_1a.webp 1940w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9d19433a6bc55bf386a8fbcc572dba74/81c8e/5_2_1a.png 230w","/static/9d19433a6bc55bf386a8fbcc572dba74/08a84/5_2_1a.png 460w","/static/9d19433a6bc55bf386a8fbcc572dba74/c0255/5_2_1a.png 920w","/static/9d19433a6bc55bf386a8fbcc572dba74/b1001/5_2_1a.png 1380w","/static/9d19433a6bc55bf386a8fbcc572dba74/161ec/5_2_1a.png 1840w","/static/9d19433a6bc55bf386a8fbcc572dba74/94cea/5_2_1a.png 1940w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9d19433a6bc55bf386a8fbcc572dba74/c0255/5_2_1a.png","alt":"5 2 1a","title":"5 2 1a","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så är det ändå inte i verkligheten – strängar behandlas också internt av Python på samma sätt som listor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flera av Pythons inbyggda datatyper – som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" – är oföränderliga. Det betyder att värdet på objektet aldrig kan ändras. Däremot kan ett värde ersättas med ett nytt värde."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 281px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/5_2_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 85.65217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsSAAALEgHS3X78AAABq0lEQVQ4y42U6YrCUAyF+9jiExZEtPrDBRdQXHCt+27ky3DkjjMtBkKTu+Qm5ySNns+nPR4Pu9/v/sVH8S+Xi91uN7cR7eVpxMHpdGqtVsvq9brVajXbbDa22+0sSRKrVquWpql9K1HokMn5fP51gKwlPLRarWy9Xvsj+Njocrm0w+HwExBju916sOv16j56Op18vdls2n6/t/l87vtAwTpBuIMCDZV6QMqN49jK5bINh0Prdruu/X7fGo2GFYtFt3lEQqbcIZBksVhYBJDfCOfG47GXRzBKli0Y3hlmMSbWdQaMKVllo6HNfiTgw4uhfpKmFstkGaZ6vZ7jWKlUHEMIwGedthF+EDGbzZzdrMoilUHK4HI8Hn1D5RAEzcsqsw9DAv4THqCV1CqUz5p89jwg1OtgGCzEFpszfFHujEYjh0eEvUmZTCZWKpVcwYv2CCeGCwT4nCL6lrN/Sg5/CCgTwHQMBgMvSQK+4Mmco+BMhlrjm9vYjFq73fap6XQ6ThprPIiquZkQfDogt7EltFOhUPBgIUGUTBW5LIeBQhv8yETrEIOvX5twfgFptCTy4L9bkwAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e866c943663bc4d8360df84d7bc1c4de/a0b58/5_2_2.webp 230w","/static/e866c943663bc4d8360df84d7bc1c4de/f44a8/5_2_2.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e866c943663bc4d8360df84d7bc1c4de/81c8e/5_2_2.png 230w","/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/5_2_2.png 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e866c943663bc4d8360df84d7bc1c4de/6b1e2/5_2_2.png","alt":"5 2 2","title":"5 2 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python finns också datatyper som är föränderliga. Till exempel innehållet på en lista kan förändras utan att man behöver skapa en ny lista:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 271px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8b07a257ace229cc26a5eedf03a60986/5fce1/5_2_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 70.43478260869564%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeUlEQVQ4y5WT66rCQAyE9337w8eT/hEfQVCs4K2iWO+1VevdyDewBxE91EDYbHZ3Opmkbj6fWxzH1uv1bDab2Xa7tTzPLUkSW61WtlgslJ9Op4bd73d7PB5f3RVFIQB8v98rebvd7HA42G63U471eDxaGXP2g202G1WwXq/l7zHn7nK5iAFMKQd2+PV61Up+MBjY6XRS6TBFCkCIAVkul6qIveNStVq1Wq2mi/1+37rdrpy42WxapVKRzoB7KXgMkM/RC5XsNYPpu+AYTHH24/FYmtIw3APRMD5C2c4/xD517fUMVoDA6puXbsor+L9dRsPRaGTD4VDUKQtt/LiwZ6TKzKDmkId+HNI0tSzLBISmOGDn87n0aP1UMqMDOOu32NFB3+FPxgTAnBVZqAaJkIL8ZDLR3g+4o+1hGFq9XtccMn+dTkdO3Gg0LAgCgdFFAACCAHJABnZIppL5MgckiCmNy74BXIYVZ61WS38Na7vd1keiKPrLs38CMo80d8OuAIYAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8b07a257ace229cc26a5eedf03a60986/a0b58/5_2_3.webp 230w","/static/8b07a257ace229cc26a5eedf03a60986/cadab/5_2_3.webp 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8b07a257ace229cc26a5eedf03a60986/81c8e/5_2_3.png 230w","/static/8b07a257ace229cc26a5eedf03a60986/5fce1/5_2_3.png 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8b07a257ace229cc26a5eedf03a60986/5fce1/5_2_3.png","alt":"5 2 3","title":"5 2 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Något förvånande är att också grundläggande datatyper för lagring av siffor och sanningsvärden, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":", är oföränderliga. Låt oss använda följande kod som exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det verkar som att koden ändrar på siffran, men från teknisk synvinkel är det inte så. Istället skapar varje instruktion en ny siffra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften från det här programmet är intressant:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4535856912\n4535856944\n4535856912"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I början refererar variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" till adressen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4535856912"}]},{"type":"text","value":" och när variabelns värde förändras refererar variabeln till adressen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4535856944"}]},{"type":"text","value":". När variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" definieras och får värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", kommer variabeln att referera till samma ställe som variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffra"}]},{"type":"text","value":" när dess värde var "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det verkar som att Python har lagrat siffran 1 till adressen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4535856912"}]},{"type":"text","value":" och alltid då en variabels värde är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", refererar variabeln till det här specifika stället i \"datorns minne\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om de grundläggande datatyperna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" är referenser behöver man som programmerare inte egentligen fundera på det."}]},{"type":"element","tagName":"h2","properties":{"id":"flera-referenser-till-en-och-samma-lista","style":"position:relative;"},"children":[{"type":"text","value":"Flera referenser till en och samma lista"},{"type":"element","tagName":"a","properties":{"href":"#flera-referenser-till-en-och-samma-lista","ariaLabel":"flera referenser till en och samma lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi undersöker som ett exempel att kopiera värdet på en variabel med en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a\nb"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Deklarationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b = a"}]},{"type":"text","value":" kopierar variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":":s värde till variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". Det är ändå viktigt att observera att variabelns värde inte är en lista utan en referens till listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Deklarationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b = a"}]},{"type":"text","value":" kopierar alltså referensen, varpå det efter kopieringen finns två referenser till samma lista."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 271px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 129.99999999999997%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAAsTAAALEwEAmpwYAAACN0lEQVRIx52VV64qQQxEZ60sgC2wJvhhFXwhkMg555zx07FU3FHf0QVeS1aHoctlu9xE4/HYlsulbbdbm06nNpvNjDPm4XBoj8fDGM/n8yOLdrudX8KOx6MDbzYbOxwOvtc3fvzJiOyLsVqtbL1ee0SLxeLXmu8OeLvd7H6/OxNmGfvT6WSdTscul4ungv18PncQ1gCSHiJi74C1Ws0Gg4H1+32rVCq+xxqNhhWLRUun09Zutz0FXCRNXAZIZ5PJ5CdkMQwTzLher26wxSGXR6ORm4AoHk4IO4on+6/E801s/rIo6WI4f1phD5kw9vu9S4WchCAh8FsdojtydD6fHTQJ8CuGSSEnFYcZ6cggABGtdR594p0KogKkQyV7vZ4ba+SGTiVwZ1iv1730fCiXy67BZrNp1WrV8vm8pVIpB0N7qjSs6AydAfYKmQPoarBvtVoODjBag2G32/U8wwqDFXqksAC+Wk/NT4FgS7eo4qEOYSTNwVJrMXXAUqnkLPCEl/jQa/NVlWm9pEthF32kQ/UqplcGB9i7dkxkSEWpIFUmF0o6r/d/dUr8HdQIXx7tk9IQrr96sXlgiYACskbYrJEOxeThdUD9hyhkGbJAX9ls9vV4sGeme3hUkBpgzETigIgXj3hSPhF2oVCwXC5nmUzGv3OR/MIKzSFsgcLOcxhWkT2XKRYOVG0GOoUZ5zgFiDVOiOAVMh4Jl9ai3QCM61JF4Sz+hyb9xs9fIZMX6IddEj5j73T4DynC3t7bOJziAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73c7fae5118d6e5b03c576f1a9d14c3f/a0b58/5_2_4.webp 230w","/static/73c7fae5118d6e5b03c576f1a9d14c3f/cadab/5_2_4.webp 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73c7fae5118d6e5b03c576f1a9d14c3f/81c8e/5_2_4.png 230w","/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_4.png 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_4.png","alt":"5 2 4","title":"5 2 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan kan behandlas med båda referenserna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nlista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[10, 20, 3, 4]\n[10, 20, 3, 4]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en och samma lista har flera referenser kan den behandlas på samma sätt med vilken som helst av referenserna. Däremot återspeglas en förändring via en referens också till alla andra referenser."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Visualiseringsverktyget klargör igen vad som sker i programmet:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cf84b632e91094d24c611bd9d800a737/c6720/5_2_4a.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 32.608695652173914%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA30lEQVQY03WQwW7DIAyG8/7vtcsm7bLTTu0ydQmFMMC4CWCbKUyjUaV9Bw6WP/4fhnogpew95FyOQxEBgBBCjPHWSClxY+hLRDR+qtM4T8qu68rMfT5Ns1JXACh/iMij7J1DjIhIRCLSk9u5R4lwzvk3lojuMjMbs2htELGb+6XMLuDXbCDeiOjb2Vq51n3hLqecrF2M0Yjx+OZC/D4uz2/niwbcNutevX+K8UUEB9nLCDGXUpRxp/G62NDb7o1EXEwfFx3X3DJg21zOQYQHa60xxnvfhEpcWerDb9d/+AGpwZhBrvAmDQAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cf84b632e91094d24c611bd9d800a737/a0b58/5_2_4a.webp 230w","/static/cf84b632e91094d24c611bd9d800a737/bc10c/5_2_4a.webp 460w","/static/cf84b632e91094d24c611bd9d800a737/966d8/5_2_4a.webp 920w","/static/cf84b632e91094d24c611bd9d800a737/445df/5_2_4a.webp 1380w","/static/cf84b632e91094d24c611bd9d800a737/78de1/5_2_4a.webp 1840w","/static/cf84b632e91094d24c611bd9d800a737/da98d/5_2_4a.webp 1922w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cf84b632e91094d24c611bd9d800a737/81c8e/5_2_4a.png 230w","/static/cf84b632e91094d24c611bd9d800a737/08a84/5_2_4a.png 460w","/static/cf84b632e91094d24c611bd9d800a737/c0255/5_2_4a.png 920w","/static/cf84b632e91094d24c611bd9d800a737/b1001/5_2_4a.png 1380w","/static/cf84b632e91094d24c611bd9d800a737/161ec/5_2_4a.png 1840w","/static/cf84b632e91094d24c611bd9d800a737/c6720/5_2_4a.png 1922w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cf84b632e91094d24c611bd9d800a737/c0255/5_2_4a.png","alt":"5 2 4a","title":"5 2 4a","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"att-kopiera-en-lista","style":"position:relative;"},"children":[{"type":"text","value":"Att kopiera en lista"},{"type":"element","tagName":"a","properties":{"href":"#att-kopiera-en-lista","ariaLabel":"att kopiera en lista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill skapa en verklig kopia av en lista kan du skapa en ny lista och lägga till alla element från den ursprungliga listan till den nya listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nkopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nkopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lista\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kopia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"lista [1, 2, 3, 3, 5]\nkopia [10, 2, 3, 3, 5, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så ser det ut från visualiseringsverktyget:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/66b9f72bbac2f55cd8fac4539ba92dba/e431d/5_2_4b.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 43.04347826086957%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABGElEQVQoz32RzW7DIBCE/f4v1kY99NBTFFWtZMcQ29iADRjvD5WhcXJpPyEOLLM7mq3SHeZkZzdOFpDSEwDg7vgMAFCmOj45H1qpWjlqs4Tgmbm8bxlALBpEJCJmBoCHeJ6ttQZgI0RAfHLEef7GzDFuMW5ETMSImMWcKK5uWW5dd+s6KW/GmEPsQ5S9/q4FIjm3OG9Sgny4OnpP09Q0zfV6XZalGCu+rYsfF/F+blwkF5S1J61frH1lXh+2lVJ13Qgh+r4fxzEb2gkRL3V//pIR2IXZhxpgINr2yQB7EiUGpb3SPqxwpFV8MTPRvoIQtDFv6/pZStU4TcMweO9TSnsD+hXyE+kPqlJTSrVtK6UstxDiWOY/4h8FSwu1d8EohwAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/66b9f72bbac2f55cd8fac4539ba92dba/a0b58/5_2_4b.webp 230w","/static/66b9f72bbac2f55cd8fac4539ba92dba/bc10c/5_2_4b.webp 460w","/static/66b9f72bbac2f55cd8fac4539ba92dba/966d8/5_2_4b.webp 920w","/static/66b9f72bbac2f55cd8fac4539ba92dba/445df/5_2_4b.webp 1380w","/static/66b9f72bbac2f55cd8fac4539ba92dba/78de1/5_2_4b.webp 1840w","/static/66b9f72bbac2f55cd8fac4539ba92dba/609ff/5_2_4b.webp 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/66b9f72bbac2f55cd8fac4539ba92dba/81c8e/5_2_4b.png 230w","/static/66b9f72bbac2f55cd8fac4539ba92dba/08a84/5_2_4b.png 460w","/static/66b9f72bbac2f55cd8fac4539ba92dba/c0255/5_2_4b.png 920w","/static/66b9f72bbac2f55cd8fac4539ba92dba/b1001/5_2_4b.png 1380w","/static/66b9f72bbac2f55cd8fac4539ba92dba/161ec/5_2_4b.png 1840w","/static/66b9f72bbac2f55cd8fac4539ba92dba/e431d/5_2_4b.png 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/66b9f72bbac2f55cd8fac4539ba92dba/c0255/5_2_4b.png","alt":"5 2 4b","title":"5 2 4b","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_lista"}]},{"type":"text","value":" refererar till en annan lista än "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min_lista"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett enklare sätt att kopiera en lista är att använda hakparenteser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":", som vi använt tidigare för att extrahera innehåll från strängar och listor. Notationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[:]"}]},{"type":"text","value":" väljer alla element i en samling. Därmed skapar det en kopia av en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nkopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nkopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[10, 2, 3, 4]\n[1, 20, 3, 4]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-lista-som-parameter-i-en-funktion","style":"position:relative;"},"children":[{"type":"text","value":"En lista som parameter i en funktion"},{"type":"element","tagName":"a","properties":{"href":"#en-lista-som-parameter-i-en-funktion","ariaLabel":"en lista som parameter i en funktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När en lista ges som parameter till en funktion, förmedlas referensen till listan. Det här innebär att funktionen kan ändra på listan som getts som parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel följande funktion lägger till ett nytt element i en lista som getts som funktionens parameter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"lagg_till_element"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n nytt_element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nytt_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlagg_till_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n[1, 2, 3]\n[1, 2, 3, 10]\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lagg_till_element"}]},{"type":"text","value":" inte returnerar något, utan ändrar på den lista som getts som funktionens parameter. Visualiseringsverktyget presenterar situationen så här:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9be11dea0a28e2151e4f4f7101f82472/764d0/5_2_4c.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 35.65217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA+UlEQVQY01WO226DMBAF+f9/62P7UFVKq0BCMdgLOOs1e6sITdTO45FGZxp/oKZY6NwN5y6UUlXVzNzdzOZ5jjHmnG93aq1mJiLNYZrqVimm1Lbd5dqP47Qui6oespnpHXcXkWNh5ofMzERrXodhiDGWUojo+WxmtVZEVFUAEOFjbJ7ZiNh1bd9fc17dzf8AAG3bIqK7r+st54KIe7Y9ICKA+dJPMWVmOSKPZ2aBeaFaN1ZYPhDfVIu7NzhDShBjJCJ3Z/GNj95fzP3UTS+vX21YVK1QX8rJrO6ybBsziwgAjGOIcZqmMYSQUlJVkT3hOi7vn98hrf6fH4cdljeAKLvbAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9be11dea0a28e2151e4f4f7101f82472/a0b58/5_2_4c.webp 230w","/static/9be11dea0a28e2151e4f4f7101f82472/bc10c/5_2_4c.webp 460w","/static/9be11dea0a28e2151e4f4f7101f82472/966d8/5_2_4c.webp 920w","/static/9be11dea0a28e2151e4f4f7101f82472/445df/5_2_4c.webp 1380w","/static/9be11dea0a28e2151e4f4f7101f82472/78de1/5_2_4c.webp 1840w","/static/9be11dea0a28e2151e4f4f7101f82472/b4500/5_2_4c.webp 1898w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9be11dea0a28e2151e4f4f7101f82472/81c8e/5_2_4c.png 230w","/static/9be11dea0a28e2151e4f4f7101f82472/08a84/5_2_4c.png 460w","/static/9be11dea0a28e2151e4f4f7101f82472/c0255/5_2_4c.png 920w","/static/9be11dea0a28e2151e4f4f7101f82472/b1001/5_2_4c.png 1380w","/static/9be11dea0a28e2151e4f4f7101f82472/161ec/5_2_4c.png 1840w","/static/9be11dea0a28e2151e4f4f7101f82472/764d0/5_2_4c.png 1898w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9be11dea0a28e2151e4f4f7101f82472/c0255/5_2_4c.png","alt":"5 2 4c","title":"5 2 4c","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Global frame syftar på huvudprogrammets variabler, och den blå lådan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lagg_till_element"}]},{"type":"text","value":" på funktionens parametrar och variabler. Som visualiseringen visar, refererar funktionen till samma lista som huvudprogrammet, vilket betyder att ändringar som görs i listan inom funktionen också syns i huvudprogrammet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt är att skapa en ny lista och returnera den:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"lagg_till_element"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n nytt_element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n kopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nytt_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" kopia\n\nsiffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nsiffror2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lagg_till_element"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ursprunglig lista:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ny lista:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffror2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ursprunglig lista: [1, 2, 3]\nNy lista: [1, 2, 3, 10]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du inte är helt säker på vad som händer i en kodsnutt, kan det löna sig att utnyttja visualiseringsverktyget."}]},{"type":"element","tagName":"h2","properties":{"id":"andra-pa-en-lista-som-getts-som-argument","style":"position:relative;"},"children":[{"type":"text","value":"Ändra på en lista som getts som argument"},{"type":"element","tagName":"a","properties":{"href":"#andra-pa-en-lista-som-getts-som-argument","ariaLabel":"andra pa en lista som getts som argument permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande är ett försök på att skapa en funktion som ökar på varje element med tio:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oka_pa_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ny_lista\n\nsiffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"start:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noka_pa_alla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"efter funktionen:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"start: [1, 2, 3]\nefter funktionen: [1, 2, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Av någon orsak fungerar funktionen inte. Varför så?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen tar emot en referens till en lista som argument. Det här är lagrat i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min_lista"}]},{"type":"text","value":". Tilldelningen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min_lista = ny_lista"}]},{"type":"text","value":" tilldelar ett nytt värde till den samma variabeln. Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min_lista"}]},{"type":"text","value":" hänvisar nu till den nya listan som skapades i funktionen, vilket betyder att referensen till den ursprungliga listan inte mera är tillgänglig inom funktionen. Tilldelningen har inte dock någon påverkan utanför funktionen."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 442px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cd75af1816ee2f9b354a8fc7017fc05b/e03bf/5_2_6.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 157.82608695652175%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAgCAYAAAASYli2AAAACXBIWXMAAAsSAAALEgHS3X78AAACOklEQVRIx6WW54rDMBCE8/5vlj8hgUB6Ib333vb4FjbodIoicwYh25EmM7NFzolzvV4v91Eej4dMJhMZDodyvV4lttaunL/g+XzK+XzW5/v9Lr1eTyqVisznczkcDrLb7fSPPoH+ATydTlKtVnUj98fjUVarlSyXS2m1WjIajd5so4AxGSbfFNi6JIbMJncwGEin01FWSMbL6XSqoJkZcj8ej9VDmwFn8GdRQGNlkur1uiwWC92Ij3i43+814nh5u92ySSbKgAGOd5fLRVkie71epzG02fwByO5JF1gm56ErGY/y+bx0u10FBAy57rqvUXYXAYBX5B/SfTBfUVKUAYOh65U/khm697Hray37CymvRqMhzWZTaxnGlF6tVvuVNj5wUDIRBhD/NpvNu5Zns5kOcjMpD20BJQYbQAkMgOQfedjv97Vx/Ls5AMYgYEndxi09k2hMYAlDv9ZD0c6F/CsWi1IoFBQYMHxMjfZHyZhPDSPRLqTyTFAYXxm6MmhX9EMDhS29kHf8RnBCR0Gw23DhGZtgQt4x25nC4D7K8FuUM3voRhlm7XZb5TFzBCCTquGdyXYl2/4gIACAkuCUmpVfuVxWIPuDj4C+F/i13W71mU2kDoGhFAmOe+gHJfuAnCe0fILBZgJAkIgyrAlKcmLHzLdjwT9G/wDaC3IOn0qlkiZ2rD9GGdoLOjSGE0WrED/hM6eNXdQy4ADjGZ7iLR0H9syhL4hc6DOEhRxUVs9E19qXfd4xh0rvBwfxyIYimKgPAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cd75af1816ee2f9b354a8fc7017fc05b/a0b58/5_2_6.webp 230w","/static/cd75af1816ee2f9b354a8fc7017fc05b/3fa5c/5_2_6.webp 442w"],"sizes":"(max-width: 442px) 100vw, 442px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cd75af1816ee2f9b354a8fc7017fc05b/81c8e/5_2_6.png 230w","/static/cd75af1816ee2f9b354a8fc7017fc05b/e03bf/5_2_6.png 442w"],"sizes":"(max-width: 442px) 100vw, 442px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cd75af1816ee2f9b354a8fc7017fc05b/e03bf/5_2_6.png","alt":"5 2 6","title":"5 2 6","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom innehåller variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_lista"}]},{"type":"text","value":" nu de nya värdena, men de är inte tillgängliga utanför funktionen. Variabeln försvinner alltså när funktionen körts och programmet fortsätter tillbaka till huvudfunktionen. Variabeln siffror i huvudfunktionen hänvisar alltid till den ursprungliga listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Visualiseringsverktyget hjälper igen. När du går igenom stegen utförligt märker du att den ursprungliga listan inte påverkas på något sätt av funktionen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/19d3606e35d311c11edb9891cf40eeeb/75d3b/5_2_4d.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 41.30434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAABLklEQVQY01WQ227bMBBE/f/fFgRFAhQwEECuK9GWaZCWRHGXe20UJW1znvblDGb24B+oKCKkPJ9+j+GaEFtrzczcHRFzzsuyAAARMbOZiYiZHT5l1VLK+Xy+Xq8xxnVdAUBE3H2PEBEiMrM9VFW/ZFNuqGYppRhjCGGeZyJqrbk7MwNirRURVTWlTMTywS4bE6Wcp2kqpQCA/wc2jLfbJYTW0Mzm+bGu8571WZuZh2Houm4YhtPptNfeCzfiqUBMU1m30FKOtb66q7sfmBoiAoCqNJJ7Xqfyb5W7L5V+HPvnn79CqhVpKT1iZ1Y2uU4bj8cDAJi3McxMRNshYqoF5diNzy9v4V5EdV7epumptcsm/92Wcx7Hse/7EEKMsdZqGwokOa+3yz0v337xLv8BaHfNshxTjQQAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19d3606e35d311c11edb9891cf40eeeb/a0b58/5_2_4d.webp 230w","/static/19d3606e35d311c11edb9891cf40eeeb/bc10c/5_2_4d.webp 460w","/static/19d3606e35d311c11edb9891cf40eeeb/966d8/5_2_4d.webp 920w","/static/19d3606e35d311c11edb9891cf40eeeb/445df/5_2_4d.webp 1380w","/static/19d3606e35d311c11edb9891cf40eeeb/78de1/5_2_4d.webp 1840w","/static/19d3606e35d311c11edb9891cf40eeeb/ee81e/5_2_4d.webp 1966w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19d3606e35d311c11edb9891cf40eeeb/81c8e/5_2_4d.png 230w","/static/19d3606e35d311c11edb9891cf40eeeb/08a84/5_2_4d.png 460w","/static/19d3606e35d311c11edb9891cf40eeeb/c0255/5_2_4d.png 920w","/static/19d3606e35d311c11edb9891cf40eeeb/b1001/5_2_4d.png 1380w","/static/19d3606e35d311c11edb9891cf40eeeb/161ec/5_2_4d.png 1840w","/static/19d3606e35d311c11edb9891cf40eeeb/75d3b/5_2_4d.png 1966w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/19d3606e35d311c11edb9891cf40eeeb/c0255/5_2_4d.png","alt":"5 2 4d","title":"5 2 4d","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett enkelt sätt att korrigera problemet är att kopiera över alla element från den nya listan till den gamla:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oka_pa_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi kopierar de nya värdena till den gamla listan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eller lite enklare tack vare Python:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan ersätts en del av en lista med värden från en annan samling."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som vi vet, kan vi också göra detta för en hel samling:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"98"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"97"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" lista\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"98"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"97"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Allt innehåll i den gamla listan ersätts. Inspirerat av det här har vi nu skapat en fungerande version av funktionen som ökar på elementens värden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oka_pa_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny_lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"element "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ny_lista"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Egentligen finns det ingen orsak att skapa en ny lista inom funktionen. Vi kan helt enkelt tilldela värdena direkt till den ursprungliga listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oka_pa_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Elementen fördubblade","tmcname":"osa05-06a_elementen_x2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elementen_fordubblade(siffror: list)"}]},{"type":"text","value":" som får som argument en lista med siffror."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera en ny lista där alla siffror är multiplicerade med två. Funktionen får inte ändra på den ursprungliga listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fordubblade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" elementen_fordubblade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ursprunglig:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fördubblade:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fordubblade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ursprunglig: [2, 4, 5, 3, 11, -4]\nfördubblade: [4, 8, 10, 6, 22, -8]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bort med minsta","tmcname":"osa05-06b_minsta_bort"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"avlagsna_minsta(siffror: list)"}]},{"type":"text","value":" som får som argument en lista med siffror."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska ta bort den minsta siffran från listan. Du kan anta att den minsta siffran endast förekommer en gång."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska inte returnera något, den ska endast ändra på listan som getts som argument!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n avlagsna_minsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 4, 6, 3, 5]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Utskrift och att lägga till en siffra","tmcname":"osa05-07_sudoku_utskrift_ny_siffra"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi ännu två funktioner för ett sudoku: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lagg_till"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"text","value":" får som argument en matris och skriver den ut enligt exemplet nedan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lagg_till(sudoku: list, radnummer: int, kolumnnummer: int, siffra: int)"}]},{"type":"text","value":" tar emot som argument en matris, två siffror som indikerar positionen och ett tal (1-9) som ska lagras."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlagg_till"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlagg_till"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlagg_till"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tre siffror tillagda:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\nTre siffror tillagda:\n\n2 _ _ _ _ _ _ _ _\n_ _ 7 _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ 3 _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Du kan dra nytta av att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-instruktionen kan användas så att radbyten inte görs:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tecken \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"utan mellanrum\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"tecken utan mellanrum"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En radbrytning kan åstadkommas så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: Lägga till en siffra i en kopia av sudokut","tmcname":"osa05-08_sudoku_ny_siffra_kopia"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi en lite annorlunda version av funktionen som lägger till nya tal i ett sudoku."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kopiera_och_lagg_till(sudoku: list, radnummer: int, kolumnnummer: int, siffra: int)"}]},{"type":"text","value":" får som argument en matris, två siffror som indikerar en position samt en siffra (1-9) som ska lagras. Funktionen ska returnera en kopia av matrisen som gavs som argument, med den angivna siffran lagrad på korrekt ställe. Funktionen får inte ändra på matrisen som getts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här utnyttjar vi funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"text","value":" från den föregående uppgiften:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"sudoku "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nkopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kopiera_och_lagg_till"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ursprunglig:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kopia:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Ursprunglig:\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\nKopia:\n2 _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: I den här uppgiften måste man vara noga med vad allt som behöver kopieras och vart man slutligen lagrar värdet som getts till funktionen. "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" kan hjälpa. P.g.a. storleken av sudokut kan vyn dock vara mer råddig än vanligtvis."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Tre i rad","tmcname":"osa05-09_tre_i_rad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tre i rad spelas med ett 3 x 3 -rutnät, där spelarna turvis markerar ett kryss eller en ring. Spelaren som får tre markeringar i rad, vågrätt, lodrätt eller diagonalt, vinner. Om ingendera av spelarna får det, är spelet oavgjort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tur(brade: list, x: int, y: int, markering: str)"}]},{"type":"text","value":" där den givna markeringen görs på stället som indikeras av koordinaterna (0-2)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" indikerar kolumn och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spelbrädet består av följande strängar:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":": tom ruta"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"X\""}]},{"type":"text","value":": markeing, spelare 1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"O\""}]},{"type":"text","value":": markeing, spelare 2"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" om markeringen lyckades (stället var tomt på brädet), och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" om stället var reserverat eller koordinaterna inte var i intervallet 0-2."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"brade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"X\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\n[['', '', 'X'], ['', '', ''], ['', '', '']]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Transponera","tmcname":"osa05-10_transponera"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"transponera(matris: lista)"}]},{"type":"text","value":" som får som argument en matris. Funktionen ska transponera matrisen, alltså byta om rader till kolumner och tvärtom."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att matrisen har lika många rader och kolumner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här matrisen..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...skulle se ut så här efter transponeringen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska inte returnera något. Den ska ändra på matrisen som den fått som argument."}]}]},{"type":"element","tagName":"h2","properties":{"id":"sidoeffekter-hos-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Sidoeffekter hos funktioner"},{"type":"element","tagName":"a","properties":{"href":"#sidoeffekter-hos-funktioner","ariaLabel":"sidoeffekter hos funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en funktion tar emot en referens till en lista som argument, kan funktionen ändra på listan. Om programmeraren inte har tagit i beaktande att listan kan ändras på direkt, kan ändringar i listan förorsaka problem på andra håll i programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en funktion som borde hitta det näst minsta värdet i en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nast_minst"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# i en ordnad lista finns det näst minsta elementet vid index 1"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsiffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nast_minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n2\n[1, 2, 3, 4, 4, 5, 6, 7]\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen hittar det näst minsta värdet, men dessutom ordnar funktionen listan. Om ordningen av elementen har betydelse på andra håll i programmet kommer det här funktionsanropet eventuellt att orsaka fel. Oplanerade modifikationer som görs hos objekt som ges som referens till en funktion kallas sidoeffekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan förhindra den här sidoeffekten genom att göra en liten ändring i funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"nast_minst"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kopia "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" kopia"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsiffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nast_minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n[1, 4, 2, 5, 3, 6, 4, 7]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" returnerar en ny ordnad kopia av listan, så vi behöver inte mera \"sabotera\" den ursprungliga listan när vi söker efter det näst minsta värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är en bra vana att undvika sidoeffekter i funktioner. Sidoeffekter kan göra det svårare att säkerställa att programmet fungerar som det ska i alla situationer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktioner som saknar sidoeffekter kallas rena funktioner. Då man arbetar med funktionell programmering är rena funktioner speciellt viktiga. Vi dyker djupare i det här under fortsättningskursen i programmering."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"21ff9f90-0aed-591d-8ff0-60e790724739"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad som menas med en referens till en variabel
  • \n
  • vet du att det kan finnas flera referenser till ett och samma objekt
  • \n
  • kan du använda listor som parameter i funktioner
  • \n
  • förstår du vad som menas med sidoeffekt hos en funktion.
  • \n

Hittills har vi tänkt att en variabel är en slags \"låda\" som innehåller variabelns värde. Från teknisk synvinkel stämmer detta inte i Python – variabler innehåller inte ett värde, utan en referens till ett objekt, som en siffra, sträng eller en lista.

I praktiken innebär det att man inte lagrar ett värde i en variabel, utan ett ställe varifrån variabelns värde hittas från.

En referens kan beskrivas som en pil från variabeln till dess riktiga värde:

\n \n \n \n \n \n \"5\n \n \n

I Python finns också datatyper som är föränderliga. Till exempel innehållet på en lista kan förändras utan att man behöver skapa en ny lista:

\n \n \n \n \n \n \"5\n \n \n

Listan kan behandlas med båda referenserna:

lista = [1, 2, 3, 4]\nlista2 = lista\n\nlista[0] = 10\nlista2[1] = 20\n\nprint(lista)\nprint(lista2)

[10, 20, 3, 4]\n[10, 20, 3, 4]

Om en och samma lista har flera referenser kan den behandlas på samma sätt med vilken som helst av referenserna. Däremot återspeglas en förändring via en referens också till alla andra referenser.

Visualiseringsverktyget klargör igen vad som sker i programmet:

\n \n \n \n \n \n \"5\n \n \n

Att kopiera en lista

Om du vill skapa en verklig kopia av en lista kan du skapa en ny lista och lägga till alla element från den ursprungliga listan till den nya listan:

lista = [1, 2, 3, 3, 5]\n\nkopia = []\nfor element in lista:\n    kopia.append(element)\n\nkopia[0] = 10\nkopia.append(6)\nprint(\"lista\", lista)\nprint(\"kopia\", kopia)

lista [1, 2, 3, 3, 5]\nkopia [10, 2, 3, 3, 5, 6]

Så ser det ut från visualiseringsverktyget:

\n \n \n \n \n \n \"5\n \n \n

Variabeln ny_lista refererar till en annan lista än min_lista.

Ett enklare sätt att kopiera en lista är att använda hakparenteser [], som vi använt tidigare för att extrahera innehåll från strängar och listor. Notationen [:] väljer alla element i en samling. Därmed skapar det en kopia av en lista:

lista = [1,2,3,4]\nkopia = lista[:]\n\nlista[0] = 10\nkopia[1] = 20\n\nprint(lista)\nprint(kopia)

[10, 2, 3, 4]\n[1, 20, 3, 4]

En lista som parameter i en funktion

När en lista ges som parameter till en funktion, förmedlas referensen till listan. Det här innebär att funktionen kan ändra på listan som getts som parameter.

Till exempel följande funktion lägger till ett nytt element i en lista som getts som funktionens parameter:

def lagg_till_element(lista: list):\n    nytt_element = 10\n    lista.append(nytt_element)\n\nlista = [1,2,3]\nprint(lista)\nlagg_till_element(lista)\nprint(lista)
\n[1, 2, 3]\n[1, 2, 3, 10]\n

Märk att funktionen lagg_till_element inte returnerar något, utan ändrar på den lista som getts som funktionens parameter. Visualiseringsverktyget presenterar situationen så här:

\n \n \n \n \n \n \"5\n \n \n

Global frame syftar på huvudprogrammets variabler, och den blå lådan lagg_till_element på funktionens parametrar och variabler. Som visualiseringen visar, refererar funktionen till samma lista som huvudprogrammet, vilket betyder att ändringar som görs i listan inom funktionen också syns i huvudprogrammet.

Ett annat sätt är att skapa en ny lista och returnera den:

def lagg_till_element(lista: list) -> list:\n    nytt_element = 10\n    kopia = lista[:]\n    kopia.append(nytt_element)\n    return kopia\n\nsiffror = [1, 2, 3]\nsiffror2 = lagg_till_element(siffror)\n\nprint(\"Ursprunglig lista:\", siffror)\nprint(\"Ny lista:\", siffror2)

Ursprunglig lista: [1, 2, 3]\nNy lista: [1, 2, 3, 10]

Om du inte är helt säker på vad som händer i en kodsnutt, kan det löna sig att utnyttja visualiseringsverktyget.

Ändra på en lista som getts som argument

Det följande är ett försök på att skapa en funktion som ökar på varje element med tio:

def oka_pa_alla(lista: list):\n    ny_lista = []\n    for element in lista:\n        ny_lista.append(element + 10)\n    lista = ny_lista\n\nsiffror = [1, 2, 3]\nprint(\"start:\",siffror)\noka_pa_alla(siffror)\nprint(\"efter funktionen:\", siffror)

start: [1, 2, 3]\nefter funktionen: [1, 2, 3]

Av någon orsak fungerar funktionen inte. Varför så?

Funktionen tar emot en referens till en lista som argument. Det här är lagrat i variabeln min_lista. Tilldelningen min_lista = ny_lista tilldelar ett nytt värde till den samma variabeln. Variabeln min_lista hänvisar nu till den nya listan som skapades i funktionen, vilket betyder att referensen till den ursprungliga listan inte mera är tillgänglig inom funktionen. Tilldelningen har inte dock någon påverkan utanför funktionen.

\n \n \n \n \n \n \"5\n \n \n

Dessutom innehåller variabeln ny_lista nu de nya värdena, men de är inte tillgängliga utanför funktionen. Variabeln försvinner alltså när funktionen körts och programmet fortsätter tillbaka till huvudfunktionen. Variabeln siffror i huvudfunktionen hänvisar alltid till den ursprungliga listan.

Visualiseringsverktyget hjälper igen. När du går igenom stegen utförligt märker du att den ursprungliga listan inte påverkas på något sätt av funktionen:

\n \n

Skapa funktionen elementen_fordubblade(siffror: list) som får som argument en lista med siffror.

Funktionen ska returnera en ny lista där alla siffror är multiplicerade med två. Funktionen får inte ändra på den ursprungliga listan.

Exempel:

if __name__ == \"__main__\":\n    siffror = [2, 4, 5, 3, 11, -4]\n    fordubblade = elementen_fordubblade(siffror)\n    print(\"ursprunglig:\", siffror)\n    print(\"fördubblade:\", fordubblade)

ursprunglig: [2, 4, 5, 3, 11, -4]\nfördubblade: [4, 8, 10, 6, 22, -8]

Skapa funktionen avlagsna_minsta(siffror: list) som får som argument en lista med siffror.

Funktionen ska ta bort den minsta siffran från listan. Du kan anta att den minsta siffran endast förekommer en gång.

Funktionen ska inte returnera något, den ska endast ändra på listan som getts som argument!

Exempel:

if __name__ == \"__main__\":\n    siffror = [2, 4, 6, 1, 3, 5]\n    avlagsna_minsta(siffror)\n    print(siffror)

[2, 4, 6, 3, 5]

I den här uppgiften skapar vi ännu två funktioner för ett sudoku: skriv_ut och lagg_till.

Funktionen skriv_ut får som argument en matris och skriver den ut enligt exemplet nedan.

Funktionen lagg_till(sudoku: list, radnummer: int, kolumnnummer: int, siffra: int) tar emot som argument en matris, två siffror som indikerar positionen och ett tal (1-9) som ska lagras.

sudoku  = [\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n]\n\nskriv_ut(sudoku)\nlagg_till(sudoku, 0, 0, 2)\nlagg_till(sudoku, 1, 2, 7)\nlagg_till(sudoku, 5, 7, 3)\nprint()\nprint(\"Tre siffror tillagda:\")\nprint()\nskriv_ut(sudoku)
\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\nTre siffror tillagda:\n\n2 _ _  _ _ _  _ _ _\n_ _ 7  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ 3 _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n

Tips: Du kan dra nytta av att print-instruktionen kan användas så att radbyten inte görs:

print(\"tecken \", end=\"\")\nprint(\"utan mellanrum\", end=\"\")

tecken utan mellanrum

En radbrytning kan åstadkommas så här:

print()

I den här uppgiften skapar vi en lite annorlunda version av funktionen som lägger till nya tal i ett sudoku.

Funktionen kopiera_och_lagg_till(sudoku: list, radnummer: int, kolumnnummer: int, siffra: int) får som argument en matris, två siffror som indikerar en position samt en siffra (1-9) som ska lagras. Funktionen ska returnera en kopia av matrisen som gavs som argument, med den angivna siffran lagrad på korrekt ställe. Funktionen får inte ändra på matrisen som getts som argument.

Här utnyttjar vi funktionen skriv_ut från den föregående uppgiften:

sudoku  = [\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n]\n\nkopia = kopiera_och_lagg_till(sudoku, 0, 0, 2)\nprint(\"Ursprunglig:\")\nskriv_ut(sudoku)\nprint()\nprint(\"Kopia:\")\nskriv_ut(kopia)
\nUrsprunglig:\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\nKopia:\n2 _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n

Tips: I den här uppgiften måste man vara noga med vad allt som behöver kopieras och vart man slutligen lagrar värdet som getts till funktionen. Visualiseringsverktyget kan hjälpa. P.g.a. storleken av sudokut kan vyn dock vara mer råddig än vanligtvis.

Tre i rad spelas med ett 3 x 3 -rutnät, där spelarna turvis markerar ett kryss eller en ring. Spelaren som får tre markeringar i rad, vågrätt, lodrätt eller diagonalt, vinner. Om ingendera av spelarna får det, är spelet oavgjort.

Skapa funktionen tur(brade: list, x: int, y: int, markering: str) där den givna markeringen görs på stället som indikeras av koordinaterna (0-2).

Observera att x indikerar kolumn och y rad.

Spelbrädet består av följande strängar:

    \n
  • \"\": tom ruta
  • \n
  • \"X\": markeing, spelare 1
  • \n
  • \"O\": markeing, spelare 2
  • \n

Funktionen returnerar True om markeringen lyckades (stället var tomt på brädet), och False om stället var reserverat eller koordinaterna inte var i intervallet 0-2.

Exempel:

brade = [[\"\", \"\", \"\"], [\"\", \"\", \"\"], [\"\", \"\", \"\"]]\nprint(tur(brade, 2, 0, \"X\"))\nprint(brade)

True\n[['', '', 'X'], ['', '', ''], ['', '', '']]

Skapa funktionen transponera(matris: lista) som får som argument en matris. Funktionen ska transponera matrisen, alltså byta om rader till kolumner och tvärtom.

Du kan anta att matrisen har lika många rader och kolumner.

Den här matrisen...

1 2 3\n4 5 6\n7 8 9

...skulle se ut så här efter transponeringen:

1 4 7\n2 5 8\n3 6 9

Funktionen ska inte returnera något. Den ska ändra på matrisen som den fått som argument.

Sidoeffekter hos funktioner

Om en funktion tar emot en referens till en lista som argument, kan funktionen ändra på listan. Om programmeraren inte har tagit i beaktande att listan kan ändras på direkt, kan ändringar i listan förorsaka problem på andra håll i programmet.

Låt oss ta en titt på en funktion som borde hitta det näst minsta värdet i en lista:

def nast_minst(lista: list) -> int:\n    # i en ordnad lista finns det näst minsta elementet vid index 1\n    lista.sort()\n    return lista[1]\n\nsiffror = [1, 4, 2, 5, 3, 6, 4, 7]\nprint(nast_minst(siffror))\nprint(siffror)
\n2\n[1, 2, 3, 4, 4, 5, 6, 7]\n

Funktionen hittar det näst minsta värdet, men dessutom ordnar funktionen listan. Om ordningen av elementen har betydelse på andra håll i programmet kommer det här funktionsanropet eventuellt att orsaka fel. Oplanerade modifikationer som görs hos objekt som ges som referens till en funktion kallas sidoeffekt.

Vi kan förhindra den här sidoeffekten genom att göra en liten ändring i funktionen:

def nast_minst(lista: list) -> int:\n    kopia = sorted(lista)\n    return kopia[1]\n\nsiffror = [1, 4, 2, 5, 3, 6, 4, 7]\nprint(nast_minst(siffror))\nprint(siffror)

2\n[1, 4, 2, 5, 3, 6, 4, 7]

Funktionen sorted returnerar en ny ordnad kopia av listan, så vi behöver inte mera \"sabotera\" den ursprungliga listan när vi söker efter det näst minsta värdet.

Det är en bra vana att undvika sidoeffekter i funktioner. Sidoeffekter kan göra det svårare att säkerställa att programmet fungerar som det ska i alla situationer.

Funktioner som saknar sidoeffekter kallas rena funktioner. Då man arbetar med funktionell programmering är rena funktioner speciellt viktiga. Vi dyker djupare i det här under fortsättningskursen i programmering.

","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/2-referenser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-5/3-lexikon/page-data.json b/page-data/osa-5/3-lexikon/page-data.json index 75586284b..c23457ef2 100644 --- a/page-data/osa-5/3-lexikon/page-data.json +++ b/page-data/osa-5/3-lexikon/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-5/3-lexikon", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till datatypen lexikon"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda lexikon med olika typer av nycklar och värden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man går igenom värden i ett lexikon"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"har du koll på olika användningsområden för lexikon."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listor kan vara händiga i flera situationer, men deras svaga punkt är att elementen hämtas med hjälp av index (0, 1, 2 o.s.v.). Om du vill hitta ett element i en lista måste du alltså veta dess index, eller alternativt gå igenom hela listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En ytterligare central datastruktur i Python är lexikon, som vi nu ska se på. I lexikon är elementen indexerade enligt nycklar. Varje nyckel har ett värde. Värden som lagrats i ett lexikon kan hämtas och ändras med hjälp av dess nyckel."}]},{"type":"element","tagName":"h2","properties":{"id":"att-anvanda-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Att använda lexikon"},{"type":"element","tagName":"a","properties":{"href":"#att-anvanda-lexikon","ariaLabel":"att anvanda lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande visar hur datastrukturen hos lexikon fungerar. Här är ett enkelt lexikon som innehåller översättningar från finska till svenska:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n{'apina': 'apa', 'banaani': 'banan', 'cembalo': 'cembalo'}\napa"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{}"}]},{"type":"text","value":" skapar ett tomt lexikon dit vi kan lägga till element. Tre stycken nyckel-värdepar skapas: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"text","value":" är knutet till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"cembalo"}]},{"type":"text","value":"\" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":". Till slut skriver vi ut antalet nyckel-värdepar i lexikonet, lexikonets innehåll samt det värde som tillhör nyckeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter att vi har skapat ett lexikon kan vi också använda det med indata från användaren:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Översättning:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ordet hittades inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk hur vi använder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":"-operatorn ovan. När vi använder operatorn för variabler med typen lexikon, kollar operatorn om den första operanden finns bland nycklarna i lexikonet. Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"apina"}]},{"type":"text","value":"\nÖversättning: apa"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pöllö"}]},{"type":"text","value":"\nOrdet hittades inte"}]}]},{"type":"element","tagName":"h2","properties":{"id":"vad-kan-lagras-i-ett-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Vad kan lagras i ett lexikon?"},{"type":"element","tagName":"a","properties":{"href":"#vad-kan-lagras-i-ett-lexikon","ariaLabel":"vad kan lagras i ett lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datatypen kallas lexikon, men det innebär inte att man bara skulle kunna lagra strängar där. I det här exemplet är nycklarna strängar men värdena är heltal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lisa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kalle\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är nycklarna heltal medan värdena är listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"listor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nlistor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlistor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlistor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"hur-nycklar-och-varden-fungerar","style":"position:relative;"},"children":[{"type":"text","value":"Hur nycklar och värden fungerar"},{"type":"element","tagName":"a","properties":{"href":"#hur-nycklar-och-varden-fungerar","ariaLabel":"hur nycklar och varden fungerar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje nyckel kan endast förekomma en gång i ett lexikon. Om du lägger till ett nytt värde med en nyckel som redan finns i lexikonet, kommer det ursprungliga värdet kopplat till nyckeln att ersättas med det nya värdet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"väldig\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"stor\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"stor"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla nycklar i ett lexikon måste vara oföränderliga. Det betyder att en lista inte kan vara en nyckel, eftersom listor kan ändras på. Den här koden ger till exempel ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unhashable type: 'list'"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Hashtabell"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk ordet \"unhashable\" i felmeddelandet ovan. Det här är en hänvisning till de inte strukturerna i ett lexikon. Python lagrar innehållet i ett lexikon i en hashtabell. Varje nyckel har ett hashvärde som indikerar var nyckeln finns lagrad i minnet. Felmeddelandet ovan indikerar att en lista inte kan förvandlas till ett hashvärde, och kan därmed inte användas som en nyckel i lexikonet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kursen Datastrukturer och algoritmer ger en större insikt i hashtabeller."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till skillnad från nycklar, kan värden i ett lexikon ändra och därmed kan vilken som helst typ av data lagras som ett värde. Ett och samma värde kan också vara kopplat till flera nycklar i samma lexikon."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Gånger tio","tmcname":"osa05-10b_x10"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ganger_tio(start: int, slut: int)"}]},{"type":"text","value":" som skapar och returnerar ett lexikon. Lexikonet ska ha nycklarna i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start-slut"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdet för varje nyckel ska vara nyckeln multiplicerat med tio."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"d "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ganger_tio"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{3: 30, 4: 40, 5: 50, 6: 60}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fakulteter","tmcname":"osa05-11_fakulteter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fakulteter(n: int)"}]},{"type":"text","value":" som returnerar fakulteterna för talen i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1-n"}]},{"type":"text","value":" i ett lexikon så att nyckeln är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" och värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":":s fakultet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som en påminnelse: Talet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":":s fakultet ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n!"}]},{"type":"text","value":") räknas genom att multiplicera talet med alla föregående positiva heltal. T.ex. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4! = 4 * 3 * 2 * 1 = 24"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"k"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"k"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"k"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n6\n120"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"ga-igenom-ett-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Gå igenom ett lexikon"},{"type":"element","tagName":"a","properties":{"href":"#ga-igenom-ett-lexikon","ariaLabel":"ga igenom ett lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den bekanta "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for element in samling"}]},{"type":"text","value":" -loopen kan också användas för att gå igenom ett lexikon. När det används direkt hos ett lexikon kommer loopen att en för en gå igenom nycklarna i lexikonet. I följande exempel skrivs varje nyckel och respektive värde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nyckel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"nyckel: apina\nvärde: apa\nnyckel: banaani\nvärde: banan\nnyckel: cembalo\nvärde: cembalo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland behöver du gå igenom allt innehåll i ett lexikon. Då kan du använda metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"items"}]},{"type":"text","value":" som returnerar alla nycklar och värden, ett par i sänder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nyckel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan märkte du kanske att nycklar behandlas i den ordning som de lagts till i lexikonet. Eftersom nycklarna behandlas enligt deras hashvärden, borde ordningen inte ha någon skillnad i programmen. I flera äldre versioner av Python är det dessutom inte garanterat att ordningen är den samma som nycklarna lagts till."}]},{"type":"element","tagName":"h2","properties":{"id":"nagra-mer-avancerade-satt-att-anvanda-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Några mer avancerade sätt att använda lexikon"},{"type":"element","tagName":"a","properties":{"href":"#nagra-mer-avancerade-satt-att-anvanda-lexikon","ariaLabel":"nagra mer avancerade satt att anvanda lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss kika på en lista med ord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mjölk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ost\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jordnöt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pasta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mjöl\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"majs\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tomat\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"korv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vitlök\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"margarin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jordnöt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"majs\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ost\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pasta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pasta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vitlök\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ost\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"socker\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skulle vilja analysera den här ordlistan på olika sätt. Vi är till exempel intresserade av hur många gånger de olika orden förekommer i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett lexikon fungera väl för att hålla reda på sådan här information. I exemplet nedan går vi igenom orden i listan. Vi använder sedan orden som nycklar i ett lexikon som vi skapat, så att värdet som är kopplat till varje nyckel indikerar hur många gånger det specifika ordet har förekommit:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"antal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ordsamling "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# om ordet inte förekommit ska nyckeln skapas och få ett värde"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" ordsamling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ordsamling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# öka på antalet gånger ordet förekommit"}]},{"type":"text","value":"\n ordsamling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" ordsamling\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi anropar funktionen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet skriver ut det följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'banan': 1, 'mjölk': 1, 'ost': 3, 'jordnöt': 2, 'pasta': 3, 'mjöl': 1, 'majs': 2, 'tomat': 1, 'korv': 1, 'vitlök': 2, 'margarin': 1, 'socker': 1}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi då skulle vela ordna orden enligt den första bokstaven i varje ord? Här kunde vi också kunna använda lexikon:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"enligt_forsta_bokstaven"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n grupper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n forsta_bokstaven "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skapa lista då bokstaven förekommer för den första gången"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsta_bokstaven "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" grupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n grupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"forsta_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till ordet under den korrekta bokstaven"}]},{"type":"text","value":"\n grupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"forsta_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" grupper\n\ngrupper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" enligt_forsta_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" grupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"ord som börjar med "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": \""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionens struktur liknar mycket den som finns i det tidigare exemplet, men den här gången är värden lagrade i form av listor. Programmet skriver ut det följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ord som börjar med b:\nbanan\nord som börjar med m:\nmjölk\nmjöl\nmajs\nmargarin\nmajs\nord som börjar med o:\nost\nost\nost\nord som börjar med j:\njordnöt\njordnöt\nord som börjar med p:\npasta\npasta\npasta\nord som börjar med t:\ntomat\nord som börjar med k:\nkorv\nord som börjar med v:\nvitlök\nvitlök\nord som börjar med s:\nsocker"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Histogram","tmcname":"osa05-12_histogram"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram"}]},{"type":"text","value":" som får som argument en sträng. Funktionen ska skriva ut ett histogram som beskriver förekomsten av olika bokstäver."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempelvis för anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram(\"abba\")"}]},{"type":"text","value":" ska utskriften vara:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a **\nb **\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram(\"lyxvilla\")"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"l *\ny *\nx *\nv *\ni *\nl **\na *\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Telefonkatalog, version 1","tmcname":"osa05-13_katalog_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en telefonkatalog som fungerar på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"kommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"040-5466745"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"emilia"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-1212344"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\n040-5466745\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"maija"}]},{"type":"text","value":"\ninget nummer\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"09-22223333"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\n09-22223333\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\navslutar..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att varje namn endast kan vara förknippat till ett nummer. Om ett nytt nummer ges för en person kommer det tidigare numret att ersättas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska du inte placera kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Telefonkatalog, version 2","tmcname":"osa05-14_katalog_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa nu en förbättrad version av telefonkatalogen. Samma person ska nu kunna ha flera nummer. I övrigt fungerar programmet som den tidigare versionen."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"kommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"040-5466745"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"emilia"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-1212344"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\n040-5466745\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"maija"}]},{"type":"text","value":"\ninget nummer\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"09-22223333"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\n040-5466745\n09-22223333\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\navslutar..."}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-ta-bort-nycklar-och-varden-fran-ett-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Att ta bort nycklar och värden från ett lexikon"},{"type":"element","tagName":"a","properties":{"href":"#att-ta-bort-nycklar-och-varden-fran-ett-lexikon","ariaLabel":"att ta bort nycklar och varden fran ett lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är naturligtvis möjligt att ta bort nyckel-värdepar från ett lexikon. Det finns två sätt att göra det här. Det första sättet är att använda instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Antti': 'lektor', 'Emilia': 'professor'}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du försöker använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":"-instruktionen för att ta bort en nyckel som inte finns i listan, kommer ett fel att uppstå:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jukka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":">>> del personal[\"Jukka\"]\nTraceback (most recent call last):\n File \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nKeyError: 'Jukka'\n"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Därmed lönar det sig att kolla om en nyckel existerar före du försöker avlägsna den från listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jukka\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jukka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Avlägsnades\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittade inte personen som skulle avlägsnas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt att ta bort element från listan är att använda metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nborttagen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Avlägsnade\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" borttagen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Antti': 'lektor', 'Emilia': 'professor'}\nAvlägsnade lektor"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" returnerar också värdet på elementet som togs bort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" kommer också i vanliga fall att ge ett fel om nyckeln som man försöker ta bort saknas i lexikonet. Det här kan man dock undvika genom att som ett andra argument ge till funktionen ett returvärde som funktionen kan returnera då en nyckel saknas. Värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" kan till exempel användas här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nborttagen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jukka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" borttagen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittade inte personen som skulle avlägsnas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Avlägsnade\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" borttagen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittade inte personen som skulle avlägsnas"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om du vill tömma ett lexikon och försöker göra det med en for-loop…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…kommer du att få ett felmeddelande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"RuntimeError: dictionary changed size during iteration"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man går igenom en samling med en for-loop, kan man inte ändra på samlingens innehåll så länge for-loopen är igång."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lyckligtvis har lexikon en inbyggd metod som kan användas istället:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Att vända ett lexikon","tmcname":"osa05-15_vanda_lexikon"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vand(lexikon: dict)"}]},{"type":"text","value":" som får som argument ett lexikon. Funktionen ska vända på nycklarna och värdena enligt exemplet nedan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nvand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{\"första\": 1, \"andra\": 2, \"tredje\": 3, \"fjärde\": 4}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att det här också gäller för lexikon som getts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyget"}]},{"type":"text","value":" om du stöter på problem."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Siffror som ord","tmcname":"osa05-16_siffror_ord"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffersamling()"}]},{"type":"text","value":" som returnerar ett nytt lexikon. Lexikonet ska innehålla nycklarna noll till 99. Värdena ska innehålla nyckeln i skriven form. Se exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffersamling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"två\nelva\nfyrtiofem\nnittionio\nnoll"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Bilda inte varje ord skilt för sig utan fundera hur du kan använda loopar och lexikon till nytta i din lösning."}]}]},{"type":"element","tagName":"h2","properties":{"id":"anvanda-lexikon-for-strukturerade-data","style":"position:relative;"},"children":[{"type":"text","value":"Använda lexikon för strukturerade data"},{"type":"element","tagName":"a","properties":{"href":"#anvanda-lexikon-for-strukturerade-data","ariaLabel":"anvanda lexikon for strukturerade data permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lexikon fungerar bra för att strukturera data. Följande kodsnutt skapar ett lexikon som innehåller information om en person:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peppa Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"154"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vikt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"61"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ålder:\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi alltså en person som heter Peppa Python. Hennes längd är 154, vikt 61 och ålder 44. Samma information kunde också lagras i skilda variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peppa Python\""}]},{"type":"text","value":"\nlängd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"154"}]},{"type":"text","value":"\nvikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"61"}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fördelen med lexikon är att det är en samling. Det samlar relaterade data under en variabel och det är enkelt att komma åt den information man är ute efter. Samma funktionalitet erbjuds också av listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peppa Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"153"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"61"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men med listor måste programmeraren minnas vilket index används för vilken information. Det finns inget som indikerar att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person[2]"}]},{"type":"text","value":" innehåller vikten och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person[3]"}]},{"type":"text","value":" åldern hos en person. När man använder lexikon, undviker man det här problemet eftersom all information finns lagrad under namngivna nycklar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi antar att det finns flera personer som definierats i samma format, kan vi komma åt deras information på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peppa Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"154"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vikt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"61"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Philip Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"174"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vikt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"103"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"31"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pedro Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"191"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vikt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"71"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\npersoner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntotal_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n total_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medellängden är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" total_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peppa Python\nPhilip Python\nPedro Python\nMedellängden är 173.0"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filmregister","tmcname":"osa05-17_filmregister"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_film(register: list, namn: str, regissor: str, ar: int, langd: int)"}]},{"type":"text","value":". Funktionen ska lägga till en ny film i ett register."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Registret är en lista och varje element är ett lexikon med dessa nycklar:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"regissör"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"år"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"längd"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdena ges som argument till funktionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"register "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nny_film"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"register"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drunknad i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Philip Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2017"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"116"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_film"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"register"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python vs. Java – vol. 32\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Renny Pytholin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2001"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"94"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"register"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116}, {\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Pytholin\", \"år\": 2001, \"längd\": 94}]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hitta film","tmcname":"osa05-17b_hitta_film"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hitta_filmer(register: list, term: str)"}]},{"type":"text","value":". Funktionen ska skapa en ny lista som innehåller de filmer i vars namn söktermen hittas. Gemener och versaler ska inte påverka – med termen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lil"}]},{"type":"text","value":" hittar man t.ex. både filmerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lilja 4-ever"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Den lilla Pythonkodaren"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"register "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drunknad i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"regissör\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Philip Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2017"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"116"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python vs. Java – vol. 32\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"regissör\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Renny Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2001"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"94"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Skymning i kodarlandet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"regissör\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"M. Night Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2011"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"101"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hitta_filmer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"register"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116}, {\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Python\", \"år\": 2001, \"längd\": 94}]"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7228e663-3942-5c8c-b00e-49e194a6d394"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • känner du till datatypen lexikon
  • \n
  • kan du använda lexikon med olika typer av nycklar och värden
  • \n
  • vet du hur man går igenom värden i ett lexikon
  • \n
  • har du koll på olika användningsområden för lexikon.
  • \n

Listor kan vara händiga i flera situationer, men deras svaga punkt är att elementen hämtas med hjälp av index (0, 1, 2 o.s.v.). Om du vill hitta ett element i en lista måste du alltså veta dess index, eller alternativt gå igenom hela listan.

En ytterligare central datastruktur i Python är lexikon, som vi nu ska se på. I lexikon är elementen indexerade enligt nycklar. Varje nyckel har ett värde. Värden som lagrats i ett lexikon kan hämtas och ändras med hjälp av dess nyckel.

Att använda lexikon

Det följande visar hur datastrukturen hos lexikon fungerar. Här är ett enkelt lexikon som innehåller översättningar från finska till svenska:

lexikon = {}\n\nlexikon[\"apina\"] = \"apa\"\nlexikon[\"banaani\"] = \"banan\"\nlexikon[\"cembalo\"] = \"cembalo\"\n\nprint(len(lexikon))\nprint(lexikon)\nprint(lexikon[\"apina\"])

3\n{'apina': 'apa', 'banaani': 'banan', 'cembalo': 'cembalo'}\napa

Notationen {} skapar ett tomt lexikon dit vi kan lägga till element. Tre stycken nyckel-värdepar skapas: \"apina\" är knutet till \"apa\", \"banaani\" till \"banan\" och \"cembalo\" till \"cembalo\". Till slut skriver vi ut antalet nyckel-värdepar i lexikonet, lexikonets innehåll samt det värde som tillhör nyckeln \"apina\".

Efter att vi har skapat ett lexikon kan vi också använda det med indata från användaren:

ord = input(\"Ange ord: \")\nif ord in lexikon:\n    print(\"Översättning:\", lexikon[ord])\nelse:\n    print(\"Ordet hittades inte\")

Märk hur vi använder in-operatorn ovan. När vi använder operatorn för variabler med typen lexikon, kollar operatorn om den första operanden finns bland nycklarna i lexikonet. Så här kan det se ut när programmet körs:

Ange ord: apina\nÖversättning: apa

Ange ord: pöllö\nOrdet hittades inte

Vad kan lagras i ett lexikon?

Datatypen kallas lexikon, men det innebär inte att man bara skulle kunna lagra strängar där. I det här exemplet är nycklarna strängar men värdena är heltal:

resultat = {}\nresultat[\"Maja\"] = 4\nresultat[\"Lisa\"] = 5\nresultat[\"Kalle\"] = 2

Här är nycklarna heltal medan värdena är listor:

listor = {}\nlistor[5] = [1, 2, 3]\nlistor[42] = [5, 4, 5, 4, 5]\nlistor[100] = [5, 2, 3]

Hur nycklar och värden fungerar

Varje nyckel kan endast förekomma en gång i ett lexikon. Om du lägger till ett nytt värde med en nyckel som redan finns i lexikonet, kommer det ursprungliga värdet kopplat till nyckeln att ersättas med det nya värdet:

lexikon[\"suuri\"] = \"väldig\"\nlexikon[\"suuri\"] = \"stor\"\nprint(lexikon[\"suuri\"])

stor

Alla nycklar i ett lexikon måste vara oföränderliga. Det betyder att en lista inte kan vara en nyckel, eftersom listor kan ändras på. Den här koden ger till exempel ett fel:

lexikon[[1, 2, 3]] = 5

TypeError: unhashable type: 'list'

Märk ordet \"unhashable\" i felmeddelandet ovan. Det här är en hänvisning till de inte strukturerna i ett lexikon. Python lagrar innehållet i ett lexikon i en hashtabell. Varje nyckel har ett hashvärde som indikerar var nyckeln finns lagrad i minnet. Felmeddelandet ovan indikerar att en lista inte kan förvandlas till ett hashvärde, och kan därmed inte användas som en nyckel i lexikonet.

Kursen Datastrukturer och algoritmer ger en större insikt i hashtabeller.

Till skillnad från nycklar, kan värden i ett lexikon ändra och därmed kan vilken som helst typ av data lagras som ett värde. Ett och samma värde kan också vara kopplat till flera nycklar i samma lexikon.

Skapa funktionen ganger_tio(start: int, slut: int) som skapar och returnerar ett lexikon. Lexikonet ska ha nycklarna i intervallet start-slut.

Värdet för varje nyckel ska vara nyckeln multiplicerat med tio.

Exempel:

d = ganger_tio(3, 6)\nprint(d)

{3: 30, 4: 40, 5: 50, 6: 60}

Skapa funktionen fakulteter(n: int) som returnerar fakulteterna för talen i intervallet 1-n i ett lexikon så att nyckeln är n och värdet n:s fakultet.

Som en påminnelse: Talet n:s fakultet (n!) räknas genom att multiplicera talet med alla föregående positiva heltal. T.ex. 4! = 4 * 3 * 2 * 1 = 24.

Exempel:

k = fakulteter(5)\nprint(k[1])\nprint(k[3])\nprint(k[5])

1\n6\n120

Gå igenom ett lexikon

Den bekanta for element in samling -loopen kan också användas för att gå igenom ett lexikon. När det används direkt hos ett lexikon kommer loopen att en för en gå igenom nycklarna i lexikonet. I följande exempel skrivs varje nyckel och respektive värde:

lexikon = {}\n\nlexikon[\"apina\"] = \"apa\"\nlexikon[\"banaani\"] = \"banan\"\nlexikon[\"cembalo\"] = \"cembalo\"\n\nfor nyckel in lexikon:\n    print(\"nyckel:\", nyckel)\n    print(\"värde:\", lexikon[nyckel])

nyckel: apina\nvärde: apa\nnyckel: banaani\nvärde: banan\nnyckel: cembalo\nvärde: cembalo

Ibland behöver du gå igenom allt innehåll i ett lexikon. Då kan du använda metoden items som returnerar alla nycklar och värden, ett par i sänder:

for nyckel, varde in lexikon.items():\n    print(\"nyckel:\", nyckel)\n    print(\"värde:\", varde)

I exemplen ovan märkte du kanske att nycklar behandlas i den ordning som de lagts till i lexikonet. Eftersom nycklarna behandlas enligt deras hashvärden, borde ordningen inte ha någon skillnad i programmen. I flera äldre versioner av Python är det dessutom inte garanterat att ordningen är den samma som nycklarna lagts till.

Några mer avancerade sätt att använda lexikon

Låt oss kika på en lista med ord:

ordlista = [\n  \"banan\", \"mjölk\", \"ost\", \"jordnöt\", \"pasta\", \"mjöl\", \"majs\",\n  \"tomat\", \"korv\", \"vitlök\", \"margarin\", \"jordnöt\", \"majs\",\n  \"ost\", \"pasta\", \"pasta\", \"vitlök\", \"ost\", \"socker\"\n]

Vi skulle vilja analysera den här ordlistan på olika sätt. Vi är till exempel intresserade av hur många gånger de olika orden förekommer i listan.

Ett lexikon fungera väl för att hålla reda på sådan här information. I exemplet nedan går vi igenom orden i listan. Vi använder sedan orden som nycklar i ett lexikon som vi skapat, så att värdet som är kopplat till varje nyckel indikerar hur många gånger det specifika ordet har förekommit:

def antal(lista):\n    ordsamling = {}\n    for ord in lista:\n        # om ordet inte förekommit ska nyckeln skapas och få ett värde\n        if ord not in ordsamling:\n            ordsamling[ord] = 0\n        # öka på antalet gånger ordet förekommit\n        ordsamling[ord] += 1\n    return ordsamling\n\n# vi anropar funktionen\nprint(antal(ordlista))

Programmet skriver ut det följande:

{'banan': 1, 'mjölk': 1, 'ost': 3, 'jordnöt': 2, 'pasta': 3, 'mjöl': 1, 'majs': 2, 'tomat': 1, 'korv': 1, 'vitlök': 2, 'margarin': 1, 'socker': 1}

Om vi då skulle vela ordna orden enligt den första bokstaven i varje ord? Här kunde vi också kunna använda lexikon:

def enligt_forsta_bokstaven(lista):\n    grupper = {}\n    for ord in lista:\n        forsta_bokstaven = ord[0]\n        # skapa lista då bokstaven förekommer för den första gången\n        if forsta_bokstaven not in grupper:\n            grupper[forsta_bokstaven] = []\n        # lägg till ordet under den korrekta bokstaven\n        grupper[forsta_bokstaven].append(ord)\n    return grupper\n\ngrupper = enligt_forsta_bokstaven(ordlista)\n\nfor nyckel, varde in grupper.items():\n    print(f\"ord som börjar med {nyckel}: \")\n    for ord in varde:\n        print(ord)

Funktionens struktur liknar mycket den som finns i det tidigare exemplet, men den här gången är värden lagrade i form av listor. Programmet skriver ut det följande:

ord som börjar med b:\nbanan\nord som börjar med m:\nmjölk\nmjöl\nmajs\nmargarin\nmajs\nord som börjar med o:\nost\nost\nost\nord som börjar med j:\njordnöt\njordnöt\nord som börjar med p:\npasta\npasta\npasta\nord som börjar med t:\ntomat\nord som börjar med k:\nkorv\nord som börjar med v:\nvitlök\nvitlök\nord som börjar med s:\nsocker

Skapa funktionen histogram som får som argument en sträng. Funktionen ska skriva ut ett histogram som beskriver förekomsten av olika bokstäver.

Exempelvis för anropet histogram(\"abba\") ska utskriften vara:

\na **\nb **\n

Eller histogram(\"lyxvilla\"):

\nl *\ny *\nx *\nv *\ni *\nl **\na *\n

Skapa en telefonkatalog som fungerar på följande sätt:

kommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: pekka\nnummer: 040-5466745\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: emilia\nnummer: 045-1212344\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: pekka\n040-5466745\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: maija\ninget nummer\nkommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: pekka\nnummer: 09-22223333\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: pekka\n09-22223333\nkommando (1 sök, 2 lägg till, 3 avsluta): 3\navslutar...

Observera att varje namn endast kan vara förknippat till ett nummer. Om ett nytt nummer ges för en person kommer det tidigare numret att ersättas.

Obs! I dessa uppgifter ska du inte placera kod i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Skapa nu en förbättrad version av telefonkatalogen. Samma person ska nu kunna ha flera nummer. I övrigt fungerar programmet som den tidigare versionen.

kommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: pekka\nnummer: 040-5466745\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: emilia\nnummer: 045-1212344\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: pekka\n040-5466745\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: maija\ninget nummer\nkommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: pekka\nnummer: 09-22223333\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: pekka\n040-5466745\n09-22223333\nkommando (1 sök, 2 lägg till, 3 avsluta): 3\navslutar...

Att ta bort nycklar och värden från ett lexikon

Det är naturligtvis möjligt att ta bort nyckel-värdepar från ett lexikon. Det finns två sätt att göra det här. Det första sättet är att använda instruktionen del:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\ndel personal[\"Arto\"]\nprint(personal)

{'Antti': 'lektor', 'Emilia': 'professor'}

Om du försöker använda del-instruktionen för att ta bort en nyckel som inte finns i listan, kommer ett fel att uppstå:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\ndel personal[\"Jukka\"]
\n>>> del personal[\"Jukka\"]\nTraceback (most recent call last):\n  File \"\", line 1, in \nKeyError: 'Jukka'\n

Därmed lönar det sig att kolla om en nyckel existerar före du försöker avlägsna den från listan:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\nif \"Jukka\" in personal:\n  del personal[\"Jukka\"]\n  print(\"Avlägsnades\")\nelse:\n  print(\"Hittade inte personen som skulle avlägsnas\")

Ett annat sätt att ta bort element från listan är att använda metoden pop:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\nborttagen = personal.pop(\"Arto\")\nprint(personal)\nprint(\"Avlägsnade\", borttagen)

{'Antti': 'lektor', 'Emilia': 'professor'}\nAvlägsnade lektor

Metoden pop returnerar också värdet på elementet som togs bort.

Metoden pop kommer också i vanliga fall att ge ett fel om nyckeln som man försöker ta bort saknas i lexikonet. Det här kan man dock undvika genom att som ett andra argument ge till funktionen ett returvärde som funktionen kan returnera då en nyckel saknas. Värdet None kan till exempel användas här:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\nborttagen = personal.pop(\"Jukka\", None)\nif borttagen == None:\n  print(\"Hittade inte personen som skulle avlägsnas\")\nelse:\n  print(\"Avlägsnade\", borttagen)

Hittade inte personen som skulle avlägsnas

Obs! Om du vill tömma ett lexikon och försöker göra det med en for-loop…

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\nfor nyckel in personal:\n  del personal[nyckel]

…kommer du att få ett felmeddelande:

RuntimeError: dictionary changed size during iteration

När man går igenom en samling med en for-loop, kan man inte ändra på samlingens innehåll så länge for-loopen är igång.

Lyckligtvis har lexikon en inbyggd metod som kan användas istället:

personal.clear()

Skapa funktionen vand(lexikon: dict) som får som argument ett lexikon. Funktionen ska vända på nycklarna och värdena enligt exemplet nedan.

s = {1: \"första\", 2: \"andra\", 3: \"tredje\", 4: \"fjärde\"}\nvand(s)\nprint(s)

{\"första\": 1, \"andra\": 2, \"tredje\": 3, \"fjärde\": 4}

Observera att det här också gäller för lexikon som getts som argument.

Använd visualiseringsverktyget om du stöter på problem.

Skapa funktionen siffersamling() som returnerar ett nytt lexikon. Lexikonet ska innehålla nycklarna noll till 99. Värdena ska innehålla nyckeln i skriven form. Se exemplet nedan:

siffror = siffersamling()\nprint(siffror[2])\nprint(siffror[11])\nprint(siffror[45])\nprint(siffror[99])\nprint(siffror[0])

två\nelva\nfyrtiofem\nnittionio\nnoll

Obs! Bilda inte varje ord skilt för sig utan fundera hur du kan använda loopar och lexikon till nytta i din lösning.

Använda lexikon för strukturerade data

Lexikon fungerar bra för att strukturera data. Följande kodsnutt skapar ett lexikon som innehåller information om en person:

person = {\"namn\": \"Peppa Python\", \"längd\": 154, \"vikt\": 61, \"ålder:\" 44}

Här har vi alltså en person som heter Peppa Python. Hennes längd är 154, vikt 61 och ålder 44. Samma information kunde också lagras i skilda variabler:

namn = \"Peppa Python\"\nlängd = 154\nvikt = 61\nalder = 44

Fördelen med lexikon är att det är en samling. Det samlar relaterade data under en variabel och det är enkelt att komma åt den information man är ute efter. Samma funktionalitet erbjuds också av listor:

person = [\"Peppa Python\", 153, 61, 44]

Men med listor måste programmeraren minnas vilket index används för vilken information. Det finns inget som indikerar att person[2] innehåller vikten och person[3] åldern hos en person. När man använder lexikon, undviker man det här problemet eftersom all information finns lagrad under namngivna nycklar.

Om vi antar att det finns flera personer som definierats i samma format, kan vi komma åt deras information på följande sätt:

person1 = {\"namn\": \"Peppa Python\", \"längd\": 154, \"vikt\": 61, \"ålder\": 44}\nperson2 = {\"namn\": \"Philip Python\", \"längd\": 174, \"vikt\": 103, \"ålder\": 31}\nperson3 = {\"namn\": \"Pedro Python\", \"längd\": 191, \"vikt\": 71, \"ålder\": 14}\n\npersoner = [person1, person2, person3]\n\nfor person in personer:\n    print(person[\"namn\"])\n\ntotal_langd = 0\nfor person in personer:\n    total_langd += person[\"längd\"]\n\nprint(\"Medellängden är\", total_langd / len(personer))

Peppa Python\nPhilip Python\nPedro Python\nMedellängden är 173.0

Skapa funktionen ny_film(register: list, namn: str, regissor: str, ar: int, langd: int). Funktionen ska lägga till en ny film i ett register.

Registret är en lista och varje element är ett lexikon med dessa nycklar:

    \n
  • namn
  • \n
  • regissör
  • \n
  • år
  • \n
  • längd
  • \n

Värdena ges som argument till funktionen.

Exempel:

register = []\nny_film(register, \"Drunknad i Python\", \"Philip Python\", 2017, 116)\nny_film(register, \"Python vs. Java – vol. 32\", \"Renny Pytholin\", 2001, 94)\nprint(register)

[{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116}, {\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Pytholin\", \"år\": 2001, \"längd\": 94}]

Skapa funktionen hitta_filmer(register: list, term: str). Funktionen ska skapa en ny lista som innehåller de filmer i vars namn söktermen hittas. Gemener och versaler ska inte påverka – med termen Lil hittar man t.ex. både filmerna Lilja 4-ever och Den lilla Pythonkodaren.

Exempel:

register = [{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116},\n{\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Python\", \"år\": 2001, \"längd\": 94},\n{\"namn\": \"Skymning i kodarlandet\", \"regissör\": \"M. Night Python\", \"år\": 2011, \"längd\": 101}]\n\nlista = hitta_filmer(register, \"python\")\nprint(lista)

[{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116}, {\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Python\", \"år\": 2001, \"längd\": 94}]

","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/3-lexikon.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till datatypen lexikon"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda lexikon med olika typer av nycklar och värden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man går igenom värden i ett lexikon"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"har du koll på olika användningsområden för lexikon."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listor kan vara händiga i flera situationer, men deras svaga punkt är att elementen hämtas med hjälp av index (0, 1, 2 o.s.v.). Om du vill hitta ett element i en lista måste du alltså veta dess index, eller alternativt gå igenom hela listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En ytterligare central datastruktur i Python är lexikon, som vi nu ska se på. I lexikon är elementen indexerade enligt nycklar. Varje nyckel har ett värde. Värden som lagrats i ett lexikon kan hämtas och ändras med hjälp av dess nyckel."}]},{"type":"element","tagName":"h2","properties":{"id":"att-anvanda-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Att använda lexikon"},{"type":"element","tagName":"a","properties":{"href":"#att-anvanda-lexikon","ariaLabel":"att anvanda lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det följande visar hur datastrukturen hos lexikon fungerar. Här är ett enkelt lexikon som innehåller översättningar från finska till svenska:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n{'apina': 'apa', 'banaani': 'banan', 'cembalo': 'cembalo'}\napa"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{}"}]},{"type":"text","value":" skapar ett tomt lexikon dit vi kan lägga till element. Tre stycken nyckel-värdepar skapas: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"text","value":" är knutet till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"cembalo"}]},{"type":"text","value":"\" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":". Till slut skriver vi ut antalet nyckel-värdepar i lexikonet, lexikonets innehåll samt det värde som tillhör nyckeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter att vi har skapat ett lexikon kan vi också använda det med indata från användaren:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange ord: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Översättning:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ordet hittades inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk hur vi använder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":"-operatorn ovan. När vi använder operatorn för variabler med typen lexikon, kollar operatorn om den första operanden finns bland nycklarna i lexikonet. Så här kan det se ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"apina"}]},{"type":"text","value":"\nÖversättning: apa"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pöllö"}]},{"type":"text","value":"\nOrdet hittades inte"}]}]},{"type":"element","tagName":"h2","properties":{"id":"vad-kan-lagras-i-ett-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Vad kan lagras i ett lexikon?"},{"type":"element","tagName":"a","properties":{"href":"#vad-kan-lagras-i-ett-lexikon","ariaLabel":"vad kan lagras i ett lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datatypen kallas lexikon, men det innebär inte att man bara skulle kunna lagra strängar där. I det här exemplet är nycklarna strängar men värdena är heltal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lisa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nresultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kalle\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är nycklarna heltal medan värdena är listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"listor "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nlistor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlistor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlistor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"hur-nycklar-och-varden-fungerar","style":"position:relative;"},"children":[{"type":"text","value":"Hur nycklar och värden fungerar"},{"type":"element","tagName":"a","properties":{"href":"#hur-nycklar-och-varden-fungerar","ariaLabel":"hur nycklar och varden fungerar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje nyckel kan endast förekomma en gång i ett lexikon. Om du lägger till ett nytt värde med en nyckel som redan finns i lexikonet, kommer det ursprungliga värdet kopplat till nyckeln att ersättas med det nya värdet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"väldig\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"stor\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"stor"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla nycklar i ett lexikon måste vara oföränderliga. Det betyder att en lista inte kan vara en nyckel, eftersom listor kan ändras på. Den här koden ger till exempel ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unhashable type: 'list'"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Hashtabell"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk ordet \"unhashable\" i felmeddelandet ovan. Det här är en hänvisning till de inte strukturerna i ett lexikon. Python lagrar innehållet i ett lexikon i en hashtabell. Varje nyckel har ett hashvärde som indikerar var nyckeln finns lagrad i minnet. Felmeddelandet ovan indikerar att en lista inte kan förvandlas till ett hashvärde, och kan därmed inte användas som en nyckel i lexikonet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kursen Datastrukturer och algoritmer ger en större insikt i hashtabeller."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till skillnad från nycklar, kan värden i ett lexikon ändra och därmed kan vilken som helst typ av data lagras som ett värde. Ett och samma värde kan också vara kopplat till flera nycklar i samma lexikon."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Gånger tio","tmcname":"osa05-10b_x10"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ganger_tio(start: int, slut: int)"}]},{"type":"text","value":" som skapar och returnerar ett lexikon. Lexikonet ska ha nycklarna i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start-slut"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdet för varje nyckel ska vara nyckeln multiplicerat med tio."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"d "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ganger_tio"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"d"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{3: 30, 4: 40, 5: 50, 6: 60}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fakulteter","tmcname":"osa05-11_fakulteter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fakulteter(n: int)"}]},{"type":"text","value":" som returnerar fakulteterna för talen i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1-n"}]},{"type":"text","value":" i ett lexikon så att nyckeln är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" och värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":":s fakultet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som en påminnelse: Talet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":":s fakultet ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n!"}]},{"type":"text","value":") räknas genom att multiplicera talet med alla föregående positiva heltal. T.ex. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4! = 4 * 3 * 2 * 1 = 24"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fakulteter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"k"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"k"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"k"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n6\n120"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"ga-igenom-ett-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Gå igenom ett lexikon"},{"type":"element","tagName":"a","properties":{"href":"#ga-igenom-ett-lexikon","ariaLabel":"ga igenom ett lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den bekanta "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for element in samling"}]},{"type":"text","value":" -loopen kan också användas för att gå igenom ett lexikon. När det används direkt hos ett lexikon kommer loopen att en för en gå igenom nycklarna i lexikonet. I följande exempel skrivs varje nyckel och respektive värde:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nyckel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"nyckel: apina\nvärde: apa\nnyckel: banaani\nvärde: banan\nnyckel: cembalo\nvärde: cembalo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland behöver du gå igenom allt innehåll i ett lexikon. Då kan du använda metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"items"}]},{"type":"text","value":" som returnerar alla nycklar och värden, ett par i sänder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nyckel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan märkte du kanske att nycklar behandlas i den ordning som de lagts till i lexikonet. Eftersom nycklarna behandlas enligt deras hashvärden, borde ordningen inte ha någon skillnad i programmen. I flera äldre versioner av Python är det dessutom inte garanterat att ordningen är den samma som nycklarna lagts till."}]},{"type":"element","tagName":"h2","properties":{"id":"nagra-mer-avancerade-satt-att-anvanda-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Några mer avancerade sätt att använda lexikon"},{"type":"element","tagName":"a","properties":{"href":"#nagra-mer-avancerade-satt-att-anvanda-lexikon","ariaLabel":"nagra mer avancerade satt att anvanda lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss kika på en lista med ord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"ordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mjölk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ost\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jordnöt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pasta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mjöl\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"majs\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tomat\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"korv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vitlök\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"margarin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jordnöt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"majs\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ost\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pasta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pasta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vitlök\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ost\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"socker\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skulle vilja analysera den här ordlistan på olika sätt. Vi är till exempel intresserade av hur många gånger de olika orden förekommer i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett lexikon fungera väl för att hålla reda på sådan här information. I exemplet nedan går vi igenom orden i listan. Vi använder sedan orden som nycklar i ett lexikon som vi skapat, så att värdet som är kopplat till varje nyckel indikerar hur många gånger det specifika ordet har förekommit:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"antal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ordsamling "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# om ordet inte förekommit ska nyckeln skapas och få ett värde"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" ordsamling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ordsamling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# öka på antalet gånger ordet förekommit"}]},{"type":"text","value":"\n ordsamling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" ordsamling\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi anropar funktionen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet skriver ut det följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'banan': 1, 'mjölk': 1, 'ost': 3, 'jordnöt': 2, 'pasta': 3, 'mjöl': 1, 'majs': 2, 'tomat': 1, 'korv': 1, 'vitlök': 2, 'margarin': 1, 'socker': 1}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi då skulle vela ordna orden enligt den första bokstaven i varje ord? Här kunde vi också kunna använda lexikon:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"enligt_forsta_bokstaven"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n grupper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n forsta_bokstaven "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skapa lista då bokstaven förekommer för den första gången"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" forsta_bokstaven "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" grupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n grupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"forsta_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till ordet under den korrekta bokstaven"}]},{"type":"text","value":"\n grupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"forsta_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" grupper\n\ngrupper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" enligt_forsta_bokstaven"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" grupper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"ord som börjar med "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": \""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionens struktur liknar mycket den som finns i det tidigare exemplet, men den här gången är värden lagrade i form av listor. Programmet skriver ut det följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ord som börjar med b:\nbanan\nord som börjar med m:\nmjölk\nmjöl\nmajs\nmargarin\nmajs\nord som börjar med o:\nost\nost\nost\nord som börjar med j:\njordnöt\njordnöt\nord som börjar med p:\npasta\npasta\npasta\nord som börjar med t:\ntomat\nord som börjar med k:\nkorv\nord som börjar med v:\nvitlök\nvitlök\nord som börjar med s:\nsocker"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Histogram","tmcname":"osa05-12_histogram"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram"}]},{"type":"text","value":" som får som argument en sträng. Funktionen ska skriva ut ett histogram som beskriver förekomsten av olika bokstäver."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempelvis för anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram(\"abba\")"}]},{"type":"text","value":" ska utskriften vara:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a **\nb **\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram(\"lyxvilla\")"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"l *\ny *\nx *\nv *\ni *\nl **\na *\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Telefonkatalog, version 1","tmcname":"osa05-13_katalog_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en telefonkatalog som fungerar på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"kommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"040-5466745"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"emilia"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-1212344"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\n040-5466745\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"maija"}]},{"type":"text","value":"\ninget nummer\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"09-22223333"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\n09-22223333\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\navslutar..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att varje namn endast kan vara förknippat till ett nummer. Om ett nytt nummer ges för en person kommer det tidigare numret att ersättas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska du inte placera kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Telefonkatalog, version 2","tmcname":"osa05-14_katalog_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa nu en förbättrad version av telefonkatalogen. Samma person ska nu kunna ha flera nummer. I övrigt fungerar programmet som den tidigare versionen."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"kommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"040-5466745"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"emilia"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-1212344"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\n040-5466745\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"maija"}]},{"type":"text","value":"\ninget nummer\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\nnummer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"09-22223333"}]},{"type":"text","value":"\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nnamn: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pekka"}]},{"type":"text","value":"\n040-5466745\n09-22223333\nkommando (1 sök, 2 lägg till, 3 avsluta): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\navslutar..."}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-ta-bort-nycklar-och-varden-fran-ett-lexikon","style":"position:relative;"},"children":[{"type":"text","value":"Att ta bort nycklar och värden från ett lexikon"},{"type":"element","tagName":"a","properties":{"href":"#att-ta-bort-nycklar-och-varden-fran-ett-lexikon","ariaLabel":"att ta bort nycklar och varden fran ett lexikon permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är naturligtvis möjligt att ta bort nyckel-värdepar från ett lexikon. Det finns två sätt att göra det här. Det första sättet är att använda instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Antti': 'lektor', 'Emilia': 'professor'}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du försöker använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":"-instruktionen för att ta bort en nyckel som inte finns i listan, kommer ett fel att uppstå:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jukka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":">>> del personal[\"Jukka\"]\nTraceback (most recent call last):\n File \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nKeyError: 'Jukka'\n"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Därmed lönar det sig att kolla om en nyckel existerar före du försöker avlägsna den från listan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jukka\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jukka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Avlägsnades\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittade inte personen som skulle avlägsnas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt att ta bort element från listan är att använda metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nborttagen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Avlägsnade\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" borttagen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Antti': 'lektor', 'Emilia': 'professor'}\nAvlägsnade lektor"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" returnerar också värdet på elementet som togs bort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" kommer också i vanliga fall att ge ett fel om nyckeln som man försöker ta bort saknas i lexikonet. Det här kan man dock undvika genom att som ett andra argument ge till funktionen ett returvärde som funktionen kan returnera då en nyckel saknas. Värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" kan till exempel användas här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nborttagen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jukka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" borttagen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hittade inte personen som skulle avlägsnas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Avlägsnade\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" borttagen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittade inte personen som skulle avlägsnas"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om du vill tömma ett lexikon och försöker göra det med en for-loop…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Antti\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"professor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lektor\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…kommer du att få ett felmeddelande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"RuntimeError: dictionary changed size during iteration"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man går igenom en samling med en for-loop, kan man inte ändra på samlingens innehåll så länge for-loopen är igång."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lyckligtvis har lexikon en inbyggd metod som kan användas istället:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Att vända ett lexikon","tmcname":"osa05-15_vanda_lexikon"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vand(lexikon: dict)"}]},{"type":"text","value":" som får som argument ett lexikon. Funktionen ska vända på nycklarna och värdena enligt exemplet nedan."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"första\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"andra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tredje\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fjärde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nvand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{\"första\": 1, \"andra\": 2, \"tredje\": 3, \"fjärde\": 4}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att det här också gäller för lexikon som getts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualiseringsverktyget"}]},{"type":"text","value":" om du stöter på problem."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Siffror som ord","tmcname":"osa05-16_siffror_ord"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffersamling()"}]},{"type":"text","value":" som returnerar ett nytt lexikon. Lexikonet ska innehålla nycklarna noll till 99. Värdena ska innehålla nyckeln i skriven form. Se exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" siffersamling"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"två\nelva\nfyrtiofem\nnittionio\nnoll"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Bilda inte varje ord skilt för sig utan fundera hur du kan använda loopar och lexikon till nytta i din lösning."}]}]},{"type":"element","tagName":"h2","properties":{"id":"anvanda-lexikon-for-strukturerade-data","style":"position:relative;"},"children":[{"type":"text","value":"Använda lexikon för strukturerade data"},{"type":"element","tagName":"a","properties":{"href":"#anvanda-lexikon-for-strukturerade-data","ariaLabel":"anvanda lexikon for strukturerade data permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lexikon fungerar bra för att strukturera data. Följande kodsnutt skapar ett lexikon som innehåller information om en person:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peppa Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"154"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vikt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"61"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ålder:\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi alltså en person som heter Peppa Python. Hennes längd är 154, vikt 61 och ålder 44. Samma information kunde också lagras i skilda variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peppa Python\""}]},{"type":"text","value":"\nlängd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"154"}]},{"type":"text","value":"\nvikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"61"}]},{"type":"text","value":"\nalder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fördelen med lexikon är att det är en samling. Det samlar relaterade data under en variabel och det är enkelt att komma åt den information man är ute efter. Samma funktionalitet erbjuds också av listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peppa Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"153"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"61"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men med listor måste programmeraren minnas vilket index används för vilken information. Det finns inget som indikerar att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person[2]"}]},{"type":"text","value":" innehåller vikten och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person[3]"}]},{"type":"text","value":" åldern hos en person. När man använder lexikon, undviker man det här problemet eftersom all information finns lagrad under namngivna nycklar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi antar att det finns flera personer som definierats i samma format, kan vi komma åt deras information på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peppa Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"154"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vikt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"61"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Philip Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"174"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vikt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"103"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"31"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pedro Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"191"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vikt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"71"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\npersoner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntotal_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n total_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medellängden är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" total_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peppa Python\nPhilip Python\nPedro Python\nMedellängden är 173.0"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filmregister","tmcname":"osa05-17_filmregister"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_film(register: list, namn: str, regissor: str, ar: int, langd: int)"}]},{"type":"text","value":". Funktionen ska lägga till en ny film i ett register."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Registret är en lista och varje element är ett lexikon med dessa nycklar:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"regissör"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"år"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"längd"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdena ges som argument till funktionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"register "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nny_film"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"register"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drunknad i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Philip Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2017"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"116"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_film"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"register"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python vs. Java – vol. 32\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Renny Pytholin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2001"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"94"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"register"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116}, {\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Pytholin\", \"år\": 2001, \"längd\": 94}]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hitta film","tmcname":"osa05-17b_hitta_film"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hitta_filmer(register: list, term: str)"}]},{"type":"text","value":". Funktionen ska skapa en ny lista som innehåller de filmer i vars namn söktermen hittas. Gemener och versaler ska inte påverka – med termen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lil"}]},{"type":"text","value":" hittar man t.ex. både filmerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lilja 4-ever"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Den lilla Pythonkodaren"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"register "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drunknad i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"regissör\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Philip Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2017"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"116"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Python vs. Java – vol. 32\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"regissör\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Renny Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2001"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"94"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Skymning i kodarlandet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"regissör\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"M. Night Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2011"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"längd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"101"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hitta_filmer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"register"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116}, {\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Python\", \"år\": 2001, \"längd\": 94}]"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7228e663-3942-5c8c-b00e-49e194a6d394"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • känner du till datatypen lexikon
  • \n
  • kan du använda lexikon med olika typer av nycklar och värden
  • \n
  • vet du hur man går igenom värden i ett lexikon
  • \n
  • har du koll på olika användningsområden för lexikon.
  • \n

Listor kan vara händiga i flera situationer, men deras svaga punkt är att elementen hämtas med hjälp av index (0, 1, 2 o.s.v.). Om du vill hitta ett element i en lista måste du alltså veta dess index, eller alternativt gå igenom hela listan.

En ytterligare central datastruktur i Python är lexikon, som vi nu ska se på. I lexikon är elementen indexerade enligt nycklar. Varje nyckel har ett värde. Värden som lagrats i ett lexikon kan hämtas och ändras med hjälp av dess nyckel.

Att använda lexikon

Det följande visar hur datastrukturen hos lexikon fungerar. Här är ett enkelt lexikon som innehåller översättningar från finska till svenska:

lexikon = {}\n\nlexikon[\"apina\"] = \"apa\"\nlexikon[\"banaani\"] = \"banan\"\nlexikon[\"cembalo\"] = \"cembalo\"\n\nprint(len(lexikon))\nprint(lexikon)\nprint(lexikon[\"apina\"])

3\n{'apina': 'apa', 'banaani': 'banan', 'cembalo': 'cembalo'}\napa

Notationen {} skapar ett tomt lexikon dit vi kan lägga till element. Tre stycken nyckel-värdepar skapas: \"apina\" är knutet till \"apa\", \"banaani\" till \"banan\" och \"cembalo\" till \"cembalo\". Till slut skriver vi ut antalet nyckel-värdepar i lexikonet, lexikonets innehåll samt det värde som tillhör nyckeln \"apina\".

Efter att vi har skapat ett lexikon kan vi också använda det med indata från användaren:

ord = input(\"Ange ord: \")\nif ord in lexikon:\n    print(\"Översättning:\", lexikon[ord])\nelse:\n    print(\"Ordet hittades inte\")

Märk hur vi använder in-operatorn ovan. När vi använder operatorn för variabler med typen lexikon, kollar operatorn om den första operanden finns bland nycklarna i lexikonet. Så här kan det se ut när programmet körs:

Ange ord: apina\nÖversättning: apa

Ange ord: pöllö\nOrdet hittades inte

Vad kan lagras i ett lexikon?

Datatypen kallas lexikon, men det innebär inte att man bara skulle kunna lagra strängar där. I det här exemplet är nycklarna strängar men värdena är heltal:

resultat = {}\nresultat[\"Maja\"] = 4\nresultat[\"Lisa\"] = 5\nresultat[\"Kalle\"] = 2

Här är nycklarna heltal medan värdena är listor:

listor = {}\nlistor[5] = [1, 2, 3]\nlistor[42] = [5, 4, 5, 4, 5]\nlistor[100] = [5, 2, 3]

Hur nycklar och värden fungerar

Varje nyckel kan endast förekomma en gång i ett lexikon. Om du lägger till ett nytt värde med en nyckel som redan finns i lexikonet, kommer det ursprungliga värdet kopplat till nyckeln att ersättas med det nya värdet:

lexikon[\"suuri\"] = \"väldig\"\nlexikon[\"suuri\"] = \"stor\"\nprint(lexikon[\"suuri\"])

stor

Alla nycklar i ett lexikon måste vara oföränderliga. Det betyder att en lista inte kan vara en nyckel, eftersom listor kan ändras på. Den här koden ger till exempel ett fel:

lexikon[[1, 2, 3]] = 5

TypeError: unhashable type: 'list'

Märk ordet \"unhashable\" i felmeddelandet ovan. Det här är en hänvisning till de inte strukturerna i ett lexikon. Python lagrar innehållet i ett lexikon i en hashtabell. Varje nyckel har ett hashvärde som indikerar var nyckeln finns lagrad i minnet. Felmeddelandet ovan indikerar att en lista inte kan förvandlas till ett hashvärde, och kan därmed inte användas som en nyckel i lexikonet.

Kursen Datastrukturer och algoritmer ger en större insikt i hashtabeller.

Till skillnad från nycklar, kan värden i ett lexikon ändra och därmed kan vilken som helst typ av data lagras som ett värde. Ett och samma värde kan också vara kopplat till flera nycklar i samma lexikon.

Skapa funktionen ganger_tio(start: int, slut: int) som skapar och returnerar ett lexikon. Lexikonet ska ha nycklarna i intervallet start-slut.

Värdet för varje nyckel ska vara nyckeln multiplicerat med tio.

Exempel:

d = ganger_tio(3, 6)\nprint(d)

{3: 30, 4: 40, 5: 50, 6: 60}

Skapa funktionen fakulteter(n: int) som returnerar fakulteterna för talen i intervallet 1-n i ett lexikon så att nyckeln är n och värdet n:s fakultet.

Som en påminnelse: Talet n:s fakultet (n!) räknas genom att multiplicera talet med alla föregående positiva heltal. T.ex. 4! = 4 * 3 * 2 * 1 = 24.

Exempel:

k = fakulteter(5)\nprint(k[1])\nprint(k[3])\nprint(k[5])

1\n6\n120

Gå igenom ett lexikon

Den bekanta for element in samling -loopen kan också användas för att gå igenom ett lexikon. När det används direkt hos ett lexikon kommer loopen att en för en gå igenom nycklarna i lexikonet. I följande exempel skrivs varje nyckel och respektive värde:

lexikon = {}\n\nlexikon[\"apina\"] = \"apa\"\nlexikon[\"banaani\"] = \"banan\"\nlexikon[\"cembalo\"] = \"cembalo\"\n\nfor nyckel in lexikon:\n    print(\"nyckel:\", nyckel)\n    print(\"värde:\", lexikon[nyckel])

nyckel: apina\nvärde: apa\nnyckel: banaani\nvärde: banan\nnyckel: cembalo\nvärde: cembalo

Ibland behöver du gå igenom allt innehåll i ett lexikon. Då kan du använda metoden items som returnerar alla nycklar och värden, ett par i sänder:

for nyckel, varde in lexikon.items():\n    print(\"nyckel:\", nyckel)\n    print(\"värde:\", varde)

I exemplen ovan märkte du kanske att nycklar behandlas i den ordning som de lagts till i lexikonet. Eftersom nycklarna behandlas enligt deras hashvärden, borde ordningen inte ha någon skillnad i programmen. I flera äldre versioner av Python är det dessutom inte garanterat att ordningen är den samma som nycklarna lagts till.

Några mer avancerade sätt att använda lexikon

Låt oss kika på en lista med ord:

ordlista = [\n  \"banan\", \"mjölk\", \"ost\", \"jordnöt\", \"pasta\", \"mjöl\", \"majs\",\n  \"tomat\", \"korv\", \"vitlök\", \"margarin\", \"jordnöt\", \"majs\",\n  \"ost\", \"pasta\", \"pasta\", \"vitlök\", \"ost\", \"socker\"\n]

Vi skulle vilja analysera den här ordlistan på olika sätt. Vi är till exempel intresserade av hur många gånger de olika orden förekommer i listan.

Ett lexikon fungera väl för att hålla reda på sådan här information. I exemplet nedan går vi igenom orden i listan. Vi använder sedan orden som nycklar i ett lexikon som vi skapat, så att värdet som är kopplat till varje nyckel indikerar hur många gånger det specifika ordet har förekommit:

def antal(lista):\n    ordsamling = {}\n    for ord in lista:\n        # om ordet inte förekommit ska nyckeln skapas och få ett värde\n        if ord not in ordsamling:\n            ordsamling[ord] = 0\n        # öka på antalet gånger ordet förekommit\n        ordsamling[ord] += 1\n    return ordsamling\n\n# vi anropar funktionen\nprint(antal(ordlista))

Programmet skriver ut det följande:

{'banan': 1, 'mjölk': 1, 'ost': 3, 'jordnöt': 2, 'pasta': 3, 'mjöl': 1, 'majs': 2, 'tomat': 1, 'korv': 1, 'vitlök': 2, 'margarin': 1, 'socker': 1}

Om vi då skulle vela ordna orden enligt den första bokstaven i varje ord? Här kunde vi också kunna använda lexikon:

def enligt_forsta_bokstaven(lista):\n    grupper = {}\n    for ord in lista:\n        forsta_bokstaven = ord[0]\n        # skapa lista då bokstaven förekommer för den första gången\n        if forsta_bokstaven not in grupper:\n            grupper[forsta_bokstaven] = []\n        # lägg till ordet under den korrekta bokstaven\n        grupper[forsta_bokstaven].append(ord)\n    return grupper\n\ngrupper = enligt_forsta_bokstaven(ordlista)\n\nfor nyckel, varde in grupper.items():\n    print(f\"ord som börjar med {nyckel}: \")\n    for ord in varde:\n        print(ord)

Funktionens struktur liknar mycket den som finns i det tidigare exemplet, men den här gången är värden lagrade i form av listor. Programmet skriver ut det följande:

ord som börjar med b:\nbanan\nord som börjar med m:\nmjölk\nmjöl\nmajs\nmargarin\nmajs\nord som börjar med o:\nost\nost\nost\nord som börjar med j:\njordnöt\njordnöt\nord som börjar med p:\npasta\npasta\npasta\nord som börjar med t:\ntomat\nord som börjar med k:\nkorv\nord som börjar med v:\nvitlök\nvitlök\nord som börjar med s:\nsocker

Skapa funktionen histogram som får som argument en sträng. Funktionen ska skriva ut ett histogram som beskriver förekomsten av olika bokstäver.

Exempelvis för anropet histogram(\"abba\") ska utskriften vara:

\na **\nb **\n

Eller histogram(\"lyxvilla\"):

\nl *\ny *\nx *\nv *\ni *\nl **\na *\n

Skapa en telefonkatalog som fungerar på följande sätt:

kommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: pekka\nnummer: 040-5466745\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: emilia\nnummer: 045-1212344\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: pekka\n040-5466745\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: maija\ninget nummer\nkommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: pekka\nnummer: 09-22223333\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: pekka\n09-22223333\nkommando (1 sök, 2 lägg till, 3 avsluta): 3\navslutar...

Observera att varje namn endast kan vara förknippat till ett nummer. Om ett nytt nummer ges för en person kommer det tidigare numret att ersättas.

Obs! I dessa uppgifter ska du inte placera kod i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Skapa nu en förbättrad version av telefonkatalogen. Samma person ska nu kunna ha flera nummer. I övrigt fungerar programmet som den tidigare versionen.

kommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: pekka\nnummer: 040-5466745\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: emilia\nnummer: 045-1212344\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: pekka\n040-5466745\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: maija\ninget nummer\nkommando (1 sök, 2 lägg till, 3 avsluta): 2\nnamn: pekka\nnummer: 09-22223333\nok!\nkommando (1 sök, 2 lägg till, 3 avsluta): 1\nnamn: pekka\n040-5466745\n09-22223333\nkommando (1 sök, 2 lägg till, 3 avsluta): 3\navslutar...

Att ta bort nycklar och värden från ett lexikon

Det är naturligtvis möjligt att ta bort nyckel-värdepar från ett lexikon. Det finns två sätt att göra det här. Det första sättet är att använda instruktionen del:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\ndel personal[\"Arto\"]\nprint(personal)

{'Antti': 'lektor', 'Emilia': 'professor'}

Om du försöker använda del-instruktionen för att ta bort en nyckel som inte finns i listan, kommer ett fel att uppstå:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\ndel personal[\"Jukka\"]
\n>>> del personal[\"Jukka\"]\nTraceback (most recent call last):\n  File \"\", line 1, in \nKeyError: 'Jukka'\n

Därmed lönar det sig att kolla om en nyckel existerar före du försöker avlägsna den från listan:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\nif \"Jukka\" in personal:\n  del personal[\"Jukka\"]\n  print(\"Avlägsnades\")\nelse:\n  print(\"Hittade inte personen som skulle avlägsnas\")

Ett annat sätt att ta bort element från listan är att använda metoden pop:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\nborttagen = personal.pop(\"Arto\")\nprint(personal)\nprint(\"Avlägsnade\", borttagen)

{'Antti': 'lektor', 'Emilia': 'professor'}\nAvlägsnade lektor

Metoden pop returnerar också värdet på elementet som togs bort.

Metoden pop kommer också i vanliga fall att ge ett fel om nyckeln som man försöker ta bort saknas i lexikonet. Det här kan man dock undvika genom att som ett andra argument ge till funktionen ett returvärde som funktionen kan returnera då en nyckel saknas. Värdet None kan till exempel användas här:

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\nborttagen = personal.pop(\"Jukka\", None)\nif borttagen == None:\n  print(\"Hittade inte personen som skulle avlägsnas\")\nelse:\n  print(\"Avlägsnade\", borttagen)

Hittade inte personen som skulle avlägsnas

Obs! Om du vill tömma ett lexikon och försöker göra det med en for-loop…

personal = {\"Antti\": \"lektor\", \"Emilia\": \"professor\", \"Arto\": \"lektor\"}\nfor nyckel in personal:\n  del personal[nyckel]

…kommer du att få ett felmeddelande:

RuntimeError: dictionary changed size during iteration

När man går igenom en samling med en for-loop, kan man inte ändra på samlingens innehåll så länge for-loopen är igång.

Lyckligtvis har lexikon en inbyggd metod som kan användas istället:

personal.clear()

Skapa funktionen vand(lexikon: dict) som får som argument ett lexikon. Funktionen ska vända på nycklarna och värdena enligt exemplet nedan.

s = {1: \"första\", 2: \"andra\", 3: \"tredje\", 4: \"fjärde\"}\nvand(s)\nprint(s)

{\"första\": 1, \"andra\": 2, \"tredje\": 3, \"fjärde\": 4}

Observera att det här också gäller för lexikon som getts som argument.

Använd visualiseringsverktyget om du stöter på problem.

Skapa funktionen siffersamling() som returnerar ett nytt lexikon. Lexikonet ska innehålla nycklarna noll till 99. Värdena ska innehålla nyckeln i skriven form. Se exemplet nedan:

siffror = siffersamling()\nprint(siffror[2])\nprint(siffror[11])\nprint(siffror[45])\nprint(siffror[99])\nprint(siffror[0])

två\nelva\nfyrtiofem\nnittionio\nnoll

Obs! Bilda inte varje ord skilt för sig utan fundera hur du kan använda loopar och lexikon till nytta i din lösning.

Använda lexikon för strukturerade data

Lexikon fungerar bra för att strukturera data. Följande kodsnutt skapar ett lexikon som innehåller information om en person:

person = {\"namn\": \"Peppa Python\", \"längd\": 154, \"vikt\": 61, \"ålder:\" 44}

Här har vi alltså en person som heter Peppa Python. Hennes längd är 154, vikt 61 och ålder 44. Samma information kunde också lagras i skilda variabler:

namn = \"Peppa Python\"\nlängd = 154\nvikt = 61\nalder = 44

Fördelen med lexikon är att det är en samling. Det samlar relaterade data under en variabel och det är enkelt att komma åt den information man är ute efter. Samma funktionalitet erbjuds också av listor:

person = [\"Peppa Python\", 153, 61, 44]

Men med listor måste programmeraren minnas vilket index används för vilken information. Det finns inget som indikerar att person[2] innehåller vikten och person[3] åldern hos en person. När man använder lexikon, undviker man det här problemet eftersom all information finns lagrad under namngivna nycklar.

Om vi antar att det finns flera personer som definierats i samma format, kan vi komma åt deras information på följande sätt:

person1 = {\"namn\": \"Peppa Python\", \"längd\": 154, \"vikt\": 61, \"ålder\": 44}\nperson2 = {\"namn\": \"Philip Python\", \"längd\": 174, \"vikt\": 103, \"ålder\": 31}\nperson3 = {\"namn\": \"Pedro Python\", \"längd\": 191, \"vikt\": 71, \"ålder\": 14}\n\npersoner = [person1, person2, person3]\n\nfor person in personer:\n    print(person[\"namn\"])\n\ntotal_langd = 0\nfor person in personer:\n    total_langd += person[\"längd\"]\n\nprint(\"Medellängden är\", total_langd / len(personer))

Peppa Python\nPhilip Python\nPedro Python\nMedellängden är 173.0

Skapa funktionen ny_film(register: list, namn: str, regissor: str, ar: int, langd: int). Funktionen ska lägga till en ny film i ett register.

Registret är en lista och varje element är ett lexikon med dessa nycklar:

    \n
  • namn
  • \n
  • regissör
  • \n
  • år
  • \n
  • längd
  • \n

Värdena ges som argument till funktionen.

Exempel:

register = []\nny_film(register, \"Drunknad i Python\", \"Philip Python\", 2017, 116)\nny_film(register, \"Python vs. Java – vol. 32\", \"Renny Pytholin\", 2001, 94)\nprint(register)

[{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116}, {\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Pytholin\", \"år\": 2001, \"längd\": 94}]

Skapa funktionen hitta_filmer(register: list, term: str). Funktionen ska skapa en ny lista som innehåller de filmer i vars namn söktermen hittas. Gemener och versaler ska inte påverka – med termen Lil hittar man t.ex. både filmerna Lilja 4-ever och Den lilla Pythonkodaren.

Exempel:

register = [{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116},\n{\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Python\", \"år\": 2001, \"längd\": 94},\n{\"namn\": \"Skymning i kodarlandet\", \"regissör\": \"M. Night Python\", \"år\": 2011, \"längd\": 101}]\n\nlista = hitta_filmer(register, \"python\")\nprint(lista)

[{\"namn\": \"Drunknad i Python\", \"regissör\": \"Philip Python\", \"år\": 2017, \"längd\": 116}, {\"namn\": \"Python vs. Java – vol. 32\", \"regissör\": \"Renny Python\", \"år\": 2001, \"längd\": 94}]

","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/3-lexikon.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-5/4-tuple/page-data.json b/page-data/osa-5/4-tuple/page-data.json index a2c54c2a8..56d9c6ad1 100644 --- a/page-data/osa-5/4-tuple/page-data.json +++ b/page-data/osa-5/4-tuple/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-5/4-tuple", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till datatypen tuple"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa tupler av olika typer av värden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad skillnaden mellan en lista och tuple är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du nämna några vanliga användningsområden för tupler."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En tuple är en datastruktur som på flera sätt påminner listan. De största skillnaderna mellan dessa två är:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tupler är inom parenteser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"()"}]},{"type":"text","value":" medan listor är inom hakparenteser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tupler är oföränderliga, medan innehållet i en lista kan ändra."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den följande kodsnutten skapar en tuple som innehåller koordinaterna för en punkt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Elementen lagrade i tupler kan kommas åt med index, på samma sätt som med listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"x-koordinat:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"y-koordinat:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"x-koordinat: 10\ny-koordinat: 20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värden som lagrats i en tuple kan inte ändras efter att en tuple har skapats. Det följande kommer inte att fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npunkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: 'tuple' object does not support item assignment"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Skapa tuple","tmcname":"osa05-17c_skapa_tuple"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skapa_tuple(x: int, y: int, z: int)"}]},{"type":"text","value":" som skapar och returnerar en tuple enligt dessa regler:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tuplens första element är det minsta av de givna argumenten"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tuplens andra element är det största av de givna argumenten"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tuplens tredje element är de givna argumentens summa"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skapa_tuple"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(-1, 5, 7)"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Äldst","tmcname":"osa05-18_aldst"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aldst(personer: list)"}]},{"type":"text","value":" som får som argument en lista med tupler som innehåller information om personer. Funktionen ska hitta den äldsta personen och returnera hennes namn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje tuple består av ett namn (element 1) och ett födelseår (element 2)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Algot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1977"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eskil\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mette\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1953"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ellie\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npersoner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mette"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Äldre","tmcname":"osa05-19_aldre"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi antar att vi fortfarande har tuplerna från den föregående uppgiften till vårt förfogande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aldre(personer: list, ar: int)"}]},{"type":"text","value":". Funktionen ska returnera en ny lista som innehåller namnen på personerna i den givna listan som fötts före det givna året."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Algot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1977"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eskil\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mette\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1953"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ellie\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npersoner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\naldre_personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" aldre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1979"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"aldre_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[ 'Algot', Mette' ]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"vad-behover-man-tupler-for","style":"position:relative;"},"children":[{"type":"text","value":"Vad behöver man tupler för?"},{"type":"element","tagName":"a","properties":{"href":"#vad-behover-man-tupler-for","ariaLabel":"vad behover man tupler for permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tupler är nyttiga i situationer där det finns en samling av värden som är på något sätt sammankopplade. Till exempel då man behandlar x- och y-koordinaterna hos en punkt, är tuplen ett naturligt val eftersom koordinater alltid består av två värden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är tekniskt möjligt att använda en lista för att lagra dessa:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En lista är en samling av element i en viss ordning. Listans storlek kan också ändra. Eftersom vi lagrar koordinaterna för en punkt, vill vi lagra x- och y-koordinaterna specifikt – inte en godtycklig lista med dessa värden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom tupler är oföränderliga – till skillnad från listor – kan de användas som nycklar i lexikon. Det följande kodexemplet skapar ett lexikon där koordinater används som nycklar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\napa\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är inte möjligt att skapa ett liknande lexikon med hjälp av listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unhashable type: 'list'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"tupler-utan-parenteser","style":"position:relative;"},"children":[{"type":"text","value":"Tupler utan parenteser"},{"type":"element","tagName":"a","properties":{"href":"#tupler-utan-parenteser","ariaLabel":"tupler utan parenteser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Parenteser är inte obligatoriska då man skapar tupler. Följande variabeltilldelningar ger likadana resultat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det innebär att vi enkelt kan returnera flera värden med hjälp av tupler. Ta en titt på följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"minmax"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"21"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"88"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"312"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nminst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Minsta talet är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och största är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Minsta talet är 5 och största är 312"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar två värden i en tuple. Returvärdet kan tilldelas till två variabler samtidigt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda parenteser kan göra notationen tydligare. Till vänster av tilldelningssatsen har vi också en tuple som består av två variabelnamn. Värdena som finns i tuplen som funktionen returnerar tilldelas till dessa två variabler."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kanske minns metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"items"}]},{"type":"text","value":" från den förra delen. Vi använde den för att komma åt nycklarna och värdena som lagrats i ett lexikon:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nyckel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tupler finns i bakgrunden här också. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lexikon.items()"}]},{"type":"text","value":" returnerar varje nyckel-värdepar som en tuple, där det första elementet innehåller nyckeln och det andra värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat användningsområde för tupler är att byta värden sinsemellan två variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"tal1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tal2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" tal2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tal1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tilldelningssatsen ovan svänger på värdena lagrade i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tal1"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tal2"}]},{"type":"text","value":". Resultatet är detsamma som vi skulle uppnå med hjälp av en hjälpvariabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hjalp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" tal1\ntal1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" tal2\ntal2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hjalp"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Studeranderegister","tmcname":"osa05-20_studeranderegister"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna uppgift skapar vi ett enkelt register över studerande. Före du börjar programmera lönar det sig att fundera hurdan datastruktur som behövs för att organisera den data som ska lagras."}]},{"type":"element","tagName":"h4","properties":{"id":"lagga-till-studerande","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till studerande"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-studerande","ariaLabel":"lagga till studerande permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Börja med att skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skapa_studerande"}]},{"type":"text","value":" samt en första version av funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"text","value":", som används för att skriva ut information om en studerande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här används funktionerna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här skedet skriver programmet ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Robert:\n inga prestationer\nLilou:\n inga prestationer\nhittade ingen med namnet Siri\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"lagga-till-prestationer","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till prestationer"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-prestationer","ariaLabel":"lagga till prestationer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_prestation"}]},{"type":"text","value":", som lägger till en prestation för en studerande. Prestationen är en tuple som består av kursens namn och ett vitsord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften ändras då det finns prestationer:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Robert:\n prestationer från 2 kurser:\n introkurs i Python 3\n datastrukturer 2\n medeltal 2.5\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"hoja-pa-vitsord","style":"position:relative;"},"children":[{"type":"text","value":"Höja på vitsord"},{"type":"element","tagName":"a","properties":{"href":"#hoja-pa-vitsord","ariaLabel":"hoja pa vitsord permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man lägger till en prestation ska kurser med vitsordet noll ignorera och tidigare vitsord ska inte sänkas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer II\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Robert:\n prestationer från 2 kurser:\n introkurs i Python 3\n datastrukturer 2\n medeltal 2.5\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"oversikt","style":"position:relative;"},"children":[{"type":"text","value":"Översikt"},{"type":"element","tagName":"a","properties":{"href":"#oversikt","ariaLabel":"oversikt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"oversikt"}]},{"type":"text","value":" som skriver ut en översikt för samtliga studerandes prestationer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer II\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spaden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkooste"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften ser ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"antal studerande 2\nflest prestationer 3 Robert\nbästa medeltal 4.5 Lilou\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bokstavsruta","tmcname":"osa05-21_bokstavsruta"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här delen når nu sitt kulmen med en relativt svår uppgift som kräver problemlösningsförmåga. Uppgiften kan lösas på flera sätt. Även om vi nyss har behandlat tupler så lönar det sig dock antagligen inte att använda dem i den här uppgiften."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som skriver ut en bokstavsruta enligt följande exempel. Du kan anta att det finns högst 26 våningar."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Våningar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"CCCCC\nCBBBC\nCBABC\nCBBBC\nCCCCC\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Våningar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"DDDDDDD\nDCCCCCD\nDCBBBCD\nDCBABCD\nDCBBBCD\nDCCCCCD\nDDDDDDD\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i dessa uppgifter, om du inte skilt ombeds göra det."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät gällande materialet för den här veckan."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • känner du till datatypen tuple
  • \n
  • kan du skapa tupler av olika typer av värden
  • \n
  • vet du vad skillnaden mellan en lista och tuple är
  • \n
  • kan du nämna några vanliga användningsområden för tupler.
  • \n

En tuple är en datastruktur som på flera sätt påminner listan. De största skillnaderna mellan dessa två är:

    \n
  • tupler är inom parenteser () medan listor är inom hakparenteser []
  • \n
  • tupler är oföränderliga, medan innehållet i en lista kan ändra.
  • \n

Den följande kodsnutten skapar en tuple som innehåller koordinaterna för en punkt:

punkt = (10, 20)

Elementen lagrade i tupler kan kommas åt med index, på samma sätt som med listor:

punkt = (10, 20)\nprint(\"x-koordinat:\", punkt[0])\nprint(\"y-koordinat:\", punkt[1])

x-koordinat: 10\ny-koordinat: 20

Värden som lagrats i en tuple kan inte ändras efter att en tuple har skapats. Det följande kommer inte att fungera:

punkt = (10, 20)\npunkt[0] = 15

TypeError: 'tuple' object does not support item assignment

Skapa funktionen skapa_tuple(x: int, y: int, z: int) som skapar och returnerar en tuple enligt dessa regler:

    \n
  • tuplens första element är det minsta av de givna argumenten
  • \n
  • tuplens andra element är det största av de givna argumenten
  • \n
  • tuplens tredje element är de givna argumentens summa
  • \n

Exempel:

if __name__ == \"__main__\":\n    print(skapa_tuple(5, 3, -1))

(-1, 5, 7)

Skapa funktionen aldst(personer: list) som får som argument en lista med tupler som innehåller information om personer. Funktionen ska hitta den äldsta personen och returnera hennes namn.

Varje tuple består av ett namn (element 1) och ett födelseår (element 2).

Exempel:

p1 = (\"Algot\", 1977)\np2 = (\"Eskil\", 1985)\np3 = (\"Mette\", 1953)\np4 = (\"Ellie\", 1997)\npersoner = [p1, p2, p3, p4]\n\nprint(aldst(personer))

Mette

Vi antar att vi fortfarande har tuplerna från den föregående uppgiften till vårt förfogande.

Skapa funktionen aldre(personer: list, ar: int). Funktionen ska returnera en ny lista som innehåller namnen på personerna i den givna listan som fötts före det givna året.

Exempel:

p1 = (\"Algot\", 1977)\np2 = (\"Eskil\", 1985)\np3 = (\"Mette\", 1953)\np4 = (\"Ellie\", 1997)\npersoner = [p1, p2, p3, p4]\n\naldre_personer = aldre(personer, 1979)\nprint(aldre_personer)

[ 'Algot', Mette' ]

Vad behöver man tupler för?

Tupler är nyttiga i situationer där det finns en samling av värden som är på något sätt sammankopplade. Till exempel då man behandlar x- och y-koordinaterna hos en punkt, är tuplen ett naturligt val eftersom koordinater alltid består av två värden:

punkt = (10, 20)

Det är tekniskt möjligt att använda en lista för att lagra dessa:

punkt = [10, 20]

En lista är en samling av element i en viss ordning. Listans storlek kan också ändra. Eftersom vi lagrar koordinaterna för en punkt, vill vi lagra x- och y-koordinaterna specifikt – inte en godtycklig lista med dessa värden.

Eftersom tupler är oföränderliga – till skillnad från listor – kan de användas som nycklar i lexikon. Det följande kodexemplet skapar ett lexikon där koordinater används som nycklar:

punkter = {}\npunkter[(3, 5)] = \"apa\"\npunkter[(5, 0)] = \"banan\"\npunkter[(1, 2)] = \"cembalo\"\nprint(punkter[(3, 5)])
\napa\n

Det är inte möjligt att skapa ett liknande lexikon med hjälp av listor:

punkter = {}\npunkter[[3, 5]] = \"apa\"\npunkter[[5, 0]] = \"banan\"\npunkter[[1, 2]] = \"cembalo\"\nprint(punkter[[3, 5]])

TypeError: unhashable type: 'list'

Tupler utan parenteser

Parenteser är inte obligatoriska då man skapar tupler. Följande variabeltilldelningar ger likadana resultat:

siffror = (1, 2, 3)
siffror = 1, 2, 3

Det innebär att vi enkelt kan returnera flera värden med hjälp av tupler. Ta en titt på följande exempel:

def minmax(lista):\n  return min(lista), max(lista)\n\nlista = [33, 5, 21, 7, 88, 312, 5]\n\nminst, storst = minmax(lista)\nprint(f\"Minsta talet är {minst} och största är {storst}\")

Minsta talet är 5 och största är 312

Funktionen returnerar två värden i en tuple. Returvärdet kan tilldelas till två variabler samtidigt:

minst, storst = minmax(lista)

Att använda parenteser kan göra notationen tydligare. Till vänster av tilldelningssatsen har vi också en tuple som består av två variabelnamn. Värdena som finns i tuplen som funktionen returnerar tilldelas till dessa två variabler.

(minst, storst) = minmax(lista)

Du kanske minns metoden items från den förra delen. Vi använde den för att komma åt nycklarna och värdena som lagrats i ett lexikon:

lexikon = {}\n\nlexikon[\"apina\"] = \"apa\"\nlexikon[\"banaani\"] = \"banan\"\nlexikon[\"cembalo\"] = \"cembalo\"\n\nfor nyckel, varde in lexikon.items():\n    print(\"nyckel:\", nyckel)\n    print(\"värde:\", varde)

Tupler finns i bakgrunden här också. Metoden lexikon.items() returnerar varje nyckel-värdepar som en tuple, där det första elementet innehåller nyckeln och det andra värdet.

Ett annat användningsområde för tupler är att byta värden sinsemellan två variabler:

tal1, tal2 = tal2, tal1

Tilldelningssatsen ovan svänger på värdena lagrade i variablerna tal1 och tal2. Resultatet är detsamma som vi skulle uppnå med hjälp av en hjälpvariabel:

hjalp = tal1\ntal1 = tal2\ntal2 = hjalp

I denna uppgift skapar vi ett enkelt register över studerande. Före du börjar programmera lönar det sig att fundera hurdan datastruktur som behövs för att organisera den data som ska lagras.

Lägga till studerande

Börja med att skapa funktionen skapa_studerande samt en första version av funktionen skriv_ut, som används för att skriva ut information om en studerande.

Så här används funktionerna:

studerande = {}\nskapa_studerande(studerande, \"Robert\")\nskapa_studerande(studerande, \"Lilou\")\nskriv_ut(studerande, \"Robert\")\nskriv_ut(studerande, \"Lilou\")\nskriv_ut(studerande, \"Siri\")

I det här skedet skriver programmet ut:

\nRobert:\n inga prestationer\nLilou:\n inga prestationer\nhittade ingen med namnet Siri\n

Lägga till prestationer

Skapa funktionen ny_prestation, som lägger till en prestation för en studerande. Prestationen är en tuple som består av kursens namn och ett vitsord:

studerande = {}\nskapa_studerande(studerande, \"Robert\")\nny_prestation(studerande, \"Robert\", (\"introkurs i Python\", 3))\nny_prestation(studerande, \"Robert\", (\"datastrukturer\", 2))\nskriv_ut(studerande, \"Robert\")

Utskriften ändras då det finns prestationer:

\nRobert:\n prestationer från 2 kurser:\n  introkurs i Python 3\n  datastrukturer 2\n medeltal 2.5\n

Höja på vitsord

När man lägger till en prestation ska kurser med vitsordet noll ignorera och tidigare vitsord ska inte sänkas:

studerande = {}\nskapa_studerande(studerande, \"Robert\")\nny_prestation(studerande, \"Robert\", (\"introkurs i Python\", 3))\nny_prestation(studerande, \"Robert\", (\"datastrukturer\", 2))\nny_prestation(studerande, \"Robert\", (\"datastrukturer II\", 0))\nny_prestation(studerande, \"Robert\", (\"introkurs i Python\", 2))\nskriv_ut(studerande, \"Robert\")
\nRobert:\n prestationer från 2 kurser:\n  introkurs i Python 3\n  datastrukturer 2\n medeltal 2.5\n

Översikt

Skapa funktionen oversikt som skriver ut en översikt för samtliga studerandes prestationer:

studerande = {}\nskapa_studerande(studerande, \"Robert\")\nskapa_studerande(studerande, \"Lilou\")\nny_prestation(studerande, \"Robert\", (\"datastrukturer II\", 1))\nny_prestation(studerande, \"Robert\", (\"introkurs i Python\", 1))\nny_prestation(studerande, \"Robert\", (\"datastrukturer\", 1))\nny_prestation(studerande, \"Lilou\", (\"introkurs i Python\", 5))\nny_prestation(studerande, \"Lilou\", (\"spaden\", 4))\nkooste(studerande)

Utskriften ser ut så här:

\nantal studerande 2\nflest prestationer 3 Robert\nbästa medeltal 4.5 Lilou\n

Den här delen når nu sitt kulmen med en relativt svår uppgift som kräver problemlösningsförmåga. Uppgiften kan lösas på flera sätt. Även om vi nyss har behandlat tupler så lönar det sig dock antagligen inte att använda dem i den här uppgiften.

Skapa ett program som skriver ut en bokstavsruta enligt följande exempel. Du kan anta att det finns högst 26 våningar.

Våningar: 3

\nCCCCC\nCBBBC\nCBABC\nCBBBC\nCCCCC\n

Våningar: 4

\nDDDDDDD\nDCCCCCD\nDCBBBCD\nDCBABCD\nDCBBBCD\nDCCCCCD\nDDDDDDD\n

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i dessa uppgifter, om du inte skilt ombeds göra det.

Vänligen svara på en kort enkät gällande materialet för den här veckan.

","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/4-tuple.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till datatypen tuple"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa tupler av olika typer av värden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad skillnaden mellan en lista och tuple är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du nämna några vanliga användningsområden för tupler."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En tuple är en datastruktur som på flera sätt påminner listan. De största skillnaderna mellan dessa två är:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tupler är inom parenteser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"()"}]},{"type":"text","value":" medan listor är inom hakparenteser "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tupler är oföränderliga, medan innehållet i en lista kan ändra."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den följande kodsnutten skapar en tuple som innehåller koordinaterna för en punkt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Elementen lagrade i tupler kan kommas åt med index, på samma sätt som med listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"x-koordinat:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"y-koordinat:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"x-koordinat: 10\ny-koordinat: 20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värden som lagrats i en tuple kan inte ändras efter att en tuple har skapats. Det följande kommer inte att fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npunkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: 'tuple' object does not support item assignment"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Skapa tuple","tmcname":"osa05-17c_skapa_tuple"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skapa_tuple(x: int, y: int, z: int)"}]},{"type":"text","value":" som skapar och returnerar en tuple enligt dessa regler:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tuplens första element är det minsta av de givna argumenten"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tuplens andra element är det största av de givna argumenten"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tuplens tredje element är de givna argumentens summa"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skapa_tuple"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(-1, 5, 7)"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Äldst","tmcname":"osa05-18_aldst"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aldst(personer: list)"}]},{"type":"text","value":" som får som argument en lista med tupler som innehåller information om personer. Funktionen ska hitta den äldsta personen och returnera hennes namn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje tuple består av ett namn (element 1) och ett födelseår (element 2)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Algot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1977"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eskil\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mette\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1953"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ellie\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npersoner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mette"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Äldre","tmcname":"osa05-19_aldre"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi antar att vi fortfarande har tuplerna från den föregående uppgiften till vårt förfogande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aldre(personer: list, ar: int)"}]},{"type":"text","value":". Funktionen ska returnera en ny lista som innehåller namnen på personerna i den givna listan som fötts före det givna året."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"p1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Algot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1977"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eskil\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mette\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1953"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ellie\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npersoner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\naldre_personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" aldre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1979"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"aldre_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[ 'Algot', Mette' ]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"vad-behover-man-tupler-for","style":"position:relative;"},"children":[{"type":"text","value":"Vad behöver man tupler för?"},{"type":"element","tagName":"a","properties":{"href":"#vad-behover-man-tupler-for","ariaLabel":"vad behover man tupler for permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tupler är nyttiga i situationer där det finns en samling av värden som är på något sätt sammankopplade. Till exempel då man behandlar x- och y-koordinaterna hos en punkt, är tuplen ett naturligt val eftersom koordinater alltid består av två värden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är tekniskt möjligt att använda en lista för att lagra dessa:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En lista är en samling av element i en viss ordning. Listans storlek kan också ändra. Eftersom vi lagrar koordinaterna för en punkt, vill vi lagra x- och y-koordinaterna specifikt – inte en godtycklig lista med dessa värden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom tupler är oföränderliga – till skillnad från listor – kan de användas som nycklar i lexikon. Det följande kodexemplet skapar ett lexikon där koordinater används som nycklar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\napa\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är inte möjligt att skapa ett liknande lexikon med hjälp av listor:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\npunkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unhashable type: 'list'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"tupler-utan-parenteser","style":"position:relative;"},"children":[{"type":"text","value":"Tupler utan parenteser"},{"type":"element","tagName":"a","properties":{"href":"#tupler-utan-parenteser","ariaLabel":"tupler utan parenteser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Parenteser är inte obligatoriska då man skapar tupler. Följande variabeltilldelningar ger likadana resultat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det innebär att vi enkelt kan returnera flera värden med hjälp av tupler. Ta en titt på följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"minmax"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"21"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"88"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"312"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nminst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Minsta talet är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och största är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Minsta talet är 5 och största är 312"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar två värden i en tuple. Returvärdet kan tilldelas till två variabler samtidigt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda parenteser kan göra notationen tydligare. Till vänster av tilldelningssatsen har vi också en tuple som består av två variabelnamn. Värdena som finns i tuplen som funktionen returnerar tilldelas till dessa två variabler."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kanske minns metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"items"}]},{"type":"text","value":" från den förra delen. Vi använde den för att komma åt nycklarna och värdena som lagrats i ett lexikon:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lexikon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"text","value":"\nlexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lexikon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nyckel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nyckel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tupler finns i bakgrunden här också. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lexikon.items()"}]},{"type":"text","value":" returnerar varje nyckel-värdepar som en tuple, där det första elementet innehåller nyckeln och det andra värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat användningsområde för tupler är att byta värden sinsemellan två variabler:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"tal1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tal2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" tal2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tal1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tilldelningssatsen ovan svänger på värdena lagrade i variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tal1"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tal2"}]},{"type":"text","value":". Resultatet är detsamma som vi skulle uppnå med hjälp av en hjälpvariabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hjalp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" tal1\ntal1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" tal2\ntal2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hjalp"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Studeranderegister","tmcname":"osa05-20_studeranderegister"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna uppgift skapar vi ett enkelt register över studerande. Före du börjar programmera lönar det sig att fundera hurdan datastruktur som behövs för att organisera den data som ska lagras."}]},{"type":"element","tagName":"h4","properties":{"id":"lagga-till-studerande","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till studerande"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-studerande","ariaLabel":"lagga till studerande permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Börja med att skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skapa_studerande"}]},{"type":"text","value":" samt en första version av funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"text","value":", som används för att skriva ut information om en studerande."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här används funktionerna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siri\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här skedet skriver programmet ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Robert:\n inga prestationer\nLilou:\n inga prestationer\nhittade ingen med namnet Siri\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"lagga-till-prestationer","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till prestationer"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-prestationer","ariaLabel":"lagga till prestationer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_prestation"}]},{"type":"text","value":", som lägger till en prestation för en studerande. Prestationen är en tuple som består av kursens namn och ett vitsord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften ändras då det finns prestationer:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Robert:\n prestationer från 2 kurser:\n introkurs i Python 3\n datastrukturer 2\n medeltal 2.5\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"hoja-pa-vitsord","style":"position:relative;"},"children":[{"type":"text","value":"Höja på vitsord"},{"type":"element","tagName":"a","properties":{"href":"#hoja-pa-vitsord","ariaLabel":"hoja pa vitsord permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man lägger till en prestation ska kurser med vitsordet noll ignorera och tidigare vitsord ska inte sänkas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer II\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Robert:\n prestationer från 2 kurser:\n introkurs i Python 3\n datastrukturer 2\n medeltal 2.5\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"oversikt","style":"position:relative;"},"children":[{"type":"text","value":"Översikt"},{"type":"element","tagName":"a","properties":{"href":"#oversikt","ariaLabel":"oversikt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"oversikt"}]},{"type":"text","value":" som skriver ut en översikt för samtliga studerandes prestationer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskapa_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer II\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"datastrukturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"introkurs i Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nny_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lilou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spaden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkooste"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften ser ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"antal studerande 2\nflest prestationer 3 Robert\nbästa medeltal 4.5 Lilou\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bokstavsruta","tmcname":"osa05-21_bokstavsruta"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här delen når nu sitt kulmen med en relativt svår uppgift som kräver problemlösningsförmåga. Uppgiften kan lösas på flera sätt. Även om vi nyss har behandlat tupler så lönar det sig dock antagligen inte att använda dem i den här uppgiften."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som skriver ut en bokstavsruta enligt följande exempel. Du kan anta att det finns högst 26 våningar."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Våningar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"CCCCC\nCBBBC\nCBABC\nCBBBC\nCCCCC\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Våningar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"DDDDDDD\nDCCCCCD\nDCBBBCD\nDCBABCD\nDCBBBCD\nDCCCCCD\nDDDDDDD\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Placera inte kod i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket i dessa uppgifter, om du inte skilt ombeds göra det."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät gällande materialet för den här veckan."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • känner du till datatypen tuple
  • \n
  • kan du skapa tupler av olika typer av värden
  • \n
  • vet du vad skillnaden mellan en lista och tuple är
  • \n
  • kan du nämna några vanliga användningsområden för tupler.
  • \n

En tuple är en datastruktur som på flera sätt påminner listan. De största skillnaderna mellan dessa två är:

    \n
  • tupler är inom parenteser () medan listor är inom hakparenteser []
  • \n
  • tupler är oföränderliga, medan innehållet i en lista kan ändra.
  • \n

Den följande kodsnutten skapar en tuple som innehåller koordinaterna för en punkt:

punkt = (10, 20)

Elementen lagrade i tupler kan kommas åt med index, på samma sätt som med listor:

punkt = (10, 20)\nprint(\"x-koordinat:\", punkt[0])\nprint(\"y-koordinat:\", punkt[1])

x-koordinat: 10\ny-koordinat: 20

Värden som lagrats i en tuple kan inte ändras efter att en tuple har skapats. Det följande kommer inte att fungera:

punkt = (10, 20)\npunkt[0] = 15

TypeError: 'tuple' object does not support item assignment

Skapa funktionen skapa_tuple(x: int, y: int, z: int) som skapar och returnerar en tuple enligt dessa regler:

    \n
  • tuplens första element är det minsta av de givna argumenten
  • \n
  • tuplens andra element är det största av de givna argumenten
  • \n
  • tuplens tredje element är de givna argumentens summa
  • \n

Exempel:

if __name__ == \"__main__\":\n    print(skapa_tuple(5, 3, -1))

(-1, 5, 7)

Skapa funktionen aldst(personer: list) som får som argument en lista med tupler som innehåller information om personer. Funktionen ska hitta den äldsta personen och returnera hennes namn.

Varje tuple består av ett namn (element 1) och ett födelseår (element 2).

Exempel:

p1 = (\"Algot\", 1977)\np2 = (\"Eskil\", 1985)\np3 = (\"Mette\", 1953)\np4 = (\"Ellie\", 1997)\npersoner = [p1, p2, p3, p4]\n\nprint(aldst(personer))

Mette

Vi antar att vi fortfarande har tuplerna från den föregående uppgiften till vårt förfogande.

Skapa funktionen aldre(personer: list, ar: int). Funktionen ska returnera en ny lista som innehåller namnen på personerna i den givna listan som fötts före det givna året.

Exempel:

p1 = (\"Algot\", 1977)\np2 = (\"Eskil\", 1985)\np3 = (\"Mette\", 1953)\np4 = (\"Ellie\", 1997)\npersoner = [p1, p2, p3, p4]\n\naldre_personer = aldre(personer, 1979)\nprint(aldre_personer)

[ 'Algot', Mette' ]

Vad behöver man tupler för?

Tupler är nyttiga i situationer där det finns en samling av värden som är på något sätt sammankopplade. Till exempel då man behandlar x- och y-koordinaterna hos en punkt, är tuplen ett naturligt val eftersom koordinater alltid består av två värden:

punkt = (10, 20)

Det är tekniskt möjligt att använda en lista för att lagra dessa:

punkt = [10, 20]

En lista är en samling av element i en viss ordning. Listans storlek kan också ändra. Eftersom vi lagrar koordinaterna för en punkt, vill vi lagra x- och y-koordinaterna specifikt – inte en godtycklig lista med dessa värden.

Eftersom tupler är oföränderliga – till skillnad från listor – kan de användas som nycklar i lexikon. Det följande kodexemplet skapar ett lexikon där koordinater används som nycklar:

punkter = {}\npunkter[(3, 5)] = \"apa\"\npunkter[(5, 0)] = \"banan\"\npunkter[(1, 2)] = \"cembalo\"\nprint(punkter[(3, 5)])
\napa\n

Det är inte möjligt att skapa ett liknande lexikon med hjälp av listor:

punkter = {}\npunkter[[3, 5]] = \"apa\"\npunkter[[5, 0]] = \"banan\"\npunkter[[1, 2]] = \"cembalo\"\nprint(punkter[[3, 5]])

TypeError: unhashable type: 'list'

Tupler utan parenteser

Parenteser är inte obligatoriska då man skapar tupler. Följande variabeltilldelningar ger likadana resultat:

siffror = (1, 2, 3)
siffror = 1, 2, 3

Det innebär att vi enkelt kan returnera flera värden med hjälp av tupler. Ta en titt på följande exempel:

def minmax(lista):\n  return min(lista), max(lista)\n\nlista = [33, 5, 21, 7, 88, 312, 5]\n\nminst, storst = minmax(lista)\nprint(f\"Minsta talet är {minst} och största är {storst}\")

Minsta talet är 5 och största är 312

Funktionen returnerar två värden i en tuple. Returvärdet kan tilldelas till två variabler samtidigt:

minst, storst = minmax(lista)

Att använda parenteser kan göra notationen tydligare. Till vänster av tilldelningssatsen har vi också en tuple som består av två variabelnamn. Värdena som finns i tuplen som funktionen returnerar tilldelas till dessa två variabler.

(minst, storst) = minmax(lista)

Du kanske minns metoden items från den förra delen. Vi använde den för att komma åt nycklarna och värdena som lagrats i ett lexikon:

lexikon = {}\n\nlexikon[\"apina\"] = \"apa\"\nlexikon[\"banaani\"] = \"banan\"\nlexikon[\"cembalo\"] = \"cembalo\"\n\nfor nyckel, varde in lexikon.items():\n    print(\"nyckel:\", nyckel)\n    print(\"värde:\", varde)

Tupler finns i bakgrunden här också. Metoden lexikon.items() returnerar varje nyckel-värdepar som en tuple, där det första elementet innehåller nyckeln och det andra värdet.

Ett annat användningsområde för tupler är att byta värden sinsemellan två variabler:

tal1, tal2 = tal2, tal1

Tilldelningssatsen ovan svänger på värdena lagrade i variablerna tal1 och tal2. Resultatet är detsamma som vi skulle uppnå med hjälp av en hjälpvariabel:

hjalp = tal1\ntal1 = tal2\ntal2 = hjalp

I denna uppgift skapar vi ett enkelt register över studerande. Före du börjar programmera lönar det sig att fundera hurdan datastruktur som behövs för att organisera den data som ska lagras.

Lägga till studerande

Börja med att skapa funktionen skapa_studerande samt en första version av funktionen skriv_ut, som används för att skriva ut information om en studerande.

Så här används funktionerna:

studerande = {}\nskapa_studerande(studerande, \"Robert\")\nskapa_studerande(studerande, \"Lilou\")\nskriv_ut(studerande, \"Robert\")\nskriv_ut(studerande, \"Lilou\")\nskriv_ut(studerande, \"Siri\")

I det här skedet skriver programmet ut:

\nRobert:\n inga prestationer\nLilou:\n inga prestationer\nhittade ingen med namnet Siri\n

Lägga till prestationer

Skapa funktionen ny_prestation, som lägger till en prestation för en studerande. Prestationen är en tuple som består av kursens namn och ett vitsord:

studerande = {}\nskapa_studerande(studerande, \"Robert\")\nny_prestation(studerande, \"Robert\", (\"introkurs i Python\", 3))\nny_prestation(studerande, \"Robert\", (\"datastrukturer\", 2))\nskriv_ut(studerande, \"Robert\")

Utskriften ändras då det finns prestationer:

\nRobert:\n prestationer från 2 kurser:\n  introkurs i Python 3\n  datastrukturer 2\n medeltal 2.5\n

Höja på vitsord

När man lägger till en prestation ska kurser med vitsordet noll ignorera och tidigare vitsord ska inte sänkas:

studerande = {}\nskapa_studerande(studerande, \"Robert\")\nny_prestation(studerande, \"Robert\", (\"introkurs i Python\", 3))\nny_prestation(studerande, \"Robert\", (\"datastrukturer\", 2))\nny_prestation(studerande, \"Robert\", (\"datastrukturer II\", 0))\nny_prestation(studerande, \"Robert\", (\"introkurs i Python\", 2))\nskriv_ut(studerande, \"Robert\")
\nRobert:\n prestationer från 2 kurser:\n  introkurs i Python 3\n  datastrukturer 2\n medeltal 2.5\n

Översikt

Skapa funktionen oversikt som skriver ut en översikt för samtliga studerandes prestationer:

studerande = {}\nskapa_studerande(studerande, \"Robert\")\nskapa_studerande(studerande, \"Lilou\")\nny_prestation(studerande, \"Robert\", (\"datastrukturer II\", 1))\nny_prestation(studerande, \"Robert\", (\"introkurs i Python\", 1))\nny_prestation(studerande, \"Robert\", (\"datastrukturer\", 1))\nny_prestation(studerande, \"Lilou\", (\"introkurs i Python\", 5))\nny_prestation(studerande, \"Lilou\", (\"spaden\", 4))\nkooste(studerande)

Utskriften ser ut så här:

\nantal studerande 2\nflest prestationer 3 Robert\nbästa medeltal 4.5 Lilou\n

Den här delen når nu sitt kulmen med en relativt svår uppgift som kräver problemlösningsförmåga. Uppgiften kan lösas på flera sätt. Även om vi nyss har behandlat tupler så lönar det sig dock antagligen inte att använda dem i den här uppgiften.

Skapa ett program som skriver ut en bokstavsruta enligt följande exempel. Du kan anta att det finns högst 26 våningar.

Våningar: 3

\nCCCCC\nCBBBC\nCBABC\nCBBBC\nCCCCC\n

Våningar: 4

\nDDDDDDD\nDCCCCCD\nDCBBBCD\nDCBABCD\nDCBBBCD\nDCCCCCD\nDDDDDDD\n

Obs! Placera inte kod i if __name__ == \"__main__\" -blocket i dessa uppgifter, om du inte skilt ombeds göra det.

Vänligen svara på en kort enkät gällande materialet för den här veckan.

","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/4-tuple.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-5/page-data.json b/page-data/osa-5/page-data.json index dae38557d..5be9e4874 100644 --- a/page-data/osa-5/page-data.json +++ b/page-data/osa-5/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-5", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-5","title":"Modul 5"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-5","title":"Modul 5"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-5/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-6/1-lasa-filer/page-data.json b/page-data/osa-6/1-lasa-filer/page-data.json index 101b83e21..f8e5b732e 100644 --- a/page-data/osa-6/1-lasa-filer/page-data.json +++ b/page-data/osa-6/1-lasa-filer/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-6/1-lasa-filer", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du läsa en fil med Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad en textfil och en CSV-fil är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du behandla innehållet i en CSV-fil i dina program."}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"vastaava teksti löytyy osioista 3-1, 5-1 ja 6-1, tsekkaa kaikki jos muokkaat tätä"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Om uppgifterna i den här kursen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man programmerar kan det uppstå ett behov att behandla data som finns lagrad i filer. Datorprogram kan läsa data från filer och skriva data till filer. Också stora mängder data i filer kan enkelt behandlas automatiskt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Under den här kursen kommer vi endast att arbeta med textfiler. De här filerna består av rader med text. Till exempel kodeditorn Visual Studio Code är kompatibel med textfiler. Obs! Även om ordbehandlingsprogram som Microsoft Word ofta används med filer som innehåller text, är Word-dokument inte textfiler. Dokumenten innehåller också annan information om till exempel textformat, vilket gör det mer komplicerat att behandla filerna i ett program."}]},{"type":"element","tagName":"h2","properties":{"id":"att-lasa-data-fran-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Att läsa data från en fil"},{"type":"element","tagName":"a","properties":{"href":"#att-lasa-data-fran-en-fil","ariaLabel":"att lasa data fran en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi börjar att arbeta med filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exempel.txt"}]},{"type":"text","value":" som innehållet det följande:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!\nVår exempelfil består av tre rader.\nDet här är den sista raden."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett enkelt sätt att använda filer i Python är med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":"-satsen. Den inledande raden öppnar filen och blocket där vi kan komma åt filen följer. Efter blocket stängs filen automatiskt och då kan den inte mera behandlas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här koden öppnar alltså filen, läser dess innehåll och skriver det ut, och till slut stängs filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n innehall "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"innehall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!\nVår exempelfil består av tre rader.\nDet här är den sista raden."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fil"}]},{"type":"text","value":" är en file handle (\"filhandtag\"). Via variabeln kan vi komma åt filen så länge den är öppen. Här använde vi metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":" som returnerar filens innehåll som en hel sträng. I det här fallet skulle strängen se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Hej alla!\\nVår exempelfil består av tre rader.\\nDet här är den sista raden.\""}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"ga-igenom-innehallet-i-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Gå igenom innehållet i en fil"},{"type":"element","tagName":"a","properties":{"href":"#ga-igenom-innehallet-i-en-fil","ariaLabel":"ga igenom innehallet i en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":" fungerar väl för att skriva ut hela innehållet i en fil, men ofta vill vi gå igenom innehållet rad för rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan tänka att textfiler är som listor med strängar, där varje sträng finns på sin egen rad i filen. Vi kan gå igenom listan med en for-loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel läser in vår exempelfil med hjälp av en for-loop, tar bort radbrytningarna, räknar antalet rader och skriver ut varje rad med sitt radnummer. Programmet håller också koll på radernas längder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n totallangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rad\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n totallangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" langd\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Radernas totallängd:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" totallangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rad 1 Hej alla!\nRad 2 Vår exempelfil består av tre rader.\nRad 3 Det här är den sista raden.\nRadernas totallängd: 63"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns en radbrytning "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" i slutet av varje rad i filen, men "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-funktionen lägger också automatiskt till en rad i slutet av utskriften. Det finns inga extra radbyten i utskriften ovan eftersom radbrytningarna avlägsnats med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":"-metoden. Metoden ersätter alla radbrytningstecken med en tom sträng. I och med detta räknas radernas längder också korrekt."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Största siffran","tmcname":"osa06-01_storsta_siffran"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror.txt"}]},{"type":"text","value":" finns siffror listade på olika rader enligt exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"108"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","parameter","variable"]},"children":[{"type":"text","value":"-10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1100"}]},{"type":"text","value":"\no.s.v. "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storst"}]},{"type":"text","value":" som ska läsa filen och returnera den största siffran som hittas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att filnamnet alltid är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror.txt"}]},{"type":"text","value":" och att funktionen inte har några parametrar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hitta din fil även om namnet är korrekt skrivet ska du följa instruktionerna nedan."}]}]},{"type":"element","tagName":"h2","properties":{"id":"om-visual-studio-code-inte-hittar-min-fil","style":"position:relative;"},"children":[{"type":"text","value":"Om Visual Studio Code inte hittar min fil?"},{"type":"element","tagName":"a","properties":{"href":"#om-visual-studio-code-inte-hittar-min-fil","ariaLabel":"om visual studio code inte hittar min fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du kör din kod är det möjligt att Visual Studio Code meddelar att filen – även efter att du kollat att filen finns och att namnet är korrekt skrivet. Att ändra på följande inställning kan lösa problemet:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"öppna inställningarna från menyraden: File -> Preferences -> Settings"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"sök efter den inställning som ska ändras med sökordet \"executeinfile\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"välj fliken Workspace"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"bocka i valet under Python -> Terminal -> Execute in file dir."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inställningsfönstret borde ungefär se ut så här:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6e5d89f76dfd47af6f9ea67b097452e8/e405b/6_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 16.521739130434785%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAIAAAAcOLh5AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAZElEQVQI11XOQQqAMAwEwP5DknRNkNIilaInvXn3/8+RKIoOOe4uCQAQHYDWWq01/xS/UnLO07rP+9HbEEU8DQT+SCmZmZqlB77GRdvGUCa687+yiDAzVH3i8hb9MyZQJ0xv/gSEjBk5LU20dgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e5d89f76dfd47af6f9ea67b097452e8/a0b58/6_1_1.webp 230w","/static/6e5d89f76dfd47af6f9ea67b097452e8/bc10c/6_1_1.webp 460w","/static/6e5d89f76dfd47af6f9ea67b097452e8/966d8/6_1_1.webp 920w","/static/6e5d89f76dfd47af6f9ea67b097452e8/445df/6_1_1.webp 1380w","/static/6e5d89f76dfd47af6f9ea67b097452e8/9b2c8/6_1_1.webp 1566w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e5d89f76dfd47af6f9ea67b097452e8/81c8e/6_1_1.png 230w","/static/6e5d89f76dfd47af6f9ea67b097452e8/08a84/6_1_1.png 460w","/static/6e5d89f76dfd47af6f9ea67b097452e8/c0255/6_1_1.png 920w","/static/6e5d89f76dfd47af6f9ea67b097452e8/b1001/6_1_1.png 1380w","/static/6e5d89f76dfd47af6f9ea67b097452e8/e405b/6_1_1.png 1566w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6e5d89f76dfd47af6f9ea67b097452e8/c0255/6_1_1.png","alt":"6 1 1","title":"6 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det här inte fungerar kan du kopiera filen i src-mappen…"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a57483e6084d39a596ee24c41a42de70/6297e/6_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 22.17391304347826%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAApklEQVQI123N0QqCQBAFUH/CUVlnt3JnVtcSyx1XikKCHur/PyhQih463KcLl5s87rcQRcLoa98037TO1czsFtba6p/kFESm8+U6HyTuT4FcY11Dtd9WVH4gol7keQ4Aa4OIySBjfxwkyKuLc90P5ImYiHeWtdmYRVmWSqmiKLIsAwCllDFGa50EiW3Xj3F6tjKQr5YlE28tozbrISKmaQoAiPg7fgOYxyLFEKvBSAAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a57483e6084d39a596ee24c41a42de70/a0b58/6_1_2.webp 230w","/static/a57483e6084d39a596ee24c41a42de70/bc10c/6_1_2.webp 460w","/static/a57483e6084d39a596ee24c41a42de70/966d8/6_1_2.webp 920w","/static/a57483e6084d39a596ee24c41a42de70/445df/6_1_2.webp 1380w","/static/a57483e6084d39a596ee24c41a42de70/78de1/6_1_2.webp 1840w","/static/a57483e6084d39a596ee24c41a42de70/4b7c9/6_1_2.webp 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a57483e6084d39a596ee24c41a42de70/81c8e/6_1_2.png 230w","/static/a57483e6084d39a596ee24c41a42de70/08a84/6_1_2.png 460w","/static/a57483e6084d39a596ee24c41a42de70/c0255/6_1_2.png 920w","/static/a57483e6084d39a596ee24c41a42de70/b1001/6_1_2.png 1380w","/static/a57483e6084d39a596ee24c41a42de70/161ec/6_1_2.png 1840w","/static/a57483e6084d39a596ee24c41a42de70/6297e/6_1_2.png 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a57483e6084d39a596ee24c41a42de70/c0255/6_1_2.png","alt":"6 1 2","title":"6 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…direkt till roten av uppgiftsmappen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/bc83b27162a67ce98f825821dc304a7e/6297e/6_1_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 26.956521739130434%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAt0lEQVQY023P3Q6CMAwFYN6DjqjtNta1MxlDdILGxBvf/4UMGO/4cm5Pf5pa79N0m+eHSBJJMSqzMKv3vaqklPq+995ba51zdkNkPR2COzallJzL8/lalketc86FWWJUIsfMqtq2bdd1sDF/AOaE2AzDUIYyjmPOWUQRiWidjYgxRlU1xnQ7DBGtmyWdy2ViZqL1ul8QSSQyMwDsVI1BxOZ6md65fsYlyTms3/6iznnd7JYBIITwBcyRLmVWTxqEAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bc83b27162a67ce98f825821dc304a7e/a0b58/6_1_3.webp 230w","/static/bc83b27162a67ce98f825821dc304a7e/bc10c/6_1_3.webp 460w","/static/bc83b27162a67ce98f825821dc304a7e/966d8/6_1_3.webp 920w","/static/bc83b27162a67ce98f825821dc304a7e/445df/6_1_3.webp 1380w","/static/bc83b27162a67ce98f825821dc304a7e/78de1/6_1_3.webp 1840w","/static/bc83b27162a67ce98f825821dc304a7e/4b7c9/6_1_3.webp 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bc83b27162a67ce98f825821dc304a7e/81c8e/6_1_3.png 230w","/static/bc83b27162a67ce98f825821dc304a7e/08a84/6_1_3.png 460w","/static/bc83b27162a67ce98f825821dc304a7e/c0255/6_1_3.png 920w","/static/bc83b27162a67ce98f825821dc304a7e/b1001/6_1_3.png 1380w","/static/bc83b27162a67ce98f825821dc304a7e/161ec/6_1_3.png 1840w","/static/bc83b27162a67ce98f825821dc304a7e/6297e/6_1_3.png 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/bc83b27162a67ce98f825821dc304a7e/c0255/6_1_3.png","alt":"6 1 3","title":"6 1 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"att-debugga-kod-som-behandlar-filer","style":"position:relative;"},"children":[{"type":"text","value":"Att debugga kod som behandlar filer"},{"type":"element","tagName":"a","properties":{"href":"#att-debugga-kod-som-behandlar-filer","ariaLabel":"att debugga kod som behandlar filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man använder Visual Studio Codes debuggare med program som behandlar filer, kan man stöta på följande felmeddelande:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b0c7745f7a4cc9c485a52d483e0aa290/eff3b/6_1_4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 31.304347826086953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAABMklEQVQY003PyU7CQACA4d7EBWQpLdDS6XSWzkw702VIIBESgyaYWBZRE60QUIzv/wguB0PyX7/Db2itx5PJcDiKYym4ICFXaSKkZFKGMhUyESKmlAEY+BD5EAGIMA5iThhjRpZlOhsooTjlmFAKgaBgPMSjHOS8kwtHx55itgqt4xIllEqNPM85F4xxSmhAyAziDabbiG8F28voBcGnvrNot+ZmY2E252azMBtFvSZ7XQADQ19dI4QJoX+Y3vXhgcbvcbYL5Z6pHYlKQEpAS0DWrd6yZi3r1uK0Hpm26wND6xxj8o9nrv8Zxl/p4CCSQ5R8cFV6aAPwm4eeLWfd7Dy2OqvzRtS2XQB+n4/x1IOvNHrw8dIBSwcUtlPYzsqF9+3e7dnltFKdVqo3JxfC7v7gb6ThPL16POKXAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b0c7745f7a4cc9c485a52d483e0aa290/a0b58/6_1_4.webp 230w","/static/b0c7745f7a4cc9c485a52d483e0aa290/bc10c/6_1_4.webp 460w","/static/b0c7745f7a4cc9c485a52d483e0aa290/966d8/6_1_4.webp 920w","/static/b0c7745f7a4cc9c485a52d483e0aa290/445df/6_1_4.webp 1380w","/static/b0c7745f7a4cc9c485a52d483e0aa290/78de1/6_1_4.webp 1840w","/static/b0c7745f7a4cc9c485a52d483e0aa290/5340a/6_1_4.webp 2072w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b0c7745f7a4cc9c485a52d483e0aa290/81c8e/6_1_4.png 230w","/static/b0c7745f7a4cc9c485a52d483e0aa290/08a84/6_1_4.png 460w","/static/b0c7745f7a4cc9c485a52d483e0aa290/c0255/6_1_4.png 920w","/static/b0c7745f7a4cc9c485a52d483e0aa290/b1001/6_1_4.png 1380w","/static/b0c7745f7a4cc9c485a52d483e0aa290/161ec/6_1_4.png 1840w","/static/b0c7745f7a4cc9c485a52d483e0aa290/eff3b/6_1_4.png 2072w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b0c7745f7a4cc9c485a52d483e0aa290/c0255/6_1_4.png","alt":"6 1 4","title":"6 1 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orsaken är att debuggaren alltid söker efter filer i roten av uppgiftsmappen. Inställningen Execute in file dir som nämndes ovan har ingen påverkan här. Den enklaste lösningen är att kopiera filen till rotmappen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du behöver kanske också starta om Visual Studio Code efter att du har kopierat alla filer som behövs."}]},{"type":"element","tagName":"h2","properties":{"id":"lasa-csv-filer","style":"position:relative;"},"children":[{"type":"text","value":"Läsa CSV-filer"},{"type":"element","tagName":"a","properties":{"href":"#lasa-csv-filer","ariaLabel":"lasa csv filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En CSV-fil (kommaseparerade värden) är en textfil som innehåller data som separerats med ett visst tecken. Det här tecknet är vanligtvis komma ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":","}]},{"type":"text","value":") eller semikolon ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"), men vilket som helst tecken är i princip möjligt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV-filer är ett vanligt sätt att lagra olika typer av data. Flera databaser och kalkylprogram – exempelvis Excel – kan importera och exportera data i CSV-format. Det här möjliggör enkel dataöverföring mellan olika system."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan bekantat oss med hur man kan gå igenom rader i en fil med en for-loop, men hur kan vi separera fält på en och samma rad? Python har en strängmetod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":", som kan användas för detta. Metoden tar separatortecknet eller -tecknen som ett strängargument och returnerar innehållet i den ursprungliga strängen som en lista av strängar – separerade vid separatortecknen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här finns ett exempel för att tydliggöra det här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa,banan,cembalo\""}]},{"type":"text","value":"\nordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"apa\nbanan\ncembalo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss säga att vi har filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vitsord.csv"}]},{"type":"text","value":", som innehåller namn på elever samt vitsord de fått av olika kurser. Varje rad har data som tillhör en studerande och data separeras med semikolon."}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter;5;4;5;3;4;5;5;4;2;4\nPauline;3;4;2;4;4;2;3;1;3;3\nPia;4;5;5;4;5;5;4;5;4;4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program går igenom filen rad för rad, delar upp raderna i delar och skriver ut namnen på eleverna samt deras vitsord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vitsord.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vitsord:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Namn: Peter\nVitsord: ['5', '4', '5', '3', '4', '5', '5', '4', '2', '4']\nNamn: Pauline\nVitsord: ['3', '4', '2', '4', '4', '2', '3', '1', '3', '3']\nNamn: Pia\nVitsord: ['4', '5', '5', '4', '5', '5', '4', '5', '4', '4']"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fruktaffär","tmcname":"osa06-02_fruktaffar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"frukter.csv"}]},{"type":"text","value":" finns frukter med deras pris enligt exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"banan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6.50"}]},{"type":"text","value":"\näpple"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"text","value":"\napelsin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.0"}]},{"type":"text","value":"\no.s.v. "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"las_frukter"}]},{"type":"text","value":" som ska läsa filen och skapa ett lexikon där nyckeln är fruktens namn och värdet fruktens pris. Priset ska vara av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att filnamnet alltid är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"frukter.csv"}]},{"type":"text","value":" och funktionen har inga parametrar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska till slut returnera lexikonet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Matris","tmcname":"osa06-03_matris"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matris.txt"}]},{"type":"text","value":" finns en matris enligt exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1,0"}]},{"type":"text","value":",2,8,2,1,3,2,5,2,2,2\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9,2"}]},{"type":"text","value":",4,5,2,4,2,4,1,10,4,2\no.s.v. "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"maximum"}]},{"type":"text","value":" som returnerar summan av elementen i matrisen respektive det största elementet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa också funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"radsummor"}]},{"type":"text","value":" som returnerar som en lista summorna av matrisens rader. Till exempel för matrisen..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1,2"}]},{"type":"text","value":",3\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2,3"}]},{"type":"text","value":",4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...returnerar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[6, 9]"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Du kan också implementera andra funktioner i programmet. Fundera vilka gemensamma funktioner de ovan nämnda funktionerna kan behöva."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att filen alltid heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matris.txt"}]},{"type":"text","value":" och funktionerna inte har parametrar. Ytterligare funktioner du eventuellt skapar kan ha parametrar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lasa-samma-fil-flera-ganger","style":"position:relative;"},"children":[{"type":"text","value":"Läsa samma fil flera gånger"},{"type":"element","tagName":"a","properties":{"href":"#lasa-samma-fil-flera-ganger","ariaLabel":"lasa samma fil flera ganger permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland kan man behöva läsa innehållet i en fil flera gånger i samma program. Vi tittar på ett program som behandlar data om några personer i en CSV-fil:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"text","value":"\nPeter;40;Helsingfors\nEmilia;34;Esbo\nErik;42;Åbo\nAntonia;100;Helsingfors\nLisa;58;Suonenjoki\n"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriver ut namn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# söker efter den äldsta personen"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" hogsta_aldern"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n aldst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den äldsta är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi kör programmet får vi det här felmeddelandet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"Traceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call last"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den äldsta är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nUnboundLocalError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" local variable "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'aldst'"}]},{"type":"text","value":" referenced before assignment"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orsaken till at det här sker är att den andra for-loopen aldrig körs. Detta eftersom filen endast kan behandlas en gång. När den sista raden har lästs stannar file handlen i slutet av filen och data i filen kan inte längre kommas åt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill komma åt innehållet i filen i den andra for-loopen, måste vi öppna filen på nytt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriver ut namn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# söker efter den äldsta personen"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" hogsta_aldern"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n aldst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den äldsta är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om den ovanstående koden fungerar, innehåller den onödig upprepning. Det lönar sig vanligtvis att läsa filen bara en gång, och spara dess innehåll i ett passligt format för fortsatt behandling:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi läser in personerna till listan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriver ut namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# söker efter den äldsta personen"}]},{"type":"text","value":"\nhogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" hogsta_aldern"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n aldst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den äldsta är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"mera-om-att-behandla-csv-filer","style":"position:relative;"},"children":[{"type":"text","value":"Mera om att behandla CSV-filer"},{"type":"element","tagName":"a","properties":{"href":"#mera-om-att-behandla-csv-filer","ariaLabel":"mera om att behandla csv filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi fortsätter behandla filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vitsord.csv"}]},{"type":"text","value":", som innehåller det följande:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter;5;4;5;3;4;5;5;4;2;4\nPauline;3;4;2;4;4;2;3;1;3;3\nPia;4;5;5;4;5;5;4;5;4;4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program skapar lexikonet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vitsord"}]},{"type":"text","value":" baserat på innehållet i filen. Nycklarna är elevernas namn och värdet som är kopplat till nycklarna innehåller elevens vitsord. Programmet konverterar vitsorden till heltal så att de kan behandlas enklare."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vitsord.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" givet_vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"givet_vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Peter': [5, 4, 5, 3, 4, 5, 5, 4, 2, 4], 'Pauline': [3, 4, 2, 4, 4, 2, 3, 1, 3, 3], 'Pia': [4, 5, 5, 4, 5, 5, 4, 5, 4, 4]}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu kan vi skriva ut statistik om varje studerande, baserat på värdena i lexikonet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n basta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n medeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": bästa vitsordet "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"basta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", medeltal "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":".2f"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter: bästa vitsordet 5, medeltal 4.10\nPauline: bästa vitsordet 4, medeltal 2.90\nPia: bästa vitsordet 5, medeltal 4.50"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ta en titt på programmet i exemplet ovan. Det kan verka något komplicerat på en första titt, men tekniken kan användas med flera olika typer av data."}]},{"type":"element","tagName":"h2","properties":{"id":"ta-bort-overflodiga-rader-mellanslag-och-radbrytningar","style":"position:relative;"},"children":[{"type":"text","value":"Ta bort överflödiga rader, mellanslag och radbrytningar"},{"type":"element","tagName":"a","properties":{"href":"#ta-bort-overflodiga-rader-mellanslag-och-radbrytningar","ariaLabel":"ta bort overflodiga rader mellanslag och radbrytningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss säga att vi har en CSV-fil med namn, exporterat från Excel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"förnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" efternamn\nPeter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Python\nJaana"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Java\nHeikki"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Haskell"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Excel är ökänt för att lägga till extra mellanrum lite här och där. Här har vi ett extra mellanrum mellan elementen, efter varje semikolon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skulle vilja skriva ut efternamnet på varje person som finns i listan. Den första raden i filen innehåller information om den data som följer och kan skippas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"efternamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skippar raden med rubriker"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"förnamn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"\n efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När koden körs får vi den här utskriften:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[' Python\\n', ' Java\\n', ' Haskell']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De två första elementen har ett radbrytningstecken i slutet och alla tre element har ett mellanslag i början. Vi har redan använt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":"-metoden för att ta bort onödigt mellanrum, men ett bättre sätt är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strip"}]},{"type":"text","value":"-metoden hos strängar. Den här metoden tar bort mellanrum från början och slutet av en sträng. Metoden tar bort mellanrum, radbrytningar, samt tabb- och andra tecken som normalt inte skulle skrivas ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan testa på metoden i Python-terminalen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" prov \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'prov'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\\ntest\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'test'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att ta bort de onödiga tecknen kräver bara en liten ändring i programmet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"efternamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"förnamn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skippar raden med rubriker"}]},{"type":"text","value":"\n efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu har får vi den önskade utskriften:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['Python', 'Java', 'Haskell']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängmetoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lstrip"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rstrip"}]},{"type":"text","value":" fungerar lika som metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strip"}]},{"type":"text","value":", men gör det då bara för antingen vänstra (l) eller högra (r) kanten av strängen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" teststräng \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rstrip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"' teststräng'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" teststräng \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lstrip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'teststräng '"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"kombinera-data-fran-olika-filer","style":"position:relative;"},"children":[{"type":"text","value":"Kombinera data från olika filer"},{"type":"element","tagName":"a","properties":{"href":"#kombinera-data-fran-olika-filer","ariaLabel":"kombinera data fran olika filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är mycket vanligt att data som behandlas av ett program finns utspritt i flera filer. Vi tar en titt på ett exempel där personalens information i ett företag finns i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"personal.csv"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"personnr;namn;adress;adressort"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"080488-123X;Peter Mikkola;Filpusvägen 7;00700 HELSINGFORS"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"290274-044S;Lisa Marttinen;Mannerheimvägen 100 A 10;00100 HELSINGFORS"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010479-007Z;Arto Vihavainen;Tiilitehtaankatu 10;04260 KERAVA"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010499-345K;Leevi Hellas;Tapiolavägen 9;02100 ESBO"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Löneuppgifterna finns i en skild fil, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lon.csv"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"personnr;lön;bonus"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"080488-123X;3300;0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"290274-044S;4150;200"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010479-007Z;1300;1200"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla rader i båda filerna innehåller en personlig id-kod (pic) som identifierar vems data vi arbetar med. När vi använder det här id:t som gemensam faktor, är det lätt att koppla en arbetstagares namn med hennes lön. Vi kan till exempel skriva ut en lista över de månatliga inkomsterna:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"inkomster:\nPeter Mikkola 3300 euro\nLisa Marttinen 4350 euro\nArto Vihavainen 2500 euro\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet använder två lexikon som hjälpdatastrukturer: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"loner"}]},{"type":"text","value":". Båda använder pic som nyckel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personal.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personnr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"\n namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nloner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lon.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personnr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"\n loner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"inkomster:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" personnr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" personnr "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" loner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" loner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"personnr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 0 euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först skapar programmet lexikonen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"loner"}]},{"type":"text","value":". De har dessa innehåll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'080488-123X'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Peter Mikkola'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'290274-044S'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Lisa Marttinen'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010479-007Z'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Arto Vihavainen'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010499-345K'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Leevi Hellas'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'080488-123X'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3300"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'290274-044S'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4350"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010479-007Z'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For-loopen i slutet av programmet kombinerar namnen på arbetstagarna med deras löner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet kan också ta i beaktande situationer där pic saknas för en arbetstagare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att ordningen som elementen är lagrade i lexikon inte har någon skillnad, eftersom nycklarna behandlas med hjälp av hashvärden."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursresultat, del 1","tmcname":"osa06-04_resultat_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet behandlar två CSV-filen. I den ena finns information om studerande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"studerandenr;förnamn;efternamn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;pekka;peloton"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;jaana;javanainen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;liisa;virtanen"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Och i den andra antalet gjorda uppgifter på veckonivå:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"studerandenr;v1;v2;v3;v4;v5;v6;v7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;4;1;1;4;5;2;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;3;5;3;1;5;4;6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;10;2;2;7;10;2;2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I de båda CSV-filerna innehåller den första rade rubriker."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter filnamnen och skriver därefter ut antalet gjorda uppgifter för varje studerande. Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"studerande1.csv"}]},{"type":"text","value":"\nUppgifter (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"uppgifter1.csv"}]},{"type":"text","value":"\npekka peloton 21\njaana javanainen 27\nliisa virtanen 35"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Hårdkoda värdena medan du testar programmet, så behöver du inte hela tiden mata in dem på nytt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hit kommer vi aldrig"}]},{"type":"text","value":"\n studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studerande (CSV): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uppgifter (CSV): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# de hårdkodade värdena"}]},{"type":"text","value":"\n studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studerande1.csv\""}]},{"type":"text","value":"\n uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"uppgifter1.csv\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den egentliga funktionaliteten är nu \"gömd\" bakom en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"-förgrening som aldrig körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill testa inmatning av information (på \"normalt\" sätt), kan vi ändra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studerande (CSV): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uppgifter (CSV): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hit kommer vi aldrig!"}]},{"type":"text","value":"\n studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studerande1.csv\""}]},{"type":"text","value":"\n uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"uppgifter1.csv\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När koden är i skick kan if-satsen tas bort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursresultat, del 2","tmcname":"osa06-05_resultat_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi utvidgar nu föregående uppgift så att de studerandes provpoäng också läses från en CSV-fil. Filens struktur är den följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"studerandenr;u1;u2;u3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;4;1;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;3;5;3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;10;2;2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel studerande "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"12345678"}]},{"type":"text","value":" har fått 4 + 1 + 4, alltså nio poäng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fråga efter filnamnen och skriva ut vitsordet för varje studerande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"studerande1.csv"}]},{"type":"text","value":"\nUppgifter (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"uppgifter1.csv"}]},{"type":"text","value":"\nProvpoäng (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"provpoang1.csv"}]},{"type":"text","value":"\npekka peloton 0\njaana javanainen 1\nliisa virtanen 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Av gjorda uppgifter får man poäng så att 10 % gjorda uppgifter ger ett poäng ända till 100 % (40 uppgifter), som ger tio poäng. Poängen är ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vitsordet för kursen bildas på basis av prov- och uppgiftspoängsumman:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"prov- och uppgiftspoäng tillsammans"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"vitsord"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0-14"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0 (underkänt)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"15-17"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"18-20"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"21-23"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"24-27"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"28-"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursresultat, del 3","tmcname":"osa06-06_resultat_3"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften formaterar vi utskriften från den föregående uppgiften:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"studerande1.csv"}]},{"type":"text","value":"\nUppgifter (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"uppgifter1.csv"}]},{"type":"text","value":"\nProvpoäng (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"provpoang1.csv"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"namn uppg_ant uppg_p prov_p tot_p vitsord\npekka peloton 21 5 9 14 0\njaana javanainen 27 6 11 17 1\nliisa virtanen 35 8 14 22 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På varje rad skrivs alltså ut den studerandes uppgiftsantal, uppgiftspoäng, provpoäng, totalpoäng samt vitsord. Det här görs \"prydligt\" så att namnkolumnen är 30 tecken bred och de övriga kolumnerna 10."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det lönar sig att utnyttja f-strängar (modul fyra)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att utskrift av strängar och siffror fungerar med lite olik logik i f-strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"fortsätter\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":">10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"fortsätter\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"python fortsätter\n pythonfortsätter\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I vanliga fall är strängar vänsterjusterade, men med tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" kan man justera strängen till höger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När siffror skrivs ut är logiken motsatt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"fortsätter\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"<10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"fortsätter\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":" 42fortsätter\n42 fortsätter\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffror är normalt högerjusterade men med tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" kan vi justera siffran till vänster."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spell checker","tmcname":"osa06-07_spellcheck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange text på engelska. Programmet ska utföra en språkkontroll och skriva ut texten så att felstavade ord är markerade med asterisker. Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"We use ptython to make a spell checker"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"We use *ptython* to make a spell checker\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"This is acually a good and usefull program"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"This is *acually* good and *usefull* program\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bokstavsstorleken ska inte påverka programmets funktionalitet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet använder sig av filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"wordlist.txt"}]},{"type":"text","value":" för att känna igen om orden är korrekt skrivna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Receptsök","tmcname":"osa06-08_receptsok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi ett program som låter användaren söka efter ett recept på basis av dess namn, tillagningstid eller ingrediens. Programmets recept finns lagrade i en fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje recept består av tre eller fler rader i receptfilen. Den första raden innehåller receptets namn, den andra tillagningstiden (heltal) och tredje raden framåt ingredienser. Ingredienslistan avsluts med en tom rad (exkl. det sista receptet). Filen kan inntehålla flera recept. Se exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"Plättdeg\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nmjölk\nägg\nmjöl\nsocker\nsalt\nsmör\n\nKöttbullar\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nmalet kött\nägg\nskorpmjöl\n\nTofurullar\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\ntofu\nris\nvatten\nmorot\ngurka\navocado\nwasabi\n\nBulldeg\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"text","value":"\nmjölk\njäst\nägg\nsalt\nsocker\nkardemumma\nsmör"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: I den här uppgiften lönar det sig kanske att läsa in filens rader i en lista och sedan behandla den här listan enligt den här uppgiftens specifikationer."}]},{"type":"element","tagName":"h4","properties":{"id":"sokning-med-receptnamn","style":"position:relative;"},"children":[{"type":"text","value":"Sökning med receptnamn"},{"type":"element","tagName":"a","properties":{"href":"#sokning-med-receptnamn","ariaLabel":"sokning med receptnamn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnsok(fil: str, ord: str)"}]},{"type":"text","value":" som söker efter recept vars namn innehåller den givna strängen. Funktionen ska returnera en lista med namnen på de matchande recepten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namnsok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recept1.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bull\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" recept "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Köttbullar\nBulldeg"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att bokstavsstorleken inte har någon skillnad. Med ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bull"}]},{"type":"text","value":" hittar vi också "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bulldeg"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]},{"type":"element","tagName":"h4","properties":{"id":"sokning-med-tillagningstid","style":"position:relative;"},"children":[{"type":"text","value":"Sökning med tillagningstid"},{"type":"element","tagName":"a","properties":{"href":"#sokning-med-tillagningstid","ariaLabel":"sokning med tillagningstid permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tidssok(fil: str, tid: int)"}]},{"type":"text","value":" som söker efter de recept vars tillagningstid är högst det som angetts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Matchande recept returneras som en lista och nu ska också tillagningstiden inkluderas. Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" tidssok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recept1.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" recept "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Plättdeg, tillagningstid 15 min"}]}]},{"type":"element","tagName":"h4","properties":{"id":"sokning-med-ingrediens","style":"position:relative;"},"children":[{"type":"text","value":"Sökning med ingrediens"},{"type":"element","tagName":"a","properties":{"href":"#sokning-med-ingrediens","ariaLabel":"sokning med ingrediens permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varning!! Den här delen är mycket svårare än de tidigare delarna. Om du har svårigheter, lönar det sig att göra de andra delarna först och sedan återkomma hit. Märk att du också kan skicka enskilda delar till servern."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ingredienssok(fil: str, ingrediens: str)"}]},{"type":"text","value":" som ska hitta recepten med den givna ingrediensen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Matchande recept returneras som en lista. Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ingredienssok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recept1.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mjölk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" recept "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Plättdeg, tillagningstid 15 min\nBulldeg, tillagningstid 60 min"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Stadscyklar","tmcname":"osa06-09_stadscyklar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi några funktioner, med vilka vi kan inspektera en fil med information om "},{"type":"element","tagName":"a","properties":{"href":"https://www.hsl.fi/sv/stadscyklar","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"stadscyklars"}]},{"type":"text","value":" parkeringsstationer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ser filerna ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Longitude;Latitude;FID;name;total_slot;operative;id"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje parkeringsstation finns det en skild rad i filen: här specificeras koordinater, namnet på stationen m.m."}]},{"type":"element","tagName":"h4","properties":{"id":"avstand-mellan-stationer","style":"position:relative;"},"children":[{"type":"text","value":"Avstånd mellan stationer"},{"type":"element","tagName":"a","properties":{"href":"#avstand-mellan-stationer","ariaLabel":"avstand mellan stationer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"stationsinfo(fil: str)"}]},{"type":"text","value":" som läser in stationsinfon och returnerar den som ett lexikon:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"{\n \"Kaivopuisto: (24.950292890004903, 60.155444793742276),\n \"Laivasillankatu: (24.956347471358754, 60.160959093887129),\n \"Kapteeninpuistikko: (24.944927399779715, 60.158189199971673)\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som nyckel kommer alltså stationens namn och som värde en tuple som består av longituden (1) och latituden (2)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa nu funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"avstand(stationer: dict, station1: str, station2: str)"}]},{"type":"text","value":" som returnerar avståndet mellan de två givna stationerna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande formel används för att räkna avståndet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här behövs för att funktionen sqrt ska fungera"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\nx_km "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"longitude1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" longitude2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55.26"}]},{"type":"text","value":"\ny_km "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"latitude1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" latitude2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"111.2"}]},{"type":"text","value":"\navstand "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x_km"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y_km"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"stationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stationsinfo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'stations1.csv'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" avstand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Designmuseo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hietalahdentori\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" avstand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Viiskulma\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kaivopuisto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0.9032737292463177\n0.7753594392019532"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]},{"type":"element","tagName":"h4","properties":{"id":"langsta-avstand","style":"position:relative;"},"children":[{"type":"text","value":"Längsta avstånd"},{"type":"element","tagName":"a","properties":{"href":"#langsta-avstand","ariaLabel":"langsta avstand permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta_avstand(stationer: dict)"}]},{"type":"text","value":" som ska ta reda på vilka stationer som är längst ifrån varandra. Funktionen ska returnera en tuple vars två första värden syftar till stationernas namn och det tredje värdet är avståndet i fråga."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"stationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stationsinfo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'stations1.csv'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" station2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" suurin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta_avstand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" station2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" suurin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Laivasillankatu Hietalahdentori 1.478708873076181"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"61d39363-2bed-504e-8181-41badf9b7e57"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du läsa en fil med Python
  • \n
  • vet du vad en textfil och en CSV-fil är
  • \n
  • kan du behandla innehållet i en CSV-fil i dina program.
  • \n

Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen.

En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida.

Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter.

En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad.

När man programmerar kan det uppstå ett behov att behandla data som finns lagrad i filer. Datorprogram kan läsa data från filer och skriva data till filer. Också stora mängder data i filer kan enkelt behandlas automatiskt.

Under den här kursen kommer vi endast att arbeta med textfiler. De här filerna består av rader med text. Till exempel kodeditorn Visual Studio Code är kompatibel med textfiler. Obs! Även om ordbehandlingsprogram som Microsoft Word ofta används med filer som innehåller text, är Word-dokument inte textfiler. Dokumenten innehåller också annan information om till exempel textformat, vilket gör det mer komplicerat att behandla filerna i ett program.

Att läsa data från en fil

Vi börjar att arbeta med filen exempel.txt som innehållet det följande:

Hej alla!\nVår exempelfil består av tre rader.\nDet här är den sista raden.

Ett enkelt sätt att använda filer i Python är med with-satsen. Den inledande raden öppnar filen och blocket där vi kan komma åt filen följer. Efter blocket stängs filen automatiskt och då kan den inte mera behandlas.

Den här koden öppnar alltså filen, läser dess innehåll och skriver det ut, och till slut stängs filen:

with open(\"exempel.txt\") as fil:\n    innehall = fil.read()\n    print(innehall)

Hej alla!\nVår exempelfil består av tre rader.\nDet här är den sista raden.

Variabeln fil är en file handle (\"filhandtag\"). Via variabeln kan vi komma åt filen så länge den är öppen. Här använde vi metoden read som returnerar filens innehåll som en hel sträng. I det här fallet skulle strängen se ut så här:

\"Hej alla!\\nVår exempelfil består av tre rader.\\nDet här är den sista raden.\"

Gå igenom innehållet i en fil

Metoden read fungerar väl för att skriva ut hela innehållet i en fil, men ofta vill vi gå igenom innehållet rad för rad.

Man kan tänka att textfiler är som listor med strängar, där varje sträng finns på sin egen rad i filen. Vi kan gå igenom listan med en for-loop.

Följande exempel läser in vår exempelfil med hjälp av en for-loop, tar bort radbrytningarna, räknar antalet rader och skriver ut varje rad med sitt radnummer. Programmet håller också koll på radernas längder:

with open(\"exempel.txt\") as fil:\n    raknare = 0\n    totallangd = 0\n\n    for rad in fil:\n        rad = rad.replace(\"\\n\", \"\")\n        raknare += 1\n        print(\"Rad\", raknare, rad)\n        langd = len(rad)\n        totallangd += langd\n\nprint(\"Radernas totallängd:\", totallangd)

Rad 1 Hej alla!\nRad 2 Vår exempelfil består av tre rader.\nRad 3 Det här är den sista raden.\nRadernas totallängd: 63

Det finns en radbrytning \\n i slutet av varje rad i filen, men print-funktionen lägger också automatiskt till en rad i slutet av utskriften. Det finns inga extra radbyten i utskriften ovan eftersom radbrytningarna avlägsnats med hjälp av replace-metoden. Metoden ersätter alla radbrytningstecken med en tom sträng. I och med detta räknas radernas längder också korrekt.

I filen siffror.txt finns siffror listade på olika rader enligt exemplet nedan:

2\n45\n108\n3\n-10\n1100\no.s.v. ...

Skapa funktionen storst som ska läsa filen och returnera den största siffran som hittas.

Observera att filnamnet alltid är siffror.txt och att funktionen inte har några parametrar.

Obs! Om Visual Studio Code inte hitta din fil även om namnet är korrekt skrivet ska du följa instruktionerna nedan.

Om Visual Studio Code inte hittar min fil?

När du kör din kod är det möjligt att Visual Studio Code meddelar att filen – även efter att du kollat att filen finns och att namnet är korrekt skrivet. Att ändra på följande inställning kan lösa problemet:

    \n
  • öppna inställningarna från menyraden: File -> Preferences -> Settings
  • \n
  • sök efter den inställning som ska ändras med sökordet \"executeinfile\"
  • \n
  • välj fliken Workspace
  • \n
  • bocka i valet under Python -> Terminal -> Execute in file dir.
  • \n

Inställningsfönstret borde ungefär se ut så här:

\n \n \n \n \n \n \"6\n \n \n

Om det här inte fungerar kan du kopiera filen i src-mappen…

\n \n \n \n \n \n \"6\n \n \n

…direkt till roten av uppgiftsmappen:

\n \n \n \n \n \n \"6\n \n \n

Att debugga kod som behandlar filer

När man använder Visual Studio Codes debuggare med program som behandlar filer, kan man stöta på följande felmeddelande:

\n \n \n \n \n \n \"6\n \n \n

Orsaken är att debuggaren alltid söker efter filer i roten av uppgiftsmappen. Inställningen Execute in file dir som nämndes ovan har ingen påverkan här. Den enklaste lösningen är att kopiera filen till rotmappen.

Du behöver kanske också starta om Visual Studio Code efter att du har kopierat alla filer som behövs.

Läsa CSV-filer

En CSV-fil (kommaseparerade värden) är en textfil som innehåller data som separerats med ett visst tecken. Det här tecknet är vanligtvis komma (,) eller semikolon (;), men vilket som helst tecken är i princip möjligt.

CSV-filer är ett vanligt sätt att lagra olika typer av data. Flera databaser och kalkylprogram – exempelvis Excel – kan importera och exportera data i CSV-format. Det här möjliggör enkel dataöverföring mellan olika system.

Vi har redan bekantat oss med hur man kan gå igenom rader i en fil med en for-loop, men hur kan vi separera fält på en och samma rad? Python har en strängmetod split, som kan användas för detta. Metoden tar separatortecknet eller -tecknen som ett strängargument och returnerar innehållet i den ursprungliga strängen som en lista av strängar – separerade vid separatortecknen.

Här finns ett exempel för att tydliggöra det här:

text = \"apa,banan,cembalo\"\nordlista = text.split(\",\")\nfor ord in ordlista:\n    print(ord)

apa\nbanan\ncembalo

Låt oss säga att vi har filen vitsord.csv, som innehåller namn på elever samt vitsord de fått av olika kurser. Varje rad har data som tillhör en studerande och data separeras med semikolon.

Peter;5;4;5;3;4;5;5;4;2;4\nPauline;3;4;2;4;4;2;3;1;3;3\nPia;4;5;5;4;5;5;4;5;4;4

Följande program går igenom filen rad för rad, delar upp raderna i delar och skriver ut namnen på eleverna samt deras vitsord:

with open(\"vitsord.csv\") as fil:\n    for rad in fil:\n        rad = rad.replace(\"\\n\", \"\")\n        delar = rad.split(\";\")\n        namn = delar[0]\n        vitsord = delar[1:]\n        print(\"Namn:\", namn)\n        print(\"Vitsord:\", vitsord)

Namn: Peter\nVitsord: ['5', '4', '5', '3', '4', '5', '5', '4', '2', '4']\nNamn: Pauline\nVitsord: ['3', '4', '2', '4', '4', '2', '3', '1', '3', '3']\nNamn: Pia\nVitsord: ['4', '5', '5', '4', '5', '5', '4', '5', '4', '4']

I filen frukter.csv finns frukter med deras pris enligt exemplet nedan:

banan;6.50\näpple;4.95\napelsin;8.0\no.s.v. ...

Skapa funktionen las_frukter som ska läsa filen och skapa ett lexikon där nyckeln är fruktens namn och värdet fruktens pris. Priset ska vara av typen float.

Observera att filnamnet alltid är frukter.csv och funktionen har inga parametrar.

Funktionen ska till slut returnera lexikonet.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

I filen matris.txt finns en matris enligt exemplet nedan:

1,0,2,8,2,1,3,2,5,2,2,2\n9,2,4,5,2,4,2,4,1,10,4,2\no.s.v. ...

Skapa funktionerna summa och maximum som returnerar summan av elementen i matrisen respektive det största elementet.

Skapa också funktionen radsummor som returnerar som en lista summorna av matrisens rader. Till exempel för matrisen...

1,2,3\n2,3,4

...returnerar funktionen [6, 9].

Tips: Du kan också implementera andra funktioner i programmet. Fundera vilka gemensamma funktioner de ovan nämnda funktionerna kan behöva.

Observera att filen alltid heter matris.txt och funktionerna inte har parametrar. Ytterligare funktioner du eventuellt skapar kan ha parametrar.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

Läsa samma fil flera gånger

Ibland kan man behöva läsa innehållet i en fil flera gånger i samma program. Vi tittar på ett program som behandlar data om några personer i en CSV-fil:

\nPeter;40;Helsingfors\nEmilia;34;Esbo\nErik;42;Åbo\nAntonia;100;Helsingfors\nLisa;58;Suonenjoki\n
with open(\"personer.csv\") as fil:\n    # skriver ut namn\n    for rad in fil:\n        delar = rad.split(\";\")\n        print(\"Namn:\", delar[0])\n\n    # söker efter den äldsta personen\n    hogsta_aldern = -1\n    for rad in fil:\n        delar = rad.split(\";\")\n        namn = delar[0]\n        alder = int(delar[1])\n        if alder > hogsta_aldern:\n            hogsta_aldern = alder\n            aldst = namn\n    print(\"Den äldsta är\", aldst)

När vi kör programmet får vi det här felmeddelandet:

Traceback (most recent call last):\n    print(\"Den äldsta är\"; aldst)\nUnboundLocalError: local variable 'aldst' referenced before assignment

Orsaken till at det här sker är att den andra for-loopen aldrig körs. Detta eftersom filen endast kan behandlas en gång. När den sista raden har lästs stannar file handlen i slutet av filen och data i filen kan inte längre kommas åt.

Om vi vill komma åt innehållet i filen i den andra for-loopen, måste vi öppna filen på nytt:

with open(\"personer.csv\") as fil:\n    # skriver ut namn\n    for rad in fil:\n        delar = rad.split(\";\")\n        print(\"Namn:\", delar[0])\n\nwith open(\"personer.csv\") as fil:\n    # söker efter den äldsta personen\n    hogsta_aldern = -1\n    for rad in fil:\n        delar = rad.split(\";\")\n        namn = delar[0]\n        alder = int(delar[1])\n        if alder > hogsta_aldern:\n            hogsta_aldern = alder\n            aldst = namn\n    print(\"Den äldsta är\", aldst)

Även om den ovanstående koden fungerar, innehåller den onödig upprepning. Det lönar sig vanligtvis att läsa filen bara en gång, och spara dess innehåll i ett passligt format för fortsatt behandling:

personer = []\n# vi läser in personerna till listan\nwith open(\"personer.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(\";\")\n        personer.append((delar[0], int(delar[1]), delar[2]))\n\n# skriver ut namn\nfor person in personer:\n    print(\"Namn:\", person[0])\n\n# söker efter den äldsta personen\nhogsta_aldern = -1\nfor person in personer:\n    namn = person[0]\n    alder = person[1]\n    if alder > hogsta_aldern:\n        hogsta_aldern = alder\n        aldst = namn\nprint(\"Den äldsta är\", aldst)

Mera om att behandla CSV-filer

Vi fortsätter behandla filen vitsord.csv, som innehåller det följande:

Peter;5;4;5;3;4;5;5;4;2;4\nPauline;3;4;2;4;4;2;3;1;3;3\nPia;4;5;5;4;5;5;4;5;4;4

Följande program skapar lexikonet vitsord baserat på innehållet i filen. Nycklarna är elevernas namn och värdet som är kopplat till nycklarna innehåller elevens vitsord. Programmet konverterar vitsorden till heltal så att de kan behandlas enklare.

vitsord = {}\nwith open(\"vitsord.csv\") as fil:\n    for rad in fil:\n        rad = rad.replace(\"\\n\", \"\")\n        delar = rad.split(\";\")\n        namn = delar[0]\n        vitsord[namn] = []\n        for givet_vitsord in delar[1:]:\n            vitsord[namn].append(int(givet_vitsord))\n\nprint(vitsord)

{'Peter': [5, 4, 5, 3, 4, 5, 5, 4, 2, 4], 'Pauline': [3, 4, 2, 4, 4, 2, 3, 1, 3, 3], 'Pia': [4, 5, 5, 4, 5, 5, 4, 5, 4, 4]}

Nu kan vi skriva ut statistik om varje studerande, baserat på värdena i lexikonet:

for namn, lista in vitsord.items():\n    basta = max(lista)\n    medeltal = sum(lista) / len(lista)\n    print(f\"{namn}: bästa vitsordet {basta}, medeltal {medeltal:.2f}\")

Peter: bästa vitsordet 5, medeltal 4.10\nPauline: bästa vitsordet 4, medeltal 2.90\nPia: bästa vitsordet 5, medeltal 4.50

Ta en titt på programmet i exemplet ovan. Det kan verka något komplicerat på en första titt, men tekniken kan användas med flera olika typer av data.

Ta bort överflödiga rader, mellanslag och radbrytningar

Låt oss säga att vi har en CSV-fil med namn, exporterat från Excel:

förnamn; efternamn\nPeter; Python\nJaana; Java\nHeikki; Haskell

Excel är ökänt för att lägga till extra mellanrum lite här och där. Här har vi ett extra mellanrum mellan elementen, efter varje semikolon.

Vi skulle vilja skriva ut efternamnet på varje person som finns i listan. Den första raden i filen innehåller information om den data som följer och kan skippas:

efternamn = []\nwith open(\"personer.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(\";\")\n        # skippar raden med rubriker\n        if delar[0] == \"förnamn\":\n            continue\n        efternamn.append(delar[1])\n\nprint(efternamn)

När koden körs får vi den här utskriften:

[' Python\\n', ' Java\\n', ' Haskell']

De två första elementen har ett radbrytningstecken i slutet och alla tre element har ett mellanslag i början. Vi har redan använt replace-metoden för att ta bort onödigt mellanrum, men ett bättre sätt är strip-metoden hos strängar. Den här metoden tar bort mellanrum från början och slutet av en sträng. Metoden tar bort mellanrum, radbrytningar, samt tabb- och andra tecken som normalt inte skulle skrivas ut.

Vi kan testa på metoden i Python-terminalen:

>>> \" prov \".strip()\n'prov'\n>>> \"\\n\\ntest\\n\".strip()\n'test'\n>>>

Att ta bort de onödiga tecknen kräver bara en liten ändring i programmet:

efternamn = []\nwith open(\"personer.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(';')\n        if delar[0] == \"förnamn\":\n            continue # skippar raden med rubriker\n        efternamn.append(delar[1].strip())\nprint(efternamn)

Nu har får vi den önskade utskriften:

['Python', 'Java', 'Haskell']

Strängmetoderna lstrip och rstrip fungerar lika som metoden strip, men gör det då bara för antingen vänstra (l) eller högra (r) kanten av strängen:

>>> \" teststräng  \".rstrip()\n' teststräng'\n>>> \" teststräng  \".lstrip()\n'teststräng  '

Kombinera data från olika filer

Det är mycket vanligt att data som behandlas av ett program finns utspritt i flera filer. Vi tar en titt på ett exempel där personalens information i ett företag finns i filen personal.csv:

personnr;namn;adress;adressort\n080488-123X;Peter Mikkola;Filpusvägen 7;00700 HELSINGFORS\n290274-044S;Lisa Marttinen;Mannerheimvägen 100 A 10;00100 HELSINGFORS\n010479-007Z;Arto Vihavainen;Tiilitehtaankatu 10;04260 KERAVA\n010499-345K;Leevi Hellas;Tapiolavägen 9;02100 ESBO

Löneuppgifterna finns i en skild fil, lon.csv:

personnr;lön;bonus\n080488-123X;3300;0\n290274-044S;4150;200\n010479-007Z;1300;1200

Alla rader i båda filerna innehåller en personlig id-kod (pic) som identifierar vems data vi arbetar med. När vi använder det här id:t som gemensam faktor, är det lätt att koppla en arbetstagares namn med hennes lön. Vi kan till exempel skriva ut en lista över de månatliga inkomsterna:

\ninkomster:\nPeter Mikkola    3300 euro\nLisa Marttinen   4350 euro\nArto Vihavainen  2500 euro\n

Programmet använder två lexikon som hjälpdatastrukturer: namn och loner. Båda använder pic som nyckel:

namn = {}\n\nwith open(\"personal.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(';')\n        if delar[0] == \"personnr\":\n            continue\n        namn[delar[0]] = delar[1]\n\nloner = {}\n\nwith open(\"lon.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(';')\n        if delar[0] == \"personnr\":\n            continue\n        loner[delar[0]] = int(delar[1]) +int(delar[2])\n\nprint(\"inkomster:\")\n\nfor personnr, person in namn.items():\n    if personnr in loner:\n        lon = loner[personnr]\n        print(f\"{person:16} {lon} euro\")\n    else:\n        print(f\"{person:16} 0 euro\")

Först skapar programmet lexikonen namn och loner. De har dessa innehåll:

{\n    '080488-123X': 'Peter Mikkola',\n    '290274-044S': 'Lisa Marttinen',\n    '010479-007Z': 'Arto Vihavainen',\n    '010499-345K': 'Leevi Hellas'\n}\n\n{\n    '080488-123X': 3300,\n    '290274-044S': 4350,\n    '010479-007Z': 2500\n}

For-loopen i slutet av programmet kombinerar namnen på arbetstagarna med deras löner.

Programmet kan också ta i beaktande situationer där pic saknas för en arbetstagare.

Kom ihåg att ordningen som elementen är lagrade i lexikon inte har någon skillnad, eftersom nycklarna behandlas med hjälp av hashvärden.

Programmet behandlar två CSV-filen. I den ena finns information om studerande:

studerandenr;förnamn;efternamn\n12345678;pekka;peloton\n12345687;jaana;javanainen\n12345699;liisa;virtanen

Och i den andra antalet gjorda uppgifter på veckonivå:

studerandenr;v1;v2;v3;v4;v5;v6;v7\n12345678;4;1;1;4;5;2;4\n12345687;3;5;3;1;5;4;6\n12345699;10;2;2;7;10;2;2

I de båda CSV-filerna innehåller den första rade rubriker.

Skapa ett program som frågar efter filnamnen och skriver därefter ut antalet gjorda uppgifter för varje studerande. Exempel:

Studerande (CSV): studerande1.csv\nUppgifter (CSV): uppgifter1.csv\npekka peloton 21\njaana javanainen 27\nliisa virtanen 35

Tips: Hårdkoda värdena medan du testar programmet, så behöver du inte hela tiden mata in dem på nytt:

if False:\n    # hit kommer vi aldrig\n    studerande = input(\"Studerande (CSV): \")\n    uppgifter = input(\"Uppgifter (CSV): \")\nelse:\n    # de hårdkodade värdena\n    studerande = \"studerande1.csv\"\n    uppgifter = \"uppgifter1.csv\"

Den egentliga funktionaliteten är nu \"gömd\" bakom en False-förgrening som aldrig körs.

Om vi vill testa inmatning av information (på \"normalt\" sätt), kan vi ändra False till True:

if True:\n    studerande = input(\"Studerande (CSV): \")\n    uppgifter = input(\"Uppgifter (CSV): \")\nelse:\n    # hit kommer vi aldrig!\n    studerande = \"studerande1.csv\"\n    uppgifter = \"uppgifter1.csv\"

När koden är i skick kan if-satsen tas bort.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

Vi utvidgar nu föregående uppgift så att de studerandes provpoäng också läses från en CSV-fil. Filens struktur är den följande:

studerandenr;u1;u2;u3\n12345678;4;1;4\n12345687;3;5;3\n12345699;10;2;2

Till exempel studerande 12345678 har fått 4 + 1 + 4, alltså nio poäng.

Programmet ska fråga efter filnamnen och skriva ut vitsordet för varje studerande:

Studerande (CSV): studerande1.csv\nUppgifter (CSV): uppgifter1.csv\nProvpoäng (CSV): provpoang1.csv\npekka peloton 0\njaana javanainen 1\nliisa virtanen 3

Av gjorda uppgifter får man poäng så att 10 % gjorda uppgifter ger ett poäng ända till 100 % (40 uppgifter), som ger tio poäng. Poängen är ett heltal.

Vitsordet för kursen bildas på basis av prov- och uppgiftspoängsumman:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
prov- och uppgiftspoäng tillsammansvitsord
0-140 (underkänt)
15-171
18-202
21-233
24-274
28-5

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

I den här uppgiften formaterar vi utskriften från den föregående uppgiften:

Studerande (CSV): studerande1.csv\nUppgifter (CSV): uppgifter1.csv\nProvpoäng (CSV): provpoang1.csv

\nnamn                          uppg_ant  uppg_p    prov_p    tot_p     vitsord\npekka peloton                 21        5         9         14        0\njaana javanainen              27        6         11        17        1\nliisa virtanen                35        8         14        22        3\n

På varje rad skrivs alltså ut den studerandes uppgiftsantal, uppgiftspoäng, provpoäng, totalpoäng samt vitsord. Det här görs \"prydligt\" så att namnkolumnen är 30 tecken bred och de övriga kolumnerna 10.

Det lönar sig att utnyttja f-strängar (modul fyra).

Märk att utskrift av strängar och siffror fungerar med lite olik logik i f-strängar:

ord = \"python\"\nprint(f\"{ord:10}fortsätter\")\nprint(f\"{ord:>10}fortsätter\")
\npython    fortsätter\n    pythonfortsätter\n

I vanliga fall är strängar vänsterjusterade, men med tecknet > kan man justera strängen till höger.

När siffror skrivs ut är logiken motsatt:

siffra = 42\nprint(f\"{siffra:10}fortsätter\")\nprint(f\"{siffra:<10}fortsätter\")
\n        42fortsätter\n42        fortsätter\n

Siffror är normalt högerjusterade men med tecknet < kan vi justera siffran till vänster.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Skapa ett program som ber användaren ange text på engelska. Programmet ska utföra en språkkontroll och skriva ut texten så att felstavade ord är markerade med asterisker. Exempel:

Write text: We use ptython to make a spell checker

\nWe use *ptython* to make a spell checker\n

Write text: This is acually a good and usefull program

\nThis is *acually* good and *usefull* program\n

Bokstavsstorleken ska inte påverka programmets funktionalitet.

Programmet använder sig av filen wordlist.txt för att känna igen om orden är korrekt skrivna.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

I den här uppgiften skapar vi ett program som låter användaren söka efter ett recept på basis av dess namn, tillagningstid eller ingrediens. Programmets recept finns lagrade i en fil.

Varje recept består av tre eller fler rader i receptfilen. Den första raden innehåller receptets namn, den andra tillagningstiden (heltal) och tredje raden framåt ingredienser. Ingredienslistan avsluts med en tom rad (exkl. det sista receptet). Filen kan inntehålla flera recept. Se exemplet nedan:

Plättdeg\n15\nmjölk\nägg\nmjöl\nsocker\nsalt\nsmör\n\nKöttbullar\n45\nmalet kött\nägg\nskorpmjöl\n\nTofurullar\n30\ntofu\nris\nvatten\nmorot\ngurka\navocado\nwasabi\n\nBulldeg\n60\nmjölk\njäst\nägg\nsalt\nsocker\nkardemumma\nsmör

Tips: I den här uppgiften lönar det sig kanske att läsa in filens rader i en lista och sedan behandla den här listan enligt den här uppgiftens specifikationer.

Sökning med receptnamn

Skapa funktionen namnsok(fil: str, ord: str) som söker efter recept vars namn innehåller den givna strängen. Funktionen ska returnera en lista med namnen på de matchande recepten.

Exempel:

hittade = namnsok(\"recept1.txt\", \"bull\")\n\nfor recept in hittade:\n    print(recept)

Köttbullar\nBulldeg

Märk att bokstavsstorleken inte har någon skillnad. Med ordet bull hittar vi också Bulldeg.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

Sökning med tillagningstid

Skapa funktionen tidssok(fil: str, tid: int) som söker efter de recept vars tillagningstid är högst det som angetts som argument.

Matchande recept returneras som en lista och nu ska också tillagningstiden inkluderas. Exempel:

hittade = tidssok(\"recept1.txt\", 20)\n\nfor recept in hittade:\n    print(recept)

Plättdeg, tillagningstid 15 min

Sökning med ingrediens

Varning!! Den här delen är mycket svårare än de tidigare delarna. Om du har svårigheter, lönar det sig att göra de andra delarna först och sedan återkomma hit. Märk att du också kan skicka enskilda delar till servern.

Skapa funktionen ingredienssok(fil: str, ingrediens: str) som ska hitta recepten med den givna ingrediensen.

Matchande recept returneras som en lista. Exempel:

hittade = ingredienssok(\"recept1.txt\", \"mjölk\")\n\nfor recept in hittade:\n    print(recept)

Plättdeg, tillagningstid 15 min\nBulldeg, tillagningstid 60 min

I den här uppgiften skapar vi några funktioner, med vilka vi kan inspektera en fil med information om stadscyklars parkeringsstationer.

Så här ser filerna ut:

Longitude;Latitude;FID;name;total_slot;operative;id\n24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001\n24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002\n24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003

För varje parkeringsstation finns det en skild rad i filen: här specificeras koordinater, namnet på stationen m.m.

Avstånd mellan stationer

Skapa funktionen stationsinfo(fil: str) som läser in stationsinfon och returnerar den som ett lexikon:

\n{\n  \"Kaivopuisto: (24.950292890004903, 60.155444793742276),\n  \"Laivasillankatu: (24.956347471358754, 60.160959093887129),\n  \"Kapteeninpuistikko: (24.944927399779715, 60.158189199971673)\n}\n

Som nyckel kommer alltså stationens namn och som värde en tuple som består av longituden (1) och latituden (2).

Skapa nu funktionen avstand(stationer: dict, station1: str, station2: str) som returnerar avståndet mellan de två givna stationerna.

Följande formel används för att räkna avståndet:

# det här behövs för att funktionen sqrt ska fungera\nimport math\n\nx_km = (longitude1 - longitude2) * 55.26\ny_km = (latitude1 - latitude2) * 111.2\navstand = math.sqrt(x_km**2 + y_km**2)

Exempel:

stationer = stationsinfo('stations1.csv')\ne = avstand(stationer, \"Designmuseo\", \"Hietalahdentori\")\nprint(e)\ne = avstand(stationer, \"Viiskulma\", \"Kaivopuisto\")\nprint(e)

0.9032737292463177\n0.7753594392019532

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

Längsta avstånd

Skapa funktionen langsta_avstand(stationer: dict) som ska ta reda på vilka stationer som är längst ifrån varandra. Funktionen ska returnera en tuple vars två första värden syftar till stationernas namn och det tredje värdet är avståndet i fråga.

stationer = stationsinfo('stations1.csv')\nstation1, station2, suurin = langsta_avstand(stationer)\nprint(station1, station2, suurin)

Laivasillankatu Hietalahdentori 1.478708873076181

","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/1-lasa-filer.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du läsa en fil med Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad en textfil och en CSV-fil är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du behandla innehållet i en CSV-fil i dina program."}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"vastaava teksti löytyy osioista 3-1, 5-1 ja 6-1, tsekkaa kaikki jos muokkaat tätä"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Om uppgifterna i den här kursen"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man programmerar kan det uppstå ett behov att behandla data som finns lagrad i filer. Datorprogram kan läsa data från filer och skriva data till filer. Också stora mängder data i filer kan enkelt behandlas automatiskt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Under den här kursen kommer vi endast att arbeta med textfiler. De här filerna består av rader med text. Till exempel kodeditorn Visual Studio Code är kompatibel med textfiler. Obs! Även om ordbehandlingsprogram som Microsoft Word ofta används med filer som innehåller text, är Word-dokument inte textfiler. Dokumenten innehåller också annan information om till exempel textformat, vilket gör det mer komplicerat att behandla filerna i ett program."}]},{"type":"element","tagName":"h2","properties":{"id":"att-lasa-data-fran-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Att läsa data från en fil"},{"type":"element","tagName":"a","properties":{"href":"#att-lasa-data-fran-en-fil","ariaLabel":"att lasa data fran en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi börjar att arbeta med filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exempel.txt"}]},{"type":"text","value":" som innehållet det följande:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!\nVår exempelfil består av tre rader.\nDet här är den sista raden."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett enkelt sätt att använda filer i Python är med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":"-satsen. Den inledande raden öppnar filen och blocket där vi kan komma åt filen följer. Efter blocket stängs filen automatiskt och då kan den inte mera behandlas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här koden öppnar alltså filen, läser dess innehåll och skriver det ut, och till slut stängs filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n innehall "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"innehall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!\nVår exempelfil består av tre rader.\nDet här är den sista raden."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fil"}]},{"type":"text","value":" är en file handle (\"filhandtag\"). Via variabeln kan vi komma åt filen så länge den är öppen. Här använde vi metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":" som returnerar filens innehåll som en hel sträng. I det här fallet skulle strängen se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Hej alla!\\nVår exempelfil består av tre rader.\\nDet här är den sista raden.\""}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"ga-igenom-innehallet-i-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Gå igenom innehållet i en fil"},{"type":"element","tagName":"a","properties":{"href":"#ga-igenom-innehallet-i-en-fil","ariaLabel":"ga igenom innehallet i en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":" fungerar väl för att skriva ut hela innehållet i en fil, men ofta vill vi gå igenom innehållet rad för rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man kan tänka att textfiler är som listor med strängar, där varje sträng finns på sin egen rad i filen. Vi kan gå igenom listan med en for-loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel läser in vår exempelfil med hjälp av en for-loop, tar bort radbrytningarna, räknar antalet rader och skriver ut varje rad med sitt radnummer. Programmet håller också koll på radernas längder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n totallangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rad\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n totallangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" langd\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Radernas totallängd:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" totallangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rad 1 Hej alla!\nRad 2 Vår exempelfil består av tre rader.\nRad 3 Det här är den sista raden.\nRadernas totallängd: 63"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns en radbrytning "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" i slutet av varje rad i filen, men "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-funktionen lägger också automatiskt till en rad i slutet av utskriften. Det finns inga extra radbyten i utskriften ovan eftersom radbrytningarna avlägsnats med hjälp av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":"-metoden. Metoden ersätter alla radbrytningstecken med en tom sträng. I och med detta räknas radernas längder också korrekt."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Största siffran","tmcname":"osa06-01_storsta_siffran"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror.txt"}]},{"type":"text","value":" finns siffror listade på olika rader enligt exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"108"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","parameter","variable"]},"children":[{"type":"text","value":"-10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1100"}]},{"type":"text","value":"\no.s.v. "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storst"}]},{"type":"text","value":" som ska läsa filen och returnera den största siffran som hittas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att filnamnet alltid är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror.txt"}]},{"type":"text","value":" och att funktionen inte har några parametrar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hitta din fil även om namnet är korrekt skrivet ska du följa instruktionerna nedan."}]}]},{"type":"element","tagName":"h2","properties":{"id":"om-visual-studio-code-inte-hittar-min-fil","style":"position:relative;"},"children":[{"type":"text","value":"Om Visual Studio Code inte hittar min fil?"},{"type":"element","tagName":"a","properties":{"href":"#om-visual-studio-code-inte-hittar-min-fil","ariaLabel":"om visual studio code inte hittar min fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du kör din kod är det möjligt att Visual Studio Code meddelar att filen – även efter att du kollat att filen finns och att namnet är korrekt skrivet. Att ändra på följande inställning kan lösa problemet:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"öppna inställningarna från menyraden: File -> Preferences -> Settings"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"sök efter den inställning som ska ändras med sökordet \"executeinfile\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"välj fliken Workspace"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"bocka i valet under Python -> Terminal -> Execute in file dir."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inställningsfönstret borde ungefär se ut så här:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6e5d89f76dfd47af6f9ea67b097452e8/e405b/6_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 16.521739130434785%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAIAAAAcOLh5AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAZElEQVQI11XOQQqAMAwEwP5DknRNkNIilaInvXn3/8+RKIoOOe4uCQAQHYDWWq01/xS/UnLO07rP+9HbEEU8DQT+SCmZmZqlB77GRdvGUCa687+yiDAzVH3i8hb9MyZQJ0xv/gSEjBk5LU20dgAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e5d89f76dfd47af6f9ea67b097452e8/a0b58/6_1_1.webp 230w","/static/6e5d89f76dfd47af6f9ea67b097452e8/bc10c/6_1_1.webp 460w","/static/6e5d89f76dfd47af6f9ea67b097452e8/966d8/6_1_1.webp 920w","/static/6e5d89f76dfd47af6f9ea67b097452e8/445df/6_1_1.webp 1380w","/static/6e5d89f76dfd47af6f9ea67b097452e8/9b2c8/6_1_1.webp 1566w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e5d89f76dfd47af6f9ea67b097452e8/81c8e/6_1_1.png 230w","/static/6e5d89f76dfd47af6f9ea67b097452e8/08a84/6_1_1.png 460w","/static/6e5d89f76dfd47af6f9ea67b097452e8/c0255/6_1_1.png 920w","/static/6e5d89f76dfd47af6f9ea67b097452e8/b1001/6_1_1.png 1380w","/static/6e5d89f76dfd47af6f9ea67b097452e8/e405b/6_1_1.png 1566w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6e5d89f76dfd47af6f9ea67b097452e8/c0255/6_1_1.png","alt":"6 1 1","title":"6 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det här inte fungerar kan du kopiera filen i src-mappen…"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a57483e6084d39a596ee24c41a42de70/6297e/6_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 22.17391304347826%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAApklEQVQI123N0QqCQBAFUH/CUVlnt3JnVtcSyx1XikKCHur/PyhQih463KcLl5s87rcQRcLoa98037TO1czsFtba6p/kFESm8+U6HyTuT4FcY11Dtd9WVH4gol7keQ4Aa4OIySBjfxwkyKuLc90P5ImYiHeWtdmYRVmWSqmiKLIsAwCllDFGa50EiW3Xj3F6tjKQr5YlE28tozbrISKmaQoAiPg7fgOYxyLFEKvBSAAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a57483e6084d39a596ee24c41a42de70/a0b58/6_1_2.webp 230w","/static/a57483e6084d39a596ee24c41a42de70/bc10c/6_1_2.webp 460w","/static/a57483e6084d39a596ee24c41a42de70/966d8/6_1_2.webp 920w","/static/a57483e6084d39a596ee24c41a42de70/445df/6_1_2.webp 1380w","/static/a57483e6084d39a596ee24c41a42de70/78de1/6_1_2.webp 1840w","/static/a57483e6084d39a596ee24c41a42de70/4b7c9/6_1_2.webp 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a57483e6084d39a596ee24c41a42de70/81c8e/6_1_2.png 230w","/static/a57483e6084d39a596ee24c41a42de70/08a84/6_1_2.png 460w","/static/a57483e6084d39a596ee24c41a42de70/c0255/6_1_2.png 920w","/static/a57483e6084d39a596ee24c41a42de70/b1001/6_1_2.png 1380w","/static/a57483e6084d39a596ee24c41a42de70/161ec/6_1_2.png 1840w","/static/a57483e6084d39a596ee24c41a42de70/6297e/6_1_2.png 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a57483e6084d39a596ee24c41a42de70/c0255/6_1_2.png","alt":"6 1 2","title":"6 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…direkt till roten av uppgiftsmappen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/bc83b27162a67ce98f825821dc304a7e/6297e/6_1_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 26.956521739130434%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAt0lEQVQY023P3Q6CMAwFYN6DjqjtNta1MxlDdILGxBvf/4UMGO/4cm5Pf5pa79N0m+eHSBJJMSqzMKv3vaqklPq+995ba51zdkNkPR2COzallJzL8/lalketc86FWWJUIsfMqtq2bdd1sDF/AOaE2AzDUIYyjmPOWUQRiWidjYgxRlU1xnQ7DBGtmyWdy2ViZqL1ul8QSSQyMwDsVI1BxOZ6md65fsYlyTms3/6iznnd7JYBIITwBcyRLmVWTxqEAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bc83b27162a67ce98f825821dc304a7e/a0b58/6_1_3.webp 230w","/static/bc83b27162a67ce98f825821dc304a7e/bc10c/6_1_3.webp 460w","/static/bc83b27162a67ce98f825821dc304a7e/966d8/6_1_3.webp 920w","/static/bc83b27162a67ce98f825821dc304a7e/445df/6_1_3.webp 1380w","/static/bc83b27162a67ce98f825821dc304a7e/78de1/6_1_3.webp 1840w","/static/bc83b27162a67ce98f825821dc304a7e/4b7c9/6_1_3.webp 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bc83b27162a67ce98f825821dc304a7e/81c8e/6_1_3.png 230w","/static/bc83b27162a67ce98f825821dc304a7e/08a84/6_1_3.png 460w","/static/bc83b27162a67ce98f825821dc304a7e/c0255/6_1_3.png 920w","/static/bc83b27162a67ce98f825821dc304a7e/b1001/6_1_3.png 1380w","/static/bc83b27162a67ce98f825821dc304a7e/161ec/6_1_3.png 1840w","/static/bc83b27162a67ce98f825821dc304a7e/6297e/6_1_3.png 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/bc83b27162a67ce98f825821dc304a7e/c0255/6_1_3.png","alt":"6 1 3","title":"6 1 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"att-debugga-kod-som-behandlar-filer","style":"position:relative;"},"children":[{"type":"text","value":"Att debugga kod som behandlar filer"},{"type":"element","tagName":"a","properties":{"href":"#att-debugga-kod-som-behandlar-filer","ariaLabel":"att debugga kod som behandlar filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När man använder Visual Studio Codes debuggare med program som behandlar filer, kan man stöta på följande felmeddelande:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b0c7745f7a4cc9c485a52d483e0aa290/eff3b/6_1_4.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 31.304347826086953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAABMklEQVQY003PyU7CQACA4d7EBWQpLdDS6XSWzkw702VIIBESgyaYWBZRE60QUIzv/wguB0PyX7/Db2itx5PJcDiKYym4ICFXaSKkZFKGMhUyESKmlAEY+BD5EAGIMA5iThhjRpZlOhsooTjlmFAKgaBgPMSjHOS8kwtHx55itgqt4xIllEqNPM85F4xxSmhAyAziDabbiG8F28voBcGnvrNot+ZmY2E252azMBtFvSZ7XQADQ19dI4QJoX+Y3vXhgcbvcbYL5Z6pHYlKQEpAS0DWrd6yZi3r1uK0Hpm26wND6xxj8o9nrv8Zxl/p4CCSQ5R8cFV6aAPwm4eeLWfd7Dy2OqvzRtS2XQB+n4/x1IOvNHrw8dIBSwcUtlPYzsqF9+3e7dnltFKdVqo3JxfC7v7gb6ThPL16POKXAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b0c7745f7a4cc9c485a52d483e0aa290/a0b58/6_1_4.webp 230w","/static/b0c7745f7a4cc9c485a52d483e0aa290/bc10c/6_1_4.webp 460w","/static/b0c7745f7a4cc9c485a52d483e0aa290/966d8/6_1_4.webp 920w","/static/b0c7745f7a4cc9c485a52d483e0aa290/445df/6_1_4.webp 1380w","/static/b0c7745f7a4cc9c485a52d483e0aa290/78de1/6_1_4.webp 1840w","/static/b0c7745f7a4cc9c485a52d483e0aa290/5340a/6_1_4.webp 2072w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b0c7745f7a4cc9c485a52d483e0aa290/81c8e/6_1_4.png 230w","/static/b0c7745f7a4cc9c485a52d483e0aa290/08a84/6_1_4.png 460w","/static/b0c7745f7a4cc9c485a52d483e0aa290/c0255/6_1_4.png 920w","/static/b0c7745f7a4cc9c485a52d483e0aa290/b1001/6_1_4.png 1380w","/static/b0c7745f7a4cc9c485a52d483e0aa290/161ec/6_1_4.png 1840w","/static/b0c7745f7a4cc9c485a52d483e0aa290/eff3b/6_1_4.png 2072w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b0c7745f7a4cc9c485a52d483e0aa290/c0255/6_1_4.png","alt":"6 1 4","title":"6 1 4","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orsaken är att debuggaren alltid söker efter filer i roten av uppgiftsmappen. Inställningen Execute in file dir som nämndes ovan har ingen påverkan här. Den enklaste lösningen är att kopiera filen till rotmappen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du behöver kanske också starta om Visual Studio Code efter att du har kopierat alla filer som behövs."}]},{"type":"element","tagName":"h2","properties":{"id":"lasa-csv-filer","style":"position:relative;"},"children":[{"type":"text","value":"Läsa CSV-filer"},{"type":"element","tagName":"a","properties":{"href":"#lasa-csv-filer","ariaLabel":"lasa csv filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En CSV-fil (kommaseparerade värden) är en textfil som innehåller data som separerats med ett visst tecken. Det här tecknet är vanligtvis komma ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":","}]},{"type":"text","value":") eller semikolon ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":"), men vilket som helst tecken är i princip möjligt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV-filer är ett vanligt sätt att lagra olika typer av data. Flera databaser och kalkylprogram – exempelvis Excel – kan importera och exportera data i CSV-format. Det här möjliggör enkel dataöverföring mellan olika system."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan bekantat oss med hur man kan gå igenom rader i en fil med en for-loop, men hur kan vi separera fält på en och samma rad? Python har en strängmetod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":", som kan användas för detta. Metoden tar separatortecknet eller -tecknen som ett strängargument och returnerar innehållet i den ursprungliga strängen som en lista av strängar – separerade vid separatortecknen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här finns ett exempel för att tydliggöra det här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa,banan,cembalo\""}]},{"type":"text","value":"\nordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" ordlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"apa\nbanan\ncembalo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss säga att vi har filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vitsord.csv"}]},{"type":"text","value":", som innehåller namn på elever samt vitsord de fått av olika kurser. Varje rad har data som tillhör en studerande och data separeras med semikolon."}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter;5;4;5;3;4;5;5;4;2;4\nPauline;3;4;2;4;4;2;3;1;3;3\nPia;4;5;5;4;5;5;4;5;4;4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program går igenom filen rad för rad, delar upp raderna i delar och skriver ut namnen på eleverna samt deras vitsord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vitsord.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vitsord:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Namn: Peter\nVitsord: ['5', '4', '5', '3', '4', '5', '5', '4', '2', '4']\nNamn: Pauline\nVitsord: ['3', '4', '2', '4', '4', '2', '3', '1', '3', '3']\nNamn: Pia\nVitsord: ['4', '5', '5', '4', '5', '5', '4', '5', '4', '4']"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fruktaffär","tmcname":"osa06-02_fruktaffar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"frukter.csv"}]},{"type":"text","value":" finns frukter med deras pris enligt exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"banan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6.50"}]},{"type":"text","value":"\näpple"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.95"}]},{"type":"text","value":"\napelsin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8.0"}]},{"type":"text","value":"\no.s.v. "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"las_frukter"}]},{"type":"text","value":" som ska läsa filen och skapa ett lexikon där nyckeln är fruktens namn och värdet fruktens pris. Priset ska vara av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att filnamnet alltid är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"frukter.csv"}]},{"type":"text","value":" och funktionen har inga parametrar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska till slut returnera lexikonet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Matris","tmcname":"osa06-03_matris"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matris.txt"}]},{"type":"text","value":" finns en matris enligt exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1,0"}]},{"type":"text","value":",2,8,2,1,3,2,5,2,2,2\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9,2"}]},{"type":"text","value":",4,5,2,4,2,4,1,10,4,2\no.s.v. "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"maximum"}]},{"type":"text","value":" som returnerar summan av elementen i matrisen respektive det största elementet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa också funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"radsummor"}]},{"type":"text","value":" som returnerar som en lista summorna av matrisens rader. Till exempel för matrisen..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1,2"}]},{"type":"text","value":",3\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2,3"}]},{"type":"text","value":",4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...returnerar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[6, 9]"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Du kan också implementera andra funktioner i programmet. Fundera vilka gemensamma funktioner de ovan nämnda funktionerna kan behöva."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att filen alltid heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matris.txt"}]},{"type":"text","value":" och funktionerna inte har parametrar. Ytterligare funktioner du eventuellt skapar kan ha parametrar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lasa-samma-fil-flera-ganger","style":"position:relative;"},"children":[{"type":"text","value":"Läsa samma fil flera gånger"},{"type":"element","tagName":"a","properties":{"href":"#lasa-samma-fil-flera-ganger","ariaLabel":"lasa samma fil flera ganger permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland kan man behöva läsa innehållet i en fil flera gånger i samma program. Vi tittar på ett program som behandlar data om några personer i en CSV-fil:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"text","value":"\nPeter;40;Helsingfors\nEmilia;34;Esbo\nErik;42;Åbo\nAntonia;100;Helsingfors\nLisa;58;Suonenjoki\n"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriver ut namn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# söker efter den äldsta personen"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" hogsta_aldern"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n aldst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den äldsta är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi kör programmet får vi det här felmeddelandet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"Traceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call last"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den äldsta är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nUnboundLocalError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" local variable "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'aldst'"}]},{"type":"text","value":" referenced before assignment"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orsaken till at det här sker är att den andra for-loopen aldrig körs. Detta eftersom filen endast kan behandlas en gång. När den sista raden har lästs stannar file handlen i slutet av filen och data i filen kan inte längre kommas åt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill komma åt innehållet i filen i den andra for-loopen, måste vi öppna filen på nytt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriver ut namn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# söker efter den äldsta personen"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" hogsta_aldern"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n aldst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den äldsta är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även om den ovanstående koden fungerar, innehåller den onödig upprepning. Det lönar sig vanligtvis att läsa filen bara en gång, och spara dess innehåll i ett passligt format för fortsatt behandling:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"personer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi läser in personerna till listan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skriver ut namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# söker efter den äldsta personen"}]},{"type":"text","value":"\nhogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" hogsta_aldern"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hogsta_aldern "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alder\n aldst "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den äldsta är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aldst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"mera-om-att-behandla-csv-filer","style":"position:relative;"},"children":[{"type":"text","value":"Mera om att behandla CSV-filer"},{"type":"element","tagName":"a","properties":{"href":"#mera-om-att-behandla-csv-filer","ariaLabel":"mera om att behandla csv filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi fortsätter behandla filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vitsord.csv"}]},{"type":"text","value":", som innehåller det följande:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter;5;4;5;3;4;5;5;4;2;4\nPauline;3;4;2;4;4;2;3;1;3;3\nPia;4;5;5;4;5;5;4;5;4;4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program skapar lexikonet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vitsord"}]},{"type":"text","value":" baserat på innehållet i filen. Nycklarna är elevernas namn och värdet som är kopplat till nycklarna innehåller elevens vitsord. Programmet konverterar vitsorden till heltal så att de kan behandlas enklare."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"vitsord.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"replace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" givet_vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"givet_vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Peter': [5, 4, 5, 3, 4, 5, 5, 4, 2, 4], 'Pauline': [3, 4, 2, 4, 4, 2, 3, 1, 3, 3], 'Pia': [4, 5, 5, 4, 5, 5, 4, 5, 4, 4]}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu kan vi skriva ut statistik om varje studerande, baserat på värdena i lexikonet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n basta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n medeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": bästa vitsordet "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"basta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", medeltal "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":".2f"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter: bästa vitsordet 5, medeltal 4.10\nPauline: bästa vitsordet 4, medeltal 2.90\nPia: bästa vitsordet 5, medeltal 4.50"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ta en titt på programmet i exemplet ovan. Det kan verka något komplicerat på en första titt, men tekniken kan användas med flera olika typer av data."}]},{"type":"element","tagName":"h2","properties":{"id":"ta-bort-overflodiga-rader-mellanslag-och-radbrytningar","style":"position:relative;"},"children":[{"type":"text","value":"Ta bort överflödiga rader, mellanslag och radbrytningar"},{"type":"element","tagName":"a","properties":{"href":"#ta-bort-overflodiga-rader-mellanslag-och-radbrytningar","ariaLabel":"ta bort overflodiga rader mellanslag och radbrytningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss säga att vi har en CSV-fil med namn, exporterat från Excel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"förnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" efternamn\nPeter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Python\nJaana"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Java\nHeikki"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Haskell"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Excel är ökänt för att lägga till extra mellanrum lite här och där. Här har vi ett extra mellanrum mellan elementen, efter varje semikolon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skulle vilja skriva ut efternamnet på varje person som finns i listan. Den första raden i filen innehåller information om den data som följer och kan skippas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"efternamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skippar raden med rubriker"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"förnamn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"\n efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När koden körs får vi den här utskriften:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[' Python\\n', ' Java\\n', ' Haskell']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De två första elementen har ett radbrytningstecken i slutet och alla tre element har ett mellanslag i början. Vi har redan använt "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":"-metoden för att ta bort onödigt mellanrum, men ett bättre sätt är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strip"}]},{"type":"text","value":"-metoden hos strängar. Den här metoden tar bort mellanrum från början och slutet av en sträng. Metoden tar bort mellanrum, radbrytningar, samt tabb- och andra tecken som normalt inte skulle skrivas ut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan testa på metoden i Python-terminalen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" prov \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'prov'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\\ntest\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'test'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att ta bort de onödiga tecknen kräver bara en liten ändring i programmet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"efternamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personer.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"förnamn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skippar raden med rubriker"}]},{"type":"text","value":"\n efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu har får vi den önskade utskriften:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['Python', 'Java', 'Haskell']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängmetoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lstrip"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rstrip"}]},{"type":"text","value":" fungerar lika som metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strip"}]},{"type":"text","value":", men gör det då bara för antingen vänstra (l) eller högra (r) kanten av strängen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" teststräng \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rstrip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"' teststräng'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" teststräng \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lstrip"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'teststräng '"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"kombinera-data-fran-olika-filer","style":"position:relative;"},"children":[{"type":"text","value":"Kombinera data från olika filer"},{"type":"element","tagName":"a","properties":{"href":"#kombinera-data-fran-olika-filer","ariaLabel":"kombinera data fran olika filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är mycket vanligt att data som behandlas av ett program finns utspritt i flera filer. Vi tar en titt på ett exempel där personalens information i ett företag finns i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"personal.csv"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"personnr;namn;adress;adressort"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"080488-123X;Peter Mikkola;Filpusvägen 7;00700 HELSINGFORS"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"290274-044S;Lisa Marttinen;Mannerheimvägen 100 A 10;00100 HELSINGFORS"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010479-007Z;Arto Vihavainen;Tiilitehtaankatu 10;04260 KERAVA"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010499-345K;Leevi Hellas;Tapiolavägen 9;02100 ESBO"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Löneuppgifterna finns i en skild fil, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lon.csv"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"personnr;lön;bonus"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"080488-123X;3300;0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"290274-044S;4150;200"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010479-007Z;1300;1200"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla rader i båda filerna innehåller en personlig id-kod (pic) som identifierar vems data vi arbetar med. När vi använder det här id:t som gemensam faktor, är det lätt att koppla en arbetstagares namn med hennes lön. Vi kan till exempel skriva ut en lista över de månatliga inkomsterna:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"inkomster:\nPeter Mikkola 3300 euro\nLisa Marttinen 4350 euro\nArto Vihavainen 2500 euro\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet använder två lexikon som hjälpdatastrukturer: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"loner"}]},{"type":"text","value":". Båda använder pic som nyckel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personal.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personnr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"\n namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nloner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"lon.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"';'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"personnr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":"\n loner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"inkomster:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" personnr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" personnr "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" loner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" loner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"personnr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"lon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 0 euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först skapar programmet lexikonen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"loner"}]},{"type":"text","value":". De har dessa innehåll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'080488-123X'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Peter Mikkola'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'290274-044S'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Lisa Marttinen'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010479-007Z'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Arto Vihavainen'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010499-345K'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Leevi Hellas'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'080488-123X'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3300"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'290274-044S'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4350"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010479-007Z'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For-loopen i slutet av programmet kombinerar namnen på arbetstagarna med deras löner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet kan också ta i beaktande situationer där pic saknas för en arbetstagare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att ordningen som elementen är lagrade i lexikon inte har någon skillnad, eftersom nycklarna behandlas med hjälp av hashvärden."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursresultat, del 1","tmcname":"osa06-04_resultat_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet behandlar två CSV-filen. I den ena finns information om studerande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"studerandenr;förnamn;efternamn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;pekka;peloton"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;jaana;javanainen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;liisa;virtanen"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Och i den andra antalet gjorda uppgifter på veckonivå:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"studerandenr;v1;v2;v3;v4;v5;v6;v7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;4;1;1;4;5;2;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;3;5;3;1;5;4;6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;10;2;2;7;10;2;2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I de båda CSV-filerna innehåller den första rade rubriker."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar efter filnamnen och skriver därefter ut antalet gjorda uppgifter för varje studerande. Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"studerande1.csv"}]},{"type":"text","value":"\nUppgifter (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"uppgifter1.csv"}]},{"type":"text","value":"\npekka peloton 21\njaana javanainen 27\nliisa virtanen 35"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Hårdkoda värdena medan du testar programmet, så behöver du inte hela tiden mata in dem på nytt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hit kommer vi aldrig"}]},{"type":"text","value":"\n studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studerande (CSV): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uppgifter (CSV): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# de hårdkodade värdena"}]},{"type":"text","value":"\n studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studerande1.csv\""}]},{"type":"text","value":"\n uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"uppgifter1.csv\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den egentliga funktionaliteten är nu \"gömd\" bakom en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"-förgrening som aldrig körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill testa inmatning av information (på \"normalt\" sätt), kan vi ändra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studerande (CSV): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uppgifter (CSV): \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hit kommer vi aldrig!"}]},{"type":"text","value":"\n studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studerande1.csv\""}]},{"type":"text","value":"\n uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"uppgifter1.csv\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När koden är i skick kan if-satsen tas bort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursresultat, del 2","tmcname":"osa06-05_resultat_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi utvidgar nu föregående uppgift så att de studerandes provpoäng också läses från en CSV-fil. Filens struktur är den följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"studerandenr;u1;u2;u3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;4;1;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;3;5;3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;10;2;2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel studerande "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"12345678"}]},{"type":"text","value":" har fått 4 + 1 + 4, alltså nio poäng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fråga efter filnamnen och skriva ut vitsordet för varje studerande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"studerande1.csv"}]},{"type":"text","value":"\nUppgifter (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"uppgifter1.csv"}]},{"type":"text","value":"\nProvpoäng (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"provpoang1.csv"}]},{"type":"text","value":"\npekka peloton 0\njaana javanainen 1\nliisa virtanen 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Av gjorda uppgifter får man poäng så att 10 % gjorda uppgifter ger ett poäng ända till 100 % (40 uppgifter), som ger tio poäng. Poängen är ett heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vitsordet för kursen bildas på basis av prov- och uppgiftspoängsumman:"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"prov- och uppgiftspoäng tillsammans"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"vitsord"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0-14"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0 (underkänt)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"15-17"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"18-20"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"21-23"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"24-27"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"28-"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursresultat, del 3","tmcname":"osa06-06_resultat_3"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften formaterar vi utskriften från den föregående uppgiften:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"studerande1.csv"}]},{"type":"text","value":"\nUppgifter (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"uppgifter1.csv"}]},{"type":"text","value":"\nProvpoäng (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"provpoang1.csv"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"namn uppg_ant uppg_p prov_p tot_p vitsord\npekka peloton 21 5 9 14 0\njaana javanainen 27 6 11 17 1\nliisa virtanen 35 8 14 22 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På varje rad skrivs alltså ut den studerandes uppgiftsantal, uppgiftspoäng, provpoäng, totalpoäng samt vitsord. Det här görs \"prydligt\" så att namnkolumnen är 30 tecken bred och de övriga kolumnerna 10."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det lönar sig att utnyttja f-strängar (modul fyra)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att utskrift av strängar och siffror fungerar med lite olik logik i f-strängar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"fortsätter\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":">10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"fortsätter\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"python fortsätter\n pythonfortsätter\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I vanliga fall är strängar vänsterjusterade, men med tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" kan man justera strängen till höger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När siffror skrivs ut är logiken motsatt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"fortsätter\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","format-spec"]},"children":[{"type":"text","value":"<10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"fortsätter\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":" 42fortsätter\n42 fortsätter\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Siffror är normalt högerjusterade men med tecknet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" kan vi justera siffran till vänster."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spell checker","tmcname":"osa06-07_spellcheck"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber användaren ange text på engelska. Programmet ska utföra en språkkontroll och skriva ut texten så att felstavade ord är markerade med asterisker. Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"We use ptython to make a spell checker"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"We use *ptython* to make a spell checker\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"This is acually a good and usefull program"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"This is *acually* good and *usefull* program\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bokstavsstorleken ska inte påverka programmets funktionalitet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet använder sig av filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"wordlist.txt"}]},{"type":"text","value":" för att känna igen om orden är korrekt skrivna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Receptsök","tmcname":"osa06-08_receptsok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi ett program som låter användaren söka efter ett recept på basis av dess namn, tillagningstid eller ingrediens. Programmets recept finns lagrade i en fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje recept består av tre eller fler rader i receptfilen. Den första raden innehåller receptets namn, den andra tillagningstiden (heltal) och tredje raden framåt ingredienser. Ingredienslistan avsluts med en tom rad (exkl. det sista receptet). Filen kan inntehålla flera recept. Se exemplet nedan:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"Plättdeg\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nmjölk\nägg\nmjöl\nsocker\nsalt\nsmör\n\nKöttbullar\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nmalet kött\nägg\nskorpmjöl\n\nTofurullar\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\ntofu\nris\nvatten\nmorot\ngurka\navocado\nwasabi\n\nBulldeg\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"text","value":"\nmjölk\njäst\nägg\nsalt\nsocker\nkardemumma\nsmör"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: I den här uppgiften lönar det sig kanske att läsa in filens rader i en lista och sedan behandla den här listan enligt den här uppgiftens specifikationer."}]},{"type":"element","tagName":"h4","properties":{"id":"sokning-med-receptnamn","style":"position:relative;"},"children":[{"type":"text","value":"Sökning med receptnamn"},{"type":"element","tagName":"a","properties":{"href":"#sokning-med-receptnamn","ariaLabel":"sokning med receptnamn permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namnsok(fil: str, ord: str)"}]},{"type":"text","value":" som söker efter recept vars namn innehåller den givna strängen. Funktionen ska returnera en lista med namnen på de matchande recepten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namnsok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recept1.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bull\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" recept "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Köttbullar\nBulldeg"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att bokstavsstorleken inte har någon skillnad. Med ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bull"}]},{"type":"text","value":" hittar vi också "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bulldeg"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]},{"type":"element","tagName":"h4","properties":{"id":"sokning-med-tillagningstid","style":"position:relative;"},"children":[{"type":"text","value":"Sökning med tillagningstid"},{"type":"element","tagName":"a","properties":{"href":"#sokning-med-tillagningstid","ariaLabel":"sokning med tillagningstid permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tidssok(fil: str, tid: int)"}]},{"type":"text","value":" som söker efter de recept vars tillagningstid är högst det som angetts som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Matchande recept returneras som en lista och nu ska också tillagningstiden inkluderas. Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" tidssok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recept1.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" recept "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Plättdeg, tillagningstid 15 min"}]}]},{"type":"element","tagName":"h4","properties":{"id":"sokning-med-ingrediens","style":"position:relative;"},"children":[{"type":"text","value":"Sökning med ingrediens"},{"type":"element","tagName":"a","properties":{"href":"#sokning-med-ingrediens","ariaLabel":"sokning med ingrediens permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varning!! Den här delen är mycket svårare än de tidigare delarna. Om du har svårigheter, lönar det sig att göra de andra delarna först och sedan återkomma hit. Märk att du också kan skicka enskilda delar till servern."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ingredienssok(fil: str, ingrediens: str)"}]},{"type":"text","value":" som ska hitta recepten med den givna ingrediensen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Matchande recept returneras som en lista. Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hittade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ingredienssok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recept1.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"mjölk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" recept "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hittade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recept"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Plättdeg, tillagningstid 15 min\nBulldeg, tillagningstid 60 min"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Stadscyklar","tmcname":"osa06-09_stadscyklar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften skapar vi några funktioner, med vilka vi kan inspektera en fil med information om "},{"type":"element","tagName":"a","properties":{"href":"https://www.hsl.fi/sv/stadscyklar","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"stadscyklars"}]},{"type":"text","value":" parkeringsstationer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ser filerna ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Longitude;Latitude;FID;name;total_slot;operative;id"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje parkeringsstation finns det en skild rad i filen: här specificeras koordinater, namnet på stationen m.m."}]},{"type":"element","tagName":"h4","properties":{"id":"avstand-mellan-stationer","style":"position:relative;"},"children":[{"type":"text","value":"Avstånd mellan stationer"},{"type":"element","tagName":"a","properties":{"href":"#avstand-mellan-stationer","ariaLabel":"avstand mellan stationer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"stationsinfo(fil: str)"}]},{"type":"text","value":" som läser in stationsinfon och returnerar den som ett lexikon:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"{\n \"Kaivopuisto: (24.950292890004903, 60.155444793742276),\n \"Laivasillankatu: (24.956347471358754, 60.160959093887129),\n \"Kapteeninpuistikko: (24.944927399779715, 60.158189199971673)\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som nyckel kommer alltså stationens namn och som värde en tuple som består av longituden (1) och latituden (2)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa nu funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"avstand(stationer: dict, station1: str, station2: str)"}]},{"type":"text","value":" som returnerar avståndet mellan de två givna stationerna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande formel används för att räkna avståndet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här behövs för att funktionen sqrt ska fungera"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\nx_km "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"longitude1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" longitude2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55.26"}]},{"type":"text","value":"\ny_km "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"latitude1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" latitude2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"111.2"}]},{"type":"text","value":"\navstand "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x_km"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y_km"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"stationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stationsinfo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'stations1.csv'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" avstand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Designmuseo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hietalahdentori\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" avstand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Viiskulma\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kaivopuisto\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"e"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0.9032737292463177\n0.7753594392019532"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här."}]},{"type":"element","tagName":"h4","properties":{"id":"langsta-avstand","style":"position:relative;"},"children":[{"type":"text","value":"Längsta avstånd"},{"type":"element","tagName":"a","properties":{"href":"#langsta-avstand","ariaLabel":"langsta avstand permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta_avstand(stationer: dict)"}]},{"type":"text","value":" som ska ta reda på vilka stationer som är längst ifrån varandra. Funktionen ska returnera en tuple vars två första värden syftar till stationernas namn och det tredje värdet är avståndet i fråga."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"stationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" stationsinfo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'stations1.csv'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" station2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" suurin "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langsta_avstand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" station2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" suurin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Laivasillankatu Hietalahdentori 1.478708873076181"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"61d39363-2bed-504e-8181-41badf9b7e57"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du läsa en fil med Python
  • \n
  • vet du vad en textfil och en CSV-fil är
  • \n
  • kan du behandla innehållet i en CSV-fil i dina program.
  • \n

Att bli en skicklig programmerare kräver mycket övning. Man måste utveckla en problemlösningsförmåga och ha en förmåga att intuition komma fram till korrekta lösningar. Därför finns det massor av övningar av olika typer i den här kursen. Vissa övningar är enklare och baserar sig mera eller mindre direkt på materialet medan andra uppgifter är svårare och kräver tillämpande av kunskaper som man lärt sig under kursen.

En del uppgifter kan kännas svåra, men det är inte något att oroa sig över. Ingen av uppgifterna är obligatorisk och du behöver bara 25 % av poängen från varje modul för att klara den här kursen. Du kan läsa mera på kursens bedömningssida.

Uppgifterna är inte i svårighetsordning. Varje del introducerar vanligtvis några nya saker inom programmering och i samband finns relaterade uppgifter – både enklare och svårare. Om du stöter på en uppgift som känns oöverkomlig ska du fortsätta till nästa uppgift. Du kan alltid senare återkomma till tidigare uppgifter.

En uppgift som känns för svår just nu kommer sannolikt att vara ganska enkel om en månad.

När man programmerar kan det uppstå ett behov att behandla data som finns lagrad i filer. Datorprogram kan läsa data från filer och skriva data till filer. Också stora mängder data i filer kan enkelt behandlas automatiskt.

Under den här kursen kommer vi endast att arbeta med textfiler. De här filerna består av rader med text. Till exempel kodeditorn Visual Studio Code är kompatibel med textfiler. Obs! Även om ordbehandlingsprogram som Microsoft Word ofta används med filer som innehåller text, är Word-dokument inte textfiler. Dokumenten innehåller också annan information om till exempel textformat, vilket gör det mer komplicerat att behandla filerna i ett program.

Att läsa data från en fil

Vi börjar att arbeta med filen exempel.txt som innehållet det följande:

Hej alla!\nVår exempelfil består av tre rader.\nDet här är den sista raden.

Ett enkelt sätt att använda filer i Python är med with-satsen. Den inledande raden öppnar filen och blocket där vi kan komma åt filen följer. Efter blocket stängs filen automatiskt och då kan den inte mera behandlas.

Den här koden öppnar alltså filen, läser dess innehåll och skriver det ut, och till slut stängs filen:

with open(\"exempel.txt\") as fil:\n    innehall = fil.read()\n    print(innehall)

Hej alla!\nVår exempelfil består av tre rader.\nDet här är den sista raden.

Variabeln fil är en file handle (\"filhandtag\"). Via variabeln kan vi komma åt filen så länge den är öppen. Här använde vi metoden read som returnerar filens innehåll som en hel sträng. I det här fallet skulle strängen se ut så här:

\"Hej alla!\\nVår exempelfil består av tre rader.\\nDet här är den sista raden.\"

Gå igenom innehållet i en fil

Metoden read fungerar väl för att skriva ut hela innehållet i en fil, men ofta vill vi gå igenom innehållet rad för rad.

Man kan tänka att textfiler är som listor med strängar, där varje sträng finns på sin egen rad i filen. Vi kan gå igenom listan med en for-loop.

Följande exempel läser in vår exempelfil med hjälp av en for-loop, tar bort radbrytningarna, räknar antalet rader och skriver ut varje rad med sitt radnummer. Programmet håller också koll på radernas längder:

with open(\"exempel.txt\") as fil:\n    raknare = 0\n    totallangd = 0\n\n    for rad in fil:\n        rad = rad.replace(\"\\n\", \"\")\n        raknare += 1\n        print(\"Rad\", raknare, rad)\n        langd = len(rad)\n        totallangd += langd\n\nprint(\"Radernas totallängd:\", totallangd)

Rad 1 Hej alla!\nRad 2 Vår exempelfil består av tre rader.\nRad 3 Det här är den sista raden.\nRadernas totallängd: 63

Det finns en radbrytning \\n i slutet av varje rad i filen, men print-funktionen lägger också automatiskt till en rad i slutet av utskriften. Det finns inga extra radbyten i utskriften ovan eftersom radbrytningarna avlägsnats med hjälp av replace-metoden. Metoden ersätter alla radbrytningstecken med en tom sträng. I och med detta räknas radernas längder också korrekt.

I filen siffror.txt finns siffror listade på olika rader enligt exemplet nedan:

2\n45\n108\n3\n-10\n1100\no.s.v. ...

Skapa funktionen storst som ska läsa filen och returnera den största siffran som hittas.

Observera att filnamnet alltid är siffror.txt och att funktionen inte har några parametrar.

Obs! Om Visual Studio Code inte hitta din fil även om namnet är korrekt skrivet ska du följa instruktionerna nedan.

Om Visual Studio Code inte hittar min fil?

När du kör din kod är det möjligt att Visual Studio Code meddelar att filen – även efter att du kollat att filen finns och att namnet är korrekt skrivet. Att ändra på följande inställning kan lösa problemet:

    \n
  • öppna inställningarna från menyraden: File -> Preferences -> Settings
  • \n
  • sök efter den inställning som ska ändras med sökordet \"executeinfile\"
  • \n
  • välj fliken Workspace
  • \n
  • bocka i valet under Python -> Terminal -> Execute in file dir.
  • \n

Inställningsfönstret borde ungefär se ut så här:

\n \n \n \n \n \n \"6\n \n \n

Om det här inte fungerar kan du kopiera filen i src-mappen…

\n \n \n \n \n \n \"6\n \n \n

…direkt till roten av uppgiftsmappen:

\n \n \n \n \n \n \"6\n \n \n

Att debugga kod som behandlar filer

När man använder Visual Studio Codes debuggare med program som behandlar filer, kan man stöta på följande felmeddelande:

\n \n \n \n \n \n \"6\n \n \n

Orsaken är att debuggaren alltid söker efter filer i roten av uppgiftsmappen. Inställningen Execute in file dir som nämndes ovan har ingen påverkan här. Den enklaste lösningen är att kopiera filen till rotmappen.

Du behöver kanske också starta om Visual Studio Code efter att du har kopierat alla filer som behövs.

Läsa CSV-filer

En CSV-fil (kommaseparerade värden) är en textfil som innehåller data som separerats med ett visst tecken. Det här tecknet är vanligtvis komma (,) eller semikolon (;), men vilket som helst tecken är i princip möjligt.

CSV-filer är ett vanligt sätt att lagra olika typer av data. Flera databaser och kalkylprogram – exempelvis Excel – kan importera och exportera data i CSV-format. Det här möjliggör enkel dataöverföring mellan olika system.

Vi har redan bekantat oss med hur man kan gå igenom rader i en fil med en for-loop, men hur kan vi separera fält på en och samma rad? Python har en strängmetod split, som kan användas för detta. Metoden tar separatortecknet eller -tecknen som ett strängargument och returnerar innehållet i den ursprungliga strängen som en lista av strängar – separerade vid separatortecknen.

Här finns ett exempel för att tydliggöra det här:

text = \"apa,banan,cembalo\"\nordlista = text.split(\",\")\nfor ord in ordlista:\n    print(ord)

apa\nbanan\ncembalo

Låt oss säga att vi har filen vitsord.csv, som innehåller namn på elever samt vitsord de fått av olika kurser. Varje rad har data som tillhör en studerande och data separeras med semikolon.

Peter;5;4;5;3;4;5;5;4;2;4\nPauline;3;4;2;4;4;2;3;1;3;3\nPia;4;5;5;4;5;5;4;5;4;4

Följande program går igenom filen rad för rad, delar upp raderna i delar och skriver ut namnen på eleverna samt deras vitsord:

with open(\"vitsord.csv\") as fil:\n    for rad in fil:\n        rad = rad.replace(\"\\n\", \"\")\n        delar = rad.split(\";\")\n        namn = delar[0]\n        vitsord = delar[1:]\n        print(\"Namn:\", namn)\n        print(\"Vitsord:\", vitsord)

Namn: Peter\nVitsord: ['5', '4', '5', '3', '4', '5', '5', '4', '2', '4']\nNamn: Pauline\nVitsord: ['3', '4', '2', '4', '4', '2', '3', '1', '3', '3']\nNamn: Pia\nVitsord: ['4', '5', '5', '4', '5', '5', '4', '5', '4', '4']

I filen frukter.csv finns frukter med deras pris enligt exemplet nedan:

banan;6.50\näpple;4.95\napelsin;8.0\no.s.v. ...

Skapa funktionen las_frukter som ska läsa filen och skapa ett lexikon där nyckeln är fruktens namn och värdet fruktens pris. Priset ska vara av typen float.

Observera att filnamnet alltid är frukter.csv och funktionen har inga parametrar.

Funktionen ska till slut returnera lexikonet.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

I filen matris.txt finns en matris enligt exemplet nedan:

1,0,2,8,2,1,3,2,5,2,2,2\n9,2,4,5,2,4,2,4,1,10,4,2\no.s.v. ...

Skapa funktionerna summa och maximum som returnerar summan av elementen i matrisen respektive det största elementet.

Skapa också funktionen radsummor som returnerar som en lista summorna av matrisens rader. Till exempel för matrisen...

1,2,3\n2,3,4

...returnerar funktionen [6, 9].

Tips: Du kan också implementera andra funktioner i programmet. Fundera vilka gemensamma funktioner de ovan nämnda funktionerna kan behöva.

Observera att filen alltid heter matris.txt och funktionerna inte har parametrar. Ytterligare funktioner du eventuellt skapar kan ha parametrar.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

Läsa samma fil flera gånger

Ibland kan man behöva läsa innehållet i en fil flera gånger i samma program. Vi tittar på ett program som behandlar data om några personer i en CSV-fil:

\nPeter;40;Helsingfors\nEmilia;34;Esbo\nErik;42;Åbo\nAntonia;100;Helsingfors\nLisa;58;Suonenjoki\n
with open(\"personer.csv\") as fil:\n    # skriver ut namn\n    for rad in fil:\n        delar = rad.split(\";\")\n        print(\"Namn:\", delar[0])\n\n    # söker efter den äldsta personen\n    hogsta_aldern = -1\n    for rad in fil:\n        delar = rad.split(\";\")\n        namn = delar[0]\n        alder = int(delar[1])\n        if alder > hogsta_aldern:\n            hogsta_aldern = alder\n            aldst = namn\n    print(\"Den äldsta är\", aldst)

När vi kör programmet får vi det här felmeddelandet:

Traceback (most recent call last):\n    print(\"Den äldsta är\"; aldst)\nUnboundLocalError: local variable 'aldst' referenced before assignment

Orsaken till at det här sker är att den andra for-loopen aldrig körs. Detta eftersom filen endast kan behandlas en gång. När den sista raden har lästs stannar file handlen i slutet av filen och data i filen kan inte längre kommas åt.

Om vi vill komma åt innehållet i filen i den andra for-loopen, måste vi öppna filen på nytt:

with open(\"personer.csv\") as fil:\n    # skriver ut namn\n    for rad in fil:\n        delar = rad.split(\";\")\n        print(\"Namn:\", delar[0])\n\nwith open(\"personer.csv\") as fil:\n    # söker efter den äldsta personen\n    hogsta_aldern = -1\n    for rad in fil:\n        delar = rad.split(\";\")\n        namn = delar[0]\n        alder = int(delar[1])\n        if alder > hogsta_aldern:\n            hogsta_aldern = alder\n            aldst = namn\n    print(\"Den äldsta är\", aldst)

Även om den ovanstående koden fungerar, innehåller den onödig upprepning. Det lönar sig vanligtvis att läsa filen bara en gång, och spara dess innehåll i ett passligt format för fortsatt behandling:

personer = []\n# vi läser in personerna till listan\nwith open(\"personer.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(\";\")\n        personer.append((delar[0], int(delar[1]), delar[2]))\n\n# skriver ut namn\nfor person in personer:\n    print(\"Namn:\", person[0])\n\n# söker efter den äldsta personen\nhogsta_aldern = -1\nfor person in personer:\n    namn = person[0]\n    alder = person[1]\n    if alder > hogsta_aldern:\n        hogsta_aldern = alder\n        aldst = namn\nprint(\"Den äldsta är\", aldst)

Mera om att behandla CSV-filer

Vi fortsätter behandla filen vitsord.csv, som innehåller det följande:

Peter;5;4;5;3;4;5;5;4;2;4\nPauline;3;4;2;4;4;2;3;1;3;3\nPia;4;5;5;4;5;5;4;5;4;4

Följande program skapar lexikonet vitsord baserat på innehållet i filen. Nycklarna är elevernas namn och värdet som är kopplat till nycklarna innehåller elevens vitsord. Programmet konverterar vitsorden till heltal så att de kan behandlas enklare.

vitsord = {}\nwith open(\"vitsord.csv\") as fil:\n    for rad in fil:\n        rad = rad.replace(\"\\n\", \"\")\n        delar = rad.split(\";\")\n        namn = delar[0]\n        vitsord[namn] = []\n        for givet_vitsord in delar[1:]:\n            vitsord[namn].append(int(givet_vitsord))\n\nprint(vitsord)

{'Peter': [5, 4, 5, 3, 4, 5, 5, 4, 2, 4], 'Pauline': [3, 4, 2, 4, 4, 2, 3, 1, 3, 3], 'Pia': [4, 5, 5, 4, 5, 5, 4, 5, 4, 4]}

Nu kan vi skriva ut statistik om varje studerande, baserat på värdena i lexikonet:

for namn, lista in vitsord.items():\n    basta = max(lista)\n    medeltal = sum(lista) / len(lista)\n    print(f\"{namn}: bästa vitsordet {basta}, medeltal {medeltal:.2f}\")

Peter: bästa vitsordet 5, medeltal 4.10\nPauline: bästa vitsordet 4, medeltal 2.90\nPia: bästa vitsordet 5, medeltal 4.50

Ta en titt på programmet i exemplet ovan. Det kan verka något komplicerat på en första titt, men tekniken kan användas med flera olika typer av data.

Ta bort överflödiga rader, mellanslag och radbrytningar

Låt oss säga att vi har en CSV-fil med namn, exporterat från Excel:

förnamn; efternamn\nPeter; Python\nJaana; Java\nHeikki; Haskell

Excel är ökänt för att lägga till extra mellanrum lite här och där. Här har vi ett extra mellanrum mellan elementen, efter varje semikolon.

Vi skulle vilja skriva ut efternamnet på varje person som finns i listan. Den första raden i filen innehåller information om den data som följer och kan skippas:

efternamn = []\nwith open(\"personer.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(\";\")\n        # skippar raden med rubriker\n        if delar[0] == \"förnamn\":\n            continue\n        efternamn.append(delar[1])\n\nprint(efternamn)

När koden körs får vi den här utskriften:

[' Python\\n', ' Java\\n', ' Haskell']

De två första elementen har ett radbrytningstecken i slutet och alla tre element har ett mellanslag i början. Vi har redan använt replace-metoden för att ta bort onödigt mellanrum, men ett bättre sätt är strip-metoden hos strängar. Den här metoden tar bort mellanrum från början och slutet av en sträng. Metoden tar bort mellanrum, radbrytningar, samt tabb- och andra tecken som normalt inte skulle skrivas ut.

Vi kan testa på metoden i Python-terminalen:

>>> \" prov \".strip()\n'prov'\n>>> \"\\n\\ntest\\n\".strip()\n'test'\n>>>

Att ta bort de onödiga tecknen kräver bara en liten ändring i programmet:

efternamn = []\nwith open(\"personer.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(';')\n        if delar[0] == \"förnamn\":\n            continue # skippar raden med rubriker\n        efternamn.append(delar[1].strip())\nprint(efternamn)

Nu har får vi den önskade utskriften:

['Python', 'Java', 'Haskell']

Strängmetoderna lstrip och rstrip fungerar lika som metoden strip, men gör det då bara för antingen vänstra (l) eller högra (r) kanten av strängen:

>>> \" teststräng  \".rstrip()\n' teststräng'\n>>> \" teststräng  \".lstrip()\n'teststräng  '

Kombinera data från olika filer

Det är mycket vanligt att data som behandlas av ett program finns utspritt i flera filer. Vi tar en titt på ett exempel där personalens information i ett företag finns i filen personal.csv:

personnr;namn;adress;adressort\n080488-123X;Peter Mikkola;Filpusvägen 7;00700 HELSINGFORS\n290274-044S;Lisa Marttinen;Mannerheimvägen 100 A 10;00100 HELSINGFORS\n010479-007Z;Arto Vihavainen;Tiilitehtaankatu 10;04260 KERAVA\n010499-345K;Leevi Hellas;Tapiolavägen 9;02100 ESBO

Löneuppgifterna finns i en skild fil, lon.csv:

personnr;lön;bonus\n080488-123X;3300;0\n290274-044S;4150;200\n010479-007Z;1300;1200

Alla rader i båda filerna innehåller en personlig id-kod (pic) som identifierar vems data vi arbetar med. När vi använder det här id:t som gemensam faktor, är det lätt att koppla en arbetstagares namn med hennes lön. Vi kan till exempel skriva ut en lista över de månatliga inkomsterna:

\ninkomster:\nPeter Mikkola    3300 euro\nLisa Marttinen   4350 euro\nArto Vihavainen  2500 euro\n

Programmet använder två lexikon som hjälpdatastrukturer: namn och loner. Båda använder pic som nyckel:

namn = {}\n\nwith open(\"personal.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(';')\n        if delar[0] == \"personnr\":\n            continue\n        namn[delar[0]] = delar[1]\n\nloner = {}\n\nwith open(\"lon.csv\") as fil:\n    for rad in fil:\n        delar = rad.split(';')\n        if delar[0] == \"personnr\":\n            continue\n        loner[delar[0]] = int(delar[1]) +int(delar[2])\n\nprint(\"inkomster:\")\n\nfor personnr, person in namn.items():\n    if personnr in loner:\n        lon = loner[personnr]\n        print(f\"{person:16} {lon} euro\")\n    else:\n        print(f\"{person:16} 0 euro\")

Först skapar programmet lexikonen namn och loner. De har dessa innehåll:

{\n    '080488-123X': 'Peter Mikkola',\n    '290274-044S': 'Lisa Marttinen',\n    '010479-007Z': 'Arto Vihavainen',\n    '010499-345K': 'Leevi Hellas'\n}\n\n{\n    '080488-123X': 3300,\n    '290274-044S': 4350,\n    '010479-007Z': 2500\n}

For-loopen i slutet av programmet kombinerar namnen på arbetstagarna med deras löner.

Programmet kan också ta i beaktande situationer där pic saknas för en arbetstagare.

Kom ihåg att ordningen som elementen är lagrade i lexikon inte har någon skillnad, eftersom nycklarna behandlas med hjälp av hashvärden.

Programmet behandlar två CSV-filen. I den ena finns information om studerande:

studerandenr;förnamn;efternamn\n12345678;pekka;peloton\n12345687;jaana;javanainen\n12345699;liisa;virtanen

Och i den andra antalet gjorda uppgifter på veckonivå:

studerandenr;v1;v2;v3;v4;v5;v6;v7\n12345678;4;1;1;4;5;2;4\n12345687;3;5;3;1;5;4;6\n12345699;10;2;2;7;10;2;2

I de båda CSV-filerna innehåller den första rade rubriker.

Skapa ett program som frågar efter filnamnen och skriver därefter ut antalet gjorda uppgifter för varje studerande. Exempel:

Studerande (CSV): studerande1.csv\nUppgifter (CSV): uppgifter1.csv\npekka peloton 21\njaana javanainen 27\nliisa virtanen 35

Tips: Hårdkoda värdena medan du testar programmet, så behöver du inte hela tiden mata in dem på nytt:

if False:\n    # hit kommer vi aldrig\n    studerande = input(\"Studerande (CSV): \")\n    uppgifter = input(\"Uppgifter (CSV): \")\nelse:\n    # de hårdkodade värdena\n    studerande = \"studerande1.csv\"\n    uppgifter = \"uppgifter1.csv\"

Den egentliga funktionaliteten är nu \"gömd\" bakom en False-förgrening som aldrig körs.

Om vi vill testa inmatning av information (på \"normalt\" sätt), kan vi ändra False till True:

if True:\n    studerande = input(\"Studerande (CSV): \")\n    uppgifter = input(\"Uppgifter (CSV): \")\nelse:\n    # hit kommer vi aldrig!\n    studerande = \"studerande1.csv\"\n    uppgifter = \"uppgifter1.csv\"

När koden är i skick kan if-satsen tas bort.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

Vi utvidgar nu föregående uppgift så att de studerandes provpoäng också läses från en CSV-fil. Filens struktur är den följande:

studerandenr;u1;u2;u3\n12345678;4;1;4\n12345687;3;5;3\n12345699;10;2;2

Till exempel studerande 12345678 har fått 4 + 1 + 4, alltså nio poäng.

Programmet ska fråga efter filnamnen och skriva ut vitsordet för varje studerande:

Studerande (CSV): studerande1.csv\nUppgifter (CSV): uppgifter1.csv\nProvpoäng (CSV): provpoang1.csv\npekka peloton 0\njaana javanainen 1\nliisa virtanen 3

Av gjorda uppgifter får man poäng så att 10 % gjorda uppgifter ger ett poäng ända till 100 % (40 uppgifter), som ger tio poäng. Poängen är ett heltal.

Vitsordet för kursen bildas på basis av prov- och uppgiftspoängsumman:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
prov- och uppgiftspoäng tillsammansvitsord
0-140 (underkänt)
15-171
18-202
21-233
24-274
28-5

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

I den här uppgiften formaterar vi utskriften från den föregående uppgiften:

Studerande (CSV): studerande1.csv\nUppgifter (CSV): uppgifter1.csv\nProvpoäng (CSV): provpoang1.csv

\nnamn                          uppg_ant  uppg_p    prov_p    tot_p     vitsord\npekka peloton                 21        5         9         14        0\njaana javanainen              27        6         11        17        1\nliisa virtanen                35        8         14        22        3\n

På varje rad skrivs alltså ut den studerandes uppgiftsantal, uppgiftspoäng, provpoäng, totalpoäng samt vitsord. Det här görs \"prydligt\" så att namnkolumnen är 30 tecken bred och de övriga kolumnerna 10.

Det lönar sig att utnyttja f-strängar (modul fyra).

Märk att utskrift av strängar och siffror fungerar med lite olik logik i f-strängar:

ord = \"python\"\nprint(f\"{ord:10}fortsätter\")\nprint(f\"{ord:>10}fortsätter\")
\npython    fortsätter\n    pythonfortsätter\n

I vanliga fall är strängar vänsterjusterade, men med tecknet > kan man justera strängen till höger.

När siffror skrivs ut är logiken motsatt:

siffra = 42\nprint(f\"{siffra:10}fortsätter\")\nprint(f\"{siffra:<10}fortsätter\")
\n        42fortsätter\n42        fortsätter\n

Siffror är normalt högerjusterade men med tecknet < kan vi justera siffran till vänster.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Skapa ett program som ber användaren ange text på engelska. Programmet ska utföra en språkkontroll och skriva ut texten så att felstavade ord är markerade med asterisker. Exempel:

Write text: We use ptython to make a spell checker

\nWe use *ptython* to make a spell checker\n

Write text: This is acually a good and usefull program

\nThis is *acually* good and *usefull* program\n

Bokstavsstorleken ska inte påverka programmets funktionalitet.

Programmet använder sig av filen wordlist.txt för att känna igen om orden är korrekt skrivna.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

I den här uppgiften skapar vi ett program som låter användaren söka efter ett recept på basis av dess namn, tillagningstid eller ingrediens. Programmets recept finns lagrade i en fil.

Varje recept består av tre eller fler rader i receptfilen. Den första raden innehåller receptets namn, den andra tillagningstiden (heltal) och tredje raden framåt ingredienser. Ingredienslistan avsluts med en tom rad (exkl. det sista receptet). Filen kan inntehålla flera recept. Se exemplet nedan:

Plättdeg\n15\nmjölk\nägg\nmjöl\nsocker\nsalt\nsmör\n\nKöttbullar\n45\nmalet kött\nägg\nskorpmjöl\n\nTofurullar\n30\ntofu\nris\nvatten\nmorot\ngurka\navocado\nwasabi\n\nBulldeg\n60\nmjölk\njäst\nägg\nsalt\nsocker\nkardemumma\nsmör

Tips: I den här uppgiften lönar det sig kanske att läsa in filens rader i en lista och sedan behandla den här listan enligt den här uppgiftens specifikationer.

Sökning med receptnamn

Skapa funktionen namnsok(fil: str, ord: str) som söker efter recept vars namn innehåller den givna strängen. Funktionen ska returnera en lista med namnen på de matchande recepten.

Exempel:

hittade = namnsok(\"recept1.txt\", \"bull\")\n\nfor recept in hittade:\n    print(recept)

Köttbullar\nBulldeg

Märk att bokstavsstorleken inte har någon skillnad. Med ordet bull hittar vi också Bulldeg.

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

Sökning med tillagningstid

Skapa funktionen tidssok(fil: str, tid: int) som söker efter de recept vars tillagningstid är högst det som angetts som argument.

Matchande recept returneras som en lista och nu ska också tillagningstiden inkluderas. Exempel:

hittade = tidssok(\"recept1.txt\", 20)\n\nfor recept in hittade:\n    print(recept)

Plättdeg, tillagningstid 15 min

Sökning med ingrediens

Varning!! Den här delen är mycket svårare än de tidigare delarna. Om du har svårigheter, lönar det sig att göra de andra delarna först och sedan återkomma hit. Märk att du också kan skicka enskilda delar till servern.

Skapa funktionen ingredienssok(fil: str, ingrediens: str) som ska hitta recepten med den givna ingrediensen.

Matchande recept returneras som en lista. Exempel:

hittade = ingredienssok(\"recept1.txt\", \"mjölk\")\n\nfor recept in hittade:\n    print(recept)

Plättdeg, tillagningstid 15 min\nBulldeg, tillagningstid 60 min

I den här uppgiften skapar vi några funktioner, med vilka vi kan inspektera en fil med information om stadscyklars parkeringsstationer.

Så här ser filerna ut:

Longitude;Latitude;FID;name;total_slot;operative;id\n24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001\n24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002\n24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003

För varje parkeringsstation finns det en skild rad i filen: här specificeras koordinater, namnet på stationen m.m.

Avstånd mellan stationer

Skapa funktionen stationsinfo(fil: str) som läser in stationsinfon och returnerar den som ett lexikon:

\n{\n  \"Kaivopuisto: (24.950292890004903, 60.155444793742276),\n  \"Laivasillankatu: (24.956347471358754, 60.160959093887129),\n  \"Kapteeninpuistikko: (24.944927399779715, 60.158189199971673)\n}\n

Som nyckel kommer alltså stationens namn och som värde en tuple som består av longituden (1) och latituden (2).

Skapa nu funktionen avstand(stationer: dict, station1: str, station2: str) som returnerar avståndet mellan de två givna stationerna.

Följande formel används för att räkna avståndet:

# det här behövs för att funktionen sqrt ska fungera\nimport math\n\nx_km = (longitude1 - longitude2) * 55.26\ny_km = (latitude1 - latitude2) * 111.2\navstand = math.sqrt(x_km**2 + y_km**2)

Exempel:

stationer = stationsinfo('stations1.csv')\ne = avstand(stationer, \"Designmuseo\", \"Hietalahdentori\")\nprint(e)\ne = avstand(stationer, \"Viiskulma\", \"Kaivopuisto\")\nprint(e)

0.9032737292463177\n0.7753594392019532

Obs! Om Visual Studio Code inte hittar filen även om namnet är korrekt skrivet, ska du följa instruktionerna här.

Längsta avstånd

Skapa funktionen langsta_avstand(stationer: dict) som ska ta reda på vilka stationer som är längst ifrån varandra. Funktionen ska returnera en tuple vars två första värden syftar till stationernas namn och det tredje värdet är avståndet i fråga.

stationer = stationsinfo('stations1.csv')\nstation1, station2, suurin = langsta_avstand(stationer)\nprint(station1, station2, suurin)

Laivasillankatu Hietalahdentori 1.478708873076181

","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/1-lasa-filer.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-6/2-skriva-filer/page-data.json b/page-data/osa-6/2-skriva-filer/page-data.json index 5469ee2a9..f8aef4e0a 100644 --- a/page-data/osa-6/2-skriva-filer/page-data.json +++ b/page-data/osa-6/2-skriva-filer/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-6/2-skriva-filer", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa filer med Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skriva textdata till en fil"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man skapar en CSV-fil."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu har vi sett hur man kan läsa data från filer, men det är naturligtvis också möjligt att skriva data till filer. I en vanlig situation behandlar programmet någon data och skriver det i en fil, så att det kan senare användas eller behandlas vidare av något annat program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan skapa en ny fil varje gång vi vill skriva data, men vi kan också lägga till data i en fil som redan finns. I båda fallen använder vi "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":"-funktionen som vi bekantade oss med i den förra delen. För att skriva filer måste vi ge funktionen ett andra argument."}]},{"type":"element","tagName":"h2","properties":{"id":"skapa-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Skapa en fil"},{"type":"element","tagName":"a","properties":{"href":"#skapa-en-fil","ariaLabel":"skapa en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill skapa en ny fil ska du anropa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":" med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"w"}]},{"type":"text","value":" som andra argument. Det här indikerar att filen ska öppnas i skrivläge. Funktionsanropet skulle då kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi skriver till filen här"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om filen redan finns, kommer allt innehåll att skrivas över. Man ska vara väldigt försiktig när man skapar nya filer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När filen är öppen, kan du skriva data i den. Du kan använda metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":", som tar strängen som ska skrivas som sitt argument:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du kör programmet, dyker en ny fil med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_fil.txt"}]},{"type":"text","value":" upp i mappen. Innehållet borde se ut så här:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill ha radbyten i filen, måste du lägga till dem för hand. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":" fungerar inte som print-funktionen, även om de har likheter. Följande program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andra raden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista raden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skulle resultera i en fil som denna:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!Andra radenSista raden"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Radbrytningar får man till stånd med att inkludera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" i strängarna som ges som argument till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andra raden\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista raden\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu borde innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_fil.txt"}]},{"type":"text","value":" se ut så här:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!\nAndra raden\nSista raden"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Tillägnad för...","tmcname":"osa06-10_tillagnad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber om ett namn och skapar en liten text som är tillägnad till personen i fråga. Texten ska sparas i en fil. Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tillägnas till: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Stella"}]},{"type":"text","value":"\nSkrivs till: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"till_min_van.txt"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"till_min_van.txt"}]},{"type":"text","value":" är:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Stella! Vi önskar dig trevliga stunder med Python-kursmaterialet. Mvh. MOOC-teamet"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lagga-till-data-i-en-befintlig-fil","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till data i en befintlig fil"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-data-i-en-befintlig-fil","ariaLabel":"lagga till data i en befintlig fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill lägga till data i slutet av en fil istället för att skriva över en hel fil, kan du använda argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" istället för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"w"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om filen inte ännu finns, fungerar det här läget på samma sätt som skrivläget."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program öppnar filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_fil.txt"}]},{"type":"text","value":" och lägger till några rader text i slutet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rad fyra\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ännu en till rad\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, borde innehållet i filen se ut på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!\nAndra raden\nSista raden\nRad fyra\nÄnnu en till rad"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I praktiken är det inte så vanligt att man lägger till innehåll i en befintlig fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Oftare läser man en fil, behandlar man data och till sist skrivs filen över med nya data. Till exempel om man vill ändra på innehållet i mitten av en fil, är det vanligtvis enklast att skriva över hela filen."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Dagbok","tmcname":"osa06-11_dagbok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som låter användaren skapa en enkel dagbok. Dagboksinläggen ska lagras i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dagbok.txt"}]},{"type":"text","value":". När programmet körs läses inläggen in från filen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! De lokala testen kan ändra på filens innehåll. Skapa alltså en kopia av filen om du vill behålla dess innehåll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSkriv inlägg: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Idag åt jag gröt"}]},{"type":"text","value":"\nSparat"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nInlägg:\nIdag åt jag gröt\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSkriv inlägg: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"På kvällen var jag i bastu"}]},{"type":"text","value":"\nSparat"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nInlägg:\nIdag åt jag gröt\nPå kvällen var jag i bastu\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi startar programmet på nytt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nInlägg:\nIdag åt jag gröt\nPå kvällen var jag i bastu\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"skriva-csv-filer","style":"position:relative;"},"children":[{"type":"text","value":"Skriva CSV-filer"},{"type":"element","tagName":"a","properties":{"href":"#skriva-csv-filer","ariaLabel":"skriva csv filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV-filer kan skrivas rad för rad med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":"-metoden, precis som med vilken som helst annan fil. Det följande exemplet skapar filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"programmerare.csv"}]},{"type":"text","value":" som innehåller namnet, programmeringsmiljön, favoritprogrammeringsspråket och antalet år arbetserfarenhet hos en programmerare. Fälten skiljs åt med semikolon."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmerare.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik;Windows;Pascal;10\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mats;Linux;PHP;2\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton;Linux;Java;17\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia;Mac;Cobol;9\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs får vi följande fil:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik;Windows;Pascal;10\nMats;Linux;PHP;2\nAnton;Linux;Java;17\nEmilia;Mac;Cobol;9"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur är det om data som ska skrivas finns i en lista i datorns minne?"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"programmerare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogrammerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Windows\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pascal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogrammerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mats\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Linux\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PHP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogrammerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Linux\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogrammerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mac\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cobol\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan konstruera den sträng som vi vill skriva som en f-sträng, och skriva den klara raden i filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmerare.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" programmerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om varje lista skulle vara väldigt lång, skulle det vara arbetsdrygt att konstruera strängen för hand och då skulle vi istället kunna använda en for-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmerare.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" programmerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";\""}]}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"tomma-innehallet-i-en-fil-och-ta-bort-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Tömma innehållet i en fil och ta bort en fil"},{"type":"element","tagName":"a","properties":{"href":"#tomma-innehallet-i-en-fil-och-ta-bort-en-fil","ariaLabel":"tomma innehallet i en fil och ta bort en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland behöver vi tömma innehållet i en fil. Då kan vi helt enkelt öppna filen i skrivläge och därefter stänga filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fil_som_ska_tommas.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":"-blocket nu bara innehåller instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":", som inte gör något. Python tillåter inte tomma block, så därför använder vi den här instruktionen här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att undvika "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":"-blocket och istället använda följande instruktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'fil_som_ska_tommas.txt'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'w'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"close"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Ta bort filer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också helt och hållet avlägsna en fil. Vi måste be operativsystemet om hjälp för att uppnå detta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här behöver vi för att kunna använda remove-funktionen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" os\n\nos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"onodig_fil.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Det här fungerar inte i testmiljön som används för de automatiska testen. Om du ombeds tömma innehållet i en fil, ska du använda någon av metoderna som beskrevs ovan."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filtrera data","tmcname":"osa06-12_filtrera_data"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kalkyler.csv"}]},{"type":"text","value":" finns lösningar på uppgifter enligt följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Anton;2+5;7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Pärla;3-2;1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Erik;9+3;11"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Anton;8-3;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Pärla;5+5;10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"o.s.v. ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Formatet för varje rad är alltså "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande;uppgift;resultat"}]},{"type":"text","value":". Uppgifterna är alltså additions- eller subtraktionsräkningar med två operander."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ordna_uppgifter()"}]},{"type":"text","value":" som"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"läser innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kalkyler.csv"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"skriver filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"korrekta.csv"}]},{"type":"text","value":" med de uppgifter som är korrekt lösta"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"skriver filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inkorrekta.csv"}]},{"type":"text","value":" med de övriga uppgifterna."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"korrekta.csv"}]},{"type":"text","value":" skulle se ut så här med det föregående exemplet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"Anton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"+5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nPärla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"-2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nPärla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"+5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De två resterande raderna skulle vara i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inkorrekta.csv"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Raderna ska förekomma i samma ordning som i den ursprungliga filen. Ändra inte på den filen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att funktionen ska fungera oavsett hur många gånger den anropas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"suodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"och..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"suodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...ska ge likadant slutresultat."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lagra personer","tmcname":"osa06-13_lagra_personer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spara_person(person: tuple)"}]},{"type":"text","value":" som får som argument en tuple som representerar en person. Tuplen innehåller följande information:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Namn (sträng)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Ålder (heltal)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Längd (flyttal)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spara informationen i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"personer.csv"}]},{"type":"text","value":", efter befintliga data. Formatet ska vara "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn;ålder;längd"}]},{"type":"text","value":". En person är alltså lika med en rad. Om tuplen är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(\"Sara Simonsson\", 37, 175.5)"}]},{"type":"text","value":" skulle programmet skriva följande rad i slutet av filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sara Simonsson;37;175.5"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"behandla-data-i-csv-format","style":"position:relative;"},"children":[{"type":"text","value":"Behandla data i CSV-format"},{"type":"element","tagName":"a","properties":{"href":"#behandla-data-i-csv-format","ariaLabel":"behandla data i csv format permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss skapa ett program som utvärderar elevernas framgång under en kurs. Programmet läser en CSV-fil som innehåller de veckovisa poängen som den studerande fått under kursens lopp. Programmet räknar ihop poängen och utvärderar därefter vilket vitsord de räcker till. Till slut skapar programmet en CSV-fil som innehåller poängsumman och vitsordet för varje elev."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV-filen som vi har tillgång till när vi börjar ser ut så här:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pärla;4;2;3;5;4;0;0\nPaula;7;2;8;3;5;4;5\nPirjo;3;4;3;5;3;4;4\nEmilia;6;6;5;5;0;4;8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets logik är uppdelad i tre funktioner: läsande av filen och behandlande av innehållet, vitsordsberäkning och skrivande av en ny fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filens inläsning sker enligt det vi lärt oss i den förra delen. Data lagras i ett lexikon där nyckeln är elevens namn och värdet är en lista på poängen som eleven i fråga har fått, i heltalsform:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_veckopoang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n veckopoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" veckopoang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den andra funktionen beräknar vilket vitsord ett visst antal poäng räcker till. Funktionen används av den tredje funktionen, som skriver resultaten i en fil:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"vitsord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spara_resultat"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\\n\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strukturen tillåter oss skapa en mycket enkel huvudfunktion. Observera att filnamnen ges som argument i huvudfunktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"veckopoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"veckopoang.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nspara_resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När huvudfunktionen körs, skapas filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat.csv"}]},{"type":"text","value":" med följande innehåll:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pärla;18;0\nPaula;34;3\nPirjo;26;2\nEmilia;41;5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notera att alla funktioner ovan är relativt enkla till sin funktionalitet – de har en uppgift var. Det här är ett vanligt och rekommenderat sätt att skapa större helheter när man programmerar. När en funktion har en uppgift är det enklare att säkerställa att den fungerar på önskat sätt. Det här gör det också enklare att senare ändra på programmet och lägga till ny funktionalitet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi till exempel vill ha en funktion för att skriva ut vitsordet för enskild studerande, kan vi skapa en ny funktion som använder sig av en befintlig funktion i koden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_vitsord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\nveckopoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"veckopoang.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hamta_vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi märker att någon funktionalitet i programmet kräver åtgärdande, kommer ändringar inte att orsaka följder överallt i koden. Om vi till exempel vill ändra på vitsordsgränserna, behöver vi bara ändra på funktionen som räknar ut vitsordet – alla andra funktioner som använder den här funktionen skulle fortfarande fungera, med de nya gränserna. Om koden för den här funktionaliteten skulle vara splittrad, finns det en risk att vi glömmer att uppdatera koden på något ställe. Det här skulle antagligen orsaka problem."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursresultat, del 4","tmcname":"osa06-14_resultat_4"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi utvecklar ännu lite programmet som genererar kursresultat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För tillfället läses studerandenas namn, uppgifts- och provpoäng från respektive filer. Vi ändrar på programmet så att också kursens namn och studiepoäng läses in från en fil. Formatet är det följande:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"\nnamn: Introkurs i programmering\nsp: 5\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska skapa två filer. Filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat.txt"}]},{"type":"text","value":" ska följa det här formatet:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Introkurs i programmering, 5 studiepoäng\n========================================\nnamn uppg_ant uppg_p prov_p tot_p vitsord\npekka peloton 21 5 9 14 0\njaana javanainen 27 6 11 17 1\nliisa virtanen 35 8 14 22 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här delen liknar alltså utskriften från den förra versionen av programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom ska filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat.csv"}]},{"type":"text","value":" skapas. Dess format ska följa detta exempel:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"12345678;pekka peloton;0\n12345687;jaana javanainen;1\n12345699;liisa virtanen;3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempelkörning:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"studerande1.csv"}]},{"type":"text","value":"\nUppgifter (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"uppgifter1.csv"}]},{"type":"text","value":"\nProvpoäng (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"provpoang1.csv"}]},{"type":"text","value":"\nKursinfo (textfil): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kurs1.txt"}]},{"type":"text","value":"\nResultaten lagrade i filerna resultat.txt och resultat.csv"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet frågar bara alltså om filnamnen och skriver resultaten i resultatfilerna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ordsök","tmcname":"osa06-15_ordsok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften behandlar vi filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord.txt"}]},{"type":"text","value":" som innehåller ord på engelska."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sok_ord(term: str)"}]},{"type":"text","value":" som returnerar en lista över matchande ord i filen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I söktermen kan man, förutom gemener, använda dessa specialtecken:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"punkt betyder att vilket som helst tecken accepteras ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ca."}]},{"type":"text","value":" matchar t.ex. cat och car och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".a.e"}]},{"type":"text","value":" care och late)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"asterisk betyder att ordet kan börja eller avslutas på valfritt vis ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ca*"}]},{"type":"text","value":" matchar t.ex. california och cat, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*ane"}]},{"type":"text","value":" matchar insane och aeroplane) – du kan anta att asterisken endast finns i början eller slutet av ett ord och att söktermen innehåller högst en asterisk"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om söktermen inte innehåller specialtecken ska termen matcha exakt med det sökta ordet."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi bestämmer också att en sökterm inte kan innehålla båda typer av specialtecken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orden är skrivna med gemener. Du kan också anta att argumentet inte innehåller versaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om inga resultat hittas ska funktionen returnera en tom lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Pythons strängmetoder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"startswith()"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"endswith()"}]},{"type":"text","value":" kan vara händiga. Sök på nätet för mera information."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sok_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*vokes\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['convokes', 'equivokes', 'evokes', 'invokes', 'provokes', 'reinvokes', 'revokes']"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ordbok som minns","tmcname":"osa06-16_ordbok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som fungerar som en ordbok, dit man kan lägga till nya ord eller där man kan söka efter befintliga ord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nAnge ord på svenska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bil"}]},{"type":"text","value":"\nAnge ord på engelska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nAnge ord på svenska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sopa"}]},{"type":"text","value":"\nAnge ord på engelska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"garbage"}]},{"type":"text","value":"\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nAnge ord på svenska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"väska"}]},{"type":"text","value":"\nAnge ord på engelska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bag"}]},{"type":"text","value":"\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bag"}]},{"type":"text","value":"\nsopa - garbage\nväska - bag\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nbil - car\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"väska"}]},{"type":"text","value":"\nväska - bag\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orden ska lagras i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ordbok.txt"}]},{"type":"text","value":". Programmet ska läsa in den här filen när det startar. Nya ordpar läggs till genast."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan själv bestämma i vilket format data lagras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att körande av de lokala testen kan tömma ordboksfilen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"33ad7375-10df-5850-9744-813bb01ac2b4"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa filer med Python
  • \n
  • kan du skriva textdata till en fil
  • \n
  • vet du hur man skapar en CSV-fil.
  • \n

Nu har vi sett hur man kan läsa data från filer, men det är naturligtvis också möjligt att skriva data till filer. I en vanlig situation behandlar programmet någon data och skriver det i en fil, så att det kan senare användas eller behandlas vidare av något annat program.

Vi kan skapa en ny fil varje gång vi vill skriva data, men vi kan också lägga till data i en fil som redan finns. I båda fallen använder vi open-funktionen som vi bekantade oss med i den förra delen. För att skriva filer måste vi ge funktionen ett andra argument.

Skapa en fil

Om du vill skapa en ny fil ska du anropa funktionen open med w som andra argument. Det här indikerar att filen ska öppnas i skrivläge. Funktionsanropet skulle då kunna se ut så här:

with open(\"ny_fil.txt\", \"w\") as fil:\n    # vi skriver till filen här

Obs! Om filen redan finns, kommer allt innehåll att skrivas över. Man ska vara väldigt försiktig när man skapar nya filer.

När filen är öppen, kan du skriva data i den. Du kan använda metoden write, som tar strängen som ska skrivas som sitt argument:

with open(\"ny_fil.txt\", \"w\") as fil:\n    fil.write(\"Hej alla!\")

När du kör programmet, dyker en ny fil med namnet ny_fil.txt upp i mappen. Innehållet borde se ut så här:

Hej alla!

Om du vill ha radbyten i filen, måste du lägga till dem för hand. Funktionen write fungerar inte som print-funktionen, även om de har likheter. Följande program…

with open(\"ny_fil.txt\", \"w\") as fil:\n    fil.write(\"Hej alla!\")\n    fil.write(\"Andra raden\")\n    fil.write(\"Sista raden\")

…skulle resultera i en fil som denna:

Hej alla!Andra radenSista raden

Radbrytningar får man till stånd med att inkludera \\n i strängarna som ges som argument till write-funktionen:

with open(\"ny_fil.txt\", \"w\") as fil:\n    fil.write(\"Hej alla!\\n\")\n    fil.write(\"Andra raden\\n\")\n    fil.write(\"Sista raden\\n\")

Nu borde innehållet i ny_fil.txt se ut så här:

Hej alla!\nAndra raden\nSista raden

Skapa ett program som ber om ett namn och skapar en liten text som är tillägnad till personen i fråga. Texten ska sparas i en fil. Exempel:

Tillägnas till: Stella\nSkrivs till: till_min_van.txt

Innehållet i till_min_van.txt är:

Hej Stella! Vi önskar dig trevliga stunder med Python-kursmaterialet. Mvh. MOOC-teamet

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Lägga till data i en befintlig fil

Om du vill lägga till data i slutet av en fil istället för att skriva över en hel fil, kan du använda argumentet a istället för w.

Om filen inte ännu finns, fungerar det här läget på samma sätt som skrivläget.

Följande program öppnar filen ny_fil.txt och lägger till några rader text i slutet:

with open(\"ny_fil.txt\", \"a\") as fil:\n    fil.write(\"Rad fyra\\n\")\n    fil.write(\"Ännu en till rad\\n\")

När programmet körs, borde innehållet i filen se ut på följande sätt:

Hej alla!\nAndra raden\nSista raden\nRad fyra\nÄnnu en till rad

I praktiken är det inte så vanligt att man lägger till innehåll i en befintlig fil.

Oftare läser man en fil, behandlar man data och till sist skrivs filen över med nya data. Till exempel om man vill ändra på innehållet i mitten av en fil, är det vanligtvis enklast att skriva över hela filen.

Skapa ett program som låter användaren skapa en enkel dagbok. Dagboksinläggen ska lagras i filen dagbok.txt. När programmet körs läses inläggen in från filen.

Obs! De lokala testen kan ändra på filens innehåll. Skapa alltså en kopia av filen om du vill behålla dess innehåll.

Programmet ska fungera så här:

1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 1\nSkriv inlägg: Idag åt jag gröt\nSparat

1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 2\nInlägg:\nIdag åt jag gröt\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 1\nSkriv inlägg: På kvällen var jag i bastu\nSparat

1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 2\nInlägg:\nIdag åt jag gröt\nPå kvällen var jag i bastu\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 0\nHejdå!

Vi startar programmet på nytt:

1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 2\nInlägg:\nIdag åt jag gröt\nPå kvällen var jag i bastu\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 0\nHejdå!

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Skriva CSV-filer

CSV-filer kan skrivas rad för rad med write-metoden, precis som med vilken som helst annan fil. Det följande exemplet skapar filen programmerare.csv som innehåller namnet, programmeringsmiljön, favoritprogrammeringsspråket och antalet år arbetserfarenhet hos en programmerare. Fälten skiljs åt med semikolon.

with open(\"programmerare.csv\", \"w\") as fil:\n    fil.write(\"Erik;Windows;Pascal;10\\n\")\n    fil.write(\"Mats;Linux;PHP;2\\n\")\n    fil.write(\"Anton;Linux;Java;17\\n\")\n    fil.write(\"Emilia;Mac;Cobol;9\\n\")

När programmet körs får vi följande fil:

Erik;Windows;Pascal;10\nMats;Linux;PHP;2\nAnton;Linux;Java;17\nEmilia;Mac;Cobol;9

Hur är det om data som ska skrivas finns i en lista i datorns minne?

programmerare = []\nprogrammerare.append([\"Erik\", \"Windows\", \"Pascal\", 10])\nprogrammerare.append([\"Mats\", \"Linux\", \"PHP\", 2])\nprogrammerare.append([\"Anton\", \"Linux\", \"Java\", 17])\nprogrammerare.append([\"Emilia\", \"Mac\", \"Cobol\", 9])

Vi kan konstruera den sträng som vi vill skriva som en f-sträng, och skriva den klara raden i filen:

with open(\"programmerare.csv\", \"w\") as fil:\n    for person in programmerare:\n        rad = f\"{person[0]};{person[1]};{person[2]};{person[3]}\"\n        fil.write(rad+\"\\n\")

Om varje lista skulle vara väldigt lång, skulle det vara arbetsdrygt att konstruera strängen för hand och då skulle vi istället kunna använda en for-loop:

with open(\"programmerare.csv\", \"w\") as fil:\n    for person in programmerare:\n        rad = \"\"\n        for varde in person:\n            rad += f\"{varde};\"\n        rad = rad[:-1]\n        fil.write(rad+\"\\n\")

Tömma innehållet i en fil och ta bort en fil

Ibland behöver vi tömma innehållet i en fil. Då kan vi helt enkelt öppna filen i skrivläge och därefter stänga filen:

with open(\"fil_som_ska_tommas.txt\", \"w\") as fil:\n    pass

Observera att with-blocket nu bara innehåller instruktionen pass, som inte gör något. Python tillåter inte tomma block, så därför använder vi den här instruktionen här.

Det är också möjligt att undvika with-blocket och istället använda följande instruktion:

open('fil_som_ska_tommas.txt', 'w').close()

Du kan också helt och hållet avlägsna en fil. Vi måste be operativsystemet om hjälp för att uppnå detta:

# det här behöver vi för att kunna använda remove-funktionen\nimport os\n\nos.remove(\"onodig_fil.csv\")

Obs! Det här fungerar inte i testmiljön som används för de automatiska testen. Om du ombeds tömma innehållet i en fil, ska du använda någon av metoderna som beskrevs ovan.

I filen kalkyler.csv finns lösningar på uppgifter enligt följande exempel:

Anton;2+5;7\nPärla;3-2;1\nErik;9+3;11\nAnton;8-3;4\nPärla;5+5;10\no.s.v. ...

Formatet för varje rad är alltså studerande;uppgift;resultat. Uppgifterna är alltså additions- eller subtraktionsräkningar med två operander.

Skapa funktionen ordna_uppgifter() som

    \n
  • läser innehållet i kalkyler.csv
  • \n
  • skriver filen korrekta.csv med de uppgifter som är korrekt lösta
  • \n
  • skriver filen inkorrekta.csv med de övriga uppgifterna.
  • \n

Filen korrekta.csv skulle se ut så här med det föregående exemplet:

Anton;2+5;7\nPärla;3-2;1\nPärla;5+5;10

De två resterande raderna skulle vara i inkorrekta.csv.

Raderna ska förekomma i samma ordning som i den ursprungliga filen. Ändra inte på den filen.

Märk att funktionen ska fungera oavsett hur många gånger den anropas:

suodata_laskut()

och...

suodata_laskut()\nsuodata_laskut()\nsuodata_laskut()\nsuodata_laskut()

...ska ge likadant slutresultat.

Skapa funktionen spara_person(person: tuple) som får som argument en tuple som representerar en person. Tuplen innehåller följande information:

    \n
  1. Namn (sträng)
  2. \n
  3. Ålder (heltal)
  4. \n
  5. Längd (flyttal)
  6. \n

Spara informationen i filen personer.csv, efter befintliga data. Formatet ska vara namn;ålder;längd. En person är alltså lika med en rad. Om tuplen är (\"Sara Simonsson\", 37, 175.5) skulle programmet skriva följande rad i slutet av filen:

Sara Simonsson;37;175.5

Behandla data i CSV-format

Låt oss skapa ett program som utvärderar elevernas framgång under en kurs. Programmet läser en CSV-fil som innehåller de veckovisa poängen som den studerande fått under kursens lopp. Programmet räknar ihop poängen och utvärderar därefter vilket vitsord de räcker till. Till slut skapar programmet en CSV-fil som innehåller poängsumman och vitsordet för varje elev.

CSV-filen som vi har tillgång till när vi börjar ser ut så här:

Pärla;4;2;3;5;4;0;0\nPaula;7;2;8;3;5;4;5\nPirjo;3;4;3;5;3;4;4\nEmilia;6;6;5;5;0;4;8

Programmets logik är uppdelad i tre funktioner: läsande av filen och behandlande av innehållet, vitsordsberäkning och skrivande av en ny fil.

Filens inläsning sker enligt det vi lärt oss i den förra delen. Data lagras i ett lexikon där nyckeln är elevens namn och värdet är en lista på poängen som eleven i fråga har fått, i heltalsform:

def las_veckopoang(filnamn):\n    veckopoang = {}\n    with open(filnamn) as fil:\n        for rad in fil:\n            delar = rad.split(\";\")\n            lista = []\n            for poang in delar[1:]:\n                lista.append(int(poang))\n            veckopoang[delar[0]] = lista\n\n    return veckopoang

Den andra funktionen beräknar vilket vitsord ett visst antal poäng räcker till. Funktionen används av den tredje funktionen, som skriver resultaten i en fil:

def vitsord(poang):\n    if poang < 20:\n        return 0\n    elif poang < 25:\n        return 1\n    elif poang < 30:\n        return 2\n    elif poang < 35:\n        return 3\n    elif poang < 40:\n        return 4\n    else:\n        return 5\n\ndef spara_resultat(filnamn, veckopoang):\n    with open(filnamn, \"w\") as fil:\n        for namn, lista in veckopoang.items():\n            summa = sum(lista)\n            fil.write(f\"{namn};{summa};{vitsord(summa)}\\n\")

Strukturen tillåter oss skapa en mycket enkel huvudfunktion. Observera att filnamnen ges som argument i huvudfunktionen:

veckopoang = las_veckopoang(\"veckopoang.csv\")\nspara_resultat(\"resultat.csv\", veckopoang)

När huvudfunktionen körs, skapas filen resultat.csv med följande innehåll:

Pärla;18;0\nPaula;34;3\nPirjo;26;2\nEmilia;41;5

Notera att alla funktioner ovan är relativt enkla till sin funktionalitet – de har en uppgift var. Det här är ett vanligt och rekommenderat sätt att skapa större helheter när man programmerar. När en funktion har en uppgift är det enklare att säkerställa att den fungerar på önskat sätt. Det här gör det också enklare att senare ändra på programmet och lägga till ny funktionalitet.

Om vi till exempel vill ha en funktion för att skriva ut vitsordet för enskild studerande, kan vi skapa en ny funktion som använder sig av en befintlig funktion i koden:

def hamta_vitsord(person, veckopoang):\n    for namn, lista in veckopoang.items():\n        if namn == person:\n            return vitsord(sum(lista))\n\n\nveckopoang = las_veckopoang(\"veckopoang.csv\")\nprint(hamta_vitsord(\"Paula\", veckopoang))

3

Om vi märker att någon funktionalitet i programmet kräver åtgärdande, kommer ändringar inte att orsaka följder överallt i koden. Om vi till exempel vill ändra på vitsordsgränserna, behöver vi bara ändra på funktionen som räknar ut vitsordet – alla andra funktioner som använder den här funktionen skulle fortfarande fungera, med de nya gränserna. Om koden för den här funktionaliteten skulle vara splittrad, finns det en risk att vi glömmer att uppdatera koden på något ställe. Det här skulle antagligen orsaka problem.

Vi utvecklar ännu lite programmet som genererar kursresultat.

För tillfället läses studerandenas namn, uppgifts- och provpoäng från respektive filer. Vi ändrar på programmet så att också kursens namn och studiepoäng läses in från en fil. Formatet är det följande:

\n\nnamn: Introkurs i programmering\nsp: 5\n

Programmet ska skapa två filer. Filen resultat.txt ska följa det här formatet:

\nIntrokurs i programmering, 5 studiepoäng\n========================================\nnamn                          uppg_ant  uppg_p    prov_p    tot_p     vitsord\npekka peloton                 21        5         9         14        0\njaana javanainen              27        6         11        17        1\nliisa virtanen                35        8         14        22        3\n

Den här delen liknar alltså utskriften från den förra versionen av programmet.

Dessutom ska filen resultat.csv skapas. Dess format ska följa detta exempel:

\n12345678;pekka peloton;0\n12345687;jaana javanainen;1\n12345699;liisa virtanen;3\n

Exempelkörning:

Studerande (CSV): studerande1.csv\nUppgifter (CSV): uppgifter1.csv\nProvpoäng (CSV): provpoang1.csv\nKursinfo (textfil): kurs1.txt\nResultaten lagrade i filerna resultat.txt och resultat.csv

Programmet frågar bara alltså om filnamnen och skriver resultaten i resultatfilerna.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

I den här uppgiften behandlar vi filen ord.txt som innehåller ord på engelska.

Skapa funktionen sok_ord(term: str) som returnerar en lista över matchande ord i filen.

I söktermen kan man, förutom gemener, använda dessa specialtecken:

    \n
  • punkt betyder att vilket som helst tecken accepteras (ca. matchar t.ex. cat och car och .a.e care och late)
  • \n
  • asterisk betyder att ordet kan börja eller avslutas på valfritt vis (ca* matchar t.ex. california och cat, *ane matchar insane och aeroplane) – du kan anta att asterisken endast finns i början eller slutet av ett ord och att söktermen innehåller högst en asterisk
  • \n
  • om söktermen inte innehåller specialtecken ska termen matcha exakt med det sökta ordet.
  • \n

Vi bestämmer också att en sökterm inte kan innehålla båda typer av specialtecken.

Orden är skrivna med gemener. Du kan också anta att argumentet inte innehåller versaler.

Om inga resultat hittas ska funktionen returnera en tom lista.

Tips: Pythons strängmetoder startswith() och endswith() kan vara händiga. Sök på nätet för mera information.

Exempel:

print(sok_ord(\"*vokes\"))

['convokes', 'equivokes', 'evokes', 'invokes', 'provokes', 'reinvokes', 'revokes']

Skapa ett program som fungerar som en ordbok, dit man kan lägga till nya ord eller där man kan söka efter befintliga ord.

Programmet ska fungera så här:

1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 1\nAnge ord på svenska: bil\nAnge ord på engelska: car\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 1\nAnge ord på svenska: sopa\nAnge ord på engelska: garbage\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 1\nAnge ord på svenska: väska\nAnge ord på engelska: bag\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 2\nAnge ord: bag\nsopa - garbage\nväska - bag\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 2\nAnge ord: car\nbil - car\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 2\nAnge ord: väska\nväska - bag\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 3\nHejdå!

Orden ska lagras i filen ordbok.txt. Programmet ska läsa in den här filen när det startar. Nya ordpar läggs till genast.

Du kan själv bestämma i vilket format data lagras.

Observera att körande av de lokala testen kan tömma ordboksfilen.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/2-skriva-filer.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa filer med Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skriva textdata till en fil"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man skapar en CSV-fil."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu har vi sett hur man kan läsa data från filer, men det är naturligtvis också möjligt att skriva data till filer. I en vanlig situation behandlar programmet någon data och skriver det i en fil, så att det kan senare användas eller behandlas vidare av något annat program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan skapa en ny fil varje gång vi vill skriva data, men vi kan också lägga till data i en fil som redan finns. I båda fallen använder vi "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":"-funktionen som vi bekantade oss med i den förra delen. För att skriva filer måste vi ge funktionen ett andra argument."}]},{"type":"element","tagName":"h2","properties":{"id":"skapa-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Skapa en fil"},{"type":"element","tagName":"a","properties":{"href":"#skapa-en-fil","ariaLabel":"skapa en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill skapa en ny fil ska du anropa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":" med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"w"}]},{"type":"text","value":" som andra argument. Det här indikerar att filen ska öppnas i skrivläge. Funktionsanropet skulle då kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi skriver till filen här"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Om filen redan finns, kommer allt innehåll att skrivas över. Man ska vara väldigt försiktig när man skapar nya filer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När filen är öppen, kan du skriva data i den. Du kan använda metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":", som tar strängen som ska skrivas som sitt argument:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du kör programmet, dyker en ny fil med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_fil.txt"}]},{"type":"text","value":" upp i mappen. Innehållet borde se ut så här:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill ha radbyten i filen, måste du lägga till dem för hand. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":" fungerar inte som print-funktionen, även om de har likheter. Följande program…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andra raden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista raden\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…skulle resultera i en fil som denna:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!Andra radenSista raden"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Radbrytningar får man till stånd med att inkludera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" i strängarna som ges som argument till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andra raden\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista raden\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu borde innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_fil.txt"}]},{"type":"text","value":" se ut så här:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!\nAndra raden\nSista raden"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Tillägnad för...","tmcname":"osa06-10_tillagnad"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som ber om ett namn och skapar en liten text som är tillägnad till personen i fråga. Texten ska sparas i en fil. Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tillägnas till: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Stella"}]},{"type":"text","value":"\nSkrivs till: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"till_min_van.txt"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"till_min_van.txt"}]},{"type":"text","value":" är:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej Stella! Vi önskar dig trevliga stunder med Python-kursmaterialet. Mvh. MOOC-teamet"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lagga-till-data-i-en-befintlig-fil","style":"position:relative;"},"children":[{"type":"text","value":"Lägga till data i en befintlig fil"},{"type":"element","tagName":"a","properties":{"href":"#lagga-till-data-i-en-befintlig-fil","ariaLabel":"lagga till data i en befintlig fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du vill lägga till data i slutet av en fil istället för att skriva över en hel fil, kan du använda argumentet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" istället för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"w"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om filen inte ännu finns, fungerar det här läget på samma sätt som skrivläget."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program öppnar filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_fil.txt"}]},{"type":"text","value":" och lägger till några rader text i slutet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ny_fil.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rad fyra\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ännu en till rad\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs, borde innehållet i filen se ut på följande sätt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej alla!\nAndra raden\nSista raden\nRad fyra\nÄnnu en till rad"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I praktiken är det inte så vanligt att man lägger till innehåll i en befintlig fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Oftare läser man en fil, behandlar man data och till sist skrivs filen över med nya data. Till exempel om man vill ändra på innehållet i mitten av en fil, är det vanligtvis enklast att skriva över hela filen."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Dagbok","tmcname":"osa06-11_dagbok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som låter användaren skapa en enkel dagbok. Dagboksinläggen ska lagras i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dagbok.txt"}]},{"type":"text","value":". När programmet körs läses inläggen in från filen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! De lokala testen kan ändra på filens innehåll. Skapa alltså en kopia av filen om du vill behålla dess innehåll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSkriv inlägg: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Idag åt jag gröt"}]},{"type":"text","value":"\nSparat"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nInlägg:\nIdag åt jag gröt\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSkriv inlägg: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"På kvällen var jag i bastu"}]},{"type":"text","value":"\nSparat"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nInlägg:\nIdag åt jag gröt\nPå kvällen var jag i bastu\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi startar programmet på nytt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nInlägg:\nIdag åt jag gröt\nPå kvällen var jag i bastu\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"h2","properties":{"id":"skriva-csv-filer","style":"position:relative;"},"children":[{"type":"text","value":"Skriva CSV-filer"},{"type":"element","tagName":"a","properties":{"href":"#skriva-csv-filer","ariaLabel":"skriva csv filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV-filer kan skrivas rad för rad med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":"-metoden, precis som med vilken som helst annan fil. Det följande exemplet skapar filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"programmerare.csv"}]},{"type":"text","value":" som innehåller namnet, programmeringsmiljön, favoritprogrammeringsspråket och antalet år arbetserfarenhet hos en programmerare. Fälten skiljs åt med semikolon."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmerare.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik;Windows;Pascal;10\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mats;Linux;PHP;2\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton;Linux;Java;17\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia;Mac;Cobol;9\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet körs får vi följande fil:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik;Windows;Pascal;10\nMats;Linux;PHP;2\nAnton;Linux;Java;17\nEmilia;Mac;Cobol;9"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hur är det om data som ska skrivas finns i en lista i datorns minne?"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"programmerare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogrammerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Windows\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pascal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogrammerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mats\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Linux\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PHP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogrammerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Linux\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Java\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogrammerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mac\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cobol\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan konstruera den sträng som vi vill skriva som en f-sträng, och skriva den klara raden i filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmerare.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" programmerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om varje lista skulle vara väldigt lång, skulle det vara arbetsdrygt att konstruera strängen för hand och då skulle vi istället kunna använda en for-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmerare.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" programmerare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";\""}]}]},{"type":"text","value":"\n rad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"tomma-innehallet-i-en-fil-och-ta-bort-en-fil","style":"position:relative;"},"children":[{"type":"text","value":"Tömma innehållet i en fil och ta bort en fil"},{"type":"element","tagName":"a","properties":{"href":"#tomma-innehallet-i-en-fil-och-ta-bort-en-fil","ariaLabel":"tomma innehallet i en fil och ta bort en fil permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland behöver vi tömma innehållet i en fil. Då kan vi helt enkelt öppna filen i skrivläge och därefter stänga filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fil_som_ska_tommas.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":"-blocket nu bara innehåller instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":", som inte gör något. Python tillåter inte tomma block, så därför använder vi den här instruktionen här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att undvika "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":"-blocket och istället använda följande instruktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'fil_som_ska_tommas.txt'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'w'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"close"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Ta bort filer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också helt och hållet avlägsna en fil. Vi måste be operativsystemet om hjälp för att uppnå detta:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här behöver vi för att kunna använda remove-funktionen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" os\n\nos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"onodig_fil.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Det här fungerar inte i testmiljön som används för de automatiska testen. Om du ombeds tömma innehållet i en fil, ska du använda någon av metoderna som beskrevs ovan."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filtrera data","tmcname":"osa06-12_filtrera_data"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kalkyler.csv"}]},{"type":"text","value":" finns lösningar på uppgifter enligt följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Anton;2+5;7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Pärla;3-2;1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Erik;9+3;11"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Anton;8-3;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Pärla;5+5;10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"o.s.v. ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Formatet för varje rad är alltså "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande;uppgift;resultat"}]},{"type":"text","value":". Uppgifterna är alltså additions- eller subtraktionsräkningar med två operander."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ordna_uppgifter()"}]},{"type":"text","value":" som"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"läser innehållet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kalkyler.csv"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"skriver filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"korrekta.csv"}]},{"type":"text","value":" med de uppgifter som är korrekt lösta"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"skriver filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inkorrekta.csv"}]},{"type":"text","value":" med de övriga uppgifterna."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"korrekta.csv"}]},{"type":"text","value":" skulle se ut så här med det föregående exemplet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"Anton"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"+5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nPärla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"-2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nPärla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"+5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De två resterande raderna skulle vara i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inkorrekta.csv"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Raderna ska förekomma i samma ordning som i den ursprungliga filen. Ändra inte på den filen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Märk att funktionen ska fungera oavsett hur många gånger den anropas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"suodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"och..."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"suodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuodata_laskut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...ska ge likadant slutresultat."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lagra personer","tmcname":"osa06-13_lagra_personer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spara_person(person: tuple)"}]},{"type":"text","value":" som får som argument en tuple som representerar en person. Tuplen innehåller följande information:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Namn (sträng)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Ålder (heltal)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Längd (flyttal)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Spara informationen i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"personer.csv"}]},{"type":"text","value":", efter befintliga data. Formatet ska vara "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn;ålder;längd"}]},{"type":"text","value":". En person är alltså lika med en rad. Om tuplen är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(\"Sara Simonsson\", 37, 175.5)"}]},{"type":"text","value":" skulle programmet skriva följande rad i slutet av filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sara Simonsson;37;175.5"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"behandla-data-i-csv-format","style":"position:relative;"},"children":[{"type":"text","value":"Behandla data i CSV-format"},{"type":"element","tagName":"a","properties":{"href":"#behandla-data-i-csv-format","ariaLabel":"behandla data i csv format permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss skapa ett program som utvärderar elevernas framgång under en kurs. Programmet läser en CSV-fil som innehåller de veckovisa poängen som den studerande fått under kursens lopp. Programmet räknar ihop poängen och utvärderar därefter vilket vitsord de räcker till. Till slut skapar programmet en CSV-fil som innehåller poängsumman och vitsordet för varje elev."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV-filen som vi har tillgång till när vi börjar ser ut så här:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pärla;4;2;3;5;4;0;0\nPaula;7;2;8;3;5;4;5\nPirjo;3;4;3;5;3;4;4\nEmilia;6;6;5;5;0;4;8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets logik är uppdelad i tre funktioner: läsande av filen och behandlande av innehållet, vitsordsberäkning och skrivande av en ny fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filens inläsning sker enligt det vi lärt oss i den förra delen. Data lagras i ett lexikon där nyckeln är elevens namn och värdet är en lista på poängen som eleven i fråga har fått, i heltalsform:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_veckopoang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n veckopoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" veckopoang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den andra funktionen beräknar vilket vitsord ett visst antal poäng räcker till. Funktionen används av den tredje funktionen, som skriver resultaten i en fil:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"vitsord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spara_resultat"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filnamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\\n\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strukturen tillåter oss skapa en mycket enkel huvudfunktion. Observera att filnamnen ges som argument i huvudfunktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"veckopoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"veckopoang.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nspara_resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När huvudfunktionen körs, skapas filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat.csv"}]},{"type":"text","value":" med följande innehåll:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pärla;18;0\nPaula;34;3\nPirjo;26;2\nEmilia;41;5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notera att alla funktioner ovan är relativt enkla till sin funktionalitet – de har en uppgift var. Det här är ett vanligt och rekommenderat sätt att skapa större helheter när man programmerar. När en funktion har en uppgift är det enklare att säkerställa att den fungerar på önskat sätt. Det här gör det också enklare att senare ändra på programmet och lägga till ny funktionalitet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi till exempel vill ha en funktion för att skriva ut vitsordet för enskild studerande, kan vi skapa en ny funktion som använder sig av en befintlig funktion i koden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_vitsord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" lista "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"items"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\nveckopoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"veckopoang.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hamta_vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckopoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi märker att någon funktionalitet i programmet kräver åtgärdande, kommer ändringar inte att orsaka följder överallt i koden. Om vi till exempel vill ändra på vitsordsgränserna, behöver vi bara ändra på funktionen som räknar ut vitsordet – alla andra funktioner som använder den här funktionen skulle fortfarande fungera, med de nya gränserna. Om koden för den här funktionaliteten skulle vara splittrad, finns det en risk att vi glömmer att uppdatera koden på något ställe. Det här skulle antagligen orsaka problem."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursresultat, del 4","tmcname":"osa06-14_resultat_4"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi utvecklar ännu lite programmet som genererar kursresultat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För tillfället läses studerandenas namn, uppgifts- och provpoäng från respektive filer. Vi ändrar på programmet så att också kursens namn och studiepoäng läses in från en fil. Formatet är det följande:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"\nnamn: Introkurs i programmering\nsp: 5\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska skapa två filer. Filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat.txt"}]},{"type":"text","value":" ska följa det här formatet:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Introkurs i programmering, 5 studiepoäng\n========================================\nnamn uppg_ant uppg_p prov_p tot_p vitsord\npekka peloton 21 5 9 14 0\njaana javanainen 27 6 11 17 1\nliisa virtanen 35 8 14 22 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här delen liknar alltså utskriften från den förra versionen av programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dessutom ska filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"resultat.csv"}]},{"type":"text","value":" skapas. Dess format ska följa detta exempel:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"12345678;pekka peloton;0\n12345687;jaana javanainen;1\n12345699;liisa virtanen;3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempelkörning:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"studerande1.csv"}]},{"type":"text","value":"\nUppgifter (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"uppgifter1.csv"}]},{"type":"text","value":"\nProvpoäng (CSV): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"provpoang1.csv"}]},{"type":"text","value":"\nKursinfo (textfil): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kurs1.txt"}]},{"type":"text","value":"\nResultaten lagrade i filerna resultat.txt och resultat.csv"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet frågar bara alltså om filnamnen och skriver resultaten i resultatfilerna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ordsök","tmcname":"osa06-15_ordsok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften behandlar vi filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord.txt"}]},{"type":"text","value":" som innehåller ord på engelska."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sok_ord(term: str)"}]},{"type":"text","value":" som returnerar en lista över matchande ord i filen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I söktermen kan man, förutom gemener, använda dessa specialtecken:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"punkt betyder att vilket som helst tecken accepteras ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ca."}]},{"type":"text","value":" matchar t.ex. cat och car och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".a.e"}]},{"type":"text","value":" care och late)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"asterisk betyder att ordet kan börja eller avslutas på valfritt vis ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ca*"}]},{"type":"text","value":" matchar t.ex. california och cat, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*ane"}]},{"type":"text","value":" matchar insane och aeroplane) – du kan anta att asterisken endast finns i början eller slutet av ett ord och att söktermen innehåller högst en asterisk"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om söktermen inte innehåller specialtecken ska termen matcha exakt med det sökta ordet."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi bestämmer också att en sökterm inte kan innehålla båda typer av specialtecken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orden är skrivna med gemener. Du kan också anta att argumentet inte innehåller versaler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om inga resultat hittas ska funktionen returnera en tom lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Pythons strängmetoder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"startswith()"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"endswith()"}]},{"type":"text","value":" kan vara händiga. Sök på nätet för mera information."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sok_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*vokes\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['convokes', 'equivokes', 'evokes', 'invokes', 'provokes', 'reinvokes', 'revokes']"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Ordbok som minns","tmcname":"osa06-16_ordbok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som fungerar som en ordbok, dit man kan lägga till nya ord eller där man kan söka efter befintliga ord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nAnge ord på svenska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bil"}]},{"type":"text","value":"\nAnge ord på engelska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nAnge ord på svenska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sopa"}]},{"type":"text","value":"\nAnge ord på engelska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"garbage"}]},{"type":"text","value":"\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nAnge ord på svenska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"väska"}]},{"type":"text","value":"\nAnge ord på engelska: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bag"}]},{"type":"text","value":"\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bag"}]},{"type":"text","value":"\nsopa - garbage\nväska - bag\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nbil - car\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nAnge ord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"väska"}]},{"type":"text","value":"\nväska - bag\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nHejdå!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Orden ska lagras i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ordbok.txt"}]},{"type":"text","value":". Programmet ska läsa in den här filen när det startar. Nya ordpar läggs till genast."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan själv bestämma i vilket format data lagras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att körande av de lokala testen kan tömma ordboksfilen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I dessa uppgifter ska kod inte placeras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -blocket, om du inte ombeds göra det."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"33ad7375-10df-5850-9744-813bb01ac2b4"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa filer med Python
  • \n
  • kan du skriva textdata till en fil
  • \n
  • vet du hur man skapar en CSV-fil.
  • \n

Nu har vi sett hur man kan läsa data från filer, men det är naturligtvis också möjligt att skriva data till filer. I en vanlig situation behandlar programmet någon data och skriver det i en fil, så att det kan senare användas eller behandlas vidare av något annat program.

Vi kan skapa en ny fil varje gång vi vill skriva data, men vi kan också lägga till data i en fil som redan finns. I båda fallen använder vi open-funktionen som vi bekantade oss med i den förra delen. För att skriva filer måste vi ge funktionen ett andra argument.

Skapa en fil

Om du vill skapa en ny fil ska du anropa funktionen open med w som andra argument. Det här indikerar att filen ska öppnas i skrivläge. Funktionsanropet skulle då kunna se ut så här:

with open(\"ny_fil.txt\", \"w\") as fil:\n    # vi skriver till filen här

Obs! Om filen redan finns, kommer allt innehåll att skrivas över. Man ska vara väldigt försiktig när man skapar nya filer.

När filen är öppen, kan du skriva data i den. Du kan använda metoden write, som tar strängen som ska skrivas som sitt argument:

with open(\"ny_fil.txt\", \"w\") as fil:\n    fil.write(\"Hej alla!\")

När du kör programmet, dyker en ny fil med namnet ny_fil.txt upp i mappen. Innehållet borde se ut så här:

Hej alla!

Om du vill ha radbyten i filen, måste du lägga till dem för hand. Funktionen write fungerar inte som print-funktionen, även om de har likheter. Följande program…

with open(\"ny_fil.txt\", \"w\") as fil:\n    fil.write(\"Hej alla!\")\n    fil.write(\"Andra raden\")\n    fil.write(\"Sista raden\")

…skulle resultera i en fil som denna:

Hej alla!Andra radenSista raden

Radbrytningar får man till stånd med att inkludera \\n i strängarna som ges som argument till write-funktionen:

with open(\"ny_fil.txt\", \"w\") as fil:\n    fil.write(\"Hej alla!\\n\")\n    fil.write(\"Andra raden\\n\")\n    fil.write(\"Sista raden\\n\")

Nu borde innehållet i ny_fil.txt se ut så här:

Hej alla!\nAndra raden\nSista raden

Skapa ett program som ber om ett namn och skapar en liten text som är tillägnad till personen i fråga. Texten ska sparas i en fil. Exempel:

Tillägnas till: Stella\nSkrivs till: till_min_van.txt

Innehållet i till_min_van.txt är:

Hej Stella! Vi önskar dig trevliga stunder med Python-kursmaterialet. Mvh. MOOC-teamet

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Lägga till data i en befintlig fil

Om du vill lägga till data i slutet av en fil istället för att skriva över en hel fil, kan du använda argumentet a istället för w.

Om filen inte ännu finns, fungerar det här läget på samma sätt som skrivläget.

Följande program öppnar filen ny_fil.txt och lägger till några rader text i slutet:

with open(\"ny_fil.txt\", \"a\") as fil:\n    fil.write(\"Rad fyra\\n\")\n    fil.write(\"Ännu en till rad\\n\")

När programmet körs, borde innehållet i filen se ut på följande sätt:

Hej alla!\nAndra raden\nSista raden\nRad fyra\nÄnnu en till rad

I praktiken är det inte så vanligt att man lägger till innehåll i en befintlig fil.

Oftare läser man en fil, behandlar man data och till sist skrivs filen över med nya data. Till exempel om man vill ändra på innehållet i mitten av en fil, är det vanligtvis enklast att skriva över hela filen.

Skapa ett program som låter användaren skapa en enkel dagbok. Dagboksinläggen ska lagras i filen dagbok.txt. När programmet körs läses inläggen in från filen.

Obs! De lokala testen kan ändra på filens innehåll. Skapa alltså en kopia av filen om du vill behålla dess innehåll.

Programmet ska fungera så här:

1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 1\nSkriv inlägg: Idag åt jag gröt\nSparat

1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 2\nInlägg:\nIdag åt jag gröt\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 1\nSkriv inlägg: På kvällen var jag i bastu\nSparat

1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 2\nInlägg:\nIdag åt jag gröt\nPå kvällen var jag i bastu\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 0\nHejdå!

Vi startar programmet på nytt:

1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 2\nInlägg:\nIdag åt jag gröt\nPå kvällen var jag i bastu\n1 – lägg till inlägg, 2 – läs inlägg, 0 – avsluta\nVal: 0\nHejdå!

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

Skriva CSV-filer

CSV-filer kan skrivas rad för rad med write-metoden, precis som med vilken som helst annan fil. Det följande exemplet skapar filen programmerare.csv som innehåller namnet, programmeringsmiljön, favoritprogrammeringsspråket och antalet år arbetserfarenhet hos en programmerare. Fälten skiljs åt med semikolon.

with open(\"programmerare.csv\", \"w\") as fil:\n    fil.write(\"Erik;Windows;Pascal;10\\n\")\n    fil.write(\"Mats;Linux;PHP;2\\n\")\n    fil.write(\"Anton;Linux;Java;17\\n\")\n    fil.write(\"Emilia;Mac;Cobol;9\\n\")

När programmet körs får vi följande fil:

Erik;Windows;Pascal;10\nMats;Linux;PHP;2\nAnton;Linux;Java;17\nEmilia;Mac;Cobol;9

Hur är det om data som ska skrivas finns i en lista i datorns minne?

programmerare = []\nprogrammerare.append([\"Erik\", \"Windows\", \"Pascal\", 10])\nprogrammerare.append([\"Mats\", \"Linux\", \"PHP\", 2])\nprogrammerare.append([\"Anton\", \"Linux\", \"Java\", 17])\nprogrammerare.append([\"Emilia\", \"Mac\", \"Cobol\", 9])

Vi kan konstruera den sträng som vi vill skriva som en f-sträng, och skriva den klara raden i filen:

with open(\"programmerare.csv\", \"w\") as fil:\n    for person in programmerare:\n        rad = f\"{person[0]};{person[1]};{person[2]};{person[3]}\"\n        fil.write(rad+\"\\n\")

Om varje lista skulle vara väldigt lång, skulle det vara arbetsdrygt att konstruera strängen för hand och då skulle vi istället kunna använda en for-loop:

with open(\"programmerare.csv\", \"w\") as fil:\n    for person in programmerare:\n        rad = \"\"\n        for varde in person:\n            rad += f\"{varde};\"\n        rad = rad[:-1]\n        fil.write(rad+\"\\n\")

Tömma innehållet i en fil och ta bort en fil

Ibland behöver vi tömma innehållet i en fil. Då kan vi helt enkelt öppna filen i skrivläge och därefter stänga filen:

with open(\"fil_som_ska_tommas.txt\", \"w\") as fil:\n    pass

Observera att with-blocket nu bara innehåller instruktionen pass, som inte gör något. Python tillåter inte tomma block, så därför använder vi den här instruktionen här.

Det är också möjligt att undvika with-blocket och istället använda följande instruktion:

open('fil_som_ska_tommas.txt', 'w').close()

Du kan också helt och hållet avlägsna en fil. Vi måste be operativsystemet om hjälp för att uppnå detta:

# det här behöver vi för att kunna använda remove-funktionen\nimport os\n\nos.remove(\"onodig_fil.csv\")

Obs! Det här fungerar inte i testmiljön som används för de automatiska testen. Om du ombeds tömma innehållet i en fil, ska du använda någon av metoderna som beskrevs ovan.

I filen kalkyler.csv finns lösningar på uppgifter enligt följande exempel:

Anton;2+5;7\nPärla;3-2;1\nErik;9+3;11\nAnton;8-3;4\nPärla;5+5;10\no.s.v. ...

Formatet för varje rad är alltså studerande;uppgift;resultat. Uppgifterna är alltså additions- eller subtraktionsräkningar med två operander.

Skapa funktionen ordna_uppgifter() som

    \n
  • läser innehållet i kalkyler.csv
  • \n
  • skriver filen korrekta.csv med de uppgifter som är korrekt lösta
  • \n
  • skriver filen inkorrekta.csv med de övriga uppgifterna.
  • \n

Filen korrekta.csv skulle se ut så här med det föregående exemplet:

Anton;2+5;7\nPärla;3-2;1\nPärla;5+5;10

De två resterande raderna skulle vara i inkorrekta.csv.

Raderna ska förekomma i samma ordning som i den ursprungliga filen. Ändra inte på den filen.

Märk att funktionen ska fungera oavsett hur många gånger den anropas:

suodata_laskut()

och...

suodata_laskut()\nsuodata_laskut()\nsuodata_laskut()\nsuodata_laskut()

...ska ge likadant slutresultat.

Skapa funktionen spara_person(person: tuple) som får som argument en tuple som representerar en person. Tuplen innehåller följande information:

    \n
  1. Namn (sträng)
  2. \n
  3. Ålder (heltal)
  4. \n
  5. Längd (flyttal)
  6. \n

Spara informationen i filen personer.csv, efter befintliga data. Formatet ska vara namn;ålder;längd. En person är alltså lika med en rad. Om tuplen är (\"Sara Simonsson\", 37, 175.5) skulle programmet skriva följande rad i slutet av filen:

Sara Simonsson;37;175.5

Behandla data i CSV-format

Låt oss skapa ett program som utvärderar elevernas framgång under en kurs. Programmet läser en CSV-fil som innehåller de veckovisa poängen som den studerande fått under kursens lopp. Programmet räknar ihop poängen och utvärderar därefter vilket vitsord de räcker till. Till slut skapar programmet en CSV-fil som innehåller poängsumman och vitsordet för varje elev.

CSV-filen som vi har tillgång till när vi börjar ser ut så här:

Pärla;4;2;3;5;4;0;0\nPaula;7;2;8;3;5;4;5\nPirjo;3;4;3;5;3;4;4\nEmilia;6;6;5;5;0;4;8

Programmets logik är uppdelad i tre funktioner: läsande av filen och behandlande av innehållet, vitsordsberäkning och skrivande av en ny fil.

Filens inläsning sker enligt det vi lärt oss i den förra delen. Data lagras i ett lexikon där nyckeln är elevens namn och värdet är en lista på poängen som eleven i fråga har fått, i heltalsform:

def las_veckopoang(filnamn):\n    veckopoang = {}\n    with open(filnamn) as fil:\n        for rad in fil:\n            delar = rad.split(\";\")\n            lista = []\n            for poang in delar[1:]:\n                lista.append(int(poang))\n            veckopoang[delar[0]] = lista\n\n    return veckopoang

Den andra funktionen beräknar vilket vitsord ett visst antal poäng räcker till. Funktionen används av den tredje funktionen, som skriver resultaten i en fil:

def vitsord(poang):\n    if poang < 20:\n        return 0\n    elif poang < 25:\n        return 1\n    elif poang < 30:\n        return 2\n    elif poang < 35:\n        return 3\n    elif poang < 40:\n        return 4\n    else:\n        return 5\n\ndef spara_resultat(filnamn, veckopoang):\n    with open(filnamn, \"w\") as fil:\n        for namn, lista in veckopoang.items():\n            summa = sum(lista)\n            fil.write(f\"{namn};{summa};{vitsord(summa)}\\n\")

Strukturen tillåter oss skapa en mycket enkel huvudfunktion. Observera att filnamnen ges som argument i huvudfunktionen:

veckopoang = las_veckopoang(\"veckopoang.csv\")\nspara_resultat(\"resultat.csv\", veckopoang)

När huvudfunktionen körs, skapas filen resultat.csv med följande innehåll:

Pärla;18;0\nPaula;34;3\nPirjo;26;2\nEmilia;41;5

Notera att alla funktioner ovan är relativt enkla till sin funktionalitet – de har en uppgift var. Det här är ett vanligt och rekommenderat sätt att skapa större helheter när man programmerar. När en funktion har en uppgift är det enklare att säkerställa att den fungerar på önskat sätt. Det här gör det också enklare att senare ändra på programmet och lägga till ny funktionalitet.

Om vi till exempel vill ha en funktion för att skriva ut vitsordet för enskild studerande, kan vi skapa en ny funktion som använder sig av en befintlig funktion i koden:

def hamta_vitsord(person, veckopoang):\n    for namn, lista in veckopoang.items():\n        if namn == person:\n            return vitsord(sum(lista))\n\n\nveckopoang = las_veckopoang(\"veckopoang.csv\")\nprint(hamta_vitsord(\"Paula\", veckopoang))

3

Om vi märker att någon funktionalitet i programmet kräver åtgärdande, kommer ändringar inte att orsaka följder överallt i koden. Om vi till exempel vill ändra på vitsordsgränserna, behöver vi bara ändra på funktionen som räknar ut vitsordet – alla andra funktioner som använder den här funktionen skulle fortfarande fungera, med de nya gränserna. Om koden för den här funktionaliteten skulle vara splittrad, finns det en risk att vi glömmer att uppdatera koden på något ställe. Det här skulle antagligen orsaka problem.

Vi utvecklar ännu lite programmet som genererar kursresultat.

För tillfället läses studerandenas namn, uppgifts- och provpoäng från respektive filer. Vi ändrar på programmet så att också kursens namn och studiepoäng läses in från en fil. Formatet är det följande:

\n\nnamn: Introkurs i programmering\nsp: 5\n

Programmet ska skapa två filer. Filen resultat.txt ska följa det här formatet:

\nIntrokurs i programmering, 5 studiepoäng\n========================================\nnamn                          uppg_ant  uppg_p    prov_p    tot_p     vitsord\npekka peloton                 21        5         9         14        0\njaana javanainen              27        6         11        17        1\nliisa virtanen                35        8         14        22        3\n

Den här delen liknar alltså utskriften från den förra versionen av programmet.

Dessutom ska filen resultat.csv skapas. Dess format ska följa detta exempel:

\n12345678;pekka peloton;0\n12345687;jaana javanainen;1\n12345699;liisa virtanen;3\n

Exempelkörning:

Studerande (CSV): studerande1.csv\nUppgifter (CSV): uppgifter1.csv\nProvpoäng (CSV): provpoang1.csv\nKursinfo (textfil): kurs1.txt\nResultaten lagrade i filerna resultat.txt och resultat.csv

Programmet frågar bara alltså om filnamnen och skriver resultaten i resultatfilerna.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

I den här uppgiften behandlar vi filen ord.txt som innehåller ord på engelska.

Skapa funktionen sok_ord(term: str) som returnerar en lista över matchande ord i filen.

I söktermen kan man, förutom gemener, använda dessa specialtecken:

    \n
  • punkt betyder att vilket som helst tecken accepteras (ca. matchar t.ex. cat och car och .a.e care och late)
  • \n
  • asterisk betyder att ordet kan börja eller avslutas på valfritt vis (ca* matchar t.ex. california och cat, *ane matchar insane och aeroplane) – du kan anta att asterisken endast finns i början eller slutet av ett ord och att söktermen innehåller högst en asterisk
  • \n
  • om söktermen inte innehåller specialtecken ska termen matcha exakt med det sökta ordet.
  • \n

Vi bestämmer också att en sökterm inte kan innehålla båda typer av specialtecken.

Orden är skrivna med gemener. Du kan också anta att argumentet inte innehåller versaler.

Om inga resultat hittas ska funktionen returnera en tom lista.

Tips: Pythons strängmetoder startswith() och endswith() kan vara händiga. Sök på nätet för mera information.

Exempel:

print(sok_ord(\"*vokes\"))

['convokes', 'equivokes', 'evokes', 'invokes', 'provokes', 'reinvokes', 'revokes']

Skapa ett program som fungerar som en ordbok, dit man kan lägga till nya ord eller där man kan söka efter befintliga ord.

Programmet ska fungera så här:

1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 1\nAnge ord på svenska: bil\nAnge ord på engelska: car\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 1\nAnge ord på svenska: sopa\nAnge ord på engelska: garbage\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 1\nAnge ord på svenska: väska\nAnge ord på engelska: bag\nOrdpar tillagt\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 2\nAnge ord: bag\nsopa - garbage\nväska - bag\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 2\nAnge ord: car\nbil - car\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 2\nAnge ord: väska\nväska - bag\n1 – lägg till ord, 2 – sök ord, 3 – avsluta\nVal: 3\nHejdå!

Orden ska lagras i filen ordbok.txt. Programmet ska läsa in den här filen när det startar. Nya ordpar läggs till genast.

Du kan själv bestämma i vilket format data lagras.

Observera att körande av de lokala testen kan tömma ordboksfilen.

Obs! I dessa uppgifter ska kod inte placeras i if __name__ == \"__main__\" -blocket, om du inte ombeds göra det.

","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/2-skriva-filer.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-6/3-fel/page-data.json b/page-data/osa-6/3-fel/page-data.json index 8dd5441d6..3fe046d8a 100644 --- a/page-data/osa-6/3-fel/page-data.json +++ b/page-data/osa-6/3-fel/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-6/3-fel", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur du kan behandla icke-valida indata"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad som menas med undantag i programmeringskontext"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till de vanligaste typerna av undantag i Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du behandla undantag i dina egna program."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns två grundläggande kategorier av fel som uppkommer i program"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"fel i syntax, som förhindrar körandet av ett program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"fel som sker medan programmet körs, och stoppar programmet"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel i den första kategorin är vanligtvis enkla att korrigera eftersom Pythontolken berättar var felet ligger när man försöker köra programmet. Vanliga syntaxfel beror på kolon som fattas på någon inledande rad eller ett citattecken som fattas i slutet av en sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel i den andra kategorin är ibland svårare att hitta, eftersom de endast sker vid något visst ställe i programmet i vissa situationer. Programmet kanske fungerar helt bra i de flesta situationer, men så finns det någon marginell situation då programmet stannar upp på grund av ett fel. Nu ska vi se hur vi kan behandla de här felen."}]},{"type":"element","tagName":"h2","properties":{"id":"validering-av-indata","style":"position:relative;"},"children":[{"type":"text","value":"Validering av indata"},{"type":"element","tagName":"a","properties":{"href":"#validering-av-indata","ariaLabel":"validering av indata permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flera fel som uppkommer då ett program körs beror på indata som är i fel format på något sätt. Här är några exempel:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"obligatoriska fält som fattas eller är tomma, till exempel tomma strängar i fall där strängens längd har betydelse"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"negativa värden i fall där endast positiva värden är giltiga, till exempel -15 st. av en ingrediens i ett recept"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"filer som saknas eller som har skrivfel i sina namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"värden som är för stora eller för små, till exempel då vi arbetar med datum och klockslag"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"icke-giltiga index, till exempel om vi försöker komma åt index 3 i strängen \"hej\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"värden av fel typ, till exempel en sträng då vi förväntar oss ett heltal."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till all lycka kan vi som programmerare förbereda oss för de flesta felen. Vi ser på ett program som frågar efter användarens ålder och kollar att siffran är giltig (mellan noll och 150):"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange din ålder: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"150"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Acceptabel ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Felaktig ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din ålder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\nAcceptabel ålder"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din ålder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nFelaktig ålder"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så länge användaren ger ett heltal verkar valideringen fungera som den ska. Men om användaren ger en sträng?"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din ålder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"tjugotre"}]},{"type":"text","value":"\nValueError: invalid literal for int() with base 10: 'tjugotre'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" kan inte behandla strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tjugotre"}]},{"type":"text","value":" som ett heltal. Programmet stoppas och ett felmeddelande skrivs ut."}]},{"type":"element","tagName":"h2","properties":{"id":"undantag","style":"position:relative;"},"children":[{"type":"text","value":"Undantag"},{"type":"element","tagName":"a","properties":{"href":"#undantag","ariaLabel":"undantag permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel som sker när programmet redan körs kallas undantag (exception). Det är möjligt att förbereda sig för undantag, så att programmet fortsätter köra även om undantag sker."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Undantag behandlas i Python med satserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":". Idén är att om något inom "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"-blocket orsakar ett undantag, kommer Python söka efter ett motsvarande "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-block och köra koden under det blocket – och så fortsätter programmet att köra som normalt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ändra på exemplet ovan så att programmet är förberett för undantag av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange din ålder: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"150"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Acceptabel ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Felaktig ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din ålder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"tjugotre"}]},{"type":"text","value":"\nFelaktig ålder"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"-blocket för att markera att koden inom blocket möjligtvis kan orsaka ett fel. I "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-satsen som direkt följer blocket nämns det felet vi kan förvänta oss. I exemplet ovan nämnde vi bara "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantaget. Om något annat fel skulle ha uppstått, skulle programmet ändå ha avslutats oavsett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-blocken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan, om ett fel sker, tilldelas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alder"}]},{"type":"text","value":" värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":". Det här är ett icke-giltigt värde som programmet redan sedan tidigare kan reagera på, eftersom programmet förväntar sig ett värde mellan noll och 150."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det följande exemplet har vi funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"las_heltal"}]},{"type":"text","value":" som ber användaren att ge ett heltal. Funktionen är också redo för icke-giltiga värden och fortsätter att fråga efter ett heltal tills ett giltigt värde har angetts."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_heltal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange heltal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Felaktiga indata\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_heltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tack!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"upphöjt till tre är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kolme"}]},{"type":"text","value":"\nFelaktiga indata\nAnge heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aybabtu"}]},{"type":"text","value":"\nFelaktiga indata\nAnge heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTack!\n5 upphöjt till tre är 125"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland räcker det med att fånga undantag med en try-except-struktur utan att göra något speciellt därtill. Vi kan alltså ignorera situationen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi ändrar på exemplet ovan så att vi endast accepterar heltal som är under 100, skulle det kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_litet_heltal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange heltal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffra\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# den här instruktionen gör inget"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Felaktiga indata\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_litet_heltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"upphöjt till tre är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kolme"}]},{"type":"text","value":"\nFelaktiga indata\nAnge heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1000"}]},{"type":"text","value":"\nFelaktiga indata\nAnge heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTack!\n5 upphöjt till tre är 125"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu innehåller except-blocket endast instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":", som inte gör något. Python tillåter inte tomma block, så instruktionen är nödvändig."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Läsa indata","tmcname":"osa06-17_lasa_indata"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"las"}]},{"type":"text","value":" som ber användaren ange ett tal, tills användaren anger ett tal som är inom intervallet som angivits. Funktionen ska returnera talet användaren gett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du gav siffran:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"seitsemän"}]},{"type":"text","value":"\nSiffran ska vara ett heltal i intervallet 5-10\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nSiffran ska vara ett heltal i intervallet 5-10\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nDu gav siffran: 8"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"vanliga-fel","style":"position:relative;"},"children":[{"type":"text","value":"Vanliga fel"},{"type":"element","tagName":"a","properties":{"href":"#vanliga-fel","ariaLabel":"vanliga fel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är en samling av vanliga fel som du sannolikt kommer att stöta på. Vi kollar också i hurdana situationer dessa kan uppstå."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ValueError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet uppstår vanligtvis då ett argument som ges till en funktion på något sätt är ogiltigt. Till exempel anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float(\"1,23\")"}]},{"type":"text","value":" orsakar ett fel eftersom decimaler alltid skiljs åt med punkt i Python."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"TypeError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet uppstår då ett värde har fel typ. Om vi till exempel anropar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(10)"}]},{"type":"text","value":" får vi ett fel, eftersom funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" kräver ett värde vars längd kan räknas – till exempel en sträng eller lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"IndexError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet uppstår då vi försöker hänvisa till ett index som inte finns. Till exempel uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abc\"[5]"}]},{"type":"text","value":" orsakar ett fel eftersom strängen i fråga inte har indexet 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ZeroDivisionError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet uppstår då man försöker dividera med noll. Om vi till exempel försöker ta reda på medeltalet av värden i en lista med uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum(min_lista) / len(min_lista)"}]},{"type":"text","value":" och listans längd är noll, kommer vi att få ett fel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Undantag när filer behandlas"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Några vanliga undantag som kan uppstå när filer behandlas är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileNotFoundError"}]},{"type":"text","value":" (filen som man försöker komma åt finns inte), "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"io.UnsupportedOperation"}]},{"type":"text","value":" (man försöker göra något åt filen som inte tillåts i det läge filen öppnats i) och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PermissionError"}]},{"type":"text","value":" (programmet har inte åtkomst till filen)."}]},{"type":"element","tagName":"h2","properties":{"id":"behandla-flera-undantag-samtidigt","style":"position:relative;"},"children":[{"type":"text","value":"Behandla flera undantag samtidigt"},{"type":"element","tagName":"a","properties":{"href":"#behandla-flera-undantag-samtidigt","ariaLabel":"behandla flera undantag samtidigt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"-block kan följas av flera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-block. Det här programmet kan exempelvis behandla situationer där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileNotFoundException"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PermissionError"}]},{"type":"text","value":"-undantagen uppkommer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" FileNotFoundError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Filen exempel.txt hittades ej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" PermissionError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Har inte åtkomst till filen exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland är det inte nödvändigt att specificera det fel som programmet förbereder sig för. Speciellt då man arbetar med filer, räcker det med att veta att något fel har skett och därmed avsluta programmet på ett säkert sätt. Man behöver inte alltid veta varför ett fel har uppstått. Om vi vill vara förberedda på alla möjliga undantag kan vi använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-blocket utan att specificera felet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel i läsandet av filen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Den här "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-satsen körs nu i alla möjliga felsituationer – det här gäller också misstag som programmeraren gjort. Endast syntaxfel kommer att orsaka fel som förhindrar programmet från att köras – detta eftersom den här typen av fel inte låter koden köras över huvud taget."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel det här programmet kommer alltid att ge ett fel, eftersom variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fil"}]},{"type":"text","value":" har skrivits fel på den tredje raden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fli"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel i läsandet av filen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-block kan gömma bakomliggande fel. Problemet här berodde inte på hur filer behandlades utan på en variabel med inkorrekt namn. Utan ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-block skulle vi kunna se vilket fel som orsakats och kunde därmed hitta källan till felet enklare. Därför lönar det sig att endast använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-blocket för fördefinierade specifika typer av fel."}]},{"type":"element","tagName":"h2","properties":{"id":"formedling-av-undantag","style":"position:relative;"},"children":[{"type":"text","value":"Förmedling av undantag"},{"type":"element","tagName":"a","properties":{"href":"#formedling-av-undantag","ariaLabel":"formedling av undantag permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om körandet av en funktion orsakar ett undantag som inte behandlas kommer undantaget att förmedlas till koden som anropat koden och fortsätta kliva upp till huvudfunktionen. Om felet inte här heller behandlas, kommer programmet att stoppas och undantaget skrivs ut för användaren att se."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det följande exemplet har vi funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test"}]},{"type":"text","value":". Om den orsakar ett undantag, kommer det inte att behandlas inne i funktionen utan i huvudfunktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n test"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Något gick fel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"tre"}]},{"type":"text","value":"\nNågot gick fel"}]}]},{"type":"element","tagName":"h2","properties":{"id":"astadkomma-undantag","style":"position:relative;"},"children":[{"type":"text","value":"Åstadkomma undantag"},{"type":"element","tagName":"a","properties":{"href":"#astadkomma-undantag","ariaLabel":"astadkomma undantag permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan åstadkomma undantag med instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":". Det kan verka som en konstig idé att själv åstadkomma fel i ditt program, men det kan vara nyttigt i olika situationer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det kan till exempel löna sig att åstadkomma ett fel när man märker icke-valida parametrar. Hittills har vi skrivit ut meddelanden när vi har validerat indata, men om vi gör en funktion som ska köras från något annat ställe så kan det hända att en utskrift inte noteras när funktionen anropas. Att åstadkomma ett fel kan göra debuggande enklare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det följande exempel har vi en funktion som räknar ut fakulteten av en siffra (t.ex. för siffran fem är fakulteten 1 * 2 * 3 * 4 * 5). Om argumentet som ges till funktionen är negativt, kommer ett fel att åstadkommas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Negativt värde: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" i\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"6\n720\nTraceback (most recent call last):\nFile \"testi.py\", line 11, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nprint(fakultet(-1))\nFile \"testi.py\", line 3, in fakultet\nraise ValueError(\"Negativt värde: \" + str(n))\nValueError: Negativt värde: -1"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Validera givna argument","tmcname":"osa06-18_validera_argument"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_person(namn: str, alder: int)"}]},{"type":"text","value":" som skapar och returnerar en tuple som representerar en person. Det första elementet är ett namn och det andra en ålder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om de givna argumenten är felaktiga, ska funktionen orsaka ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Felaktiga argument är:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tom sträng som namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett namn som inte består av minst två ord"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett namn som är längre än 40 tecken"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en negativ ålder"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en ålder som överstiger 150"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Felaktiga lottorader","tmcname":"osa06-19_lottorader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lottorader.csv"}]},{"type":"text","value":" har man lagrat lottorader enligt följande exempel:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 1;5,7,11,13,23,24,30\nvecka 2;9,13,14,24,34,35,37\no.s.v. ..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först ska rubriken "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vecka x"}]},{"type":"text","value":" komma, följt av sju siffror i intervallet 1-39."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filen är dock delvis skadad. Här följer exempel på felaktiga rader."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Veckonummer felaktigt:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka zzc;1,5,13,22,24,25,26"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett eller flera felaktiga nummer:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 22;1,**,5,6,13,2b,34"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel antal nummer:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 13;4,6,17,19,24,33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För små eller stora nummer:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 39;5,9,15,35,39,41,105"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma nummer förekommer flera gånger på samma rad:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 41;5,12,3,35,12,14,36"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filtrera_felaktiga()"}]},{"type":"text","value":" som skapar filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"korrigerade_rader.csv"}]},{"type":"text","value":". Filen ska innehålla de korrekta raderna från den ursprungliga filen."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du hur du kan behandla icke-valida indata
  • \n
  • vet du vad som menas med undantag i programmeringskontext
  • \n
  • känner du till de vanligaste typerna av undantag i Python
  • \n
  • kan du behandla undantag i dina egna program.
  • \n

Det finns två grundläggande kategorier av fel som uppkommer i program

    \n
  • fel i syntax, som förhindrar körandet av ett program
  • \n
  • fel som sker medan programmet körs, och stoppar programmet
  • \n

Fel i den första kategorin är vanligtvis enkla att korrigera eftersom Pythontolken berättar var felet ligger när man försöker köra programmet. Vanliga syntaxfel beror på kolon som fattas på någon inledande rad eller ett citattecken som fattas i slutet av en sträng.

Fel i den andra kategorin är ibland svårare att hitta, eftersom de endast sker vid något visst ställe i programmet i vissa situationer. Programmet kanske fungerar helt bra i de flesta situationer, men så finns det någon marginell situation då programmet stannar upp på grund av ett fel. Nu ska vi se hur vi kan behandla de här felen.

Validering av indata

Flera fel som uppkommer då ett program körs beror på indata som är i fel format på något sätt. Här är några exempel:

    \n
  • obligatoriska fält som fattas eller är tomma, till exempel tomma strängar i fall där strängens längd har betydelse
  • \n
  • negativa värden i fall där endast positiva värden är giltiga, till exempel -15 st. av en ingrediens i ett recept
  • \n
  • filer som saknas eller som har skrivfel i sina namn
  • \n
  • värden som är för stora eller för små, till exempel då vi arbetar med datum och klockslag
  • \n
  • icke-giltiga index, till exempel om vi försöker komma åt index 3 i strängen \"hej\"
  • \n
  • värden av fel typ, till exempel en sträng då vi förväntar oss ett heltal.
  • \n

Till all lycka kan vi som programmerare förbereda oss för de flesta felen. Vi ser på ett program som frågar efter användarens ålder och kollar att siffran är giltig (mellan noll och 150):

alder = int(input(\"Ange din ålder: \"))\nif alder >= 0 and alder <= 150:\n    print(\"Acceptabel ålder\")\nelse:\n    print(\"Felaktig ålder\")

Ange din ålder: 25\nAcceptabel ålder

Ange din ålder: -3\nFelaktig ålder

Så länge användaren ger ett heltal verkar valideringen fungera som den ska. Men om användaren ger en sträng?

Ange din ålder: tjugotre\nValueError: invalid literal for int() with base 10: 'tjugotre'

Funktionen int kan inte behandla strängen tjugotre som ett heltal. Programmet stoppas och ett felmeddelande skrivs ut.

Undantag

Fel som sker när programmet redan körs kallas undantag (exception). Det är möjligt att förbereda sig för undantag, så att programmet fortsätter köra även om undantag sker.

Undantag behandlas i Python med satserna try och except. Idén är att om något inom try-blocket orsakar ett undantag, kommer Python söka efter ett motsvarande except-block och köra koden under det blocket – och så fortsätter programmet att köra som normalt.

Låt oss ändra på exemplet ovan så att programmet är förberett för undantag av typen ValueError:

try:\n    alder = int(input(\"Ange din ålder: \"))\nexcept ValueError:\n    alder = -1\n\nif alder >= 0 and alder <= 150:\n    print(\"Acceptabel ålder\")\nelse:\n    print(\"Felaktig ålder\")

Ange din ålder: tjugotre\nFelaktig ålder

Vi kan använda try-blocket för att markera att koden inom blocket möjligtvis kan orsaka ett fel. I except-satsen som direkt följer blocket nämns det felet vi kan förvänta oss. I exemplet ovan nämnde vi bara ValueError-undantaget. Om något annat fel skulle ha uppstått, skulle programmet ändå ha avslutats oavsett try- och except-blocken.

I exemplet ovan, om ett fel sker, tilldelas alder värdet -1. Det här är ett icke-giltigt värde som programmet redan sedan tidigare kan reagera på, eftersom programmet förväntar sig ett värde mellan noll och 150.

I det följande exemplet har vi funktionen las_heltal som ber användaren att ge ett heltal. Funktionen är också redo för icke-giltiga värden och fortsätter att fråga efter ett heltal tills ett giltigt värde har angetts.

def las_heltal():\n    while True:\n        try:\n            indata = input(\"Ange heltal: \")\n            return int(indata)\n        except ValueError:\n            print(\"Felaktiga indata\")\n\nsiffra = las_heltal()\nprint(\"Tack!\")\nprint(siffra, \"upphöjt till tre är\", siffra**3)

Ange heltal: kolme\nFelaktiga indata\nAnge heltal: aybabtu\nFelaktiga indata\nAnge heltal: 5\nTack!\n5 upphöjt till tre är 125

Ibland räcker det med att fånga undantag med en try-except-struktur utan att göra något speciellt därtill. Vi kan alltså ignorera situationen i except-blocket.

Om vi ändrar på exemplet ovan så att vi endast accepterar heltal som är under 100, skulle det kunna se ut så här:

def las_litet_heltal():\n    while True:\n        try:\n            indata = input(\"Ange heltal: \")\n            siffra = int(indata)\n            if siffra < 100:\n                return siffra\n        except ValueError:\n            pass # den här instruktionen gör inget\n\n        print(\"Felaktiga indata\")\n\nsiffra = las_litet_heltal()\nprint(siffra, \"upphöjt till tre är\", siffra**3)

Ange heltal: kolme\nFelaktiga indata\nAnge heltal: 1000\nFelaktiga indata\nAnge heltal: 5\nTack!\n5 upphöjt till tre är 125

Nu innehåller except-blocket endast instruktionen pass, som inte gör något. Python tillåter inte tomma block, så instruktionen är nödvändig.

Skapa funktionen las som ber användaren ange ett tal, tills användaren anger ett tal som är inom intervallet som angivits. Funktionen ska returnera talet användaren gett.

Exempel:

siffra = las(\"Ange siffra: \", 5, 10)\nprint(\"Du gav siffran:\", siffra)

Ange siffra: seitsemän\nSiffran ska vara ett heltal i intervallet 5-10\nAnge siffra: -3\nSiffran ska vara ett heltal i intervallet 5-10\nAnge siffra: 8\nDu gav siffran: 8

Vanliga fel

Här är en samling av vanliga fel som du sannolikt kommer att stöta på. Vi kollar också i hurdana situationer dessa kan uppstå.

ValueError

Det här felet uppstår vanligtvis då ett argument som ges till en funktion på något sätt är ogiltigt. Till exempel anropet float(\"1,23\") orsakar ett fel eftersom decimaler alltid skiljs åt med punkt i Python.

TypeError

Det här felet uppstår då ett värde har fel typ. Om vi till exempel anropar len(10) får vi ett fel, eftersom funktionen len kräver ett värde vars längd kan räknas – till exempel en sträng eller lista.

IndexError

Det här felet uppstår då vi försöker hänvisa till ett index som inte finns. Till exempel uttrycket \"abc\"[5] orsakar ett fel eftersom strängen i fråga inte har indexet 5.

ZeroDivisionError

Det här felet uppstår då man försöker dividera med noll. Om vi till exempel försöker ta reda på medeltalet av värden i en lista med uttrycket sum(min_lista) / len(min_lista) och listans längd är noll, kommer vi att få ett fel.

Undantag när filer behandlas

Några vanliga undantag som kan uppstå när filer behandlas är FileNotFoundError (filen som man försöker komma åt finns inte), io.UnsupportedOperation (man försöker göra något åt filen som inte tillåts i det läge filen öppnats i) och PermissionError (programmet har inte åtkomst till filen).

Behandla flera undantag samtidigt

Ett try-block kan följas av flera except-block. Det här programmet kan exempelvis behandla situationer där FileNotFoundException- och PermissionError-undantagen uppkommer:

try:\n    with open(\"exempel.txt\") as fil:\n        for rad in fil:\n            print(rad)\nexcept FileNotFoundError:\n    print(\"Filen exempel.txt hittades ej\")\nexcept PermissionError:\n    print(\"Har inte åtkomst till filen exempel.txt\")

Ibland är det inte nödvändigt att specificera det fel som programmet förbereder sig för. Speciellt då man arbetar med filer, räcker det med att veta att något fel har skett och därmed avsluta programmet på ett säkert sätt. Man behöver inte alltid veta varför ett fel har uppstått. Om vi vill vara förberedda på alla möjliga undantag kan vi använda except-blocket utan att specificera felet:

try:\n    with open(\"exempel.txt\") as fil:\n        for rad in fil:\n            print(rad)\nexcept:\n    print(\"Fel i läsandet av filen\")

Obs! Den här except-satsen körs nu i alla möjliga felsituationer – det här gäller också misstag som programmeraren gjort. Endast syntaxfel kommer att orsaka fel som förhindrar programmet från att köras – detta eftersom den här typen av fel inte låter koden köras över huvud taget.

Till exempel det här programmet kommer alltid att ge ett fel, eftersom variabelnamnet fil har skrivits fel på den tredje raden:

try:\n    with open(\"exempel.txt\") as fil:\n        for rad in fli:\n            print(rad)\nexcept:\n    print(\"Fel i läsandet av filen\")

Ett except-block kan gömma bakomliggande fel. Problemet här berodde inte på hur filer behandlades utan på en variabel med inkorrekt namn. Utan ett except-block skulle vi kunna se vilket fel som orsakats och kunde därmed hitta källan till felet enklare. Därför lönar det sig att endast använda except-blocket för fördefinierade specifika typer av fel.

Förmedling av undantag

Om körandet av en funktion orsakar ett undantag som inte behandlas kommer undantaget att förmedlas till koden som anropat koden och fortsätta kliva upp till huvudfunktionen. Om felet inte här heller behandlas, kommer programmet att stoppas och undantaget skrivs ut för användaren att se.

I det följande exemplet har vi funktionen test. Om den orsakar ett undantag, kommer det inte att behandlas inne i funktionen utan i huvudfunktionen:

def test(x):\n    print(int(x) + 1)\n\ntry:\n    siffra = input(\"Ange siffra: \")\n    test(siffra)\nexcept:\n    print(\"Något gick fel\")

Ange siffra: tre\nNågot gick fel

Åstadkomma undantag

Du kan åstadkomma undantag med instruktionen raise. Det kan verka som en konstig idé att själv åstadkomma fel i ditt program, men det kan vara nyttigt i olika situationer.

Det kan till exempel löna sig att åstadkomma ett fel när man märker icke-valida parametrar. Hittills har vi skrivit ut meddelanden när vi har validerat indata, men om vi gör en funktion som ska köras från något annat ställe så kan det hända att en utskrift inte noteras när funktionen anropas. Att åstadkomma ett fel kan göra debuggande enklare.

I det följande exempel har vi en funktion som räknar ut fakulteten av en siffra (t.ex. för siffran fem är fakulteten 1 * 2 * 3 * 4 * 5). Om argumentet som ges till funktionen är negativt, kommer ett fel att åstadkommas:

def fakultet(n):\n    if n < 0:\n        raise ValueError(\"Negativt värde: \" + str(n))\n    k = 1\n    for i in range(2, n + 1):\n        k *= i\n    return k\n\nprint(fakultet(3))\nprint(fakultet(6))\nprint(fakultet(-1))
6\n720\nTraceback (most recent call last):\nFile \"testi.py\", line 11, in \nprint(fakultet(-1))\nFile \"testi.py\", line 3, in fakultet\nraise ValueError(\"Negativt värde: \" + str(n))\nValueError: Negativt värde: -1

Skapa funktionen ny_person(namn: str, alder: int) som skapar och returnerar en tuple som representerar en person. Det första elementet är ett namn och det andra en ålder.

Om de givna argumenten är felaktiga, ska funktionen orsaka ett ValueError-undantag.

Felaktiga argument är:

    \n
  • tom sträng som namn
  • \n
  • ett namn som inte består av minst två ord
  • \n
  • ett namn som är längre än 40 tecken
  • \n
  • en negativ ålder
  • \n
  • en ålder som överstiger 150
  • \n

I filen lottorader.csv har man lagrat lottorader enligt följande exempel:

vecka 1;5,7,11,13,23,24,30\nvecka 2;9,13,14,24,34,35,37\no.s.v. ...

Först ska rubriken vecka x komma, följt av sju siffror i intervallet 1-39.

Filen är dock delvis skadad. Här följer exempel på felaktiga rader.

Veckonummer felaktigt:

vecka zzc;1,5,13,22,24,25,26

Ett eller flera felaktiga nummer:

vecka 22;1,**,5,6,13,2b,34

Fel antal nummer:

vecka 13;4,6,17,19,24,33

För små eller stora nummer:

vecka 39;5,9,15,35,39,41,105

Samma nummer förekommer flera gånger på samma rad:

vecka 41;5,12,3,35,12,14,36

Skapa funktionen filtrera_felaktiga() som skapar filen korrigerade_rader.csv. Filen ska innehålla de korrekta raderna från den ursprungliga filen.

","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/3-fel.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur du kan behandla icke-valida indata"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad som menas med undantag i programmeringskontext"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till de vanligaste typerna av undantag i Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du behandla undantag i dina egna program."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns två grundläggande kategorier av fel som uppkommer i program"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"fel i syntax, som förhindrar körandet av ett program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"fel som sker medan programmet körs, och stoppar programmet"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel i den första kategorin är vanligtvis enkla att korrigera eftersom Pythontolken berättar var felet ligger när man försöker köra programmet. Vanliga syntaxfel beror på kolon som fattas på någon inledande rad eller ett citattecken som fattas i slutet av en sträng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel i den andra kategorin är ibland svårare att hitta, eftersom de endast sker vid något visst ställe i programmet i vissa situationer. Programmet kanske fungerar helt bra i de flesta situationer, men så finns det någon marginell situation då programmet stannar upp på grund av ett fel. Nu ska vi se hur vi kan behandla de här felen."}]},{"type":"element","tagName":"h2","properties":{"id":"validering-av-indata","style":"position:relative;"},"children":[{"type":"text","value":"Validering av indata"},{"type":"element","tagName":"a","properties":{"href":"#validering-av-indata","ariaLabel":"validering av indata permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Flera fel som uppkommer då ett program körs beror på indata som är i fel format på något sätt. Här är några exempel:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"obligatoriska fält som fattas eller är tomma, till exempel tomma strängar i fall där strängens längd har betydelse"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"negativa värden i fall där endast positiva värden är giltiga, till exempel -15 st. av en ingrediens i ett recept"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"filer som saknas eller som har skrivfel i sina namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"värden som är för stora eller för små, till exempel då vi arbetar med datum och klockslag"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"icke-giltiga index, till exempel om vi försöker komma åt index 3 i strängen \"hej\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"värden av fel typ, till exempel en sträng då vi förväntar oss ett heltal."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till all lycka kan vi som programmerare förbereda oss för de flesta felen. Vi ser på ett program som frågar efter användarens ålder och kollar att siffran är giltig (mellan noll och 150):"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange din ålder: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"150"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Acceptabel ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Felaktig ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din ålder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\nAcceptabel ålder"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din ålder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nFelaktig ålder"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så länge användaren ger ett heltal verkar valideringen fungera som den ska. Men om användaren ger en sträng?"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din ålder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"tjugotre"}]},{"type":"text","value":"\nValueError: invalid literal for int() with base 10: 'tjugotre'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" kan inte behandla strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tjugotre"}]},{"type":"text","value":" som ett heltal. Programmet stoppas och ett felmeddelande skrivs ut."}]},{"type":"element","tagName":"h2","properties":{"id":"undantag","style":"position:relative;"},"children":[{"type":"text","value":"Undantag"},{"type":"element","tagName":"a","properties":{"href":"#undantag","ariaLabel":"undantag permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel som sker när programmet redan körs kallas undantag (exception). Det är möjligt att förbereda sig för undantag, så att programmet fortsätter köra även om undantag sker."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Undantag behandlas i Python med satserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":". Idén är att om något inom "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"-blocket orsakar ett undantag, kommer Python söka efter ett motsvarande "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-block och köra koden under det blocket – och så fortsätter programmet att köra som normalt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ändra på exemplet ovan så att programmet är förberett för undantag av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange din ålder: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" alder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"150"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Acceptabel ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Felaktig ålder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din ålder: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"tjugotre"}]},{"type":"text","value":"\nFelaktig ålder"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"-blocket för att markera att koden inom blocket möjligtvis kan orsaka ett fel. I "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-satsen som direkt följer blocket nämns det felet vi kan förvänta oss. I exemplet ovan nämnde vi bara "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantaget. Om något annat fel skulle ha uppstått, skulle programmet ändå ha avslutats oavsett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-blocken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan, om ett fel sker, tilldelas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"alder"}]},{"type":"text","value":" värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":". Det här är ett icke-giltigt värde som programmet redan sedan tidigare kan reagera på, eftersom programmet förväntar sig ett värde mellan noll och 150."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det följande exemplet har vi funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"las_heltal"}]},{"type":"text","value":" som ber användaren att ge ett heltal. Funktionen är också redo för icke-giltiga värden och fortsätter att fråga efter ett heltal tills ett giltigt värde har angetts."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_heltal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange heltal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Felaktiga indata\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_heltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tack!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"upphöjt till tre är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kolme"}]},{"type":"text","value":"\nFelaktiga indata\nAnge heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aybabtu"}]},{"type":"text","value":"\nFelaktiga indata\nAnge heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTack!\n5 upphöjt till tre är 125"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland räcker det med att fånga undantag med en try-except-struktur utan att göra något speciellt därtill. Vi kan alltså ignorera situationen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-blocket."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi ändrar på exemplet ovan så att vi endast accepterar heltal som är under 100, skulle det kunna se ut så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_litet_heltal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange heltal: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffra\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# den här instruktionen gör inget"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Felaktiga indata\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsiffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_litet_heltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"upphöjt till tre är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kolme"}]},{"type":"text","value":"\nFelaktiga indata\nAnge heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1000"}]},{"type":"text","value":"\nFelaktiga indata\nAnge heltal: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nTack!\n5 upphöjt till tre är 125"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu innehåller except-blocket endast instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":", som inte gör något. Python tillåter inte tomma block, så instruktionen är nödvändig."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Läsa indata","tmcname":"osa06-17_lasa_indata"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"las"}]},{"type":"text","value":" som ber användaren ange ett tal, tills användaren anger ett tal som är inom intervallet som angivits. Funktionen ska returnera talet användaren gett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du gav siffran:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"seitsemän"}]},{"type":"text","value":"\nSiffran ska vara ett heltal i intervallet 5-10\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nSiffran ska vara ett heltal i intervallet 5-10\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nDu gav siffran: 8"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"vanliga-fel","style":"position:relative;"},"children":[{"type":"text","value":"Vanliga fel"},{"type":"element","tagName":"a","properties":{"href":"#vanliga-fel","ariaLabel":"vanliga fel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är en samling av vanliga fel som du sannolikt kommer att stöta på. Vi kollar också i hurdana situationer dessa kan uppstå."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ValueError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet uppstår vanligtvis då ett argument som ges till en funktion på något sätt är ogiltigt. Till exempel anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float(\"1,23\")"}]},{"type":"text","value":" orsakar ett fel eftersom decimaler alltid skiljs åt med punkt i Python."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"TypeError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet uppstår då ett värde har fel typ. Om vi till exempel anropar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(10)"}]},{"type":"text","value":" får vi ett fel, eftersom funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" kräver ett värde vars längd kan räknas – till exempel en sträng eller lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"IndexError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet uppstår då vi försöker hänvisa till ett index som inte finns. Till exempel uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abc\"[5]"}]},{"type":"text","value":" orsakar ett fel eftersom strängen i fråga inte har indexet 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ZeroDivisionError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här felet uppstår då man försöker dividera med noll. Om vi till exempel försöker ta reda på medeltalet av värden i en lista med uttrycket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum(min_lista) / len(min_lista)"}]},{"type":"text","value":" och listans längd är noll, kommer vi att få ett fel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Undantag när filer behandlas"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Några vanliga undantag som kan uppstå när filer behandlas är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileNotFoundError"}]},{"type":"text","value":" (filen som man försöker komma åt finns inte), "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"io.UnsupportedOperation"}]},{"type":"text","value":" (man försöker göra något åt filen som inte tillåts i det läge filen öppnats i) och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PermissionError"}]},{"type":"text","value":" (programmet har inte åtkomst till filen)."}]},{"type":"element","tagName":"h2","properties":{"id":"behandla-flera-undantag-samtidigt","style":"position:relative;"},"children":[{"type":"text","value":"Behandla flera undantag samtidigt"},{"type":"element","tagName":"a","properties":{"href":"#behandla-flera-undantag-samtidigt","ariaLabel":"behandla flera undantag samtidigt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":"-block kan följas av flera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-block. Det här programmet kan exempelvis behandla situationer där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileNotFoundException"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PermissionError"}]},{"type":"text","value":"-undantagen uppkommer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" FileNotFoundError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Filen exempel.txt hittades ej\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" PermissionError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Har inte åtkomst till filen exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ibland är det inte nödvändigt att specificera det fel som programmet förbereder sig för. Speciellt då man arbetar med filer, räcker det med att veta att något fel har skett och därmed avsluta programmet på ett säkert sätt. Man behöver inte alltid veta varför ett fel har uppstått. Om vi vill vara förberedda på alla möjliga undantag kan vi använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-blocket utan att specificera felet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel i läsandet av filen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Den här "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-satsen körs nu i alla möjliga felsituationer – det här gäller också misstag som programmeraren gjort. Endast syntaxfel kommer att orsaka fel som förhindrar programmet från att köras – detta eftersom den här typen av fel inte låter koden köras över huvud taget."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel det här programmet kommer alltid att ge ett fel, eftersom variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fil"}]},{"type":"text","value":" har skrivits fel på den tredje raden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exempel.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fli"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fel i läsandet av filen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-block kan gömma bakomliggande fel. Problemet här berodde inte på hur filer behandlades utan på en variabel med inkorrekt namn. Utan ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-block skulle vi kunna se vilket fel som orsakats och kunde därmed hitta källan till felet enklare. Därför lönar det sig att endast använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":"-blocket för fördefinierade specifika typer av fel."}]},{"type":"element","tagName":"h2","properties":{"id":"formedling-av-undantag","style":"position:relative;"},"children":[{"type":"text","value":"Förmedling av undantag"},{"type":"element","tagName":"a","properties":{"href":"#formedling-av-undantag","ariaLabel":"formedling av undantag permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om körandet av en funktion orsakar ett undantag som inte behandlas kommer undantaget att förmedlas till koden som anropat koden och fortsätta kliva upp till huvudfunktionen. Om felet inte här heller behandlas, kommer programmet att stoppas och undantaget skrivs ut för användaren att se."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det följande exemplet har vi funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test"}]},{"type":"text","value":". Om den orsakar ett undantag, kommer det inte att behandlas inne i funktionen utan i huvudfunktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n test"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Något gick fel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"tre"}]},{"type":"text","value":"\nNågot gick fel"}]}]},{"type":"element","tagName":"h2","properties":{"id":"astadkomma-undantag","style":"position:relative;"},"children":[{"type":"text","value":"Åstadkomma undantag"},{"type":"element","tagName":"a","properties":{"href":"#astadkomma-undantag","ariaLabel":"astadkomma undantag permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan åstadkomma undantag med instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":". Det kan verka som en konstig idé att själv åstadkomma fel i ditt program, men det kan vara nyttigt i olika situationer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det kan till exempel löna sig att åstadkomma ett fel när man märker icke-valida parametrar. Hittills har vi skrivit ut meddelanden när vi har validerat indata, men om vi gör en funktion som ska köras från något annat ställe så kan det hända att en utskrift inte noteras när funktionen anropas. Att åstadkomma ett fel kan göra debuggande enklare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det följande exempel har vi en funktion som räknar ut fakulteten av en siffra (t.ex. för siffran fem är fakulteten 1 * 2 * 3 * 4 * 5). Om argumentet som ges till funktionen är negativt, kommer ett fel att åstadkommas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"fakultet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Negativt värde: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" i\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fakultet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"6\n720\nTraceback (most recent call last):\nFile \"testi.py\", line 11, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nprint(fakultet(-1))\nFile \"testi.py\", line 3, in fakultet\nraise ValueError(\"Negativt värde: \" + str(n))\nValueError: Negativt värde: -1"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Validera givna argument","tmcname":"osa06-18_validera_argument"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ny_person(namn: str, alder: int)"}]},{"type":"text","value":" som skapar och returnerar en tuple som representerar en person. Det första elementet är ett namn och det andra en ålder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om de givna argumenten är felaktiga, ska funktionen orsaka ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"-undantag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Felaktiga argument är:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tom sträng som namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett namn som inte består av minst två ord"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ett namn som är längre än 40 tecken"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en negativ ålder"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en ålder som överstiger 150"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Felaktiga lottorader","tmcname":"osa06-19_lottorader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lottorader.csv"}]},{"type":"text","value":" har man lagrat lottorader enligt följande exempel:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 1;5,7,11,13,23,24,30\nvecka 2;9,13,14,24,34,35,37\no.s.v. ..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först ska rubriken "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vecka x"}]},{"type":"text","value":" komma, följt av sju siffror i intervallet 1-39."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filen är dock delvis skadad. Här följer exempel på felaktiga rader."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Veckonummer felaktigt:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka zzc;1,5,13,22,24,25,26"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett eller flera felaktiga nummer:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 22;1,**,5,6,13,2b,34"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fel antal nummer:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 13;4,6,17,19,24,33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För små eller stora nummer:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 39;5,9,15,35,39,41,105"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma nummer förekommer flera gånger på samma rad:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"vecka 41;5,12,3,35,12,14,36"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filtrera_felaktiga()"}]},{"type":"text","value":" som skapar filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"korrigerade_rader.csv"}]},{"type":"text","value":". Filen ska innehålla de korrekta raderna från den ursprungliga filen."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du hur du kan behandla icke-valida indata
  • \n
  • vet du vad som menas med undantag i programmeringskontext
  • \n
  • känner du till de vanligaste typerna av undantag i Python
  • \n
  • kan du behandla undantag i dina egna program.
  • \n

Det finns två grundläggande kategorier av fel som uppkommer i program

    \n
  • fel i syntax, som förhindrar körandet av ett program
  • \n
  • fel som sker medan programmet körs, och stoppar programmet
  • \n

Fel i den första kategorin är vanligtvis enkla att korrigera eftersom Pythontolken berättar var felet ligger när man försöker köra programmet. Vanliga syntaxfel beror på kolon som fattas på någon inledande rad eller ett citattecken som fattas i slutet av en sträng.

Fel i den andra kategorin är ibland svårare att hitta, eftersom de endast sker vid något visst ställe i programmet i vissa situationer. Programmet kanske fungerar helt bra i de flesta situationer, men så finns det någon marginell situation då programmet stannar upp på grund av ett fel. Nu ska vi se hur vi kan behandla de här felen.

Validering av indata

Flera fel som uppkommer då ett program körs beror på indata som är i fel format på något sätt. Här är några exempel:

    \n
  • obligatoriska fält som fattas eller är tomma, till exempel tomma strängar i fall där strängens längd har betydelse
  • \n
  • negativa värden i fall där endast positiva värden är giltiga, till exempel -15 st. av en ingrediens i ett recept
  • \n
  • filer som saknas eller som har skrivfel i sina namn
  • \n
  • värden som är för stora eller för små, till exempel då vi arbetar med datum och klockslag
  • \n
  • icke-giltiga index, till exempel om vi försöker komma åt index 3 i strängen \"hej\"
  • \n
  • värden av fel typ, till exempel en sträng då vi förväntar oss ett heltal.
  • \n

Till all lycka kan vi som programmerare förbereda oss för de flesta felen. Vi ser på ett program som frågar efter användarens ålder och kollar att siffran är giltig (mellan noll och 150):

alder = int(input(\"Ange din ålder: \"))\nif alder >= 0 and alder <= 150:\n    print(\"Acceptabel ålder\")\nelse:\n    print(\"Felaktig ålder\")

Ange din ålder: 25\nAcceptabel ålder

Ange din ålder: -3\nFelaktig ålder

Så länge användaren ger ett heltal verkar valideringen fungera som den ska. Men om användaren ger en sträng?

Ange din ålder: tjugotre\nValueError: invalid literal for int() with base 10: 'tjugotre'

Funktionen int kan inte behandla strängen tjugotre som ett heltal. Programmet stoppas och ett felmeddelande skrivs ut.

Undantag

Fel som sker när programmet redan körs kallas undantag (exception). Det är möjligt att förbereda sig för undantag, så att programmet fortsätter köra även om undantag sker.

Undantag behandlas i Python med satserna try och except. Idén är att om något inom try-blocket orsakar ett undantag, kommer Python söka efter ett motsvarande except-block och köra koden under det blocket – och så fortsätter programmet att köra som normalt.

Låt oss ändra på exemplet ovan så att programmet är förberett för undantag av typen ValueError:

try:\n    alder = int(input(\"Ange din ålder: \"))\nexcept ValueError:\n    alder = -1\n\nif alder >= 0 and alder <= 150:\n    print(\"Acceptabel ålder\")\nelse:\n    print(\"Felaktig ålder\")

Ange din ålder: tjugotre\nFelaktig ålder

Vi kan använda try-blocket för att markera att koden inom blocket möjligtvis kan orsaka ett fel. I except-satsen som direkt följer blocket nämns det felet vi kan förvänta oss. I exemplet ovan nämnde vi bara ValueError-undantaget. Om något annat fel skulle ha uppstått, skulle programmet ändå ha avslutats oavsett try- och except-blocken.

I exemplet ovan, om ett fel sker, tilldelas alder värdet -1. Det här är ett icke-giltigt värde som programmet redan sedan tidigare kan reagera på, eftersom programmet förväntar sig ett värde mellan noll och 150.

I det följande exemplet har vi funktionen las_heltal som ber användaren att ge ett heltal. Funktionen är också redo för icke-giltiga värden och fortsätter att fråga efter ett heltal tills ett giltigt värde har angetts.

def las_heltal():\n    while True:\n        try:\n            indata = input(\"Ange heltal: \")\n            return int(indata)\n        except ValueError:\n            print(\"Felaktiga indata\")\n\nsiffra = las_heltal()\nprint(\"Tack!\")\nprint(siffra, \"upphöjt till tre är\", siffra**3)

Ange heltal: kolme\nFelaktiga indata\nAnge heltal: aybabtu\nFelaktiga indata\nAnge heltal: 5\nTack!\n5 upphöjt till tre är 125

Ibland räcker det med att fånga undantag med en try-except-struktur utan att göra något speciellt därtill. Vi kan alltså ignorera situationen i except-blocket.

Om vi ändrar på exemplet ovan så att vi endast accepterar heltal som är under 100, skulle det kunna se ut så här:

def las_litet_heltal():\n    while True:\n        try:\n            indata = input(\"Ange heltal: \")\n            siffra = int(indata)\n            if siffra < 100:\n                return siffra\n        except ValueError:\n            pass # den här instruktionen gör inget\n\n        print(\"Felaktiga indata\")\n\nsiffra = las_litet_heltal()\nprint(siffra, \"upphöjt till tre är\", siffra**3)

Ange heltal: kolme\nFelaktiga indata\nAnge heltal: 1000\nFelaktiga indata\nAnge heltal: 5\nTack!\n5 upphöjt till tre är 125

Nu innehåller except-blocket endast instruktionen pass, som inte gör något. Python tillåter inte tomma block, så instruktionen är nödvändig.

Skapa funktionen las som ber användaren ange ett tal, tills användaren anger ett tal som är inom intervallet som angivits. Funktionen ska returnera talet användaren gett.

Exempel:

siffra = las(\"Ange siffra: \", 5, 10)\nprint(\"Du gav siffran:\", siffra)

Ange siffra: seitsemän\nSiffran ska vara ett heltal i intervallet 5-10\nAnge siffra: -3\nSiffran ska vara ett heltal i intervallet 5-10\nAnge siffra: 8\nDu gav siffran: 8

Vanliga fel

Här är en samling av vanliga fel som du sannolikt kommer att stöta på. Vi kollar också i hurdana situationer dessa kan uppstå.

ValueError

Det här felet uppstår vanligtvis då ett argument som ges till en funktion på något sätt är ogiltigt. Till exempel anropet float(\"1,23\") orsakar ett fel eftersom decimaler alltid skiljs åt med punkt i Python.

TypeError

Det här felet uppstår då ett värde har fel typ. Om vi till exempel anropar len(10) får vi ett fel, eftersom funktionen len kräver ett värde vars längd kan räknas – till exempel en sträng eller lista.

IndexError

Det här felet uppstår då vi försöker hänvisa till ett index som inte finns. Till exempel uttrycket \"abc\"[5] orsakar ett fel eftersom strängen i fråga inte har indexet 5.

ZeroDivisionError

Det här felet uppstår då man försöker dividera med noll. Om vi till exempel försöker ta reda på medeltalet av värden i en lista med uttrycket sum(min_lista) / len(min_lista) och listans längd är noll, kommer vi att få ett fel.

Undantag när filer behandlas

Några vanliga undantag som kan uppstå när filer behandlas är FileNotFoundError (filen som man försöker komma åt finns inte), io.UnsupportedOperation (man försöker göra något åt filen som inte tillåts i det läge filen öppnats i) och PermissionError (programmet har inte åtkomst till filen).

Behandla flera undantag samtidigt

Ett try-block kan följas av flera except-block. Det här programmet kan exempelvis behandla situationer där FileNotFoundException- och PermissionError-undantagen uppkommer:

try:\n    with open(\"exempel.txt\") as fil:\n        for rad in fil:\n            print(rad)\nexcept FileNotFoundError:\n    print(\"Filen exempel.txt hittades ej\")\nexcept PermissionError:\n    print(\"Har inte åtkomst till filen exempel.txt\")

Ibland är det inte nödvändigt att specificera det fel som programmet förbereder sig för. Speciellt då man arbetar med filer, räcker det med att veta att något fel har skett och därmed avsluta programmet på ett säkert sätt. Man behöver inte alltid veta varför ett fel har uppstått. Om vi vill vara förberedda på alla möjliga undantag kan vi använda except-blocket utan att specificera felet:

try:\n    with open(\"exempel.txt\") as fil:\n        for rad in fil:\n            print(rad)\nexcept:\n    print(\"Fel i läsandet av filen\")

Obs! Den här except-satsen körs nu i alla möjliga felsituationer – det här gäller också misstag som programmeraren gjort. Endast syntaxfel kommer att orsaka fel som förhindrar programmet från att köras – detta eftersom den här typen av fel inte låter koden köras över huvud taget.

Till exempel det här programmet kommer alltid att ge ett fel, eftersom variabelnamnet fil har skrivits fel på den tredje raden:

try:\n    with open(\"exempel.txt\") as fil:\n        for rad in fli:\n            print(rad)\nexcept:\n    print(\"Fel i läsandet av filen\")

Ett except-block kan gömma bakomliggande fel. Problemet här berodde inte på hur filer behandlades utan på en variabel med inkorrekt namn. Utan ett except-block skulle vi kunna se vilket fel som orsakats och kunde därmed hitta källan till felet enklare. Därför lönar det sig att endast använda except-blocket för fördefinierade specifika typer av fel.

Förmedling av undantag

Om körandet av en funktion orsakar ett undantag som inte behandlas kommer undantaget att förmedlas till koden som anropat koden och fortsätta kliva upp till huvudfunktionen. Om felet inte här heller behandlas, kommer programmet att stoppas och undantaget skrivs ut för användaren att se.

I det följande exemplet har vi funktionen test. Om den orsakar ett undantag, kommer det inte att behandlas inne i funktionen utan i huvudfunktionen:

def test(x):\n    print(int(x) + 1)\n\ntry:\n    siffra = input(\"Ange siffra: \")\n    test(siffra)\nexcept:\n    print(\"Något gick fel\")

Ange siffra: tre\nNågot gick fel

Åstadkomma undantag

Du kan åstadkomma undantag med instruktionen raise. Det kan verka som en konstig idé att själv åstadkomma fel i ditt program, men det kan vara nyttigt i olika situationer.

Det kan till exempel löna sig att åstadkomma ett fel när man märker icke-valida parametrar. Hittills har vi skrivit ut meddelanden när vi har validerat indata, men om vi gör en funktion som ska köras från något annat ställe så kan det hända att en utskrift inte noteras när funktionen anropas. Att åstadkomma ett fel kan göra debuggande enklare.

I det följande exempel har vi en funktion som räknar ut fakulteten av en siffra (t.ex. för siffran fem är fakulteten 1 * 2 * 3 * 4 * 5). Om argumentet som ges till funktionen är negativt, kommer ett fel att åstadkommas:

def fakultet(n):\n    if n < 0:\n        raise ValueError(\"Negativt värde: \" + str(n))\n    k = 1\n    for i in range(2, n + 1):\n        k *= i\n    return k\n\nprint(fakultet(3))\nprint(fakultet(6))\nprint(fakultet(-1))
6\n720\nTraceback (most recent call last):\nFile \"testi.py\", line 11, in \nprint(fakultet(-1))\nFile \"testi.py\", line 3, in fakultet\nraise ValueError(\"Negativt värde: \" + str(n))\nValueError: Negativt värde: -1

Skapa funktionen ny_person(namn: str, alder: int) som skapar och returnerar en tuple som representerar en person. Det första elementet är ett namn och det andra en ålder.

Om de givna argumenten är felaktiga, ska funktionen orsaka ett ValueError-undantag.

Felaktiga argument är:

    \n
  • tom sträng som namn
  • \n
  • ett namn som inte består av minst två ord
  • \n
  • ett namn som är längre än 40 tecken
  • \n
  • en negativ ålder
  • \n
  • en ålder som överstiger 150
  • \n

I filen lottorader.csv har man lagrat lottorader enligt följande exempel:

vecka 1;5,7,11,13,23,24,30\nvecka 2;9,13,14,24,34,35,37\no.s.v. ...

Först ska rubriken vecka x komma, följt av sju siffror i intervallet 1-39.

Filen är dock delvis skadad. Här följer exempel på felaktiga rader.

Veckonummer felaktigt:

vecka zzc;1,5,13,22,24,25,26

Ett eller flera felaktiga nummer:

vecka 22;1,**,5,6,13,2b,34

Fel antal nummer:

vecka 13;4,6,17,19,24,33

För små eller stora nummer:

vecka 39;5,9,15,35,39,41,105

Samma nummer förekommer flera gånger på samma rad:

vecka 41;5,12,3,35,12,14,36

Skapa funktionen filtrera_felaktiga() som skapar filen korrigerade_rader.csv. Filen ska innehålla de korrekta raderna från den ursprungliga filen.

","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/3-fel.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-6/4-lokala-globala-variabler/page-data.json b/page-data/osa-6/4-lokala-globala-variabler/page-data.json index 2cc9e1df5..13eaddf74 100644 --- a/page-data/osa-6/4-lokala-globala-variabler/page-data.json +++ b/page-data/osa-6/4-lokala-globala-variabler/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-6/4-lokala-globala-variabler", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad en lokal variabel är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till hur definitionsområdet för en variabel påverkar dess användning"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad nyckelordet global betyder i Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda lokala och globala variabler korrekt."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definitionsområdet för en variabel hänvisar till de områden i ett program där en specifik variabel är tillgänglig. En lokal variabel är endast tillgänglig på vissa ställen i ett program medan en global variabel kan användas överallt i programmet."}]},{"type":"element","tagName":"h2","properties":{"id":"lokala-variabler","style":"position:relative;"},"children":[{"type":"text","value":"Lokala variabler"},{"type":"element","tagName":"a","properties":{"href":"#lokala-variabler","ariaLabel":"lokala variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabler som tilldelas i Python är lokala variabler. De är endast tillgängliga i den funktion där de tilldelats. Det här gäller både funktionsparametrar och andra variabler som tilldelats inom funktionsdefinitionen. En variabel som är lokal existerar inte utanför funktionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det följande exemplet försöker vi komma åt variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" i huvudfunktionen, men det orsakar ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\nNameError: name 'x' is not defined"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" existerar endast då funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test"}]},{"type":"text","value":" körs. Andra funktioner – också huvudfunktionen – kan inte komma åt variabeln."}]},{"type":"element","tagName":"h2","properties":{"id":"globala-variabler","style":"position:relative;"},"children":[{"type":"text","value":"Globala variabler"},{"type":"element","tagName":"a","properties":{"href":"#globala-variabler","ariaLabel":"globala variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabler som tilldelas i huvudfunktionen är globala variabler. Vi har tidigare definierat att huvudfunktionen är de delar av koden i Python som inte tillhör någon annan funktion. Ett värde som lagrats i en global variabel kan användas i vilken som helst funktion i programmet. Därmed fungerar den här koden utan problem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En global variabel kan inte ändras på direkt via en annan funktion. Den här funktionen har ingen påverkan på den globala variabeln:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här skapar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test"}]},{"type":"text","value":" en ny lokal variabel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", som \"maskerar\" den globala variabeln medan funktionen körs. Variabeln har värdet 5, men det är en annan variabel än den som tilldelats i huvudfunktionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad skulle den här kodsnutten då göra?"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"UnboundLocalError: local variable 'x' referenced before assignment"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test"}]},{"type":"text","value":" tilldelar ett värde till variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", så Python antar att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" är en lokal variabel istället för en global variabel med samma namn. Funktionen försöker komma åt variabeln före den skapats, vilket orsakar ett fel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill ändra på en global variabel inom en funktion behöver vi Pythons nyckelord "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" x\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu påverkar tilldelningen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 3"}]},{"type":"text","value":" inom funktionen också i huvudfunktionen. Alla delar av programmet använder den samma globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"nar-borde-man-anvanda-globala-variabler","style":"position:relative;"},"children":[{"type":"text","value":"När borde man använda globala variabler?"},{"type":"element","tagName":"a","properties":{"href":"#nar-borde-man-anvanda-globala-variabler","ariaLabel":"nar borde man anvanda globala variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Globala variabler är inte ett sätt att undvika parametrar eller returvärden hos funktioner. De ska inte användas för det ändamålet. Det är dock möjligt att skriva en funktion som lagrar sina resultat direkt i en global variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" resultat\n resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nrakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men det är bättre att göra en funktion som returnerar ett värde, så som vi har vant oss med:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fördelen med det senare tillvägagångssättet är att funktionen är en självständig helhet. Den har specifika, fördefinierade parametrar och den returnerar ett resultat. Den har inga sidoeffekter, så den kan testas och ändras utan att man behöver bry sig om andra delar av programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Globala variabler är nyttiga i situationer där vi behöver någon gemensam information på \"högre nivå\", och den här informationen ska vara tillgänglig för alla funktioner i programmet. Det här är ett exempel på en sådan situation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" raknare\n raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_differens"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" raknare\n raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" b\n\n\nraknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_differens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Funkionerna anropades\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"gånger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n5\n10\n3\n1\nFunkionerna anropades 4 gångergånger"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet vill vi hålla koll på hur många gånger någondera av funktionerna har anropats medan programmet körts. Den globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"raknare"}]},{"type":"text","value":" är nyttig i den här situationen, eftersom vi kan öka på siffran inom funktionerna när de körs samtidigt som värdet också är tillgängligt via huvudfunktionen."}]},{"type":"element","tagName":"h2","properties":{"id":"formedla-data-fran-funktion-till-funktion--en-andra-titt","style":"position:relative;"},"children":[{"type":"text","value":"Förmedla data från funktion till funktion – en andra titt"},{"type":"element","tagName":"a","properties":{"href":"#formedla-data-fran-funktion-till-funktion--en-andra-titt","ariaLabel":"formedla data fran funktion till funktion en andra titt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om ett program består av flera funktioner, dyker ofta frågan om att förmedla data från en funktion till en annan upp."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi såg på den här frågan senast, hade vi ett program som frågar användaren efter några heltal, skriver dem ut och analyserar sifforna. Programmet var uppdelat i tre funktioner:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_fran_anvandaren"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Ange "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" siffror:\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" antal\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffror\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffrorna är: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"analysera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n medeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Tillsammans "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" siffror, medelvärde "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", minsta "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och största "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# \"huvudprogram\" som använder funktionerna"}]},{"type":"text","value":"\nindata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_fran_anvandaren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nanalys "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analysera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analys"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur det ser ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange 5 siffror:\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-32"}]},{"type":"text","value":"\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"99"}]},{"type":"text","value":"\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-53"}]},{"type":"text","value":"\nSiffrorna är:\n10\n34\n-32\n99\n-53\nTillsammans 5 siffror, medelvärde 11.6, minsta- 53 och största 99"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Basidén är att huvudfunktionen \"lagrar\" den data som behandlas av programmet. I det här fallet innebär det att sifforna som användaren anger lagras i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffor"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om siffrorna behövs i någon funktion, ger vi variabeln som argument, vilket vi ser med funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_resultat"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analysera"}]},{"type":"text","value":". Om funktionen ger upphov till ett resultat som behövs på ett annat ställe i programmet, returneras det – så som i funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"indata_fran_anvandare"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analysera"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som alltid när man programmerar, finns det flera sätt att uppnå likadan funktionalitet. Det skulle vara möjligt att använda nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" och låta funktionerna direkt komma åt variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror"}]},{"type":"text","value":" som tilldelats i huvudfunktionen. Det finns bra orsaker till att det här "},{"type":"element","tagName":"a","properties":{"href":"https://softwareengineering.stackexchange.com/q/148108","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"inte är en god idé"}]},{"type":"text","value":". Om flera funktioner kan komma åt och möjligtvis ändra på en variabel, blir det snabbt svårt att hålla koll på programmets status och programmet blir oförutsägbart. Det här märks speciellt då antalet funktioner ökar, vilket det gör oundvikligen i större programmeringsprojekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sammanfattningsvis kan man konstatera att det är bäst att använda argument och returnera värden när man arbetar med funktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också ha en skild "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":"-funktion. I det fallet skulle variabeln siffror inte längre vara global, utan en lokal variabel under "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_fran_anvandaren"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Ange "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" siffror:\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" antal\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffror\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffrorna är: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"analysera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n medeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Tillsammans "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" siffror, medelvärde "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", minsta "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och största "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktion som representerar huvudprogrammet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_fran_anvandaren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n skriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n analys "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analysera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analys"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# start av programmet"}]},{"type":"text","value":"\nmain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät gällande materialet för den här veckan."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad en lokal variabel är
  • \n
  • känner du till hur definitionsområdet för en variabel påverkar dess användning
  • \n
  • vet du vad nyckelordet global betyder i Python
  • \n
  • kan du använda lokala och globala variabler korrekt.
  • \n

Definitionsområdet för en variabel hänvisar till de områden i ett program där en specifik variabel är tillgänglig. En lokal variabel är endast tillgänglig på vissa ställen i ett program medan en global variabel kan användas överallt i programmet.

Lokala variabler

Variabler som tilldelas i Python är lokala variabler. De är endast tillgängliga i den funktion där de tilldelats. Det här gäller både funktionsparametrar och andra variabler som tilldelats inom funktionsdefinitionen. En variabel som är lokal existerar inte utanför funktionen.

I det följande exemplet försöker vi komma åt variabeln x i huvudfunktionen, men det orsakar ett fel:

def test():\n    x = 5\n    print(x)\n\ntest()\nprint(x)

5\nNameError: name 'x' is not defined

Variabeln x existerar endast då funktionen test körs. Andra funktioner – också huvudfunktionen – kan inte komma åt variabeln.

Globala variabler

Variabler som tilldelas i huvudfunktionen är globala variabler. Vi har tidigare definierat att huvudfunktionen är de delar av koden i Python som inte tillhör någon annan funktion. Ett värde som lagrats i en global variabel kan användas i vilken som helst funktion i programmet. Därmed fungerar den här koden utan problem:

def test():\n    print(x)\n\nx = 3\ntest()

3

En global variabel kan inte ändras på direkt via en annan funktion. Den här funktionen har ingen påverkan på den globala variabeln:

def test():\n    x = 5\n    print(x)\n\nx = 3\ntest()\nprint(x)

5\n3

Här skapar funktionen test en ny lokal variabel x, som \"maskerar\" den globala variabeln medan funktionen körs. Variabeln har värdet 5, men det är en annan variabel än den som tilldelats i huvudfunktionen.

Vad skulle den här kodsnutten då göra?

def test():\n    print(x)\n    x = 5\n\nx = 3\ntest()\nprint(x)

UnboundLocalError: local variable 'x' referenced before assignment

Funktionen test tilldelar ett värde till variabeln x, så Python antar att x är en lokal variabel istället för en global variabel med samma namn. Funktionen försöker komma åt variabeln före den skapats, vilket orsakar ett fel.

Om vi vill ändra på en global variabel inom en funktion behöver vi Pythons nyckelord global:

def test():\n    global x\n    x = 3\n    print(x)\n\nx = 5\ntest()\nprint(x)

3\n3

Nu påverkar tilldelningen x = 3 inom funktionen också i huvudfunktionen. Alla delar av programmet använder den samma globala variabeln x.

När borde man använda globala variabler?

Globala variabler är inte ett sätt att undvika parametrar eller returvärden hos funktioner. De ska inte användas för det ändamålet. Det är dock möjligt att skriva en funktion som lagrar sina resultat direkt i en global variabel:

def rakna_summa(a, b):\n    global resultat\n    resultat = a + b\n\nrakna_summa(2, 3)\nprint(resultat)

Men det är bättre att göra en funktion som returnerar ett värde, så som vi har vant oss med:

def rakna_summa(a, b):\n    return a + b\n\nresultat = rakna_summa(2, 3)\nprint(resultat)

Fördelen med det senare tillvägagångssättet är att funktionen är en självständig helhet. Den har specifika, fördefinierade parametrar och den returnerar ett resultat. Den har inga sidoeffekter, så den kan testas och ändras utan att man behöver bry sig om andra delar av programmet.

Globala variabler är nyttiga i situationer där vi behöver någon gemensam information på \"högre nivå\", och den här informationen ska vara tillgänglig för alla funktioner i programmet. Det här är ett exempel på en sådan situation:

def rakna_summa(a, b):\n    global raknare\n    raknare += 1\n    return a + b\n\ndef rakna_differens(a, b):\n    global raknare\n    raknare += 1\n    return a - b\n\n\nraknare = 0\nprint(rakna_summa(2, 3))\nprint(rakna_summa(5, 5))\nprint(rakna_differens(5, 2))\nprint(rakna_summa(1, 0))\nprint(\"Funkionerna anropades\", raknare, \"gånger\")
\n5\n10\n3\n1\nFunkionerna anropades 4 gånger

I det här fallet vill vi hålla koll på hur många gånger någondera av funktionerna har anropats medan programmet körts. Den globala variabeln raknare är nyttig i den här situationen, eftersom vi kan öka på siffran inom funktionerna när de körs samtidigt som värdet också är tillgängligt via huvudfunktionen.

Förmedla data från funktion till funktion – en andra titt

Om ett program består av flera funktioner, dyker ofta frågan om att förmedla data från en funktion till en annan upp.

När vi såg på den här frågan senast, hade vi ett program som frågar användaren efter några heltal, skriver dem ut och analyserar sifforna. Programmet var uppdelat i tre funktioner:

def las_fran_anvandaren(antal: int):\n    print(f\"Ange {antal} siffror:\")\n    siffror = []\n\n    i = antal\n    while i>0:\n        siffra = int(input(\"Ange siffra: \"))\n        siffror.append(siffra)\n        i -= 1\n\n    return siffror\n\ndef skriv_ut(siffror: list):\n    print(\"Siffrorna är: \")\n    for siffra in siffror:\n        print(siffra)\n\ndef analysera(siffror: list):\n    medeltal = sum(siffror) / len(siffror)\n    return f\"Tillsammans {len(siffror)} siffror, medelvärde {medeltal}, minsta {min(siffror)} och största {max(siffror)}\"\n\n# \"huvudprogram\" som använder funktionerna\nindata = las_fran_anvandaren(5)\nskriv_ut(indata)\nanalys = analysera(indata)\nprint(analys)

Exempel på hur det ser ut när programmet körs:

Ange 5 siffror:\nAnge siffra: 10\nAnge siffra: 34\nAnge siffra: -32\nAnge siffra: 99\nAnge siffra: -53\nSiffrorna är:\n10\n34\n-32\n99\n-53\nTillsammans 5 siffror, medelvärde 11.6, minsta- 53 och största 99

Basidén är att huvudfunktionen \"lagrar\" den data som behandlas av programmet. I det här fallet innebär det att sifforna som användaren anger lagras i variabeln siffor.

Om siffrorna behövs i någon funktion, ger vi variabeln som argument, vilket vi ser med funktionerna skriv_ut_resultat och analysera. Om funktionen ger upphov till ett resultat som behövs på ett annat ställe i programmet, returneras det – så som i funktionerna indata_fran_anvandare och analysera.

Som alltid när man programmerar, finns det flera sätt att uppnå likadan funktionalitet. Det skulle vara möjligt att använda nyckelordet global och låta funktionerna direkt komma åt variabeln siffror som tilldelats i huvudfunktionen. Det finns bra orsaker till att det här inte är en god idé. Om flera funktioner kan komma åt och möjligtvis ändra på en variabel, blir det snabbt svårt att hålla koll på programmets status och programmet blir oförutsägbart. Det här märks speciellt då antalet funktioner ökar, vilket det gör oundvikligen i större programmeringsprojekt.

Sammanfattningsvis kan man konstatera att det är bäst att använda argument och returnera värden när man arbetar med funktioner.

Du kan också ha en skild main-funktion. I det fallet skulle variabeln siffror inte längre vara global, utan en lokal variabel under main-funktionen:

def las_fran_anvandaren(antal: int):\n    print(f\"Ange {antal} siffror:\")\n    siffror = []\n\n    i = antal\n    while i>0:\n        siffra = int(input(\"Ange siffra: \"))\n        siffror.append(siffra)\n        i -= 1\n\n    return siffror\n\ndef skriv_ut(siffror: list):\n    print(\"Siffrorna är: \")\n    for siffra in siffror:\n        print(siffra)\n\ndef analysera(siffror: list):\n    medeltal = sum(siffror) / len(siffror)\n    return f\"Tillsammans {len(siffror)} siffror, medelvärde {medeltal}, minsta {min(siffror)} och största {max(siffror)}\"\n\n# funktion som representerar huvudprogrammet\ndef main():\n    indata = las_fran_anvandaren(5)\n    skriv_ut(indata)\n    analys = analysera(indata)\n\n    print(analys)\n\n# start av programmet\nmain()

Vänligen svara på en kort enkät gällande materialet för den här veckan.

","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/4-lokala-globala-variabler.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad en lokal variabel är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till hur definitionsområdet för en variabel påverkar dess användning"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad nyckelordet global betyder i Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda lokala och globala variabler korrekt."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definitionsområdet för en variabel hänvisar till de områden i ett program där en specifik variabel är tillgänglig. En lokal variabel är endast tillgänglig på vissa ställen i ett program medan en global variabel kan användas överallt i programmet."}]},{"type":"element","tagName":"h2","properties":{"id":"lokala-variabler","style":"position:relative;"},"children":[{"type":"text","value":"Lokala variabler"},{"type":"element","tagName":"a","properties":{"href":"#lokala-variabler","ariaLabel":"lokala variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabler som tilldelas i Python är lokala variabler. De är endast tillgängliga i den funktion där de tilldelats. Det här gäller både funktionsparametrar och andra variabler som tilldelats inom funktionsdefinitionen. En variabel som är lokal existerar inte utanför funktionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det följande exemplet försöker vi komma åt variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" i huvudfunktionen, men det orsakar ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\nNameError: name 'x' is not defined"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" existerar endast då funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test"}]},{"type":"text","value":" körs. Andra funktioner – också huvudfunktionen – kan inte komma åt variabeln."}]},{"type":"element","tagName":"h2","properties":{"id":"globala-variabler","style":"position:relative;"},"children":[{"type":"text","value":"Globala variabler"},{"type":"element","tagName":"a","properties":{"href":"#globala-variabler","ariaLabel":"globala variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabler som tilldelas i huvudfunktionen är globala variabler. Vi har tidigare definierat att huvudfunktionen är de delar av koden i Python som inte tillhör någon annan funktion. Ett värde som lagrats i en global variabel kan användas i vilken som helst funktion i programmet. Därmed fungerar den här koden utan problem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En global variabel kan inte ändras på direkt via en annan funktion. Den här funktionen har ingen påverkan på den globala variabeln:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här skapar funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test"}]},{"type":"text","value":" en ny lokal variabel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", som \"maskerar\" den globala variabeln medan funktionen körs. Variabeln har värdet 5, men det är en annan variabel än den som tilldelats i huvudfunktionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad skulle den här kodsnutten då göra?"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"UnboundLocalError: local variable 'x' referenced before assignment"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test"}]},{"type":"text","value":" tilldelar ett värde till variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", så Python antar att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" är en lokal variabel istället för en global variabel med samma namn. Funktionen försöker komma åt variabeln före den skapats, vilket orsakar ett fel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill ändra på en global variabel inom en funktion behöver vi Pythons nyckelord "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" x\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu påverkar tilldelningen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 3"}]},{"type":"text","value":" inom funktionen också i huvudfunktionen. Alla delar av programmet använder den samma globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"nar-borde-man-anvanda-globala-variabler","style":"position:relative;"},"children":[{"type":"text","value":"När borde man använda globala variabler?"},{"type":"element","tagName":"a","properties":{"href":"#nar-borde-man-anvanda-globala-variabler","ariaLabel":"nar borde man anvanda globala variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Globala variabler är inte ett sätt att undvika parametrar eller returvärden hos funktioner. De ska inte användas för det ändamålet. Det är dock möjligt att skriva en funktion som lagrar sina resultat direkt i en global variabel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" resultat\n resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nrakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Men det är bättre att göra en funktion som returnerar ett värde, så som vi har vant oss med:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fördelen med det senare tillvägagångssättet är att funktionen är en självständig helhet. Den har specifika, fördefinierade parametrar och den returnerar ett resultat. Den har inga sidoeffekter, så den kan testas och ändras utan att man behöver bry sig om andra delar av programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Globala variabler är nyttiga i situationer där vi behöver någon gemensam information på \"högre nivå\", och den här informationen ska vara tillgänglig för alla funktioner i programmet. Det här är ett exempel på en sådan situation:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_summa"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" raknare\n raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rakna_differens"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" raknare\n raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" b\n\n\nraknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_differens"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rakna_summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Funkionerna anropades\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" raknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"gånger\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n5\n10\n3\n1\nFunkionerna anropades 4 gångergånger"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här fallet vill vi hålla koll på hur många gånger någondera av funktionerna har anropats medan programmet körts. Den globala variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"raknare"}]},{"type":"text","value":" är nyttig i den här situationen, eftersom vi kan öka på siffran inom funktionerna när de körs samtidigt som värdet också är tillgängligt via huvudfunktionen."}]},{"type":"element","tagName":"h2","properties":{"id":"formedla-data-fran-funktion-till-funktion--en-andra-titt","style":"position:relative;"},"children":[{"type":"text","value":"Förmedla data från funktion till funktion – en andra titt"},{"type":"element","tagName":"a","properties":{"href":"#formedla-data-fran-funktion-till-funktion--en-andra-titt","ariaLabel":"formedla data fran funktion till funktion en andra titt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om ett program består av flera funktioner, dyker ofta frågan om att förmedla data från en funktion till en annan upp."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi såg på den här frågan senast, hade vi ett program som frågar användaren efter några heltal, skriver dem ut och analyserar sifforna. Programmet var uppdelat i tre funktioner:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_fran_anvandaren"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Ange "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" siffror:\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" antal\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffror\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffrorna är: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"analysera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n medeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Tillsammans "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" siffror, medelvärde "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", minsta "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och största "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# \"huvudprogram\" som använder funktionerna"}]},{"type":"text","value":"\nindata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_fran_anvandaren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nanalys "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analysera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analys"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur det ser ut när programmet körs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange 5 siffror:\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-32"}]},{"type":"text","value":"\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"99"}]},{"type":"text","value":"\nAnge siffra: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-53"}]},{"type":"text","value":"\nSiffrorna är:\n10\n34\n-32\n99\n-53\nTillsammans 5 siffror, medelvärde 11.6, minsta- 53 och största 99"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Basidén är att huvudfunktionen \"lagrar\" den data som behandlas av programmet. I det här fallet innebär det att sifforna som användaren anger lagras i variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffor"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om siffrorna behövs i någon funktion, ger vi variabeln som argument, vilket vi ser med funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_resultat"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analysera"}]},{"type":"text","value":". Om funktionen ger upphov till ett resultat som behövs på ett annat ställe i programmet, returneras det – så som i funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"indata_fran_anvandare"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"analysera"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som alltid när man programmerar, finns det flera sätt att uppnå likadan funktionalitet. Det skulle vara möjligt att använda nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" och låta funktionerna direkt komma åt variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"siffror"}]},{"type":"text","value":" som tilldelats i huvudfunktionen. Det finns bra orsaker till att det här "},{"type":"element","tagName":"a","properties":{"href":"https://softwareengineering.stackexchange.com/q/148108","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"inte är en god idé"}]},{"type":"text","value":". Om flera funktioner kan komma åt och möjligtvis ändra på en variabel, blir det snabbt svårt att hålla koll på programmets status och programmet blir oförutsägbart. Det här märks speciellt då antalet funktioner ökar, vilket det gör oundvikligen i större programmeringsprojekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sammanfattningsvis kan man konstatera att det är bäst att använda argument och returnera värden när man arbetar med funktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också ha en skild "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":"-funktion. I det fallet skulle variabeln siffror inte längre vara global, utan en lokal variabel under "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"las_fran_anvandaren"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Ange "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" siffror:\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" antal\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" i"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n siffra "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange siffra: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n i "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" siffror\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Siffrorna är: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"analysera"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n medeltal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Tillsammans "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" siffror, medelvärde "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", minsta "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" och största "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffror"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktion som representerar huvudprogrammet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"main"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n indata "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" las_fran_anvandaren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n skriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n analys "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" analysera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"indata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"analys"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# start av programmet"}]},{"type":"text","value":"\nmain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät gällande materialet för den här veckan."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad en lokal variabel är
  • \n
  • känner du till hur definitionsområdet för en variabel påverkar dess användning
  • \n
  • vet du vad nyckelordet global betyder i Python
  • \n
  • kan du använda lokala och globala variabler korrekt.
  • \n

Definitionsområdet för en variabel hänvisar till de områden i ett program där en specifik variabel är tillgänglig. En lokal variabel är endast tillgänglig på vissa ställen i ett program medan en global variabel kan användas överallt i programmet.

Lokala variabler

Variabler som tilldelas i Python är lokala variabler. De är endast tillgängliga i den funktion där de tilldelats. Det här gäller både funktionsparametrar och andra variabler som tilldelats inom funktionsdefinitionen. En variabel som är lokal existerar inte utanför funktionen.

I det följande exemplet försöker vi komma åt variabeln x i huvudfunktionen, men det orsakar ett fel:

def test():\n    x = 5\n    print(x)\n\ntest()\nprint(x)

5\nNameError: name 'x' is not defined

Variabeln x existerar endast då funktionen test körs. Andra funktioner – också huvudfunktionen – kan inte komma åt variabeln.

Globala variabler

Variabler som tilldelas i huvudfunktionen är globala variabler. Vi har tidigare definierat att huvudfunktionen är de delar av koden i Python som inte tillhör någon annan funktion. Ett värde som lagrats i en global variabel kan användas i vilken som helst funktion i programmet. Därmed fungerar den här koden utan problem:

def test():\n    print(x)\n\nx = 3\ntest()

3

En global variabel kan inte ändras på direkt via en annan funktion. Den här funktionen har ingen påverkan på den globala variabeln:

def test():\n    x = 5\n    print(x)\n\nx = 3\ntest()\nprint(x)

5\n3

Här skapar funktionen test en ny lokal variabel x, som \"maskerar\" den globala variabeln medan funktionen körs. Variabeln har värdet 5, men det är en annan variabel än den som tilldelats i huvudfunktionen.

Vad skulle den här kodsnutten då göra?

def test():\n    print(x)\n    x = 5\n\nx = 3\ntest()\nprint(x)

UnboundLocalError: local variable 'x' referenced before assignment

Funktionen test tilldelar ett värde till variabeln x, så Python antar att x är en lokal variabel istället för en global variabel med samma namn. Funktionen försöker komma åt variabeln före den skapats, vilket orsakar ett fel.

Om vi vill ändra på en global variabel inom en funktion behöver vi Pythons nyckelord global:

def test():\n    global x\n    x = 3\n    print(x)\n\nx = 5\ntest()\nprint(x)

3\n3

Nu påverkar tilldelningen x = 3 inom funktionen också i huvudfunktionen. Alla delar av programmet använder den samma globala variabeln x.

När borde man använda globala variabler?

Globala variabler är inte ett sätt att undvika parametrar eller returvärden hos funktioner. De ska inte användas för det ändamålet. Det är dock möjligt att skriva en funktion som lagrar sina resultat direkt i en global variabel:

def rakna_summa(a, b):\n    global resultat\n    resultat = a + b\n\nrakna_summa(2, 3)\nprint(resultat)

Men det är bättre att göra en funktion som returnerar ett värde, så som vi har vant oss med:

def rakna_summa(a, b):\n    return a + b\n\nresultat = rakna_summa(2, 3)\nprint(resultat)

Fördelen med det senare tillvägagångssättet är att funktionen är en självständig helhet. Den har specifika, fördefinierade parametrar och den returnerar ett resultat. Den har inga sidoeffekter, så den kan testas och ändras utan att man behöver bry sig om andra delar av programmet.

Globala variabler är nyttiga i situationer där vi behöver någon gemensam information på \"högre nivå\", och den här informationen ska vara tillgänglig för alla funktioner i programmet. Det här är ett exempel på en sådan situation:

def rakna_summa(a, b):\n    global raknare\n    raknare += 1\n    return a + b\n\ndef rakna_differens(a, b):\n    global raknare\n    raknare += 1\n    return a - b\n\n\nraknare = 0\nprint(rakna_summa(2, 3))\nprint(rakna_summa(5, 5))\nprint(rakna_differens(5, 2))\nprint(rakna_summa(1, 0))\nprint(\"Funkionerna anropades\", raknare, \"gånger\")
\n5\n10\n3\n1\nFunkionerna anropades 4 gånger

I det här fallet vill vi hålla koll på hur många gånger någondera av funktionerna har anropats medan programmet körts. Den globala variabeln raknare är nyttig i den här situationen, eftersom vi kan öka på siffran inom funktionerna när de körs samtidigt som värdet också är tillgängligt via huvudfunktionen.

Förmedla data från funktion till funktion – en andra titt

Om ett program består av flera funktioner, dyker ofta frågan om att förmedla data från en funktion till en annan upp.

När vi såg på den här frågan senast, hade vi ett program som frågar användaren efter några heltal, skriver dem ut och analyserar sifforna. Programmet var uppdelat i tre funktioner:

def las_fran_anvandaren(antal: int):\n    print(f\"Ange {antal} siffror:\")\n    siffror = []\n\n    i = antal\n    while i>0:\n        siffra = int(input(\"Ange siffra: \"))\n        siffror.append(siffra)\n        i -= 1\n\n    return siffror\n\ndef skriv_ut(siffror: list):\n    print(\"Siffrorna är: \")\n    for siffra in siffror:\n        print(siffra)\n\ndef analysera(siffror: list):\n    medeltal = sum(siffror) / len(siffror)\n    return f\"Tillsammans {len(siffror)} siffror, medelvärde {medeltal}, minsta {min(siffror)} och största {max(siffror)}\"\n\n# \"huvudprogram\" som använder funktionerna\nindata = las_fran_anvandaren(5)\nskriv_ut(indata)\nanalys = analysera(indata)\nprint(analys)

Exempel på hur det ser ut när programmet körs:

Ange 5 siffror:\nAnge siffra: 10\nAnge siffra: 34\nAnge siffra: -32\nAnge siffra: 99\nAnge siffra: -53\nSiffrorna är:\n10\n34\n-32\n99\n-53\nTillsammans 5 siffror, medelvärde 11.6, minsta- 53 och största 99

Basidén är att huvudfunktionen \"lagrar\" den data som behandlas av programmet. I det här fallet innebär det att sifforna som användaren anger lagras i variabeln siffor.

Om siffrorna behövs i någon funktion, ger vi variabeln som argument, vilket vi ser med funktionerna skriv_ut_resultat och analysera. Om funktionen ger upphov till ett resultat som behövs på ett annat ställe i programmet, returneras det – så som i funktionerna indata_fran_anvandare och analysera.

Som alltid när man programmerar, finns det flera sätt att uppnå likadan funktionalitet. Det skulle vara möjligt att använda nyckelordet global och låta funktionerna direkt komma åt variabeln siffror som tilldelats i huvudfunktionen. Det finns bra orsaker till att det här inte är en god idé. Om flera funktioner kan komma åt och möjligtvis ändra på en variabel, blir det snabbt svårt att hålla koll på programmets status och programmet blir oförutsägbart. Det här märks speciellt då antalet funktioner ökar, vilket det gör oundvikligen i större programmeringsprojekt.

Sammanfattningsvis kan man konstatera att det är bäst att använda argument och returnera värden när man arbetar med funktioner.

Du kan också ha en skild main-funktion. I det fallet skulle variabeln siffror inte längre vara global, utan en lokal variabel under main-funktionen:

def las_fran_anvandaren(antal: int):\n    print(f\"Ange {antal} siffror:\")\n    siffror = []\n\n    i = antal\n    while i>0:\n        siffra = int(input(\"Ange siffra: \"))\n        siffror.append(siffra)\n        i -= 1\n\n    return siffror\n\ndef skriv_ut(siffror: list):\n    print(\"Siffrorna är: \")\n    for siffra in siffror:\n        print(siffra)\n\ndef analysera(siffror: list):\n    medeltal = sum(siffror) / len(siffror)\n    return f\"Tillsammans {len(siffror)} siffror, medelvärde {medeltal}, minsta {min(siffror)} och största {max(siffror)}\"\n\n# funktion som representerar huvudprogrammet\ndef main():\n    indata = las_fran_anvandaren(5)\n    skriv_ut(indata)\n    analys = analysera(indata)\n\n    print(analys)\n\n# start av programmet\nmain()

Vänligen svara på en kort enkät gällande materialet för den här veckan.

","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/4-lokala-globala-variabler.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-6/page-data.json b/page-data/osa-6/page-data.json index 5e5706fef..4eb2868f9 100644 --- a/page-data/osa-6/page-data.json +++ b/page-data/osa-6/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-6", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-6","title":"Modul 6"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-6","title":"Modul 6"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-6/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-7/1-moduler/page-data.json b/page-data/osa-7/1-moduler/page-data.json index d77b4b458..bbcb0483b 100644 --- a/page-data/osa-7/1-moduler/page-data.json +++ b/page-data/osa-7/1-moduler/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-7/1-moduler", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad en Python-modul är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man inkluderar en modul i ett program med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man hittar mera information om innehållet i en modul."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lite-mera-om-debuggande","style":"position:relative;"},"children":[{"type":"text","value":"Lite mera om debuggande"},{"type":"element","tagName":"a","properties":{"href":"#lite-mera-om-debuggande","ariaLabel":"lite mera om debuggande permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan bekantat oss med en hel del olika debuggningsmetoder under den här kursen. "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser för debuggning är redan bekanta för dig. Du har kanske också testat på det inbyggda debuggningsverktyget i Visual Studio Code. Om du har problem med att debuggaren inte hittar dina filer, kan du bekanta dig med några tips från den förra modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sedan versionen 3.7 av Python finns ett ytterligare sätt att debugga program: instruktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/functions.html#breakpoint","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint()"}]}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan lägga till den här instruktionen på valfritt ställe i din kod (kom dock ihåg att följa reglerna som gäller syntax). När programmet körs, kommer programmet att stanna på det ställe där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":"-instruktionen finns. Här är ett exempel där vi debuggar en uppgiftslösning från den förra modulen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f9eb8b0c4a4f642d316973fc108cae86/ec5f6/7_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 62.173913043478265%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAABYlAAAWJQFJUiTwAAABeklEQVQoz43R247bIBQFUH/HhKu5GIMNPr40mQwYT+yM2qn6/79TxcnDqGqrLB0J8bBB2qdofQCAHvq+g7ZxlbUhdHYnhKCUsr/i3GheSCEQQozxvoMGAgx9bQzGmBCC/w0hpJUulFJaKoAAXees894zxu/h/0AIhbYppJQl56fTcd221geEEHkCRqi27hZWSo7TeDyefAhSSkopeU7BOWeM8pKzsiyFkEIS8nTY1rWzzlSmCyF4b4xxe88PZSkfZ+mcizEOwwAA1lpjTDEdv71fLjGllPNbiinPaZ7nJcc53a9vKQ7jOE3TsJt23vu2bYvleP55uX5eth/LusV5PadrzNeUP+blY16uKZ9hHKEHgKG//dnvvPdN0xS/XvMacyurinCFmcJ0H6bJbRRhNb7NCzqgL+7bLj7TkrfVQ6drw6TgSspKk5IfMDoQ/EIwJcRRgSmhXzwK+96/jr4Luu6MrblwQjVS11xUlFeUa8odLRsq0P7KH23/Bv61bj0zKY2KAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f9eb8b0c4a4f642d316973fc108cae86/a0b58/7_1_1.webp 230w","/static/f9eb8b0c4a4f642d316973fc108cae86/bc10c/7_1_1.webp 460w","/static/f9eb8b0c4a4f642d316973fc108cae86/966d8/7_1_1.webp 920w","/static/f9eb8b0c4a4f642d316973fc108cae86/445df/7_1_1.webp 1380w","/static/f9eb8b0c4a4f642d316973fc108cae86/78de1/7_1_1.webp 1840w","/static/f9eb8b0c4a4f642d316973fc108cae86/a02b9/7_1_1.webp 1852w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f9eb8b0c4a4f642d316973fc108cae86/81c8e/7_1_1.png 230w","/static/f9eb8b0c4a4f642d316973fc108cae86/08a84/7_1_1.png 460w","/static/f9eb8b0c4a4f642d316973fc108cae86/c0255/7_1_1.png 920w","/static/f9eb8b0c4a4f642d316973fc108cae86/b1001/7_1_1.png 1380w","/static/f9eb8b0c4a4f642d316973fc108cae86/161ec/7_1_1.png 1840w","/static/f9eb8b0c4a4f642d316973fc108cae86/ec5f6/7_1_1.png 1852w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f9eb8b0c4a4f642d316973fc108cae86/c0255/7_1_1.png","alt":"7 1 1","title":"7 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet stannar upp vid en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":"-instruktion kommer ett interaktivt terminalfönster att öppnas. Här kan du skriva kod på samma sätt som i en normal Pythonterminal. Du ser hur koden fungerar exakt vid den här punkten i ditt program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" är speciellt nyttig då du vet att någon kodrad orsakar ett fel, men du är osäker på orsaken till det. Lägg till en breakpoint just före den problematiska kodraden och kör ditt program. Nu kan du testa på olika saker i den interaktiva terminalen och lista ut vad som måste ändras på i ditt program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att fortsätta körandet av programmet där det stannat upp. Kommandot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" (kort "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":") i terminalen får programmet att fortsätta tills nästa breakpoint nås. Den här bilden illustrerar en situation där en loop har upprepat några gånger:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/0ec6d7f1917313cc4582ecd977846154/c7bb6/7_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 44.78260869565218%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/ElEQVQoz42R22rDMAyG8xi7iGMpPqSuHZ8SzErrOlfd5WDQ93+VkXjLOhij34XQjyX9Qm5Yz0ajvbdqQwhBCOmeo0HEcTQ5X1JKgNB1HX2Cn2ajjyFGY91BKQDo/nImhLRtSzaqXJsB4IW2HVCOPWMMEevgfXwFEesrAFBKa2y0MXeV3qSfD5r1TAixmn+z12mt53k2xiilOOdCCERscs63S5l8GIN3zllr/cY0TSGEYRiklMMG5/xRCiGavJSP07IM9ojbWr+p54EHdrmufTqf7+X2/lpKSLPz1rnqHGP03u9WSqk9VqSUTbmWmGZ+GGiPgFANAb6S///5E+mjTgE+v0lMAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/0ec6d7f1917313cc4582ecd977846154/a0b58/7_1_2.webp 230w","/static/0ec6d7f1917313cc4582ecd977846154/bc10c/7_1_2.webp 460w","/static/0ec6d7f1917313cc4582ecd977846154/966d8/7_1_2.webp 920w","/static/0ec6d7f1917313cc4582ecd977846154/445df/7_1_2.webp 1380w","/static/0ec6d7f1917313cc4582ecd977846154/78de1/7_1_2.webp 1840w","/static/0ec6d7f1917313cc4582ecd977846154/ca269/7_1_2.webp 1986w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/0ec6d7f1917313cc4582ecd977846154/81c8e/7_1_2.png 230w","/static/0ec6d7f1917313cc4582ecd977846154/08a84/7_1_2.png 460w","/static/0ec6d7f1917313cc4582ecd977846154/c0255/7_1_2.png 920w","/static/0ec6d7f1917313cc4582ecd977846154/b1001/7_1_2.png 1380w","/static/0ec6d7f1917313cc4582ecd977846154/161ec/7_1_2.png 1840w","/static/0ec6d7f1917313cc4582ecd977846154/c7bb6/7_1_2.png 1986w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/0ec6d7f1917313cc4582ecd977846154/c0255/7_1_2.png","alt":"7 1 2","title":"7 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns också några andra kommandon tillgängliga i debuggningsterminalen. Du kan hitta dem "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/pdb.html#debugger-commands","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"här"}]},{"type":"text","value":" eller så kan du skriva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"help"}]},{"type":"text","value":" i debuggningsterminalen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/19fd7ec3fff52d6018519fac3d868e78/ca98b/7_1_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 42.608695652173914%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA5klEQVQoz4WPy07EMAxF+yF9pHnHTds4cVxpkNixYc3/fwuiURGgmeGsbEvH9u0WAAeg12XD5IIHAKnUdA9mJqJ1XUMIWmtrbXe83N7L7cMSCWuGqR+G8WI4+W7FSVvUJl2p9TXXt8S07nlPAUIT+r6fpmme50e7vuSM6BfYqRDzlvaUUjjx3m/bBgB/hF8yIjJzrRURY4zNPI4DEUspWutnMhGllBDRe99eHcexBZNSCiHGx3S11pxzjFEpNV807Wfg+zIz55yttVJKY4w+McYopZxz/1xupnMOAJxz/qLVQognmT8B8I9MXlB9in4AAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19fd7ec3fff52d6018519fac3d868e78/a0b58/7_1_3.webp 230w","/static/19fd7ec3fff52d6018519fac3d868e78/bc10c/7_1_3.webp 460w","/static/19fd7ec3fff52d6018519fac3d868e78/966d8/7_1_3.webp 920w","/static/19fd7ec3fff52d6018519fac3d868e78/445df/7_1_3.webp 1380w","/static/19fd7ec3fff52d6018519fac3d868e78/78de1/7_1_3.webp 1840w","/static/19fd7ec3fff52d6018519fac3d868e78/28038/7_1_3.webp 1968w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19fd7ec3fff52d6018519fac3d868e78/81c8e/7_1_3.png 230w","/static/19fd7ec3fff52d6018519fac3d868e78/08a84/7_1_3.png 460w","/static/19fd7ec3fff52d6018519fac3d868e78/c0255/7_1_3.png 920w","/static/19fd7ec3fff52d6018519fac3d868e78/b1001/7_1_3.png 1380w","/static/19fd7ec3fff52d6018519fac3d868e78/161ec/7_1_3.png 1840w","/static/19fd7ec3fff52d6018519fac3d868e78/ca98b/7_1_3.png 1968w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/19fd7ec3fff52d6018519fac3d868e78/c0255/7_1_3.png","alt":"7 1 3","title":"7 1 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kommandot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":" avslutar programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du är klar med debuggandet ska du minnas att ta bort "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":"-instruktionerna från din kod!"}]},{"type":"element","tagName":"h2","properties":{"id":"anvanda-moduler","style":"position:relative;"},"children":[{"type":"text","value":"Använda moduler"},{"type":"element","tagName":"a","properties":{"href":"#anvanda-moduler","ariaLabel":"anvanda moduler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python i sig innehåller redan en del nyttiga funktioner som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" (returnerar längden på en sträng eller en lista) och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" (returnerar summan av element i en datastruktur). Dessa hjälper dock dig som programmerare bara till en viss del. Pythons standardbibliotek är en samling av standardiserade funktioner och objekt som kan utöka Pythons \"uttryckskraft\". Vi har redan använt en del av funktionerna i Pythons standardbibliotek i de tidigare övningarna – till exempel då vi räknade med kvadratrötter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Standardbiblioteket består av moduler. De innehåller funktioner och klasser som är grupperade kring olika teman och funktionaliteter. I den här modulen kommer vi att bekanta oss med några nyttiga Python-moduler. Vi lär oss också att skapa egna moduler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" gör innehållet i en given modul tillgängligt i ett program. Vi tar en närmare titt på hur vi kan använda modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":". Den innehåller matematiska funktioner som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" för kvadratrot och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"log"}]},{"type":"text","value":" för logaritm."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kvadratroten av fem"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# logaritmen av åtta (bas två)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"log"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2.23606797749979\n3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionerna är definierade i modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":", så de måste hänvisas till med notationerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math.sqrt"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math.log"}]},{"type":"text","value":" i koden."}]},{"type":"element","tagName":"h2","properties":{"id":"valja-specifika-delar-fran-en-modul","style":"position:relative;"},"children":[{"type":"text","value":"Välja specifika delar från en modul"},{"type":"element","tagName":"a","properties":{"href":"#valja-specifika-delar-fran-en-modul","ariaLabel":"valja specifika delar fran en modul permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt att använda moduler är att välja specifika delar av modulen med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":"-instruktionen. Om vi endast vill använda funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"log"}]},{"type":"text","value":" från modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":", kan vi göra på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" log\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"log"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du ser ovan, behöver vi inte "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":"-prefixet då vi importerar och använder funktioner på det här sättet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vid behov kan man använda sig av en asterisk för att importera allt innehåll i en modul:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"log"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här sättet att importera moduler kan vara nyttigt då man testar på något eller arbetar med ett mindre projekt. Det kan däremot orsaka problem, vilket vi kommer att se lite senare."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hypotenusa","tmcname":"osa07-01_hypotenusa"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hypotenusa(katet1: float, katet2: float)"}]},{"type":"text","value":" som får som argument kateternas längder i en rätvinklig triangel. Funktionen ska returnera hypotenusans längd."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Pythagoras_sats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pythagoras sats"}]},{"type":"text","value":". Kvadratroten kan du räkna med den relevanta funktionen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":"-modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hypotenusa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 5.0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hypotenusa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 13.0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hypotenusa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 1.4142135623730951"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"innehallet-i-en-modul","style":"position:relative;"},"children":[{"type":"text","value":"Innehållet i en modul"},{"type":"element","tagName":"a","properties":{"href":"#innehallet-i-en-modul","ariaLabel":"innehallet i en modul permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons dokumentation inkluderar mycket information om alla moduler som är tillgängliga i standardbiblioteket. Dokumentationen berättar om funktioner och metoder som är definierade i en modul, och därtill får man veta hur modulen kan användas. Som ett exempel följer en länk till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":"-modulens dokumentation:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://docs.python.org/3/library/math.html"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också kolla på innehållet i en modul med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här funktionen returnerar en lista av namn definierade av modulen. Det kan vara namn på klasser, fixerade värden eller funktioner:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Specialtecken","tmcname":"osa07-02_specialtecken"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/string.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"string"}]}]},{"type":"text","value":" finns strängkonstanter som definiar specifika teckengrupper (t.ex. gemener och skiljetecken). Bekanta dig med dessa konstanter och skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dela_upp(strang: str)"}]},{"type":"text","value":" som får som argument en sträng som ska returneras uppdelad i tre delar:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en sträng med alla gemener och versaler (enligt engelska alfabetet), konstanten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ascii_letters"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en sträng med tecknen som definieras i konstanten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"punctuation"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en sträng med de resterade tecknen (t.ex. mellanslag)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tecknen ska förekomma i samma ordning som i den ursprungliga strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dela_upp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett test, fungerar det?? åå\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dethrretttestfungerardet\n,??\nä ä åå"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bråk","tmcname":"osa07-03_brak"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bekanta dig med Pythonmodulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractions"}]},{"type":"text","value":" och implementera funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"delar(antal: int)"}]},{"type":"text","value":" som får som argument ett antal delar. Funktionen ska dela upp talet ett i så här många delar och returnera delarna i en lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" p "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"p"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1/3\n1/3\n1/3"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5)]"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"bd59e044-7533-5711-839f-e065dedd666d"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad en Python-modul är
  • \n
  • vet du hur man inkluderar en modul i ett program med import-satsen
  • \n
  • vet du hur man hittar mera information om innehållet i en modul.
  • \n

Lite mera om debuggande

Vi har redan bekantat oss med en hel del olika debuggningsmetoder under den här kursen. Visualiseringsverktyget och print-satser för debuggning är redan bekanta för dig. Du har kanske också testat på det inbyggda debuggningsverktyget i Visual Studio Code. Om du har problem med att debuggaren inte hittar dina filer, kan du bekanta dig med några tips från den förra modulen.

Sedan versionen 3.7 av Python finns ett ytterligare sätt att debugga program: instruktionen breakpoint().

Du kan lägga till den här instruktionen på valfritt ställe i din kod (kom dock ihåg att följa reglerna som gäller syntax). När programmet körs, kommer programmet att stanna på det ställe där breakpoint-instruktionen finns. Här är ett exempel där vi debuggar en uppgiftslösning från den förra modulen:

\n \n \n \n \n \n \"7\n \n \n

När programmet stannar upp vid en breakpoint-instruktion kommer ett interaktivt terminalfönster att öppnas. Här kan du skriva kod på samma sätt som i en normal Pythonterminal. Du ser hur koden fungerar exakt vid den här punkten i ditt program.

Instruktionen breakpoint är speciellt nyttig då du vet att någon kodrad orsakar ett fel, men du är osäker på orsaken till det. Lägg till en breakpoint just före den problematiska kodraden och kör ditt program. Nu kan du testa på olika saker i den interaktiva terminalen och lista ut vad som måste ändras på i ditt program.

Det är också möjligt att fortsätta körandet av programmet där det stannat upp. Kommandot continue (kort c) i terminalen får programmet att fortsätta tills nästa breakpoint nås. Den här bilden illustrerar en situation där en loop har upprepat några gånger:

\n \n \n \n \n \n \"7\n \n \n

Det finns också några andra kommandon tillgängliga i debuggningsterminalen. Du kan hitta dem här eller så kan du skriva help i debuggningsterminalen:

\n \n

Skapa funktionen hypotenusa(katet1: float, katet2: float) som får som argument kateternas längder i en rätvinklig triangel. Funktionen ska returnera hypotenusans längd.

Använd Pythagoras sats. Kvadratroten kan du räkna med den relevanta funktionen i math-modulen.

Exempel:

print(hypotenusa(3,4)) # 5.0\nprint(hypotenusa(5,12)) # 13.0\nprint(hypotenusa(1,1)) # 1.4142135623730951

Innehållet i en modul

Pythons dokumentation inkluderar mycket information om alla moduler som är tillgängliga i standardbiblioteket. Dokumentationen berättar om funktioner och metoder som är definierade i en modul, och därtill får man veta hur modulen kan användas. Som ett exempel följer en länk till math-modulens dokumentation:

Vi kan också kolla på innehållet i en modul med funktionen dir:

import math\n\nprint(dir(math))

Den här funktionen returnerar en lista av namn definierade av modulen. Det kan vara namn på klasser, fixerade värden eller funktioner:

['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

I modulen string finns strängkonstanter som definiar specifika teckengrupper (t.ex. gemener och skiljetecken). Bekanta dig med dessa konstanter och skapa funktionen dela_upp(strang: str) som får som argument en sträng som ska returneras uppdelad i tre delar:

    \n
  1. en sträng med alla gemener och versaler (enligt engelska alfabetet), konstanten ascii_letters
  2. \n
  3. en sträng med tecknen som definieras i konstanten punctuation
  4. \n
  5. en sträng med de resterade tecknen (t.ex. mellanslag)
  6. \n

Tecknen ska förekomma i samma ordning som i den ursprungliga strängen.

Exempel:

delar = dela_upp(\"Det här är ett test, fungerar det?? åå\")\nprint(delar[0])\nprint(delar[1])\nprint(delar[2])

Dethrretttestfungerardet\n,??\nä ä åå

Bekanta dig med Pythonmodulen fractions och implementera funktionen delar(antal: int) som får som argument ett antal delar. Funktionen ska dela upp talet ett i så här många delar och returnera delarna i en lista.

Exempel:

for p in delar(3):\n    print(p)\n\nprint()\n\nprint(delar(5))

1/3\n1/3\n1/3

[Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5)]

","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/1-moduler.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad en Python-modul är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man inkluderar en modul i ett program med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du hur man hittar mera information om innehållet i en modul."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lite-mera-om-debuggande","style":"position:relative;"},"children":[{"type":"text","value":"Lite mera om debuggande"},{"type":"element","tagName":"a","properties":{"href":"#lite-mera-om-debuggande","ariaLabel":"lite mera om debuggande permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan bekantat oss med en hel del olika debuggningsmetoder under den här kursen. "},{"type":"element","tagName":"a","properties":{"href":"https://pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":"-satser för debuggning är redan bekanta för dig. Du har kanske också testat på det inbyggda debuggningsverktyget i Visual Studio Code. Om du har problem med att debuggaren inte hittar dina filer, kan du bekanta dig med några tips från den förra modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sedan versionen 3.7 av Python finns ett ytterligare sätt att debugga program: instruktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/functions.html#breakpoint","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint()"}]}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan lägga till den här instruktionen på valfritt ställe i din kod (kom dock ihåg att följa reglerna som gäller syntax). När programmet körs, kommer programmet att stanna på det ställe där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":"-instruktionen finns. Här är ett exempel där vi debuggar en uppgiftslösning från den förra modulen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f9eb8b0c4a4f642d316973fc108cae86/ec5f6/7_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 62.173913043478265%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAABYlAAAWJQFJUiTwAAABeklEQVQoz43R247bIBQFUH/HhKu5GIMNPr40mQwYT+yM2qn6/79TxcnDqGqrLB0J8bBB2qdofQCAHvq+g7ZxlbUhdHYnhKCUsr/i3GheSCEQQozxvoMGAgx9bQzGmBCC/w0hpJUulFJaKoAAXees894zxu/h/0AIhbYppJQl56fTcd221geEEHkCRqi27hZWSo7TeDyefAhSSkopeU7BOWeM8pKzsiyFkEIS8nTY1rWzzlSmCyF4b4xxe88PZSkfZ+mcizEOwwAA1lpjTDEdv71fLjGllPNbiinPaZ7nJcc53a9vKQ7jOE3TsJt23vu2bYvleP55uX5eth/LusV5PadrzNeUP+blY16uKZ9hHKEHgKG//dnvvPdN0xS/XvMacyurinCFmcJ0H6bJbRRhNb7NCzqgL+7bLj7TkrfVQ6drw6TgSspKk5IfMDoQ/EIwJcRRgSmhXzwK+96/jr4Luu6MrblwQjVS11xUlFeUa8odLRsq0P7KH23/Bv61bj0zKY2KAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f9eb8b0c4a4f642d316973fc108cae86/a0b58/7_1_1.webp 230w","/static/f9eb8b0c4a4f642d316973fc108cae86/bc10c/7_1_1.webp 460w","/static/f9eb8b0c4a4f642d316973fc108cae86/966d8/7_1_1.webp 920w","/static/f9eb8b0c4a4f642d316973fc108cae86/445df/7_1_1.webp 1380w","/static/f9eb8b0c4a4f642d316973fc108cae86/78de1/7_1_1.webp 1840w","/static/f9eb8b0c4a4f642d316973fc108cae86/a02b9/7_1_1.webp 1852w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f9eb8b0c4a4f642d316973fc108cae86/81c8e/7_1_1.png 230w","/static/f9eb8b0c4a4f642d316973fc108cae86/08a84/7_1_1.png 460w","/static/f9eb8b0c4a4f642d316973fc108cae86/c0255/7_1_1.png 920w","/static/f9eb8b0c4a4f642d316973fc108cae86/b1001/7_1_1.png 1380w","/static/f9eb8b0c4a4f642d316973fc108cae86/161ec/7_1_1.png 1840w","/static/f9eb8b0c4a4f642d316973fc108cae86/ec5f6/7_1_1.png 1852w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f9eb8b0c4a4f642d316973fc108cae86/c0255/7_1_1.png","alt":"7 1 1","title":"7 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När programmet stannar upp vid en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":"-instruktion kommer ett interaktivt terminalfönster att öppnas. Här kan du skriva kod på samma sätt som i en normal Pythonterminal. Du ser hur koden fungerar exakt vid den här punkten i ditt program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" är speciellt nyttig då du vet att någon kodrad orsakar ett fel, men du är osäker på orsaken till det. Lägg till en breakpoint just före den problematiska kodraden och kör ditt program. Nu kan du testa på olika saker i den interaktiva terminalen och lista ut vad som måste ändras på i ditt program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att fortsätta körandet av programmet där det stannat upp. Kommandot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" (kort "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":") i terminalen får programmet att fortsätta tills nästa breakpoint nås. Den här bilden illustrerar en situation där en loop har upprepat några gånger:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/0ec6d7f1917313cc4582ecd977846154/c7bb6/7_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 44.78260869565218%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/ElEQVQoz42R22rDMAyG8xi7iGMpPqSuHZ8SzErrOlfd5WDQ93+VkXjLOhij34XQjyX9Qm5Yz0ajvbdqQwhBCOmeo0HEcTQ5X1JKgNB1HX2Cn2ajjyFGY91BKQDo/nImhLRtSzaqXJsB4IW2HVCOPWMMEevgfXwFEesrAFBKa2y0MXeV3qSfD5r1TAixmn+z12mt53k2xiilOOdCCERscs63S5l8GIN3zllr/cY0TSGEYRiklMMG5/xRCiGavJSP07IM9ojbWr+p54EHdrmufTqf7+X2/lpKSLPz1rnqHGP03u9WSqk9VqSUTbmWmGZ+GGiPgFANAb6S///5E+mjTgE+v0lMAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/0ec6d7f1917313cc4582ecd977846154/a0b58/7_1_2.webp 230w","/static/0ec6d7f1917313cc4582ecd977846154/bc10c/7_1_2.webp 460w","/static/0ec6d7f1917313cc4582ecd977846154/966d8/7_1_2.webp 920w","/static/0ec6d7f1917313cc4582ecd977846154/445df/7_1_2.webp 1380w","/static/0ec6d7f1917313cc4582ecd977846154/78de1/7_1_2.webp 1840w","/static/0ec6d7f1917313cc4582ecd977846154/ca269/7_1_2.webp 1986w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/0ec6d7f1917313cc4582ecd977846154/81c8e/7_1_2.png 230w","/static/0ec6d7f1917313cc4582ecd977846154/08a84/7_1_2.png 460w","/static/0ec6d7f1917313cc4582ecd977846154/c0255/7_1_2.png 920w","/static/0ec6d7f1917313cc4582ecd977846154/b1001/7_1_2.png 1380w","/static/0ec6d7f1917313cc4582ecd977846154/161ec/7_1_2.png 1840w","/static/0ec6d7f1917313cc4582ecd977846154/c7bb6/7_1_2.png 1986w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/0ec6d7f1917313cc4582ecd977846154/c0255/7_1_2.png","alt":"7 1 2","title":"7 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns också några andra kommandon tillgängliga i debuggningsterminalen. Du kan hitta dem "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/pdb.html#debugger-commands","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"här"}]},{"type":"text","value":" eller så kan du skriva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"help"}]},{"type":"text","value":" i debuggningsterminalen:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/19fd7ec3fff52d6018519fac3d868e78/ca98b/7_1_3.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 42.608695652173914%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA5klEQVQoz4WPy07EMAxF+yF9pHnHTds4cVxpkNixYc3/fwuiURGgmeGsbEvH9u0WAAeg12XD5IIHAKnUdA9mJqJ1XUMIWmtrbXe83N7L7cMSCWuGqR+G8WI4+W7FSVvUJl2p9TXXt8S07nlPAUIT+r6fpmme50e7vuSM6BfYqRDzlvaUUjjx3m/bBgB/hF8yIjJzrRURY4zNPI4DEUspWutnMhGllBDRe99eHcexBZNSCiHGx3S11pxzjFEpNV807Wfg+zIz55yttVJKY4w+McYopZxz/1xupnMOAJxz/qLVQognmT8B8I9MXlB9in4AAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19fd7ec3fff52d6018519fac3d868e78/a0b58/7_1_3.webp 230w","/static/19fd7ec3fff52d6018519fac3d868e78/bc10c/7_1_3.webp 460w","/static/19fd7ec3fff52d6018519fac3d868e78/966d8/7_1_3.webp 920w","/static/19fd7ec3fff52d6018519fac3d868e78/445df/7_1_3.webp 1380w","/static/19fd7ec3fff52d6018519fac3d868e78/78de1/7_1_3.webp 1840w","/static/19fd7ec3fff52d6018519fac3d868e78/28038/7_1_3.webp 1968w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19fd7ec3fff52d6018519fac3d868e78/81c8e/7_1_3.png 230w","/static/19fd7ec3fff52d6018519fac3d868e78/08a84/7_1_3.png 460w","/static/19fd7ec3fff52d6018519fac3d868e78/c0255/7_1_3.png 920w","/static/19fd7ec3fff52d6018519fac3d868e78/b1001/7_1_3.png 1380w","/static/19fd7ec3fff52d6018519fac3d868e78/161ec/7_1_3.png 1840w","/static/19fd7ec3fff52d6018519fac3d868e78/ca98b/7_1_3.png 1968w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/19fd7ec3fff52d6018519fac3d868e78/c0255/7_1_3.png","alt":"7 1 3","title":"7 1 3","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kommandot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":" avslutar programmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du är klar med debuggandet ska du minnas att ta bort "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":"-instruktionerna från din kod!"}]},{"type":"element","tagName":"h2","properties":{"id":"anvanda-moduler","style":"position:relative;"},"children":[{"type":"text","value":"Använda moduler"},{"type":"element","tagName":"a","properties":{"href":"#anvanda-moduler","ariaLabel":"anvanda moduler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python i sig innehåller redan en del nyttiga funktioner som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" (returnerar längden på en sträng eller en lista) och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" (returnerar summan av element i en datastruktur). Dessa hjälper dock dig som programmerare bara till en viss del. Pythons standardbibliotek är en samling av standardiserade funktioner och objekt som kan utöka Pythons \"uttryckskraft\". Vi har redan använt en del av funktionerna i Pythons standardbibliotek i de tidigare övningarna – till exempel då vi räknade med kvadratrötter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Standardbiblioteket består av moduler. De innehåller funktioner och klasser som är grupperade kring olika teman och funktionaliteter. I den här modulen kommer vi att bekanta oss med några nyttiga Python-moduler. Vi lär oss också att skapa egna moduler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" gör innehållet i en given modul tillgängligt i ett program. Vi tar en närmare titt på hur vi kan använda modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":". Den innehåller matematiska funktioner som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" för kvadratrot och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"log"}]},{"type":"text","value":" för logaritm."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kvadratroten av fem"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# logaritmen av åtta (bas två)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"log"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2.23606797749979\n3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionerna är definierade i modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":", så de måste hänvisas till med notationerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math.sqrt"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math.log"}]},{"type":"text","value":" i koden."}]},{"type":"element","tagName":"h2","properties":{"id":"valja-specifika-delar-fran-en-modul","style":"position:relative;"},"children":[{"type":"text","value":"Välja specifika delar från en modul"},{"type":"element","tagName":"a","properties":{"href":"#valja-specifika-delar-fran-en-modul","ariaLabel":"valja specifika delar fran en modul permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt att använda moduler är att välja specifika delar av modulen med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":"-instruktionen. Om vi endast vill använda funktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"log"}]},{"type":"text","value":" från modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":", kan vi göra på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" log\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"log"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du ser ovan, behöver vi inte "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":"-prefixet då vi importerar och använder funktioner på det här sättet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vid behov kan man använda sig av en asterisk för att importera allt innehåll i en modul:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"log"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här sättet att importera moduler kan vara nyttigt då man testar på något eller arbetar med ett mindre projekt. Det kan däremot orsaka problem, vilket vi kommer att se lite senare."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hypotenusa","tmcname":"osa07-01_hypotenusa"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hypotenusa(katet1: float, katet2: float)"}]},{"type":"text","value":" som får som argument kateternas längder i en rätvinklig triangel. Funktionen ska returnera hypotenusans längd."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Använd "},{"type":"element","tagName":"a","properties":{"href":"https://sv.wikipedia.org/wiki/Pythagoras_sats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pythagoras sats"}]},{"type":"text","value":". Kvadratroten kan du räkna med den relevanta funktionen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":"-modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hypotenusa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 5.0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hypotenusa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 13.0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hypotenusa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 1.4142135623730951"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"innehallet-i-en-modul","style":"position:relative;"},"children":[{"type":"text","value":"Innehållet i en modul"},{"type":"element","tagName":"a","properties":{"href":"#innehallet-i-en-modul","ariaLabel":"innehallet i en modul permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons dokumentation inkluderar mycket information om alla moduler som är tillgängliga i standardbiblioteket. Dokumentationen berättar om funktioner och metoder som är definierade i en modul, och därtill får man veta hur modulen kan användas. Som ett exempel följer en länk till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":"-modulens dokumentation:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://docs.python.org/3/library/math.html"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också kolla på innehållet i en modul med funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här funktionen returnerar en lista av namn definierade av modulen. Det kan vara namn på klasser, fixerade värden eller funktioner:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Specialtecken","tmcname":"osa07-02_specialtecken"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/string.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"string"}]}]},{"type":"text","value":" finns strängkonstanter som definiar specifika teckengrupper (t.ex. gemener och skiljetecken). Bekanta dig med dessa konstanter och skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dela_upp(strang: str)"}]},{"type":"text","value":" som får som argument en sträng som ska returneras uppdelad i tre delar:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en sträng med alla gemener och versaler (enligt engelska alfabetet), konstanten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ascii_letters"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en sträng med tecknen som definieras i konstanten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"punctuation"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en sträng med de resterade tecknen (t.ex. mellanslag)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tecknen ska förekomma i samma ordning som i den ursprungliga strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dela_upp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett test, fungerar det?? åå\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dethrretttestfungerardet\n,??\nä ä åå"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bråk","tmcname":"osa07-03_brak"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bekanta dig med Pythonmodulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractions"}]},{"type":"text","value":" och implementera funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"delar(antal: int)"}]},{"type":"text","value":" som får som argument ett antal delar. Funktionen ska dela upp talet ett i så här många delar och returnera delarna i en lista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" p "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"p"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1/3\n1/3\n1/3"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5)]"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"bd59e044-7533-5711-839f-e065dedd666d"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • vet du vad en Python-modul är
  • \n
  • vet du hur man inkluderar en modul i ett program med import-satsen
  • \n
  • vet du hur man hittar mera information om innehållet i en modul.
  • \n

Lite mera om debuggande

Vi har redan bekantat oss med en hel del olika debuggningsmetoder under den här kursen. Visualiseringsverktyget och print-satser för debuggning är redan bekanta för dig. Du har kanske också testat på det inbyggda debuggningsverktyget i Visual Studio Code. Om du har problem med att debuggaren inte hittar dina filer, kan du bekanta dig med några tips från den förra modulen.

Sedan versionen 3.7 av Python finns ett ytterligare sätt att debugga program: instruktionen breakpoint().

Du kan lägga till den här instruktionen på valfritt ställe i din kod (kom dock ihåg att följa reglerna som gäller syntax). När programmet körs, kommer programmet att stanna på det ställe där breakpoint-instruktionen finns. Här är ett exempel där vi debuggar en uppgiftslösning från den förra modulen:

\n \n \n \n \n \n \"7\n \n \n

När programmet stannar upp vid en breakpoint-instruktion kommer ett interaktivt terminalfönster att öppnas. Här kan du skriva kod på samma sätt som i en normal Pythonterminal. Du ser hur koden fungerar exakt vid den här punkten i ditt program.

Instruktionen breakpoint är speciellt nyttig då du vet att någon kodrad orsakar ett fel, men du är osäker på orsaken till det. Lägg till en breakpoint just före den problematiska kodraden och kör ditt program. Nu kan du testa på olika saker i den interaktiva terminalen och lista ut vad som måste ändras på i ditt program.

Det är också möjligt att fortsätta körandet av programmet där det stannat upp. Kommandot continue (kort c) i terminalen får programmet att fortsätta tills nästa breakpoint nås. Den här bilden illustrerar en situation där en loop har upprepat några gånger:

\n \n \n \n \n \n \"7\n \n \n

Det finns också några andra kommandon tillgängliga i debuggningsterminalen. Du kan hitta dem här eller så kan du skriva help i debuggningsterminalen:

\n \n

Skapa funktionen hypotenusa(katet1: float, katet2: float) som får som argument kateternas längder i en rätvinklig triangel. Funktionen ska returnera hypotenusans längd.

Använd Pythagoras sats. Kvadratroten kan du räkna med den relevanta funktionen i math-modulen.

Exempel:

print(hypotenusa(3,4)) # 5.0\nprint(hypotenusa(5,12)) # 13.0\nprint(hypotenusa(1,1)) # 1.4142135623730951

Innehållet i en modul

Pythons dokumentation inkluderar mycket information om alla moduler som är tillgängliga i standardbiblioteket. Dokumentationen berättar om funktioner och metoder som är definierade i en modul, och därtill får man veta hur modulen kan användas. Som ett exempel följer en länk till math-modulens dokumentation:

Vi kan också kolla på innehållet i en modul med funktionen dir:

import math\n\nprint(dir(math))

Den här funktionen returnerar en lista av namn definierade av modulen. Det kan vara namn på klasser, fixerade värden eller funktioner:

['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

I modulen string finns strängkonstanter som definiar specifika teckengrupper (t.ex. gemener och skiljetecken). Bekanta dig med dessa konstanter och skapa funktionen dela_upp(strang: str) som får som argument en sträng som ska returneras uppdelad i tre delar:

    \n
  1. en sträng med alla gemener och versaler (enligt engelska alfabetet), konstanten ascii_letters
  2. \n
  3. en sträng med tecknen som definieras i konstanten punctuation
  4. \n
  5. en sträng med de resterade tecknen (t.ex. mellanslag)
  6. \n

Tecknen ska förekomma i samma ordning som i den ursprungliga strängen.

Exempel:

delar = dela_upp(\"Det här är ett test, fungerar det?? åå\")\nprint(delar[0])\nprint(delar[1])\nprint(delar[2])

Dethrretttestfungerardet\n,??\nä ä åå

Bekanta dig med Pythonmodulen fractions och implementera funktionen delar(antal: int) som får som argument ett antal delar. Funktionen ska dela upp talet ett i så här många delar och returnera delarna i en lista.

Exempel:

for p in delar(3):\n    print(p)\n\nprint()\n\nprint(delar(5))

1/3\n1/3\n1/3

[Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5)]

","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/1-moduler.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-7/2-slumpmassighet/page-data.json b/page-data/osa-7/2-slumpmassighet/page-data.json index e5f4eca5a..c68b196e1 100644 --- a/page-data/osa-7/2-slumpmassighet/page-data.json +++ b/page-data/osa-7/2-slumpmassighet/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-7/2-slumpmassighet", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till några av funktionerna i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":"-modulen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av slumpmässiga tal i dina program."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här delen ser vi på "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]}]},{"type":"text","value":" i Pythons standardbibliotek. Den här modulen innehåller verktyg för att skapa slumpmässiga tal och för annan funktionalitet med ett slumpmässigt element."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Delarna i den här modulen innehåller flera länkar till "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"standardbibliotekets dokumentation"}]},{"type":"text","value":". Vi rekommenderar att du följer länkarna så att du kan bekanta dig med hur dokumentationen ser ut."}]},{"type":"element","tagName":"h2","properties":{"id":"skapa-ett-slumpmassigt-tal","style":"position:relative;"},"children":[{"type":"text","value":"Skapa ett slumpmässigt tal"},{"type":"element","tagName":"a","properties":{"href":"#skapa-ett-slumpmassigt-tal","ariaLabel":"skapa ett slumpmassigt tal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html#random.randint","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint(a, b)"}]}]},{"type":"text","value":" returnerar ett slumpmässigt heltal mellan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" (inklusive start- och slutpunkten). Till exempel det här programmet fungerar som en normal tärning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tärningen ger:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan utskriften se ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tärningen ger: 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet kastar en tärning tio gånger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tärningen ger:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften skulle kunna se ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tärningen ger: 5\nTärningen ger: 4\nTärningen ger: 3\nTärningen ger: 2\nTärningen ger: 3\nTärningen ger: 4\nTärningen ger: 6\nTärningen ger: 4\nTärningen ger: 4\nTärningen ger: 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Det är värt att notera att funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint"}]},{"type":"text","value":" fungerar lite olikt än till exempel extrahering (slicing) eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-funktionen. Funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint(1, 6)"}]},{"type":"text","value":" resulterar i ett nummer mellan 1 och 6, medan anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(1, 6)"}]},{"type":"text","value":" resulterar i ett intervall från 1 till 5."}]},{"type":"element","tagName":"h2","properties":{"id":"flera-slumpmassighetsfunktioner","style":"position:relative;"},"children":[{"type":"text","value":"Flera slumpmässighetsfunktioner"},{"type":"element","tagName":"a","properties":{"href":"#flera-slumpmassighetsfunktioner","ariaLabel":"flera slumpmassighetsfunktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html#random.shuffle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shuffle"}]}]},{"type":"text","value":" blandar elementen i den datastruktur som ges som argument. Till exempel följande program blandar en lista med ord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nshuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['banan', 'apa', 'cembalo']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"choice"}]},{"type":"text","value":" returnerar ett slumpmässigt valt element från en datastruktur:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" choice\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"'cembalo'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lottorad","style":"position:relative;"},"children":[{"type":"text","value":"Lottorad"},{"type":"element","tagName":"a","properties":{"href":"#lottorad","ariaLabel":"lottorad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett vanligt sätt att undersöka slumpmässighet är att se på lottorader. Låt oss lotta ut en vinnande rad. I Finland består lottoraden av sju siffror mellan 1 och 40."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kunde det se ut när vi försöker lotta ut en rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här skulle inte fungera i det långa loppet eftersom ett och samma nummer kan dyka upp flera gånger på samma lottorad. Vi måste se till att alla nummer är unika."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett sätt är att lagra de lottade siffrorna i en lista. Då lägger vi bara till en ny siffra om siffran inte redan finns i listan. Vi använder oss av en loop som fortsätter tills listans längd är sju:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\nrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" ny "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ny"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också spara lite utrymme genom att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shuffle"}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\nalla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén här är att vi skapar en lista med siffrorna 1 till 40, lite som att vi skulle ha 40 bollar i en lotterimaskin. Listan blandas sedan, varefter de första sju siffrorna utgör veckans vinnande rad. Nu behöver vi ingen loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" innehåller faktiskt ett ännu enklare sätt att skapa vår lottorad: funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html#random.sample","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sample"}]}]},{"type":"text","value":". Den returnerar ett slumpmässigt val av en specifik storlek från en given datastruktur:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sample\n\nalla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sample"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lottorader","tmcname":"osa07-04_lottorader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lottorad(siffror: int, nedre: int, ovre: int)"}]},{"type":"text","value":" som lottar ut det givna antalet siffror i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nedre-ovre"}]},{"type":"text","value":" och returnerar dessa i en ordnad lista (börjandes med det minsta talet)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma siffra får inte förekomma flera gånger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lottorad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4\n7\n11\n16\n22\n29\n38"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"varifran-kommer-dessa-slumpmassiga-siffror-ifran","style":"position:relative;"},"children":[{"type":"text","value":"Varifrån kommer dessa slumpmässiga siffror ifrån?"},{"type":"element","tagName":"a","properties":{"href":"#varifran-kommer-dessa-slumpmassiga-siffror-ifran","ariaLabel":"varifran kommer dessa slumpmassiga siffror ifran permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionaliteten i "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":"-modulen"}]},{"type":"text","value":" är baserad på en algoritm som skapar slumpmässiga siffror på basis av ett specifikt startvärde och några matematiska operationer. Startvärdet kallas ofta seed value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan själva ge ett sådant värde med "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html#random.seed","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"seed"}]}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" seed\n\nseed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1337"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här genererar alltid samma tal"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi har funktioner som baserar sig på slumpmässighet och har valt ett seed-värde, kommer funktionen att ge samma resultat varje gång den körs. Resultatet kan skilja sig mellan olika Python-versioner, men i grunden kommer slumpmässigheten att försvinna då vi definierar seed-värdet. Funktionaliteten kan dock vara nyttig då vi exempelvis testar på vårt program."}]},{"type":"element","tagName":"text-box","properties":{"variant":"info","name":"Sann slumpmässighet"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att vara korrekt, är de siffor som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":"-modulen ger inte i verkligheten slumpmässiga. Istället är de skenbart slumpmässiga (pseudorandom). Datorers funktionalitet är förutsebar och i en ideal situation kan man exakt bestämma hur de fungerar. Därmed är det mycket svårt att skapa sant slumpmässiga tal med en dator. I flera situationer räcket skenbart slumpmässiga tal. När sant slumpmässiga tal behövs skapas seed-värdet på basis av någon yttre källa som radioaktiv bakgrundsstrålning, ljudnivå eller lavalampor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För ytterligare information om slumpmässighet, se "},{"type":"element","tagName":"a","properties":{"href":"https://www.random.org/randomness/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"random.org"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lösenordsgenerator, del 1","tmcname":"osa07-05_losenord_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion som genererar slumpmässiga lösenord, av vald längd, bestående av bokstäverna a-z."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skapa_losenord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"lttehepy\nolsxttjl\ncbjncrzo\ndwxqjdgu\ngpfdcecs\njabyvgar\nxnbbonbl\nktmsjyww\nejhprmel\nrjkoacib"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lösenordsgenerator, del 2","tmcname":"osa07-06_losenord_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förbättra förra uppgiftens funktion. Nu tar den emot tre argument:"}]},{"type":"element","tagName":"ol","properties":{"start":2},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" ska lösenordet innehålla minst en siffra"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" ska lösenordet innehålla minst ett av tecknen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!?=+-()#"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lösenordet ska alltid innehålla minst en bokstav. Du kan anta att funktionen anropas med argument som möjliggör skapandet av önskat lösenord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skapa_bra_losenord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2?0n+u31\nu=m4nl94\nn#=i6r#(\nda9?zvm?\n7h)!)g?!\na=59x2n5\n(jr6n3b5\n9n(4i+2!\n32+qba#=\nn?b0a7ey"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Tärningssimulation","tmcname":"osa07-07_tarningar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skapar nu några funktioner som vi kan använda i spel som kräver en tärning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Istället för en normal tärning använder vi icke-transitiva tärningar. Se den här "},{"type":"element","tagName":"a","properties":{"href":"https://singingbanana.com/dice/article.htm","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"artikeln"}]},{"type":"text","value":" och "},{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/LrIp6CKUlH8","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"videon"}]},{"type":"text","value":" vid behov."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har tre tärningar:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tärning A med siffrorna 3, 3, 3, 3, 3, 6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tärning B med siffrorna 2, 2, 2, 5, 5, 5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tärning C med siffrorna 1, 4, 4, 4, 4, 4"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kasta(tarning: str)"}]},{"type":"text","value":" som kastar den valda tärningen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 6 3\n2 2 5 2 2 5 5 2 2 5 2 5 5 5 2 5 2 2 2 2\n4 4 4 4 4 1 1 4 4 4 1 4 4 4 4 4 4 4 4 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ännu funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spela(tarning1: str, tarning2: str, ganger: int)"}]},{"type":"text","value":" som kastar de valda tärningarna angivna antalet gånger. Funktionen ska returnera en tuple som berättar antalet vinster med tärning ett respektive två samt antalet oavgjoda rundor."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(292, 708, 0)\n(249, 273, 478)"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Slumpmässiga ord","tmcname":"osa07-08_slumpmassiga_ord"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften har du filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord.txt"}]},{"type":"text","value":" till ditt förfogande. Filen innehåller engelska ord, ett ord på varsin rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord(n: int, borjar: str)"}]},{"type":"text","value":" som returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" slumpmässigt valda ord som börjar med den valda strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om funktionen anropas med argumenten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord(3, \"ca\")"}]},{"type":"text","value":" kan t.ex. orden cat, car och carbon returneras i listan. Samma ord får inte förekomma flera gånger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om tillräckligt många ord inte hittas ska undantaget "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" åstadkommas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ca\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"cat\ncar\ncarbon"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • känner du till några av funktionerna i random-modulen
  • \n
  • kan du använda dig av slumpmässiga tal i dina program.
  • \n

I den här delen ser vi på modulen random i Pythons standardbibliotek. Den här modulen innehåller verktyg för att skapa slumpmässiga tal och för annan funktionalitet med ett slumpmässigt element.

Delarna i den här modulen innehåller flera länkar till standardbibliotekets dokumentation. Vi rekommenderar att du följer länkarna så att du kan bekanta dig med hur dokumentationen ser ut.

Skapa ett slumpmässigt tal

Funktionen randint(a, b) returnerar ett slumpmässigt heltal mellan a och b (inklusive start- och slutpunkten). Till exempel det här programmet fungerar som en normal tärning:

from random import randint\n\nprint(\"Tärningen ger:\", randint(1, 6))

Så här kan utskriften se ut:

Tärningen ger: 4

Det här programmet kastar en tärning tio gånger:

from random import randint\n\nfor i in range(10):\n    print(\"Tärningen ger:\", randint(1, 6))

Utskriften skulle kunna se ut så här:

Tärningen ger: 5\nTärningen ger: 4\nTärningen ger: 3\nTärningen ger: 2\nTärningen ger: 3\nTärningen ger: 4\nTärningen ger: 6\nTärningen ger: 4\nTärningen ger: 4\nTärningen ger: 3

Obs! Det är värt att notera att funktionen randint fungerar lite olikt än till exempel extrahering (slicing) eller range-funktionen. Funktionsanropet randint(1, 6) resulterar i ett nummer mellan 1 och 6, medan anropet range(1, 6) resulterar i ett intervall från 1 till 5.

Flera slumpmässighetsfunktioner

Funktionen shuffle blandar elementen i den datastruktur som ges som argument. Till exempel följande program blandar en lista med ord:

from random import shuffle\n\nord = [\"apa\", \"banan\", \"cembalo\"]\nshuffle(ord)\nprint(ord)

['banan', 'apa', 'cembalo']

Funktionen choice returnerar ett slumpmässigt valt element från en datastruktur:

from random import choice\n\nord = [\"apa\", \"banan\", \"cembalo\"]\nprint(choice(ord))

'cembalo'

Lottorad

Ett vanligt sätt att undersöka slumpmässighet är att se på lottorader. Låt oss lotta ut en vinnande rad. I Finland består lottoraden av sju siffror mellan 1 och 40.

Så här kunde det se ut när vi försöker lotta ut en rad:

from random import randint\n\nfor i in range(7):\n    print(randint(1, 40))

Det här skulle inte fungera i det långa loppet eftersom ett och samma nummer kan dyka upp flera gånger på samma lottorad. Vi måste se till att alla nummer är unika.

Ett sätt är att lagra de lottade siffrorna i en lista. Då lägger vi bara till en ny siffra om siffran inte redan finns i listan. Vi använder oss av en loop som fortsätter tills listans längd är sju:

from random import randint\n\nrad = []\nwhile len(rad) < 7:\n    ny = randint(1, 40)\n    if ny not in rad:\n        rad.append(ny)\n\nprint(rad)

Vi kan också spara lite utrymme genom att använda shuffle-funktionen:

from random import shuffle\n\nalla = list(range(1, 41))\nshuffle(alla)\nrad = alla[0:7]\nprint(rad)

Idén här är att vi skapar en lista med siffrorna 1 till 40, lite som att vi skulle ha 40 bollar i en lotterimaskin. Listan blandas sedan, varefter de första sju siffrorna utgör veckans vinnande rad. Nu behöver vi ingen loop.

Modulen random innehåller faktiskt ett ännu enklare sätt att skapa vår lottorad: funktionen sample. Den returnerar ett slumpmässigt val av en specifik storlek från en given datastruktur:

from random import sample\n\nalla = list(range(1, 41))\nrad = sample(alla, 7)\nprint(rad)

Skapa funktionen lottorad(siffror: int, nedre: int, ovre: int) som lottar ut det givna antalet siffror i intervallet nedre-ovre och returnerar dessa i en ordnad lista (börjandes med det minsta talet).

Samma siffra får inte förekomma flera gånger.

Exempel:

for siffra in lottorad(7, 1, 40):\n    print(siffra)

4\n7\n11\n16\n22\n29\n38

Varifrån kommer dessa slumpmässiga siffror ifrån?

Funktionaliteten i random-modulen är baserad på en algoritm som skapar slumpmässiga siffror på basis av ett specifikt startvärde och några matematiska operationer. Startvärdet kallas ofta seed value.

Vi kan själva ge ett sådant värde med seed-funktionen:

from random import randint, seed\n\nseed(1337)\n# det här genererar alltid samma tal\nprint(randint(1, 100))

Om vi har funktioner som baserar sig på slumpmässighet och har valt ett seed-värde, kommer funktionen att ge samma resultat varje gång den körs. Resultatet kan skilja sig mellan olika Python-versioner, men i grunden kommer slumpmässigheten att försvinna då vi definierar seed-värdet. Funktionaliteten kan dock vara nyttig då vi exempelvis testar på vårt program.

För att vara korrekt, är de siffor som random-modulen ger inte i verkligheten slumpmässiga. Istället är de skenbart slumpmässiga (pseudorandom). Datorers funktionalitet är förutsebar och i en ideal situation kan man exakt bestämma hur de fungerar. Därmed är det mycket svårt att skapa sant slumpmässiga tal med en dator. I flera situationer räcket skenbart slumpmässiga tal. När sant slumpmässiga tal behövs skapas seed-värdet på basis av någon yttre källa som radioaktiv bakgrundsstrålning, ljudnivå eller lavalampor.

För ytterligare information om slumpmässighet, se random.org.

Skapa en funktion som genererar slumpmässiga lösenord, av vald längd, bestående av bokstäverna a-z.

Exempel:

for i in range(10):\n    print(skapa_losenord(8))

lttehepy\nolsxttjl\ncbjncrzo\ndwxqjdgu\ngpfdcecs\njabyvgar\nxnbbonbl\nktmsjyww\nejhprmel\nrjkoacib

Förbättra förra uppgiftens funktion. Nu tar den emot tre argument:

    \n
  1. om värdet är True ska lösenordet innehålla minst en siffra
  2. \n
  3. om värdet är True ska lösenordet innehålla minst ett av tecknen !?=+-()#
  4. \n

Lösenordet ska alltid innehålla minst en bokstav. Du kan anta att funktionen anropas med argument som möjliggör skapandet av önskat lösenord.

Exempel:

for i in range(10):\n    print(skapa_bra_losenord(8, True, True))

2?0n+u31\nu=m4nl94\nn#=i6r#(\nda9?zvm?\n7h)!)g?!\na=59x2n5\n(jr6n3b5\n9n(4i+2!\n32+qba#=\nn?b0a7ey

Vi skapar nu några funktioner som vi kan använda i spel som kräver en tärning.

Istället för en normal tärning använder vi icke-transitiva tärningar. Se den här artikeln och videon vid behov.

Vi har tre tärningar:

    \n
  • tärning A med siffrorna 3, 3, 3, 3, 3, 6
  • \n
  • tärning B med siffrorna 2, 2, 2, 5, 5, 5
  • \n
  • tärning C med siffrorna 1, 4, 4, 4, 4, 4
  • \n

Skapa funktionen kasta(tarning: str) som kastar den valda tärningen.

Exempel:

for i in range(20):\n    print(kasta(\"A\"), \" \", end=\"\")\nprint()\nfor i in range(20):\n    print(kasta(\"B\"), \" \", end=\"\")\nprint()\nfor i in range(20):\n    print(kasta(\"C\"), \" \", end=\"\")

3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 6 3\n2 2 5 2 2 5 5 2 2 5 2 5 5 5 2 5 2 2 2 2\n4 4 4 4 4 1 1 4 4 4 1 4 4 4 4 4 4 4 4 4

Skapa ännu funktionen spela(tarning1: str, tarning2: str, ganger: int) som kastar de valda tärningarna angivna antalet gånger. Funktionen ska returnera en tuple som berättar antalet vinster med tärning ett respektive två samt antalet oavgjoda rundor.

resultat = spela(\"A\", \"C\", 1000)\nprint(resultat)\nresultat = spela(\"B\", \"B\", 1000)\nprint(resultat)

(292, 708, 0)\n(249, 273, 478)

I den här uppgiften har du filen ord.txt till ditt förfogande. Filen innehåller engelska ord, ett ord på varsin rad.

Skapa funktionen ord(n: int, borjar: str) som returnerar n slumpmässigt valda ord som börjar med den valda strängen.

Om funktionen anropas med argumenten ord(3, \"ca\") kan t.ex. orden cat, car och carbon returneras i listan. Samma ord får inte förekomma flera gånger.

Om tillräckligt många ord inte hittas ska undantaget ValueError åstadkommas.

Exempel:

lista = ord(3, \"ca\")\nfor ord in lista:\n    print(ord)

cat\ncar\ncarbon

","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/2-slumpmassighet.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"känner du till några av funktionerna i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":"-modulen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda dig av slumpmässiga tal i dina program."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här delen ser vi på "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]}]},{"type":"text","value":" i Pythons standardbibliotek. Den här modulen innehåller verktyg för att skapa slumpmässiga tal och för annan funktionalitet med ett slumpmässigt element."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Delarna i den här modulen innehåller flera länkar till "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"standardbibliotekets dokumentation"}]},{"type":"text","value":". Vi rekommenderar att du följer länkarna så att du kan bekanta dig med hur dokumentationen ser ut."}]},{"type":"element","tagName":"h2","properties":{"id":"skapa-ett-slumpmassigt-tal","style":"position:relative;"},"children":[{"type":"text","value":"Skapa ett slumpmässigt tal"},{"type":"element","tagName":"a","properties":{"href":"#skapa-ett-slumpmassigt-tal","ariaLabel":"skapa ett slumpmassigt tal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html#random.randint","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint(a, b)"}]}]},{"type":"text","value":" returnerar ett slumpmässigt heltal mellan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" (inklusive start- och slutpunkten). Till exempel det här programmet fungerar som en normal tärning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tärningen ger:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kan utskriften se ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tärningen ger: 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här programmet kastar en tärning tio gånger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tärningen ger:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften skulle kunna se ut så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tärningen ger: 5\nTärningen ger: 4\nTärningen ger: 3\nTärningen ger: 2\nTärningen ger: 3\nTärningen ger: 4\nTärningen ger: 6\nTärningen ger: 4\nTärningen ger: 4\nTärningen ger: 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Det är värt att notera att funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint"}]},{"type":"text","value":" fungerar lite olikt än till exempel extrahering (slicing) eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":"-funktionen. Funktionsanropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint(1, 6)"}]},{"type":"text","value":" resulterar i ett nummer mellan 1 och 6, medan anropet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(1, 6)"}]},{"type":"text","value":" resulterar i ett intervall från 1 till 5."}]},{"type":"element","tagName":"h2","properties":{"id":"flera-slumpmassighetsfunktioner","style":"position:relative;"},"children":[{"type":"text","value":"Flera slumpmässighetsfunktioner"},{"type":"element","tagName":"a","properties":{"href":"#flera-slumpmassighetsfunktioner","ariaLabel":"flera slumpmassighetsfunktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html#random.shuffle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shuffle"}]}]},{"type":"text","value":" blandar elementen i den datastruktur som ges som argument. Till exempel följande program blandar en lista med ord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nshuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['banan', 'apa', 'cembalo']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"choice"}]},{"type":"text","value":" returnerar ett slumpmässigt valt element från en datastruktur:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" choice\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"'cembalo'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lottorad","style":"position:relative;"},"children":[{"type":"text","value":"Lottorad"},{"type":"element","tagName":"a","properties":{"href":"#lottorad","ariaLabel":"lottorad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett vanligt sätt att undersöka slumpmässighet är att se på lottorader. Låt oss lotta ut en vinnande rad. I Finland består lottoraden av sju siffror mellan 1 och 40."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här kunde det se ut när vi försöker lotta ut en rad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det här skulle inte fungera i det långa loppet eftersom ett och samma nummer kan dyka upp flera gånger på samma lottorad. Vi måste se till att alla nummer är unika."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett sätt är att lagra de lottade siffrorna i en lista. Då lägger vi bara till en ny siffra om siffran inte redan finns i listan. Vi använder oss av en loop som fortsätter tills listans längd är sju:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\nrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n ny "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" ny "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ny"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan också spara lite utrymme genom att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shuffle"}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\nalla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" alla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Idén här är att vi skapar en lista med siffrorna 1 till 40, lite som att vi skulle ha 40 bollar i en lotterimaskin. Listan blandas sedan, varefter de första sju siffrorna utgör veckans vinnande rad. Nu behöver vi ingen loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" innehåller faktiskt ett ännu enklare sätt att skapa vår lottorad: funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html#random.sample","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sample"}]}]},{"type":"text","value":". Den returnerar ett slumpmässigt val av en specifik storlek från en given datastruktur:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sample\n\nalla "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sample"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lottorader","tmcname":"osa07-04_lottorader"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lottorad(siffror: int, nedre: int, ovre: int)"}]},{"type":"text","value":" som lottar ut det givna antalet siffror i intervallet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nedre-ovre"}]},{"type":"text","value":" och returnerar dessa i en ordnad lista (börjandes med det minsta talet)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Samma siffra får inte förekomma flera gånger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" siffra "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lottorad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"siffra"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4\n7\n11\n16\n22\n29\n38"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"varifran-kommer-dessa-slumpmassiga-siffror-ifran","style":"position:relative;"},"children":[{"type":"text","value":"Varifrån kommer dessa slumpmässiga siffror ifrån?"},{"type":"element","tagName":"a","properties":{"href":"#varifran-kommer-dessa-slumpmassiga-siffror-ifran","ariaLabel":"varifran kommer dessa slumpmassiga siffror ifran permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionaliteten i "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":"-modulen"}]},{"type":"text","value":" är baserad på en algoritm som skapar slumpmässiga siffror på basis av ett specifikt startvärde och några matematiska operationer. Startvärdet kallas ofta seed value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan själva ge ett sådant värde med "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html#random.seed","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"seed"}]}]},{"type":"text","value":"-funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" seed\n\nseed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1337"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det här genererar alltid samma tal"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi har funktioner som baserar sig på slumpmässighet och har valt ett seed-värde, kommer funktionen att ge samma resultat varje gång den körs. Resultatet kan skilja sig mellan olika Python-versioner, men i grunden kommer slumpmässigheten att försvinna då vi definierar seed-värdet. Funktionaliteten kan dock vara nyttig då vi exempelvis testar på vårt program."}]},{"type":"element","tagName":"text-box","properties":{"variant":"info","name":"Sann slumpmässighet"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att vara korrekt, är de siffor som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":"-modulen ger inte i verkligheten slumpmässiga. Istället är de skenbart slumpmässiga (pseudorandom). Datorers funktionalitet är förutsebar och i en ideal situation kan man exakt bestämma hur de fungerar. Därmed är det mycket svårt att skapa sant slumpmässiga tal med en dator. I flera situationer räcket skenbart slumpmässiga tal. När sant slumpmässiga tal behövs skapas seed-värdet på basis av någon yttre källa som radioaktiv bakgrundsstrålning, ljudnivå eller lavalampor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För ytterligare information om slumpmässighet, se "},{"type":"element","tagName":"a","properties":{"href":"https://www.random.org/randomness/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"random.org"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lösenordsgenerator, del 1","tmcname":"osa07-05_losenord_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion som genererar slumpmässiga lösenord, av vald längd, bestående av bokstäverna a-z."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skapa_losenord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"lttehepy\nolsxttjl\ncbjncrzo\ndwxqjdgu\ngpfdcecs\njabyvgar\nxnbbonbl\nktmsjyww\nejhprmel\nrjkoacib"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lösenordsgenerator, del 2","tmcname":"osa07-06_losenord_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förbättra förra uppgiftens funktion. Nu tar den emot tre argument:"}]},{"type":"element","tagName":"ol","properties":{"start":2},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" ska lösenordet innehålla minst en siffra"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om värdet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" ska lösenordet innehålla minst ett av tecknen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!?=+-()#"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lösenordet ska alltid innehålla minst en bokstav. Du kan anta att funktionen anropas med argument som möjliggör skapandet av önskat lösenord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"skapa_bra_losenord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2?0n+u31\nu=m4nl94\nn#=i6r#(\nda9?zvm?\n7h)!)g?!\na=59x2n5\n(jr6n3b5\n9n(4i+2!\n32+qba#=\nn?b0a7ey"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Tärningssimulation","tmcname":"osa07-07_tarningar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi skapar nu några funktioner som vi kan använda i spel som kräver en tärning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Istället för en normal tärning använder vi icke-transitiva tärningar. Se den här "},{"type":"element","tagName":"a","properties":{"href":"https://singingbanana.com/dice/article.htm","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"artikeln"}]},{"type":"text","value":" och "},{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/LrIp6CKUlH8","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"videon"}]},{"type":"text","value":" vid behov."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har tre tärningar:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tärning A med siffrorna 3, 3, 3, 3, 3, 6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tärning B med siffrorna 2, 2, 2, 5, 5, 5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"tärning C med siffrorna 1, 4, 4, 4, 4, 4"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kasta(tarning: str)"}]},{"type":"text","value":" som kastar den valda tärningen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 6 3\n2 2 5 2 2 5 5 2 2 5 2 5 5 5 2 5 2 2 2 2\n4 4 4 4 4 1 1 4 4 4 1 4 4 4 4 4 4 4 4 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ännu funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spela(tarning1: str, tarning2: str, ganger: int)"}]},{"type":"text","value":" som kastar de valda tärningarna angivna antalet gånger. Funktionen ska returnera en tuple som berättar antalet vinster med tärning ett respektive två samt antalet oavgjoda rundor."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(292, 708, 0)\n(249, 273, 478)"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Slumpmässiga ord","tmcname":"osa07-08_slumpmassiga_ord"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften har du filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord.txt"}]},{"type":"text","value":" till ditt förfogande. Filen innehåller engelska ord, ett ord på varsin rad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord(n: int, borjar: str)"}]},{"type":"text","value":" som returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" slumpmässigt valda ord som börjar med den valda strängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om funktionen anropas med argumenten "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord(3, \"ca\")"}]},{"type":"text","value":" kan t.ex. orden cat, car och carbon returneras i listan. Samma ord får inte förekomma flera gånger."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om tillräckligt många ord inte hittas ska undantaget "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" åstadkommas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ca\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"cat\ncar\ncarbon"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • känner du till några av funktionerna i random-modulen
  • \n
  • kan du använda dig av slumpmässiga tal i dina program.
  • \n

I den här delen ser vi på modulen random i Pythons standardbibliotek. Den här modulen innehåller verktyg för att skapa slumpmässiga tal och för annan funktionalitet med ett slumpmässigt element.

Delarna i den här modulen innehåller flera länkar till standardbibliotekets dokumentation. Vi rekommenderar att du följer länkarna så att du kan bekanta dig med hur dokumentationen ser ut.

Skapa ett slumpmässigt tal

Funktionen randint(a, b) returnerar ett slumpmässigt heltal mellan a och b (inklusive start- och slutpunkten). Till exempel det här programmet fungerar som en normal tärning:

from random import randint\n\nprint(\"Tärningen ger:\", randint(1, 6))

Så här kan utskriften se ut:

Tärningen ger: 4

Det här programmet kastar en tärning tio gånger:

from random import randint\n\nfor i in range(10):\n    print(\"Tärningen ger:\", randint(1, 6))

Utskriften skulle kunna se ut så här:

Tärningen ger: 5\nTärningen ger: 4\nTärningen ger: 3\nTärningen ger: 2\nTärningen ger: 3\nTärningen ger: 4\nTärningen ger: 6\nTärningen ger: 4\nTärningen ger: 4\nTärningen ger: 3

Obs! Det är värt att notera att funktionen randint fungerar lite olikt än till exempel extrahering (slicing) eller range-funktionen. Funktionsanropet randint(1, 6) resulterar i ett nummer mellan 1 och 6, medan anropet range(1, 6) resulterar i ett intervall från 1 till 5.

Flera slumpmässighetsfunktioner

Funktionen shuffle blandar elementen i den datastruktur som ges som argument. Till exempel följande program blandar en lista med ord:

from random import shuffle\n\nord = [\"apa\", \"banan\", \"cembalo\"]\nshuffle(ord)\nprint(ord)

['banan', 'apa', 'cembalo']

Funktionen choice returnerar ett slumpmässigt valt element från en datastruktur:

from random import choice\n\nord = [\"apa\", \"banan\", \"cembalo\"]\nprint(choice(ord))

'cembalo'

Lottorad

Ett vanligt sätt att undersöka slumpmässighet är att se på lottorader. Låt oss lotta ut en vinnande rad. I Finland består lottoraden av sju siffror mellan 1 och 40.

Så här kunde det se ut när vi försöker lotta ut en rad:

from random import randint\n\nfor i in range(7):\n    print(randint(1, 40))

Det här skulle inte fungera i det långa loppet eftersom ett och samma nummer kan dyka upp flera gånger på samma lottorad. Vi måste se till att alla nummer är unika.

Ett sätt är att lagra de lottade siffrorna i en lista. Då lägger vi bara till en ny siffra om siffran inte redan finns i listan. Vi använder oss av en loop som fortsätter tills listans längd är sju:

from random import randint\n\nrad = []\nwhile len(rad) < 7:\n    ny = randint(1, 40)\n    if ny not in rad:\n        rad.append(ny)\n\nprint(rad)

Vi kan också spara lite utrymme genom att använda shuffle-funktionen:

from random import shuffle\n\nalla = list(range(1, 41))\nshuffle(alla)\nrad = alla[0:7]\nprint(rad)

Idén här är att vi skapar en lista med siffrorna 1 till 40, lite som att vi skulle ha 40 bollar i en lotterimaskin. Listan blandas sedan, varefter de första sju siffrorna utgör veckans vinnande rad. Nu behöver vi ingen loop.

Modulen random innehåller faktiskt ett ännu enklare sätt att skapa vår lottorad: funktionen sample. Den returnerar ett slumpmässigt val av en specifik storlek från en given datastruktur:

from random import sample\n\nalla = list(range(1, 41))\nrad = sample(alla, 7)\nprint(rad)

Skapa funktionen lottorad(siffror: int, nedre: int, ovre: int) som lottar ut det givna antalet siffror i intervallet nedre-ovre och returnerar dessa i en ordnad lista (börjandes med det minsta talet).

Samma siffra får inte förekomma flera gånger.

Exempel:

for siffra in lottorad(7, 1, 40):\n    print(siffra)

4\n7\n11\n16\n22\n29\n38

Varifrån kommer dessa slumpmässiga siffror ifrån?

Funktionaliteten i random-modulen är baserad på en algoritm som skapar slumpmässiga siffror på basis av ett specifikt startvärde och några matematiska operationer. Startvärdet kallas ofta seed value.

Vi kan själva ge ett sådant värde med seed-funktionen:

from random import randint, seed\n\nseed(1337)\n# det här genererar alltid samma tal\nprint(randint(1, 100))

Om vi har funktioner som baserar sig på slumpmässighet och har valt ett seed-värde, kommer funktionen att ge samma resultat varje gång den körs. Resultatet kan skilja sig mellan olika Python-versioner, men i grunden kommer slumpmässigheten att försvinna då vi definierar seed-värdet. Funktionaliteten kan dock vara nyttig då vi exempelvis testar på vårt program.

För att vara korrekt, är de siffor som random-modulen ger inte i verkligheten slumpmässiga. Istället är de skenbart slumpmässiga (pseudorandom). Datorers funktionalitet är förutsebar och i en ideal situation kan man exakt bestämma hur de fungerar. Därmed är det mycket svårt att skapa sant slumpmässiga tal med en dator. I flera situationer räcket skenbart slumpmässiga tal. När sant slumpmässiga tal behövs skapas seed-värdet på basis av någon yttre källa som radioaktiv bakgrundsstrålning, ljudnivå eller lavalampor.

För ytterligare information om slumpmässighet, se random.org.

Skapa en funktion som genererar slumpmässiga lösenord, av vald längd, bestående av bokstäverna a-z.

Exempel:

for i in range(10):\n    print(skapa_losenord(8))

lttehepy\nolsxttjl\ncbjncrzo\ndwxqjdgu\ngpfdcecs\njabyvgar\nxnbbonbl\nktmsjyww\nejhprmel\nrjkoacib

Förbättra förra uppgiftens funktion. Nu tar den emot tre argument:

    \n
  1. om värdet är True ska lösenordet innehålla minst en siffra
  2. \n
  3. om värdet är True ska lösenordet innehålla minst ett av tecknen !?=+-()#
  4. \n

Lösenordet ska alltid innehålla minst en bokstav. Du kan anta att funktionen anropas med argument som möjliggör skapandet av önskat lösenord.

Exempel:

for i in range(10):\n    print(skapa_bra_losenord(8, True, True))

2?0n+u31\nu=m4nl94\nn#=i6r#(\nda9?zvm?\n7h)!)g?!\na=59x2n5\n(jr6n3b5\n9n(4i+2!\n32+qba#=\nn?b0a7ey

Vi skapar nu några funktioner som vi kan använda i spel som kräver en tärning.

Istället för en normal tärning använder vi icke-transitiva tärningar. Se den här artikeln och videon vid behov.

Vi har tre tärningar:

    \n
  • tärning A med siffrorna 3, 3, 3, 3, 3, 6
  • \n
  • tärning B med siffrorna 2, 2, 2, 5, 5, 5
  • \n
  • tärning C med siffrorna 1, 4, 4, 4, 4, 4
  • \n

Skapa funktionen kasta(tarning: str) som kastar den valda tärningen.

Exempel:

for i in range(20):\n    print(kasta(\"A\"), \" \", end=\"\")\nprint()\nfor i in range(20):\n    print(kasta(\"B\"), \" \", end=\"\")\nprint()\nfor i in range(20):\n    print(kasta(\"C\"), \" \", end=\"\")

3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 6 3\n2 2 5 2 2 5 5 2 2 5 2 5 5 5 2 5 2 2 2 2\n4 4 4 4 4 1 1 4 4 4 1 4 4 4 4 4 4 4 4 4

Skapa ännu funktionen spela(tarning1: str, tarning2: str, ganger: int) som kastar de valda tärningarna angivna antalet gånger. Funktionen ska returnera en tuple som berättar antalet vinster med tärning ett respektive två samt antalet oavgjoda rundor.

resultat = spela(\"A\", \"C\", 1000)\nprint(resultat)\nresultat = spela(\"B\", \"B\", 1000)\nprint(resultat)

(292, 708, 0)\n(249, 273, 478)

I den här uppgiften har du filen ord.txt till ditt förfogande. Filen innehåller engelska ord, ett ord på varsin rad.

Skapa funktionen ord(n: int, borjar: str) som returnerar n slumpmässigt valda ord som börjar med den valda strängen.

Om funktionen anropas med argumenten ord(3, \"ca\") kan t.ex. orden cat, car och carbon returneras i listan. Samma ord får inte förekomma flera gånger.

Om tillräckligt många ord inte hittas ska undantaget ValueError åstadkommas.

Exempel:

lista = ord(3, \"ca\")\nfor ord in lista:\n    print(ord)

cat\ncar\ncarbon

","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/2-slumpmassighet.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-7/3-tid-datum/page-data.json b/page-data/osa-7/3-tid-datum/page-data.json index 5e1a7eea0..75cb762d6 100644 --- a/page-data/osa-7/3-tid-datum/page-data.json +++ b/page-data/osa-7/3-tid-datum/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-7/3-tid-datum", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du behandla tid och datum med Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa och använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du jämföra och räkna skillnaden mellan två datum eller tidpunkter."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"objektet-datetime","style":"position:relative;"},"children":[{"type":"text","value":"Objektet datetime"},{"type":"element","tagName":"a","properties":{"href":"#objektet-datetime","ariaLabel":"objektet datetime permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-modul"}]},{"type":"text","value":" innehåller funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.datetime.now","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"now"}]}]},{"type":"text","value":", som returnerar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt med det nuvarande datumet och tidpunkten. Så här ser det normalt ut när man skriver ut ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2020-10-13 12:46:49.311393"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också definiera objektet själv:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1952-12-24 00:00:00"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tidpunkten är vid midnatt om vi inte själv definierar den."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olika element i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objektet kan kommas åt på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dag:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Månad:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"År:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dag: 24\nMånad: 12\nÅr: 1952"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tidpunkten under dagen kan också specificeras. Precisionen kan variera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"00"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 30/6 2020 kl 13:00"}]},{"type":"text","value":"\ntid2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"18"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 30/6 2020 kl 18:45"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"jamfora-tider-och-rakna-skillnaden-mellan-dem","style":"position:relative;"},"children":[{"type":"text","value":"Jämföra tider och räkna skillnaden mellan dem"},{"type":"element","tagName":"a","properties":{"href":"#jamfora-tider-och-rakna-skillnaden-mellan-dem","ariaLabel":"jamfora tider och rakna skillnaden mellan dem permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De bekanta jämförelseoperatorerna fungerar också för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nnu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Inte än midsommar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Trevlig midsommar!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Midsommaren kom och gick\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Midsommaren kom och gick"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skillnaden mellan två "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt kan enkelt räknas med subtraktionsoperatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nnu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nskillnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" nu\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det är ännu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" skillnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"days"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"dagar till midsommar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är ännu 37 dagar till midsommar"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Resultatet för en subtraktion med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt är ett "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#timedelta-objects","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]}]},{"type":"text","value":"-objekt. Det är inte lika flexibelt som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objektet. Du kan till exempel komma åt antalet dagar i ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":"-objekt, men inte antalet år, eftersom längden på ett år varierar. Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":"-objekt innehåller attributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"days"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"seconds"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"microseconds"}]},{"type":"text","value":". Andra storheter kan ges som argument och de konverteras automatiskt till korrekt format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Addition med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":"-objekt är också möjligt. Resultatet är ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt där det specificerade antalet dagar (eller veckor, sekunder o.s.v.) är adderade:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" timedelta\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvecka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timedelta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"days"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvecka_senare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" vecka\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"En vecka efter midsommar är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vecka_senare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlang_tid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timedelta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"weeks"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" days"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"32 veckor och 15 dagar efter midsommar är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" midsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" lang_tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En vecka efter midsommar är 2020-06-27 00:00:00\n32 veckor och 15 dagar efter midsommar är 2021-02-14 00:00:00"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kollar ännu hur det ser ut med högre precision:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidnatt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskillnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midnatt"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"nu\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Det är ännu "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"skillnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"seconds"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" sekunder till midnatt\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är ännu 8188 sekunder till midnatt"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hur gammal?","tmcname":"osa07-09_hur_gammal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar om användarens födelsetid och skriver därefter ut hur gammal hon var 31/12 1999."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nMånad: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nÅr: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1979"}]},{"type":"text","value":"\nDu var 7417 dagar gammal vid millennieskiftet."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28"}]},{"type":"text","value":"\nMånad: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nÅr: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2005"}]},{"type":"text","value":"\nDu var inte född vid millennieskiftet."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att användaren ger ett korrekt datum (inte t.ex. 31/2 1999)."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Personbeteckning rätt?","tmcname":"osa07-10_personbeteckningar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"valid(personbeteckning: str)"}]},{"type":"text","value":" som returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" beroende på om den givna personbeteckningen är korrekt. Formatet på beteckningen är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmååxyyyz"}]},{"type":"text","value":" där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmåå"}]},{"type":"text","value":" indikerar födelsetid, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" är ett skiljetecken, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yyy"}]},{"type":"text","value":" födelsenummer och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"z"}]},{"type":"text","value":" ett kontrolltecken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska kontrollera att"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"födelsetiden är ett datum som finns"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"skiljetecknet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" (1800-talet), "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" (1900-talet) eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A"}]},{"type":"text","value":" (2000-talet)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kontrolltecknet är korrekt."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kontrolltecknet får man genom att dividera den siffra som består av födelsetiden och -numret med 31. Resten av denna operationen indikerar från vilket index i strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0123456789ABCDEFHJKLMNPRSTUVWXY"}]},{"type":"text","value":" kontrolltecknet tas ifrån. Om resten är t.ex. 12, är kontrolltecknet vid index 12, dvs. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"C"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Se mer på webbplatsen för "},{"type":"element","tagName":"a","properties":{"href":"https://dvv.fi/sv/personbeteckning","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Myndigheten för digitalisering och befolkningsdata"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Se till att du inte delar din egen personbeteckning av misstag, t.ex. om du frågar om hjälp när du löser den här uppgiften."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att underlätta testandet listas några valida personbeteckningar nedan:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"230827-906F"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"120488+246L"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"310823A9877"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"formatera-tid-och-datum","style":"position:relative;"},"children":[{"type":"text","value":"Formatera tid och datum"},{"type":"element","tagName":"a","properties":{"href":"#formatera-tid-och-datum","ariaLabel":"formatera tid och datum permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" innehåller metoden "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.date.strftime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strftime"}]}]},{"type":"text","value":" som kan användas för att formatera hur ett datum representeras som sträng. Till exempel följande kodsnutt kommer att skriva ut det nuvarande datumet i formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dd.mm.åååå"}]},{"type":"text","value":" och därefter datumet och tiden i ett annat format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strftime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%d.%m.%Y\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"04.02.2020"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tidsformatering använder specifika tecken för att indikera ett visst format. Här är ett antal tecken (flera finns i "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/time.html#time.strftime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pythons dokumentation"}]},{"type":"text","value":"):"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Förkortning"}]},{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Betydelse"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%d"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"dag (01–31)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%m"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"månad (01–12)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%Y"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"år (fyra siffor)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%H"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"timme (24 timmars format)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%M"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"minut (00–59)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%S"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"sekund (00–59)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också specificera vilket tecken som används för att skilja elementen i ett datum, så som du såg i exemplet ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Formatering för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" fungerar också åt det andra hållet, det vill säga om du tar emot ett datum som en sträng från en användare och vill få det i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-format. Använd då metoden "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strptime"}]}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nsyote "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange din födelsetid i formatet dd.mm.åååå: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strptime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"syote"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%d.%m.%Y\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du föddes på förra årtusendet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du föddes på det här årtusendet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din födelsetid i formatet dd.mm.åååå: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5.11.1986"}]},{"type":"text","value":"\nDu föddes på förra årtusendet"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Skärmtid","tmcname":"osa07-11_skarmtid"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här programmet antecknar vi användarens dagliga skärmtid (tv, dator, mobil) i en fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fil: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"juni.txt"}]},{"type":"text","value":"\nFörsta dagen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"24.6.2020"}]},{"type":"text","value":"\nAntal dagar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nAnge skärmtiden i minuter (tv dator mobil):\nSkärmtid 24.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"60 120 0"}]},{"type":"text","value":"\nSkärmtid 25.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0 0 0"}]},{"type":"text","value":"\nSkärmtid 26.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"180 0 0"}]},{"type":"text","value":"\nSkärmtid 27.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25 240 15"}]},{"type":"text","value":"\nSkärmtid 28.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45 90 5"}]},{"type":"text","value":"\nInfon lagrad i filen juni.txt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje dag anger man alltså tre minutvärden skilda med mellanslag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet lagrar statistik i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"juni.txt"}]},{"type":"text","value":" som i vårt fall ser ut så här:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tidsperiod: 24.06.2020-28.06.2020\nMinuter tillsammans: 780\nMinuter i genomsnitt: 156.0\n24.06.2020: 60/120/0\n25.06.2020: 0/0/0\n26.06.2020: 180/0/0\n27.06.2020: 25/240/15\n28.06.2020: 45/90/5"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du behandla tid och datum med Python
  • \n
  • kan du skapa och använda datetime-objekt
  • \n
  • kan du jämföra och räkna skillnaden mellan två datum eller tidpunkter.
  • \n

Objektet datetime

Pythons datetime-modul innehåller funktionen now, som returnerar ett datetime-objekt med det nuvarande datumet och tidpunkten. Så här ser det normalt ut när man skriver ut ett datetime-objekt:

from datetime import datetime\n\ntid = datetime.now()\nprint(tid)

2020-10-13 12:46:49.311393

Du kan också definiera objektet själv:

from datetime import datetime\n\ntid = datetime(1952, 12, 24)\nprint(tid)

1952-12-24 00:00:00

Tidpunkten är vid midnatt om vi inte själv definierar den.

Olika element i datetime-objektet kan kommas åt på följande sätt:

from datetime import datetime\n\ntid = datetime(1952, 12, 24)\nprint(\"Dag:\", tid.day)\nprint(\"Månad:\", tid.month)\nprint(\"År:\", tid.year)

Dag: 24\nMånad: 12\nÅr: 1952

Tidpunkten under dagen kan också specificeras. Precisionen kan variera:

from datetime import datetime\n\ntid1 = datetime(2020, 6, 30, 13, 00) # 30/6 2020 kl 13:00\ntid2 = datetime(2020, 6, 30, 18, 45) # 30/6 2020 kl 18:45

Jämföra tider och räkna skillnaden mellan dem

De bekanta jämförelseoperatorerna fungerar också för datetime-objekt:

from datetime import datetime\n\nnu = datetime.now()\nmidsommar = datetime(2020, 6, 20)\n\nif nu < midsommar:\n    print(\"Inte än midsommar\")\nelif nu == midsommar:\n    print(\"Trevlig midsommar!\")\nelif nu > midsommar:\n    print(\"Midsommaren kom och gick\")

Midsommaren kom och gick

Skillnaden mellan två datetime-objekt kan enkelt räknas med subtraktionsoperatorn:

from datetime import datetime\n\nnu = datetime.now()\nmidsommar = datetime(2020, 6, 20)\n\nskillnad = midsommar - nu\nprint(\"Det är ännu\", skillnad.days, \"dagar till midsommar\")

Det är ännu 37 dagar till midsommar

Obs! Resultatet för en subtraktion med datetime-objekt är ett timedelta-objekt. Det är inte lika flexibelt som datetime-objektet. Du kan till exempel komma åt antalet dagar i ett timedelta-objekt, men inte antalet år, eftersom längden på ett år varierar. Ett timedelta-objekt innehåller attributen days, seconds och microseconds. Andra storheter kan ges som argument och de konverteras automatiskt till korrekt format.

Addition med datetime- och timedelta-objekt är också möjligt. Resultatet är ett datetime-objekt där det specificerade antalet dagar (eller veckor, sekunder o.s.v.) är adderade:

from datetime import datetime, timedelta\nmidsommar = datetime(2020, 6, 20)\n\nvecka = timedelta(days=7)\nvecka_senare = midsommar + vecka\n\nprint(\"En vecka efter midsommar är\", vecka_senare)\n\nlang_tid = timedelta(weeks=32, days=15)\n\nprint(\"32 veckor och 15 dagar efter midsommar är\", midsommar + lang_tid)

En vecka efter midsommar är 2020-06-27 00:00:00\n32 veckor och 15 dagar efter midsommar är 2021-02-14 00:00:00

Vi kollar ännu hur det ser ut med högre precision:

nu = datetime.now()\nmidnatt = datetime(2020, 6, 30)\nskillnad = midnatt-nu\nprint(f\"Det är ännu {skillnad.seconds} sekunder till midnatt\")

Det är ännu 8188 sekunder till midnatt

Skapa ett program som frågar om användarens födelsetid och skriver därefter ut hur gammal hon var 31/12 1999.

Exempel:

Dag: 10\nMånad: 9\nÅr: 1979\nDu var 7417 dagar gammal vid millennieskiftet.

Dag: 28\nMånad: 3\nÅr: 2005\nDu var inte född vid millennieskiftet.

Du kan anta att användaren ger ett korrekt datum (inte t.ex. 31/2 1999).

Skapa funktionen valid(personbeteckning: str) som returnerar True eller False beroende på om den givna personbeteckningen är korrekt. Formatet på beteckningen är ddmmååxyyyz där ddmmåå indikerar födelsetid, x är ett skiljetecken, yyy födelsenummer och z ett kontrolltecken.

Programmet ska kontrollera att

    \n
  • födelsetiden är ett datum som finns
  • \n
  • skiljetecknet är + (1800-talet), - (1900-talet) eller A (2000-talet)
  • \n
  • kontrolltecknet är korrekt.
  • \n

Kontrolltecknet får man genom att dividera den siffra som består av födelsetiden och -numret med 31. Resten av denna operationen indikerar från vilket index i strängen 0123456789ABCDEFHJKLMNPRSTUVWXY kontrolltecknet tas ifrån. Om resten är t.ex. 12, är kontrolltecknet vid index 12, dvs. C.

Se mer på webbplatsen för Myndigheten för digitalisering och befolkningsdata.

Obs! Se till att du inte delar din egen personbeteckning av misstag, t.ex. om du frågar om hjälp när du löser den här uppgiften.

För att underlätta testandet listas några valida personbeteckningar nedan:

    \n
  • 230827-906F
  • \n
  • 120488+246L
  • \n
  • 310823A9877
  • \n

Formatera tid och datum

Modulen datetime innehåller metoden strftime som kan användas för att formatera hur ett datum representeras som sträng. Till exempel följande kodsnutt kommer att skriva ut det nuvarande datumet i formatet dd.mm.åååå och därefter datumet och tiden i ett annat format:

from datetime import datetime\n\ntid = datetime.now()\nprint(tid.strftime(\"%d.%m.%Y\"))

04.02.2020

Tidsformatering använder specifika tecken för att indikera ett visst format. Här är ett antal tecken (flera finns i Pythons dokumentation):

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FörkortningBetydelse
%ddag (01–31)
%mmånad (01–12)
%Yår (fyra siffor)
%Htimme (24 timmars format)
%Mminut (00–59)
%Ssekund (00–59)

Du kan också specificera vilket tecken som används för att skilja elementen i ett datum, så som du såg i exemplet ovan.

Formatering för datetime fungerar också åt det andra hållet, det vill säga om du tar emot ett datum som en sträng från en användare och vill få det i datetime-format. Använd då metoden strptime:

from datetime import datetime\n\nsyote = input(\"Ange din födelsetid i formatet dd.mm.åååå: \")\ntid = datetime.strptime(syote, \"%d.%m.%Y\")\n\nif tid < datetime(2000, 1, 1):\n    print(\"Du föddes på förra årtusendet\")\nelse:\n    print(\"Du föddes på det här årtusendet\")

Ange din födelsetid i formatet dd.mm.åååå: 5.11.1986\nDu föddes på förra årtusendet

I det här programmet antecknar vi användarens dagliga skärmtid (tv, dator, mobil) i en fil.

Så här ska programmet fungera:

Fil: juni.txt\nFörsta dagen: 24.6.2020\nAntal dagar: 5\nAnge skärmtiden i minuter (tv dator mobil):\nSkärmtid 24.06.2020: 60 120 0\nSkärmtid 25.06.2020: 0 0 0\nSkärmtid 26.06.2020: 180 0 0\nSkärmtid 27.06.2020: 25 240 15\nSkärmtid 28.06.2020: 45 90 5\nInfon lagrad i filen juni.txt

För varje dag anger man alltså tre minutvärden skilda med mellanslag.

Programmet lagrar statistik i filen juni.txt som i vårt fall ser ut så här:

Tidsperiod: 24.06.2020-28.06.2020\nMinuter tillsammans: 780\nMinuter i genomsnitt: 156.0\n24.06.2020: 60/120/0\n25.06.2020: 0/0/0\n26.06.2020: 180/0/0\n27.06.2020: 25/240/15\n28.06.2020: 45/90/5

","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/3-tid-datum.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du behandla tid och datum med Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa och använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du jämföra och räkna skillnaden mellan två datum eller tidpunkter."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"objektet-datetime","style":"position:relative;"},"children":[{"type":"text","value":"Objektet datetime"},{"type":"element","tagName":"a","properties":{"href":"#objektet-datetime","ariaLabel":"objektet datetime permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-modul"}]},{"type":"text","value":" innehåller funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.datetime.now","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"now"}]}]},{"type":"text","value":", som returnerar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt med det nuvarande datumet och tidpunkten. Så här ser det normalt ut när man skriver ut ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2020-10-13 12:46:49.311393"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också definiera objektet själv:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1952-12-24 00:00:00"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tidpunkten är vid midnatt om vi inte själv definierar den."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olika element i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objektet kan kommas åt på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dag:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Månad:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"År:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dag: 24\nMånad: 12\nÅr: 1952"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tidpunkten under dagen kan också specificeras. Precisionen kan variera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"00"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 30/6 2020 kl 13:00"}]},{"type":"text","value":"\ntid2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"18"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 30/6 2020 kl 18:45"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"jamfora-tider-och-rakna-skillnaden-mellan-dem","style":"position:relative;"},"children":[{"type":"text","value":"Jämföra tider och räkna skillnaden mellan dem"},{"type":"element","tagName":"a","properties":{"href":"#jamfora-tider-och-rakna-skillnaden-mellan-dem","ariaLabel":"jamfora tider och rakna skillnaden mellan dem permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De bekanta jämförelseoperatorerna fungerar också för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nnu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Inte än midsommar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Trevlig midsommar!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Midsommaren kom och gick\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Midsommaren kom och gick"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skillnaden mellan två "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt kan enkelt räknas med subtraktionsoperatorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nnu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nskillnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" nu\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det är ännu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" skillnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"days"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"dagar till midsommar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är ännu 37 dagar till midsommar"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Resultatet för en subtraktion med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt är ett "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#timedelta-objects","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]}]},{"type":"text","value":"-objekt. Det är inte lika flexibelt som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objektet. Du kan till exempel komma åt antalet dagar i ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":"-objekt, men inte antalet år, eftersom längden på ett år varierar. Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":"-objekt innehåller attributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"days"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"seconds"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"microseconds"}]},{"type":"text","value":". Andra storheter kan ges som argument och de konverteras automatiskt till korrekt format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Addition med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"- och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":"-objekt är också möjligt. Resultatet är ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-objekt där det specificerade antalet dagar (eller veckor, sekunder o.s.v.) är adderade:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" timedelta\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvecka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timedelta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"days"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvecka_senare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" vecka\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"En vecka efter midsommar är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vecka_senare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlang_tid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timedelta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"weeks"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" days"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"32 veckor och 15 dagar efter midsommar är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" midsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" lang_tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En vecka efter midsommar är 2020-06-27 00:00:00\n32 veckor och 15 dagar efter midsommar är 2021-02-14 00:00:00"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kollar ännu hur det ser ut med högre precision:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"nu "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidnatt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nskillnad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midnatt"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"nu\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Det är ännu "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"skillnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"seconds"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" sekunder till midnatt\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är ännu 8188 sekunder till midnatt"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hur gammal?","tmcname":"osa07-09_hur_gammal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett program som frågar om användarens födelsetid och skriver därefter ut hur gammal hon var 31/12 1999."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nMånad: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nÅr: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1979"}]},{"type":"text","value":"\nDu var 7417 dagar gammal vid millennieskiftet."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dag: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28"}]},{"type":"text","value":"\nMånad: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nÅr: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2005"}]},{"type":"text","value":"\nDu var inte född vid millennieskiftet."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att användaren ger ett korrekt datum (inte t.ex. 31/2 1999)."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Personbeteckning rätt?","tmcname":"osa07-10_personbeteckningar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"valid(personbeteckning: str)"}]},{"type":"text","value":" som returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" beroende på om den givna personbeteckningen är korrekt. Formatet på beteckningen är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmååxyyyz"}]},{"type":"text","value":" där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmåå"}]},{"type":"text","value":" indikerar födelsetid, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" är ett skiljetecken, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yyy"}]},{"type":"text","value":" födelsenummer och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"z"}]},{"type":"text","value":" ett kontrolltecken."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska kontrollera att"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"födelsetiden är ett datum som finns"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"skiljetecknet är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" (1800-talet), "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" (1900-talet) eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A"}]},{"type":"text","value":" (2000-talet)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kontrolltecknet är korrekt."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kontrolltecknet får man genom att dividera den siffra som består av födelsetiden och -numret med 31. Resten av denna operationen indikerar från vilket index i strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0123456789ABCDEFHJKLMNPRSTUVWXY"}]},{"type":"text","value":" kontrolltecknet tas ifrån. Om resten är t.ex. 12, är kontrolltecknet vid index 12, dvs. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"C"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Se mer på webbplatsen för "},{"type":"element","tagName":"a","properties":{"href":"https://dvv.fi/sv/personbeteckning","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Myndigheten för digitalisering och befolkningsdata"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Se till att du inte delar din egen personbeteckning av misstag, t.ex. om du frågar om hjälp när du löser den här uppgiften."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att underlätta testandet listas några valida personbeteckningar nedan:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"230827-906F"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"120488+246L"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"310823A9877"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"formatera-tid-och-datum","style":"position:relative;"},"children":[{"type":"text","value":"Formatera tid och datum"},{"type":"element","tagName":"a","properties":{"href":"#formatera-tid-och-datum","ariaLabel":"formatera tid och datum permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" innehåller metoden "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.date.strftime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strftime"}]}]},{"type":"text","value":" som kan användas för att formatera hur ett datum representeras som sträng. Till exempel följande kodsnutt kommer att skriva ut det nuvarande datumet i formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dd.mm.åååå"}]},{"type":"text","value":" och därefter datumet och tiden i ett annat format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strftime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%d.%m.%Y\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"04.02.2020"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tidsformatering använder specifika tecken för att indikera ett visst format. Här är ett antal tecken (flera finns i "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/time.html#time.strftime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pythons dokumentation"}]},{"type":"text","value":"):"}]},{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Förkortning"}]},{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Betydelse"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%d"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"dag (01–31)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%m"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"månad (01–12)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%Y"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"år (fyra siffor)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%H"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"timme (24 timmars format)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%M"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"minut (00–59)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%S"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"sekund (00–59)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också specificera vilket tecken som används för att skilja elementen i ett datum, så som du såg i exemplet ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Formatering för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" fungerar också åt det andra hållet, det vill säga om du tar emot ett datum som en sträng från en användare och vill få det i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":"-format. Använd då metoden "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strptime"}]}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nsyote "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange din födelsetid i formatet dd.mm.åååå: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strptime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"syote"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%d.%m.%Y\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" tid "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du föddes på förra årtusendet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du föddes på det här årtusendet\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange din födelsetid i formatet dd.mm.åååå: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5.11.1986"}]},{"type":"text","value":"\nDu föddes på förra årtusendet"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Skärmtid","tmcname":"osa07-11_skarmtid"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här programmet antecknar vi användarens dagliga skärmtid (tv, dator, mobil) i en fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här ska programmet fungera:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fil: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"juni.txt"}]},{"type":"text","value":"\nFörsta dagen: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"24.6.2020"}]},{"type":"text","value":"\nAntal dagar: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nAnge skärmtiden i minuter (tv dator mobil):\nSkärmtid 24.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"60 120 0"}]},{"type":"text","value":"\nSkärmtid 25.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0 0 0"}]},{"type":"text","value":"\nSkärmtid 26.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"180 0 0"}]},{"type":"text","value":"\nSkärmtid 27.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25 240 15"}]},{"type":"text","value":"\nSkärmtid 28.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45 90 5"}]},{"type":"text","value":"\nInfon lagrad i filen juni.txt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje dag anger man alltså tre minutvärden skilda med mellanslag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet lagrar statistik i filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"juni.txt"}]},{"type":"text","value":" som i vårt fall ser ut så här:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tidsperiod: 24.06.2020-28.06.2020\nMinuter tillsammans: 780\nMinuter i genomsnitt: 156.0\n24.06.2020: 60/120/0\n25.06.2020: 0/0/0\n26.06.2020: 180/0/0\n27.06.2020: 25/240/15\n28.06.2020: 45/90/5"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du behandla tid och datum med Python
  • \n
  • kan du skapa och använda datetime-objekt
  • \n
  • kan du jämföra och räkna skillnaden mellan två datum eller tidpunkter.
  • \n

Objektet datetime

Pythons datetime-modul innehåller funktionen now, som returnerar ett datetime-objekt med det nuvarande datumet och tidpunkten. Så här ser det normalt ut när man skriver ut ett datetime-objekt:

from datetime import datetime\n\ntid = datetime.now()\nprint(tid)

2020-10-13 12:46:49.311393

Du kan också definiera objektet själv:

from datetime import datetime\n\ntid = datetime(1952, 12, 24)\nprint(tid)

1952-12-24 00:00:00

Tidpunkten är vid midnatt om vi inte själv definierar den.

Olika element i datetime-objektet kan kommas åt på följande sätt:

from datetime import datetime\n\ntid = datetime(1952, 12, 24)\nprint(\"Dag:\", tid.day)\nprint(\"Månad:\", tid.month)\nprint(\"År:\", tid.year)

Dag: 24\nMånad: 12\nÅr: 1952

Tidpunkten under dagen kan också specificeras. Precisionen kan variera:

from datetime import datetime\n\ntid1 = datetime(2020, 6, 30, 13, 00) # 30/6 2020 kl 13:00\ntid2 = datetime(2020, 6, 30, 18, 45) # 30/6 2020 kl 18:45

Jämföra tider och räkna skillnaden mellan dem

De bekanta jämförelseoperatorerna fungerar också för datetime-objekt:

from datetime import datetime\n\nnu = datetime.now()\nmidsommar = datetime(2020, 6, 20)\n\nif nu < midsommar:\n    print(\"Inte än midsommar\")\nelif nu == midsommar:\n    print(\"Trevlig midsommar!\")\nelif nu > midsommar:\n    print(\"Midsommaren kom och gick\")

Midsommaren kom och gick

Skillnaden mellan två datetime-objekt kan enkelt räknas med subtraktionsoperatorn:

from datetime import datetime\n\nnu = datetime.now()\nmidsommar = datetime(2020, 6, 20)\n\nskillnad = midsommar - nu\nprint(\"Det är ännu\", skillnad.days, \"dagar till midsommar\")

Det är ännu 37 dagar till midsommar

Obs! Resultatet för en subtraktion med datetime-objekt är ett timedelta-objekt. Det är inte lika flexibelt som datetime-objektet. Du kan till exempel komma åt antalet dagar i ett timedelta-objekt, men inte antalet år, eftersom längden på ett år varierar. Ett timedelta-objekt innehåller attributen days, seconds och microseconds. Andra storheter kan ges som argument och de konverteras automatiskt till korrekt format.

Addition med datetime- och timedelta-objekt är också möjligt. Resultatet är ett datetime-objekt där det specificerade antalet dagar (eller veckor, sekunder o.s.v.) är adderade:

from datetime import datetime, timedelta\nmidsommar = datetime(2020, 6, 20)\n\nvecka = timedelta(days=7)\nvecka_senare = midsommar + vecka\n\nprint(\"En vecka efter midsommar är\", vecka_senare)\n\nlang_tid = timedelta(weeks=32, days=15)\n\nprint(\"32 veckor och 15 dagar efter midsommar är\", midsommar + lang_tid)

En vecka efter midsommar är 2020-06-27 00:00:00\n32 veckor och 15 dagar efter midsommar är 2021-02-14 00:00:00

Vi kollar ännu hur det ser ut med högre precision:

nu = datetime.now()\nmidnatt = datetime(2020, 6, 30)\nskillnad = midnatt-nu\nprint(f\"Det är ännu {skillnad.seconds} sekunder till midnatt\")

Det är ännu 8188 sekunder till midnatt

Skapa ett program som frågar om användarens födelsetid och skriver därefter ut hur gammal hon var 31/12 1999.

Exempel:

Dag: 10\nMånad: 9\nÅr: 1979\nDu var 7417 dagar gammal vid millennieskiftet.

Dag: 28\nMånad: 3\nÅr: 2005\nDu var inte född vid millennieskiftet.

Du kan anta att användaren ger ett korrekt datum (inte t.ex. 31/2 1999).

Skapa funktionen valid(personbeteckning: str) som returnerar True eller False beroende på om den givna personbeteckningen är korrekt. Formatet på beteckningen är ddmmååxyyyz där ddmmåå indikerar födelsetid, x är ett skiljetecken, yyy födelsenummer och z ett kontrolltecken.

Programmet ska kontrollera att

    \n
  • födelsetiden är ett datum som finns
  • \n
  • skiljetecknet är + (1800-talet), - (1900-talet) eller A (2000-talet)
  • \n
  • kontrolltecknet är korrekt.
  • \n

Kontrolltecknet får man genom att dividera den siffra som består av födelsetiden och -numret med 31. Resten av denna operationen indikerar från vilket index i strängen 0123456789ABCDEFHJKLMNPRSTUVWXY kontrolltecknet tas ifrån. Om resten är t.ex. 12, är kontrolltecknet vid index 12, dvs. C.

Se mer på webbplatsen för Myndigheten för digitalisering och befolkningsdata.

Obs! Se till att du inte delar din egen personbeteckning av misstag, t.ex. om du frågar om hjälp när du löser den här uppgiften.

För att underlätta testandet listas några valida personbeteckningar nedan:

    \n
  • 230827-906F
  • \n
  • 120488+246L
  • \n
  • 310823A9877
  • \n

Formatera tid och datum

Modulen datetime innehåller metoden strftime som kan användas för att formatera hur ett datum representeras som sträng. Till exempel följande kodsnutt kommer att skriva ut det nuvarande datumet i formatet dd.mm.åååå och därefter datumet och tiden i ett annat format:

from datetime import datetime\n\ntid = datetime.now()\nprint(tid.strftime(\"%d.%m.%Y\"))

04.02.2020

Tidsformatering använder specifika tecken för att indikera ett visst format. Här är ett antal tecken (flera finns i Pythons dokumentation):

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FörkortningBetydelse
%ddag (01–31)
%mmånad (01–12)
%Yår (fyra siffor)
%Htimme (24 timmars format)
%Mminut (00–59)
%Ssekund (00–59)

Du kan också specificera vilket tecken som används för att skilja elementen i ett datum, så som du såg i exemplet ovan.

Formatering för datetime fungerar också åt det andra hållet, det vill säga om du tar emot ett datum som en sträng från en användare och vill få det i datetime-format. Använd då metoden strptime:

from datetime import datetime\n\nsyote = input(\"Ange din födelsetid i formatet dd.mm.åååå: \")\ntid = datetime.strptime(syote, \"%d.%m.%Y\")\n\nif tid < datetime(2000, 1, 1):\n    print(\"Du föddes på förra årtusendet\")\nelse:\n    print(\"Du föddes på det här årtusendet\")

Ange din födelsetid i formatet dd.mm.åååå: 5.11.1986\nDu föddes på förra årtusendet

I det här programmet antecknar vi användarens dagliga skärmtid (tv, dator, mobil) i en fil.

Så här ska programmet fungera:

Fil: juni.txt\nFörsta dagen: 24.6.2020\nAntal dagar: 5\nAnge skärmtiden i minuter (tv dator mobil):\nSkärmtid 24.06.2020: 60 120 0\nSkärmtid 25.06.2020: 0 0 0\nSkärmtid 26.06.2020: 180 0 0\nSkärmtid 27.06.2020: 25 240 15\nSkärmtid 28.06.2020: 45 90 5\nInfon lagrad i filen juni.txt

För varje dag anger man alltså tre minutvärden skilda med mellanslag.

Programmet lagrar statistik i filen juni.txt som i vårt fall ser ut så här:

Tidsperiod: 24.06.2020-28.06.2020\nMinuter tillsammans: 780\nMinuter i genomsnitt: 156.0\n24.06.2020: 60/120/0\n25.06.2020: 0/0/0\n26.06.2020: 180/0/0\n27.06.2020: 25/240/15\n28.06.2020: 45/90/5

","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/3-tid-datum.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-7/4-behandla-data/page-data.json b/page-data/osa-7/4-behandla-data/page-data.json index 5111d868d..9b5a3eac8 100644 --- a/page-data/osa-7/4-behandla-data/page-data.json +++ b/page-data/osa-7/4-behandla-data/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-7/4-behandla-data", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda en modul för att behandla CSV-filer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda en modul för att behandla JSON-filer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du komma åt filer från internet och läsa dem."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lasa-csv-filer","style":"position:relative;"},"children":[{"type":"text","value":"Läsa CSV-filer"},{"type":"element","tagName":"a","properties":{"href":"#lasa-csv-filer","ariaLabel":"lasa csv filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV är ett så enkelt format att vi tills vidare har behandlat dessa filer med självskriven kod. Det finns däremot en färdig modul för CSV-filer i Pythons standardbibliotek: "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/csv.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"csv"}]}]},{"type":"text","value":". Modulen fungerar så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" csv\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" csv"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delimiter"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden ovan läser alla rader i CSV-filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test.csv"}]},{"type":"text","value":", skiljer innehåller på varje rad med separatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" och skriver ut varje lista. Om innehållet i filen är det följande…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"012121212;5\n012345678;2\n015151515;4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…borde koden skriva ut det här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['012121212', '5']\n['012345678', '2']\n['015151515', '4']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom CSV-formatet är så enkelt, kan vi fråga oss vad nyttan med en skild modul är då vi bara kunde använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":"-funktionen. Det finns några bra orsaker. En orsak är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"csv"}]},{"type":"text","value":"-modulen klarar av strängar som innehåller skiljetecknet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"aaa;bbb\";\"ccc;ddd\""}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden ovan skulle då ge följande resultat:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['aaa;bbb', 'ccc;ddd']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi skulle använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":"-funktionen, skulle själva strängarna också delas i två, vilket skulle förstöra den data som ska behandlas och antagligen orsaka problem i programmet."}]},{"type":"element","tagName":"h2","properties":{"id":"lasa-json-filer","style":"position:relative;"},"children":[{"type":"text","value":"Läsa JSON-filer"},{"type":"element","tagName":"a","properties":{"href":"#lasa-json-filer","ariaLabel":"lasa json filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV är bara ett maskinläsbart dataformat. "},{"type":"element","tagName":"a","properties":{"href":"https://www.json.org/json-sv.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"JSON"}]},{"type":"text","value":" är ett annat sådant format och det används ofta när data överförs mellan olika program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"JSON-filer är textfiler som följer en strikt syntax, som eventuellt är lite mindre människovänligt än CSV-formatet. Följande exempel använder filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kurser.json"}]},{"type":"text","value":", som innehåller information om några kurser:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introkurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"id\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ohpe\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"perioder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"id\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ohja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"perioder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Databasapplikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"id\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tsoha\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"perioder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strukturen hos en JSON-fil kanske ser bekant ut. JSON-filen ser ut som en Python-lista som innehåller tre Python-lexikon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Standardbiblioteket innehåller "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/json.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"json"}]},{"type":"text","value":"-modulen"}]},{"type":"text","value":" som kan användas för att behandla JSON-filer. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"loads"}]},{"type":"text","value":" tar emot ett argument som innehåller data i JSON-format och konverterar den till Pythons egen datastruktur. När vi använder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kurser.json"}]},{"type":"text","value":" i koden nedan…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kurser.json\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n data "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" json"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"loads"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…får vi följande utskrift:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[{'namn': 'Introkurs i programmering', 'id': 'Ohpe', 'perioder': [1, 3]}, {'namn': 'Fortsättningskurs i programmering', 'id': 'Ohja', 'perioder': [2, 4]}, {'namn': 'Databasapplikation', 'id': 'Tsoha', 'perioder': [1, 2, 3, 4]}]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill skriva ut namnet på varje kurs kan vi använda en for-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" kurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Introkurs i programmering\nFortsättningskurs i programmering\nDatabasapplikation"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"JSON-fil","tmcname":"osa07-12_json"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi inspekterar en JSON-fil med innehåll om studerande i följande format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Piritta Pythonist\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"alder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"27"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"hobbyer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"att koda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"att virka\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jessica Javalig\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"alder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"hobbyer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"att koda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bergsklättring\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"att läsa\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_personer(fil: str)"}]},{"type":"text","value":" som läser en JSON-fil enligt formatet ovan och skriver ut informationen så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Piritta Pythonist 27 år (att koda, att virka)\nJessica Javalig 24 år (att koda, bergsklättring, att läsa)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hobbyerna ska vara i samma ordning som i JSON-filen."}]}]},{"type":"element","tagName":"h2","properties":{"id":"hamta-en-fil-fran-internet","style":"position:relative;"},"children":[{"type":"text","value":"Hämta en fil från internet"},{"type":"element","tagName":"a","properties":{"href":"#hamta-en-fil-fran-internet","ariaLabel":"hamta en fil fran internet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Standardbiblioteket i Python innehåller också moduler som kan användas för att hantera innehåll på internet. En nyttig funktion är "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"urllib.request.urlopen"}]}]},{"type":"text","value":". Det lönar sig att bekanta sig med modulen som helhet, men följande exempel borde räcka till för att få en insikt i hur funktionen fungerar. Den kan användas för att hämta data från internet, för vidare behandling i ditt program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande kodstunn skriver ut innehållet från huvudsidan för Helsingfors universitet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n\nbegaran "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"urlopen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://helsinki.fi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"begaran"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sidor som är skräddarsydda för människoögon ser inte vanligtvis trevliga ut när deras kod skrivs ut. I de kommande exemplen kommer vi däremot att läsa in data i maskinformat från internet. En stor del maskinläsbara data på internet är i JSON-format."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursstatistik","tmcname":"osa07-13_kursstatistik"},"children":[{"type":"element","tagName":"h4","properties":{"id":"info-om-kurser","style":"position:relative;"},"children":[{"type":"text","value":"Info om kurser"},{"type":"element","tagName":"a","properties":{"href":"#info-om-kurser","ariaLabel":"info om kurser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På adressen "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses"}]},{"type":"text","value":" finns det info om några nätkurser i JSON-format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_alla()"}]},{"type":"text","value":" som hämtar och returnerar de aktiva kursernas (fältet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enabled"}]},{"type":"text","value":" har värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":") info som en lista med tupler enligt följande exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"[\n ('Full Stack Open 2020', 'ofs2019', 2020, 201),\n ('DevOps with Docker 2019', 'docker2019', 2019, 36),\n ('DevOps with Docker 2020', 'docker2020', 2020, 36),\n ('Beta DevOps with Kubernetes', 'beta-dwk-20', 2020, 28)\n]\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla tupler innehåller alltså"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kursens namn ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fullName"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kort namn ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"år ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"totalt antal övningar ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises"}]},{"type":"text","value":")."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Du måste använda funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"urllib.request.urlopen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Hårdkoda inte data, testerna kommer att avslöja sådana försök!!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! En del Mac-användare har stött på följande problem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"File "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py\""}]},{"type":"text","value":", line "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1353"}]},{"type":"text","value":", "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" do_open\n raise URLError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nurllib.error.URLError: "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"urlopen error "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"SSL: CERTIFICATE_VERIFY_FAILED"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" certificate verify failed: unable to get "},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"local"}]},{"type":"text","value":" issuer certificate "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"_ssl.c:1124"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lösningen beror på hur Python är installerat på datorn. I vissa fall fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"/Applications/Python 3.8/\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sudo"}]},{"type":"text","value":" \"./Install Certificates.command"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sökvägen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":"-kommandot beror på din Python-version."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://stackoverflow.com/q/27835619","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Här"}]},{"type":"text","value":" hittar du flera möjliga lösningar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan testa på följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" ssl "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till det här"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# och det här i början av funktioner"}]},{"type":"text","value":"\n context "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ssl"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_create_unverified_context"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# övrig kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt att komma runt problemet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" certifi "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till det här"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n adress "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://studies.cs.helsinki.fi/stats-mock/api/courses\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägger till ett andra argument"}]},{"type":"text","value":"\n begaran "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"urlopen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adress"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" cafile"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"certifi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"where"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# övrig kod"}]}]}]}]},{"type":"element","tagName":"h4","properties":{"id":"info-om-en-kurs","style":"position:relative;"},"children":[{"type":"text","value":"Info om en kurs"},{"type":"element","tagName":"a","properties":{"href":"#info-om-en-kurs","ariaLabel":"info om en kurs permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje kurs finns uppgiftsstatistik på adressen "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats"}]},{"type":"text","value":" – ersätt asteriskerna med kursens korta namn ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"T.ex. statistik för kursen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"docker2019"}]},{"type":"text","value":" finns på "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_kurs(kurs: str)"}]},{"type":"text","value":" som hämtar statistik för den valda kursen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi anropar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_kurs(\"docker2019\")"}]},{"type":"text","value":" returnerar funktionen ett lexikon med följande innehåll:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"{\n 'veckor': 4,\n 'studerande': 220,\n 'timmar': 5966,\n 'timmar_genomsnitt': 27,\n 'uppgifter': 4988,\n 'uppgifter_genomsnitt': 22\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdena bestäms enligt följande:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"veckor"}]},{"type":"text","value":": antal JSON-element som representerar en kurs"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande"}]},{"type":"text","value":": maxantal studerande under veckorna"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timmar"}]},{"type":"text","value":": summan av alla veckornas timantal ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hour_total"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timmar_genomsnitt"}]},{"type":"text","value":": föregående dividerat med antal studerande (heltal, avrundas nedåt)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uppgifter"}]},{"type":"text","value":": alla veckors uppgiftsantal ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercise_total"}]},{"type":"text","value":") summerat"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uppgifter_genomsnitt"}]},{"type":"text","value":": föregående dividerat med antal studerande (heltal, avrundas nedåt)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Alla "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Obs!"}]},{"type":"text","value":" ovan gäller också här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":"-modulen"}]},{"type":"text","value":" hittar du en funktion som gör det lätt att avrunda nedåt."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vem lurade","tmcname":"osa07-14_lurade"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start.csv"}]},{"type":"text","value":" finns starttider för tentor i formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id;hh:mm"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;09:00"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"timo;18:42"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"kalle;13:23"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"returnering.csv"}]},{"type":"text","value":" finns returneringstiden för uppgifter i formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id;uppgift;poäng;hh:mm"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;1;8;16:05"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"timo;2;10;21:22"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;2;10;19:15"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"o.s.v. ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din uppgift är att hitta de studerande som använt över tre timmar för att göra sin tenta, dvs. någon av uppgifterna har lämnats in över tre timmar sedan tentan inleddes. Det kan alltså finnas flera inlämningar. Du kan anta att alla klockslag är under samma dygn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fuskare()"}]},{"type":"text","value":" som returnerar en lista över fuskarnas id:n."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vem lurade, andra versionen","tmcname":"osa07-15_lurade_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du har till ditt förfogande de filer som presenterades i den förra uppgiften. Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"officiella_poang()"}]},{"type":"text","value":", som returnerar studerandenas provpoäng i ett lexikon enligt följande regler:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om samma uppgift har flera inlämningar, tas inlämningen med högsta poäng i beaktande"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om en inlämning gjorts över tre timmar efter att tentan inletts, ska inlämningen inte tas i beaktande."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgifterna är numrerade 1-8 och för varje uppgift kan man få 0-6 poäng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I lexikonet är nyckeln id:t och värdet totalpoängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Du kan lagra lexikon inuti lexikon, vilket kan vara till nytta då du lagrar poängantal och tider för olika studerande."}]}]},{"type":"element","tagName":"h2","properties":{"id":"hitta-moduler","style":"position:relative;"},"children":[{"type":"text","value":"Hitta moduler"},{"type":"element","tagName":"a","properties":{"href":"#hitta-moduler","ariaLabel":"hitta moduler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons officiella dokumentation innehåller information om alla moduler som är tillgängliga i standardbiblioteket:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://docs.python.org/3/library/"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förutom standardbiblioteket är internet fullproppat med andra Python-moduler för olika ändamål. Några vanliga moduler listas på den här sidan:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://wiki.python.org/moin/UsefulModules","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://wiki.python.org/moin/UsefulModules"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spell checker, andra versionen","tmcname":"osa07-16_spellcheck_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften förbättrar vi lite vårt språkkontrollverktyg."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som i den förra versionen ska programmet be användaren ange text på engelska. Programmet utför språkkontroll och markerar felstavade ord. Dessutom ska programmet nu föreslå lösningar på skrivfelen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Två exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"We use ptython to make a spell checker"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"We use *ptython* to make a spell checker\nförslag:\nptython: python, pythons, typhon\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"this is acually a good and usefull program"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"this is *acually* a good and *usefull* program\nförslag:\nacually: actually, tactually, factually\nusefull: usefully, useful, museful\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigeringsförslagen söks med hjälp av "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/difflib.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"difflib"}]}]},{"type":"text","value":" i standardbiblioteket: använd funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/difflib.html#difflib.get_close_matches","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_close_matches"}]}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! För att testen ska fungera ska du använda funktionen med dess förinställda val – ge alltså bara två argument: felaktiga ordet och listan med korrekta ord."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"3388732e-5324-53ce-8908-865a5fbf47c8"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du använda en modul för att behandla CSV-filer
  • \n
  • kan du använda en modul för att behandla JSON-filer
  • \n
  • kan du komma åt filer från internet och läsa dem.
  • \n

Läsa CSV-filer

CSV är ett så enkelt format att vi tills vidare har behandlat dessa filer med självskriven kod. Det finns däremot en färdig modul för CSV-filer i Pythons standardbibliotek: csv. Modulen fungerar så här:

import csv\n\nwith open(\"test.csv\") as fil:\n    for rad in csv.reader(fil, delimiter=\";\"):\n        print(rad)

Koden ovan läser alla rader i CSV-filen test.csv, skiljer innehåller på varje rad med separatorn ; och skriver ut varje lista. Om innehållet i filen är det följande…

012121212;5\n012345678;2\n015151515;4

…borde koden skriva ut det här:

['012121212', '5']\n['012345678', '2']\n['015151515', '4']

Eftersom CSV-formatet är så enkelt, kan vi fråga oss vad nyttan med en skild modul är då vi bara kunde använda split-funktionen. Det finns några bra orsaker. En orsak är att csv-modulen klarar av strängar som innehåller skiljetecknet:

\"aaa;bbb\";\"ccc;ddd\"

Koden ovan skulle då ge följande resultat:

['aaa;bbb', 'ccc;ddd']

Om vi skulle använda split-funktionen, skulle själva strängarna också delas i två, vilket skulle förstöra den data som ska behandlas och antagligen orsaka problem i programmet.

Läsa JSON-filer

CSV är bara ett maskinläsbart dataformat. JSON är ett annat sådant format och det används ofta när data överförs mellan olika program.

JSON-filer är textfiler som följer en strikt syntax, som eventuellt är lite mindre människovänligt än CSV-formatet. Följande exempel använder filen kurser.json, som innehåller information om några kurser:

[\n    {\n        \"namn\": \"Introkurs i programmering\",\n        \"id\": \"Ohpe\",\n        \"perioder\": [1, 3]\n    },\n    {\n        \"namn\": \"Fortsättningskurs i programmering\",\n        \"id\": \"Ohja\",\n        \"perioder\": [2, 4]\n    },\n    {\n        \"namn\": \"Databasapplikation\",\n        \"id\": \"Tsoha\",\n        \"perioder\": [1, 2, 3, 4]\n    }\n]

Strukturen hos en JSON-fil kanske ser bekant ut. JSON-filen ser ut som en Python-lista som innehåller tre Python-lexikon.

Standardbiblioteket innehåller json-modulen som kan användas för att behandla JSON-filer. Funktionen loads tar emot ett argument som innehåller data i JSON-format och konverterar den till Pythons egen datastruktur. När vi använder kurser.json i koden nedan…

import json\n\nwith open(\"kurser.json\") as fil:\n    data = fil.read()\nkurser = json.loads(data)\nprint(kurser)

…får vi följande utskrift:

[{'namn': 'Introkurs i programmering', 'id': 'Ohpe', 'perioder': [1, 3]}, {'namn': 'Fortsättningskurs i programmering', 'id': 'Ohja', 'perioder': [2, 4]}, {'namn': 'Databasapplikation', 'id': 'Tsoha', 'perioder': [1, 2, 3, 4]}]

Om vi vill skriva ut namnet på varje kurs kan vi använda en for-loop:

for kurs in kurser:\n    print(kurs[\"namn\"])

Introkurs i programmering\nFortsättningskurs i programmering\nDatabasapplikation

Vi inspekterar en JSON-fil med innehåll om studerande i följande format:

[\n    {\n        \"namn\": \"Piritta Pythonist\",\n        \"alder\": 27,\n        \"hobbyer\": [\n            \"att koda\",\n            \"att virka\"\n        ]\n    },\n    {\n        \"namn\": \"Jessica Javalig\",\n        \"alder\": 24,\n        \"hobbyer\": [\n            \"att koda\",\n            \"bergsklättring\",\n            \"att läsa\"\n        ]\n    }\n]

Skapa funktionen skriv_ut_personer(fil: str) som läser en JSON-fil enligt formatet ovan och skriver ut informationen så här:

Piritta Pythonist 27 år (att koda, att virka)\nJessica Javalig 24 år (att koda, bergsklättring, att läsa)

Hobbyerna ska vara i samma ordning som i JSON-filen.

Hämta en fil från internet

Standardbiblioteket i Python innehåller också moduler som kan användas för att hantera innehåll på internet. En nyttig funktion är urllib.request.urlopen. Det lönar sig att bekanta sig med modulen som helhet, men följande exempel borde räcka till för att få en insikt i hur funktionen fungerar. Den kan användas för att hämta data från internet, för vidare behandling i ditt program.

Följande kodstunn skriver ut innehållet från huvudsidan för Helsingfors universitet:

import urllib.request\n\nbegaran = urllib.request.urlopen(\"https://helsinki.fi\")\nprint(begaran.read())

Sidor som är skräddarsydda för människoögon ser inte vanligtvis trevliga ut när deras kod skrivs ut. I de kommande exemplen kommer vi däremot att läsa in data i maskinformat från internet. En stor del maskinläsbara data på internet är i JSON-format.

Info om kurser

På adressen https://studies.cs.helsinki.fi/stats-mock/api/courses finns det info om några nätkurser i JSON-format.

Skapa funktionen hamta_alla() som hämtar och returnerar de aktiva kursernas (fältet enabled har värdet True) info som en lista med tupler enligt följande exempel:

\n[\n    ('Full Stack Open 2020', 'ofs2019', 2020, 201),\n    ('DevOps with Docker 2019', 'docker2019', 2019, 36),\n    ('DevOps with Docker 2020', 'docker2020', 2020, 36),\n    ('Beta DevOps with Kubernetes', 'beta-dwk-20', 2020, 28)\n]\n

Alla tupler innehåller alltså

    \n
  • kursens namn (fullName)
  • \n
  • kort namn (name)
  • \n
  • år (year)
  • \n
  • totalt antal övningar (exercises).
  • \n

Obs! Du måste använda funktionen urllib.request.urlopen.

Obs! Hårdkoda inte data, testerna kommer att avslöja sådana försök!!

Obs! En del Mac-användare har stött på följande problem:

File \"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py\", line 1353, in do_open\n    raise URLError(err)\nurllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1124)>

Lösningen beror på hur Python är installerat på datorn. I vissa fall fungerar:

cd \"/Applications/Python 3.8/\"\nsudo \"./Install Certificates.command

Sökvägen i cd-kommandot beror på din Python-version.

Här hittar du flera möjliga lösningar.

Du kan testa på följande:

import urllib.request\nimport json\nimport ssl # lägg till det här\n\ndef hamta_alla():\n    # och det här i början av funktioner\n    context = ssl._create_unverified_context()\n    # övrig kod

Ett annat sätt att komma runt problemet:

import urllib.request\nimport certifi # lägg till det här\nimport json\n\ndef hamta_alla():\n   adress = \"https://studies.cs.helsinki.fi/stats-mock/api/courses\"\n   # lägger till ett andra argument\n   begaran = urllib.request.urlopen(adress, cafile=certifi.where())\n   # övrig kod

Info om en kurs

För varje kurs finns uppgiftsstatistik på adressen https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats – ersätt asteriskerna med kursens korta namn (name).

T.ex. statistik för kursen docker2019 finns på https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats.

Skapa funktionen hamta_kurs(kurs: str) som hämtar statistik för den valda kursen.

När vi anropar hamta_kurs(\"docker2019\") returnerar funktionen ett lexikon med följande innehåll:

\n{\n    'veckor': 4,\n    'studerande': 220,\n    'timmar': 5966,\n    'timmar_genomsnitt': 27,\n    'uppgifter': 4988,\n    'uppgifter_genomsnitt': 22\n}\n

Värdena bestäms enligt följande:

    \n
  • veckor: antal JSON-element som representerar en kurs
  • \n
  • studerande: maxantal studerande under veckorna
  • \n
  • timmar: summan av alla veckornas timantal (hour_total)
  • \n
  • timmar_genomsnitt: föregående dividerat med antal studerande (heltal, avrundas nedåt)
  • \n
  • uppgifter: alla veckors uppgiftsantal (exercise_total) summerat
  • \n
  • uppgifter_genomsnitt: föregående dividerat med antal studerande (heltal, avrundas nedåt)
  • \n

Obs! Alla Obs! ovan gäller också här.

Obs! I math-modulen hittar du en funktion som gör det lätt att avrunda nedåt.

I filen start.csv finns starttider för tentor i formatet id;hh:mm:

jarmo;09:00\ntimo;18:42\nkalle;13:23

I filen returnering.csv finns returneringstiden för uppgifter i formatet id;uppgift;poäng;hh:mm:

jarmo;1;8;16:05\ntimo;2;10;21:22\njarmo;2;10;19:15\no.s.v. ...

Din uppgift är att hitta de studerande som använt över tre timmar för att göra sin tenta, dvs. någon av uppgifterna har lämnats in över tre timmar sedan tentan inleddes. Det kan alltså finnas flera inlämningar. Du kan anta att alla klockslag är under samma dygn.

Skapa funktionen fuskare() som returnerar en lista över fuskarnas id:n.

Du har till ditt förfogande de filer som presenterades i den förra uppgiften. Skapa funktionen officiella_poang(), som returnerar studerandenas provpoäng i ett lexikon enligt följande regler:

    \n
  • om samma uppgift har flera inlämningar, tas inlämningen med högsta poäng i beaktande
  • \n
  • om en inlämning gjorts över tre timmar efter att tentan inletts, ska inlämningen inte tas i beaktande.
  • \n

Uppgifterna är numrerade 1-8 och för varje uppgift kan man få 0-6 poäng.

I lexikonet är nyckeln id:t och värdet totalpoängen.

Tips: Du kan lagra lexikon inuti lexikon, vilket kan vara till nytta då du lagrar poängantal och tider för olika studerande.

Hitta moduler

Pythons officiella dokumentation innehåller information om alla moduler som är tillgängliga i standardbiblioteket:

Förutom standardbiblioteket är internet fullproppat med andra Python-moduler för olika ändamål. Några vanliga moduler listas på den här sidan:

I den här uppgiften förbättrar vi lite vårt språkkontrollverktyg.

Som i den förra versionen ska programmet be användaren ange text på engelska. Programmet utför språkkontroll och markerar felstavade ord. Dessutom ska programmet nu föreslå lösningar på skrivfelen.

Två exempel:

write text: We use ptython to make a spell checker

\nWe use *ptython* to make a spell checker\nförslag:\nptython: python, pythons, typhon\n

write text: this is acually a good and usefull program

\nthis is *acually* a good and *usefull* program\nförslag:\nacually: actually, tactually, factually\nusefull: usefully, useful, museful\n

Korrigeringsförslagen söks med hjälp av modulen difflib i standardbiblioteket: använd funktionen get_close_matches.

Obs! För att testen ska fungera ska du använda funktionen med dess förinställda val – ge alltså bara två argument: felaktiga ordet och listan med korrekta ord.

","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/4-behandla-data.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda en modul för att behandla CSV-filer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du använda en modul för att behandla JSON-filer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du komma åt filer från internet och läsa dem."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lasa-csv-filer","style":"position:relative;"},"children":[{"type":"text","value":"Läsa CSV-filer"},{"type":"element","tagName":"a","properties":{"href":"#lasa-csv-filer","ariaLabel":"lasa csv filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV är ett så enkelt format att vi tills vidare har behandlat dessa filer med självskriven kod. Det finns däremot en färdig modul för CSV-filer i Pythons standardbibliotek: "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/csv.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"csv"}]}]},{"type":"text","value":". Modulen fungerar så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" csv\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" rad "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" csv"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delimiter"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\";\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden ovan läser alla rader i CSV-filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test.csv"}]},{"type":"text","value":", skiljer innehåller på varje rad med separatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" och skriver ut varje lista. Om innehållet i filen är det följande…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"012121212;5\n012345678;2\n015151515;4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…borde koden skriva ut det här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['012121212', '5']\n['012345678', '2']\n['015151515', '4']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom CSV-formatet är så enkelt, kan vi fråga oss vad nyttan med en skild modul är då vi bara kunde använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":"-funktionen. Det finns några bra orsaker. En orsak är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"csv"}]},{"type":"text","value":"-modulen klarar av strängar som innehåller skiljetecknet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"aaa;bbb\";\"ccc;ddd\""}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koden ovan skulle då ge följande resultat:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['aaa;bbb', 'ccc;ddd']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi skulle använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":"-funktionen, skulle själva strängarna också delas i två, vilket skulle förstöra den data som ska behandlas och antagligen orsaka problem i programmet."}]},{"type":"element","tagName":"h2","properties":{"id":"lasa-json-filer","style":"position:relative;"},"children":[{"type":"text","value":"Läsa JSON-filer"},{"type":"element","tagName":"a","properties":{"href":"#lasa-json-filer","ariaLabel":"lasa json filer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV är bara ett maskinläsbart dataformat. "},{"type":"element","tagName":"a","properties":{"href":"https://www.json.org/json-sv.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"JSON"}]},{"type":"text","value":" är ett annat sådant format och det används ofta när data överförs mellan olika program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"JSON-filer är textfiler som följer en strikt syntax, som eventuellt är lite mindre människovänligt än CSV-formatet. Följande exempel använder filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kurser.json"}]},{"type":"text","value":", som innehåller information om några kurser:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introkurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"id\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ohpe\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"perioder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fortsättningskurs i programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"id\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ohja\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"perioder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Databasapplikation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"id\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tsoha\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"perioder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strukturen hos en JSON-fil kanske ser bekant ut. JSON-filen ser ut som en Python-lista som innehåller tre Python-lexikon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Standardbiblioteket innehåller "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/json.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"json"}]},{"type":"text","value":"-modulen"}]},{"type":"text","value":" som kan användas för att behandla JSON-filer. Funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"loads"}]},{"type":"text","value":" tar emot ett argument som innehåller data i JSON-format och konverterar den till Pythons egen datastruktur. När vi använder "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kurser.json"}]},{"type":"text","value":" i koden nedan…"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kurser.json\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n data "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" json"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"loads"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"…får vi följande utskrift:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[{'namn': 'Introkurs i programmering', 'id': 'Ohpe', 'perioder': [1, 3]}, {'namn': 'Fortsättningskurs i programmering', 'id': 'Ohja', 'perioder': [2, 4]}, {'namn': 'Databasapplikation', 'id': 'Tsoha', 'perioder': [1, 2, 3, 4]}]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi vill skriva ut namnet på varje kurs kan vi använda en for-loop:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" kurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Introkurs i programmering\nFortsättningskurs i programmering\nDatabasapplikation"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"JSON-fil","tmcname":"osa07-12_json"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi inspekterar en JSON-fil med innehåll om studerande i följande format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Piritta Pythonist\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"alder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"27"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"hobbyer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"att koda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"att virka\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jessica Javalig\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"alder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"hobbyer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"att koda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bergsklättring\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"att läsa\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_personer(fil: str)"}]},{"type":"text","value":" som läser en JSON-fil enligt formatet ovan och skriver ut informationen så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Piritta Pythonist 27 år (att koda, att virka)\nJessica Javalig 24 år (att koda, bergsklättring, att läsa)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hobbyerna ska vara i samma ordning som i JSON-filen."}]}]},{"type":"element","tagName":"h2","properties":{"id":"hamta-en-fil-fran-internet","style":"position:relative;"},"children":[{"type":"text","value":"Hämta en fil från internet"},{"type":"element","tagName":"a","properties":{"href":"#hamta-en-fil-fran-internet","ariaLabel":"hamta en fil fran internet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Standardbiblioteket i Python innehåller också moduler som kan användas för att hantera innehåll på internet. En nyttig funktion är "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"urllib.request.urlopen"}]}]},{"type":"text","value":". Det lönar sig att bekanta sig med modulen som helhet, men följande exempel borde räcka till för att få en insikt i hur funktionen fungerar. Den kan användas för att hämta data från internet, för vidare behandling i ditt program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande kodstunn skriver ut innehållet från huvudsidan för Helsingfors universitet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n\nbegaran "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"urlopen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://helsinki.fi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"begaran"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sidor som är skräddarsydda för människoögon ser inte vanligtvis trevliga ut när deras kod skrivs ut. I de kommande exemplen kommer vi däremot att läsa in data i maskinformat från internet. En stor del maskinläsbara data på internet är i JSON-format."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Kursstatistik","tmcname":"osa07-13_kursstatistik"},"children":[{"type":"element","tagName":"h4","properties":{"id":"info-om-kurser","style":"position:relative;"},"children":[{"type":"text","value":"Info om kurser"},{"type":"element","tagName":"a","properties":{"href":"#info-om-kurser","ariaLabel":"info om kurser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På adressen "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses"}]},{"type":"text","value":" finns det info om några nätkurser i JSON-format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_alla()"}]},{"type":"text","value":" som hämtar och returnerar de aktiva kursernas (fältet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enabled"}]},{"type":"text","value":" har värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":") info som en lista med tupler enligt följande exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"[\n ('Full Stack Open 2020', 'ofs2019', 2020, 201),\n ('DevOps with Docker 2019', 'docker2019', 2019, 36),\n ('DevOps with Docker 2020', 'docker2020', 2020, 36),\n ('Beta DevOps with Kubernetes', 'beta-dwk-20', 2020, 28)\n]\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla tupler innehåller alltså"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kursens namn ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fullName"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kort namn ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"år ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"totalt antal övningar ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises"}]},{"type":"text","value":")."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Du måste använda funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"urllib.request.urlopen"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Hårdkoda inte data, testerna kommer att avslöja sådana försök!!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! En del Mac-användare har stött på följande problem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"File "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py\""}]},{"type":"text","value":", line "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1353"}]},{"type":"text","value":", "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" do_open\n raise URLError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nurllib.error.URLError: "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"urlopen error "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"SSL: CERTIFICATE_VERIFY_FAILED"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" certificate verify failed: unable to get "},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"local"}]},{"type":"text","value":" issuer certificate "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"_ssl.c:1124"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lösningen beror på hur Python är installerat på datorn. I vissa fall fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"/Applications/Python 3.8/\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sudo"}]},{"type":"text","value":" \"./Install Certificates.command"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sökvägen i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":"-kommandot beror på din Python-version."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://stackoverflow.com/q/27835619","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Här"}]},{"type":"text","value":" hittar du flera möjliga lösningar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan testa på följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" ssl "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till det här"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# och det här i början av funktioner"}]},{"type":"text","value":"\n context "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ssl"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_create_unverified_context"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# övrig kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett annat sätt att komma runt problemet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" certifi "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägg till det här"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_alla"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n adress "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://studies.cs.helsinki.fi/stats-mock/api/courses\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# lägger till ett andra argument"}]},{"type":"text","value":"\n begaran "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"urlopen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adress"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" cafile"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"certifi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"where"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# övrig kod"}]}]}]}]},{"type":"element","tagName":"h4","properties":{"id":"info-om-en-kurs","style":"position:relative;"},"children":[{"type":"text","value":"Info om en kurs"},{"type":"element","tagName":"a","properties":{"href":"#info-om-en-kurs","ariaLabel":"info om en kurs permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För varje kurs finns uppgiftsstatistik på adressen "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats"}]},{"type":"text","value":" – ersätt asteriskerna med kursens korta namn ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"T.ex. statistik för kursen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"docker2019"}]},{"type":"text","value":" finns på "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_kurs(kurs: str)"}]},{"type":"text","value":" som hämtar statistik för den valda kursen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi anropar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_kurs(\"docker2019\")"}]},{"type":"text","value":" returnerar funktionen ett lexikon med följande innehåll:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"{\n 'veckor': 4,\n 'studerande': 220,\n 'timmar': 5966,\n 'timmar_genomsnitt': 27,\n 'uppgifter': 4988,\n 'uppgifter_genomsnitt': 22\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Värdena bestäms enligt följande:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"veckor"}]},{"type":"text","value":": antal JSON-element som representerar en kurs"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande"}]},{"type":"text","value":": maxantal studerande under veckorna"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timmar"}]},{"type":"text","value":": summan av alla veckornas timantal ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hour_total"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timmar_genomsnitt"}]},{"type":"text","value":": föregående dividerat med antal studerande (heltal, avrundas nedåt)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uppgifter"}]},{"type":"text","value":": alla veckors uppgiftsantal ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercise_total"}]},{"type":"text","value":") summerat"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uppgifter_genomsnitt"}]},{"type":"text","value":": föregående dividerat med antal studerande (heltal, avrundas nedåt)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Alla "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Obs!"}]},{"type":"text","value":" ovan gäller också här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! I "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":"-modulen"}]},{"type":"text","value":" hittar du en funktion som gör det lätt att avrunda nedåt."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vem lurade","tmcname":"osa07-14_lurade"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start.csv"}]},{"type":"text","value":" finns starttider för tentor i formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id;hh:mm"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;09:00"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"timo;18:42"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"kalle;13:23"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"returnering.csv"}]},{"type":"text","value":" finns returneringstiden för uppgifter i formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id;uppgift;poäng;hh:mm"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;1;8;16:05"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"timo;2;10;21:22"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;2;10;19:15"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"o.s.v. ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din uppgift är att hitta de studerande som använt över tre timmar för att göra sin tenta, dvs. någon av uppgifterna har lämnats in över tre timmar sedan tentan inleddes. Det kan alltså finnas flera inlämningar. Du kan anta att alla klockslag är under samma dygn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fuskare()"}]},{"type":"text","value":" som returnerar en lista över fuskarnas id:n."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vem lurade, andra versionen","tmcname":"osa07-15_lurade_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du har till ditt förfogande de filer som presenterades i den förra uppgiften. Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"officiella_poang()"}]},{"type":"text","value":", som returnerar studerandenas provpoäng i ett lexikon enligt följande regler:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om samma uppgift har flera inlämningar, tas inlämningen med högsta poäng i beaktande"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"om en inlämning gjorts över tre timmar efter att tentan inletts, ska inlämningen inte tas i beaktande."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgifterna är numrerade 1-8 och för varje uppgift kan man få 0-6 poäng."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I lexikonet är nyckeln id:t och värdet totalpoängen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tips: Du kan lagra lexikon inuti lexikon, vilket kan vara till nytta då du lagrar poängantal och tider för olika studerande."}]}]},{"type":"element","tagName":"h2","properties":{"id":"hitta-moduler","style":"position:relative;"},"children":[{"type":"text","value":"Hitta moduler"},{"type":"element","tagName":"a","properties":{"href":"#hitta-moduler","ariaLabel":"hitta moduler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pythons officiella dokumentation innehåller information om alla moduler som är tillgängliga i standardbiblioteket:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://docs.python.org/3/library/"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förutom standardbiblioteket är internet fullproppat med andra Python-moduler för olika ändamål. Några vanliga moduler listas på den här sidan:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://wiki.python.org/moin/UsefulModules","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://wiki.python.org/moin/UsefulModules"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spell checker, andra versionen","tmcname":"osa07-16_spellcheck_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiften förbättrar vi lite vårt språkkontrollverktyg."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som i den förra versionen ska programmet be användaren ange text på engelska. Programmet utför språkkontroll och markerar felstavade ord. Dessutom ska programmet nu föreslå lösningar på skrivfelen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Två exempel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"We use ptython to make a spell checker"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"We use *ptython* to make a spell checker\nförslag:\nptython: python, pythons, typhon\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"this is acually a good and usefull program"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"this is *acually* a good and *usefull* program\nförslag:\nacually: actually, tactually, factually\nusefull: usefully, useful, museful\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Korrigeringsförslagen söks med hjälp av "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/difflib.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"difflib"}]}]},{"type":"text","value":" i standardbiblioteket: använd funktionen "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/difflib.html#difflib.get_close_matches","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_close_matches"}]}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! För att testen ska fungera ska du använda funktionen med dess förinställda val – ge alltså bara två argument: felaktiga ordet och listan med korrekta ord."}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"3388732e-5324-53ce-8908-865a5fbf47c8"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du använda en modul för att behandla CSV-filer
  • \n
  • kan du använda en modul för att behandla JSON-filer
  • \n
  • kan du komma åt filer från internet och läsa dem.
  • \n

Läsa CSV-filer

CSV är ett så enkelt format att vi tills vidare har behandlat dessa filer med självskriven kod. Det finns däremot en färdig modul för CSV-filer i Pythons standardbibliotek: csv. Modulen fungerar så här:

import csv\n\nwith open(\"test.csv\") as fil:\n    for rad in csv.reader(fil, delimiter=\";\"):\n        print(rad)

Koden ovan läser alla rader i CSV-filen test.csv, skiljer innehåller på varje rad med separatorn ; och skriver ut varje lista. Om innehållet i filen är det följande…

012121212;5\n012345678;2\n015151515;4

…borde koden skriva ut det här:

['012121212', '5']\n['012345678', '2']\n['015151515', '4']

Eftersom CSV-formatet är så enkelt, kan vi fråga oss vad nyttan med en skild modul är då vi bara kunde använda split-funktionen. Det finns några bra orsaker. En orsak är att csv-modulen klarar av strängar som innehåller skiljetecknet:

\"aaa;bbb\";\"ccc;ddd\"

Koden ovan skulle då ge följande resultat:

['aaa;bbb', 'ccc;ddd']

Om vi skulle använda split-funktionen, skulle själva strängarna också delas i två, vilket skulle förstöra den data som ska behandlas och antagligen orsaka problem i programmet.

Läsa JSON-filer

CSV är bara ett maskinläsbart dataformat. JSON är ett annat sådant format och det används ofta när data överförs mellan olika program.

JSON-filer är textfiler som följer en strikt syntax, som eventuellt är lite mindre människovänligt än CSV-formatet. Följande exempel använder filen kurser.json, som innehåller information om några kurser:

[\n    {\n        \"namn\": \"Introkurs i programmering\",\n        \"id\": \"Ohpe\",\n        \"perioder\": [1, 3]\n    },\n    {\n        \"namn\": \"Fortsättningskurs i programmering\",\n        \"id\": \"Ohja\",\n        \"perioder\": [2, 4]\n    },\n    {\n        \"namn\": \"Databasapplikation\",\n        \"id\": \"Tsoha\",\n        \"perioder\": [1, 2, 3, 4]\n    }\n]

Strukturen hos en JSON-fil kanske ser bekant ut. JSON-filen ser ut som en Python-lista som innehåller tre Python-lexikon.

Standardbiblioteket innehåller json-modulen som kan användas för att behandla JSON-filer. Funktionen loads tar emot ett argument som innehåller data i JSON-format och konverterar den till Pythons egen datastruktur. När vi använder kurser.json i koden nedan…

import json\n\nwith open(\"kurser.json\") as fil:\n    data = fil.read()\nkurser = json.loads(data)\nprint(kurser)

…får vi följande utskrift:

[{'namn': 'Introkurs i programmering', 'id': 'Ohpe', 'perioder': [1, 3]}, {'namn': 'Fortsättningskurs i programmering', 'id': 'Ohja', 'perioder': [2, 4]}, {'namn': 'Databasapplikation', 'id': 'Tsoha', 'perioder': [1, 2, 3, 4]}]

Om vi vill skriva ut namnet på varje kurs kan vi använda en for-loop:

for kurs in kurser:\n    print(kurs[\"namn\"])

Introkurs i programmering\nFortsättningskurs i programmering\nDatabasapplikation

Vi inspekterar en JSON-fil med innehåll om studerande i följande format:

[\n    {\n        \"namn\": \"Piritta Pythonist\",\n        \"alder\": 27,\n        \"hobbyer\": [\n            \"att koda\",\n            \"att virka\"\n        ]\n    },\n    {\n        \"namn\": \"Jessica Javalig\",\n        \"alder\": 24,\n        \"hobbyer\": [\n            \"att koda\",\n            \"bergsklättring\",\n            \"att läsa\"\n        ]\n    }\n]

Skapa funktionen skriv_ut_personer(fil: str) som läser en JSON-fil enligt formatet ovan och skriver ut informationen så här:

Piritta Pythonist 27 år (att koda, att virka)\nJessica Javalig 24 år (att koda, bergsklättring, att läsa)

Hobbyerna ska vara i samma ordning som i JSON-filen.

Hämta en fil från internet

Standardbiblioteket i Python innehåller också moduler som kan användas för att hantera innehåll på internet. En nyttig funktion är urllib.request.urlopen. Det lönar sig att bekanta sig med modulen som helhet, men följande exempel borde räcka till för att få en insikt i hur funktionen fungerar. Den kan användas för att hämta data från internet, för vidare behandling i ditt program.

Följande kodstunn skriver ut innehållet från huvudsidan för Helsingfors universitet:

import urllib.request\n\nbegaran = urllib.request.urlopen(\"https://helsinki.fi\")\nprint(begaran.read())

Sidor som är skräddarsydda för människoögon ser inte vanligtvis trevliga ut när deras kod skrivs ut. I de kommande exemplen kommer vi däremot att läsa in data i maskinformat från internet. En stor del maskinläsbara data på internet är i JSON-format.

Info om kurser

På adressen https://studies.cs.helsinki.fi/stats-mock/api/courses finns det info om några nätkurser i JSON-format.

Skapa funktionen hamta_alla() som hämtar och returnerar de aktiva kursernas (fältet enabled har värdet True) info som en lista med tupler enligt följande exempel:

\n[\n    ('Full Stack Open 2020', 'ofs2019', 2020, 201),\n    ('DevOps with Docker 2019', 'docker2019', 2019, 36),\n    ('DevOps with Docker 2020', 'docker2020', 2020, 36),\n    ('Beta DevOps with Kubernetes', 'beta-dwk-20', 2020, 28)\n]\n

Alla tupler innehåller alltså

    \n
  • kursens namn (fullName)
  • \n
  • kort namn (name)
  • \n
  • år (year)
  • \n
  • totalt antal övningar (exercises).
  • \n

Obs! Du måste använda funktionen urllib.request.urlopen.

Obs! Hårdkoda inte data, testerna kommer att avslöja sådana försök!!

Obs! En del Mac-användare har stött på följande problem:

File \"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py\", line 1353, in do_open\n    raise URLError(err)\nurllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1124)>

Lösningen beror på hur Python är installerat på datorn. I vissa fall fungerar:

cd \"/Applications/Python 3.8/\"\nsudo \"./Install Certificates.command

Sökvägen i cd-kommandot beror på din Python-version.

Här hittar du flera möjliga lösningar.

Du kan testa på följande:

import urllib.request\nimport json\nimport ssl # lägg till det här\n\ndef hamta_alla():\n    # och det här i början av funktioner\n    context = ssl._create_unverified_context()\n    # övrig kod

Ett annat sätt att komma runt problemet:

import urllib.request\nimport certifi # lägg till det här\nimport json\n\ndef hamta_alla():\n   adress = \"https://studies.cs.helsinki.fi/stats-mock/api/courses\"\n   # lägger till ett andra argument\n   begaran = urllib.request.urlopen(adress, cafile=certifi.where())\n   # övrig kod

Info om en kurs

För varje kurs finns uppgiftsstatistik på adressen https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats – ersätt asteriskerna med kursens korta namn (name).

T.ex. statistik för kursen docker2019 finns på https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats.

Skapa funktionen hamta_kurs(kurs: str) som hämtar statistik för den valda kursen.

När vi anropar hamta_kurs(\"docker2019\") returnerar funktionen ett lexikon med följande innehåll:

\n{\n    'veckor': 4,\n    'studerande': 220,\n    'timmar': 5966,\n    'timmar_genomsnitt': 27,\n    'uppgifter': 4988,\n    'uppgifter_genomsnitt': 22\n}\n

Värdena bestäms enligt följande:

    \n
  • veckor: antal JSON-element som representerar en kurs
  • \n
  • studerande: maxantal studerande under veckorna
  • \n
  • timmar: summan av alla veckornas timantal (hour_total)
  • \n
  • timmar_genomsnitt: föregående dividerat med antal studerande (heltal, avrundas nedåt)
  • \n
  • uppgifter: alla veckors uppgiftsantal (exercise_total) summerat
  • \n
  • uppgifter_genomsnitt: föregående dividerat med antal studerande (heltal, avrundas nedåt)
  • \n

Obs! Alla Obs! ovan gäller också här.

Obs! I math-modulen hittar du en funktion som gör det lätt att avrunda nedåt.

I filen start.csv finns starttider för tentor i formatet id;hh:mm:

jarmo;09:00\ntimo;18:42\nkalle;13:23

I filen returnering.csv finns returneringstiden för uppgifter i formatet id;uppgift;poäng;hh:mm:

jarmo;1;8;16:05\ntimo;2;10;21:22\njarmo;2;10;19:15\no.s.v. ...

Din uppgift är att hitta de studerande som använt över tre timmar för att göra sin tenta, dvs. någon av uppgifterna har lämnats in över tre timmar sedan tentan inleddes. Det kan alltså finnas flera inlämningar. Du kan anta att alla klockslag är under samma dygn.

Skapa funktionen fuskare() som returnerar en lista över fuskarnas id:n.

Du har till ditt förfogande de filer som presenterades i den förra uppgiften. Skapa funktionen officiella_poang(), som returnerar studerandenas provpoäng i ett lexikon enligt följande regler:

    \n
  • om samma uppgift har flera inlämningar, tas inlämningen med högsta poäng i beaktande
  • \n
  • om en inlämning gjorts över tre timmar efter att tentan inletts, ska inlämningen inte tas i beaktande.
  • \n

Uppgifterna är numrerade 1-8 och för varje uppgift kan man få 0-6 poäng.

I lexikonet är nyckeln id:t och värdet totalpoängen.

Tips: Du kan lagra lexikon inuti lexikon, vilket kan vara till nytta då du lagrar poängantal och tider för olika studerande.

Hitta moduler

Pythons officiella dokumentation innehåller information om alla moduler som är tillgängliga i standardbiblioteket:

Förutom standardbiblioteket är internet fullproppat med andra Python-moduler för olika ändamål. Några vanliga moduler listas på den här sidan:

I den här uppgiften förbättrar vi lite vårt språkkontrollverktyg.

Som i den förra versionen ska programmet be användaren ange text på engelska. Programmet utför språkkontroll och markerar felstavade ord. Dessutom ska programmet nu föreslå lösningar på skrivfelen.

Två exempel:

write text: We use ptython to make a spell checker

\nWe use *ptython* to make a spell checker\nförslag:\nptython: python, pythons, typhon\n

write text: this is acually a good and usefull program

\nthis is *acually* a good and *usefull* program\nförslag:\nacually: actually, tactually, factually\nusefull: usefully, useful, museful\n

Korrigeringsförslagen söks med hjälp av modulen difflib i standardbiblioteket: använd funktionen get_close_matches.

Obs! För att testen ska fungera ska du använda funktionen med dess förinställda val – ge alltså bara två argument: felaktiga ordet och listan med korrekta ord.

","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/4-behandla-data.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-7/5-egna-moduler/page-data.json b/page-data/osa-7/5-egna-moduler/page-data.json index 253a5a3e1..6df5db593 100644 --- a/page-data/osa-7/5-egna-moduler/page-data.json +++ b/page-data/osa-7/5-egna-moduler/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-7/5-egna-moduler", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa dina egna moduler"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad Python-variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__name__"}]},{"type":"text","value":" och värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__main__"}]},{"type":"text","value":" har för betydelse."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skapa dina egna Python-moduler är enkelt. Vilken som helst fil som innehåller valid Python-kod kan importeras som en modul. Låt oss säga att vi har filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord.py"}]},{"type":"text","value":", med följande innehåll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ord_ett"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sista_ordet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"antal_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionerna som definierats i filen kan kommas åt genom att importera filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Polisstationen var full\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"antal_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Polisstationen\nfull\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Filen som innehåller Python-modulen måste befinna sig i samma mapp där programmet som importerar modulen finns i. Alternativt kan filen med modulen vara i någon av de andra mappar där Python söker moduler ifrån. I övriga fall kommer Pythontolken inte att hitta modulen när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan använda våra moduler exakt på samma sätt som moduler i Pythons standardbibliotek:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sista_ordet\n\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange mening: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Första ordet var: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista ordet var: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange mening: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Han höll på att svimma i den heta bussen"}]},{"type":"text","value":"\nFörsta ordet var: Han\nSista ordet var: bussen"}]}]},{"type":"element","tagName":"h2","properties":{"id":"anvanda-typledtradar","style":"position:relative;"},"children":[{"type":"text","value":"Använda typledtrådar"},{"type":"element","tagName":"a","properties":{"href":"#anvanda-typledtradar","ariaLabel":"anvanda typledtradar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi använder moduler är typledtrådar speciellt till nytta. Om du använder en kodeditor som stödjer typledtrådar, kommer användandet av moduler att bli mycket enklare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel Visual Studio Code visar typledtrådar medan du skriver kod:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 519px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 23.91304347826087%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAsklEQVQY032Q2w6DIBBE+Q4DmpSLilwEtG82sf//T9OwxD7VPpzM7IadJcv6vscdQojbPuec/DAMxOXZv0AuOKy12Pcn1jUh54JSCo7jhfN8I4RI/WmaEeMK7wNY3XRH13Wwcwvctp2ooc55Gq5B4zjBmJF8VWaMglIKWmtIKWGMofrq5ZKRc/7+poZJqfB4SNL2VpOvsBAsvHeIMWJZFqRUh1qdcoILrt1McNLKdbdffADLIo82jUnA7AAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7772b5ee03bb1ca30f17560902916121/a0b58/7_vihje.webp 230w","/static/7772b5ee03bb1ca30f17560902916121/bc10c/7_vihje.webp 460w","/static/7772b5ee03bb1ca30f17560902916121/ad2dd/7_vihje.webp 519w"],"sizes":"(max-width: 519px) 100vw, 519px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7772b5ee03bb1ca30f17560902916121/81c8e/7_vihje.png 230w","/static/7772b5ee03bb1ca30f17560902916121/08a84/7_vihje.png 460w","/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png 519w"],"sizes":"(max-width: 519px) 100vw, 519px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png","alt":"7 vihje","title":"7 vihje","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"kod-i-modulens-huvudfunktion","style":"position:relative;"},"children":[{"type":"text","value":"Kod i modulens huvudfunktion"},{"type":"element","tagName":"a","properties":{"href":"#kod-i-modulens-huvudfunktion","ariaLabel":"kod i modulens huvudfunktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en modul innehåller kod som inte finns inom en funktionsdefinition (dvs. kod i huvudfunktionen), kommer den här koden att köras automatiskt då modulen importeras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss se på en situation där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord.py"}]},{"type":"text","value":" innehåller några testutskrifter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ord_ett"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sista_ordet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"antal_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ett annat test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Postlådan var full med tidningar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi nu importerar modulen med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-sats, kommer all kod utanför de definierade funktionerna att köras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Polisstationen var full\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"antal_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det\ntest\n5\nPolisstationen\nfull\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du ser, är det här inte en riktigt bra sak i vår situation, eftersom vår utskrift nu blandas med testutskrifter från modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till all lycka finns en lösning, och den är bekant sedan tidigare. Vi måste helt enkelt testa om programmet körs självständigt eller om koden har importerats med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-sats. Python har en inbyggd variabel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__name__"}]},{"type":"text","value":" som innehåller namnet på programmet som körs. Om programmet körs självständigt är värdet på den nyss nämnda variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__main__"}]},{"type":"text","value":". Om programmet däremot har importerats är värdet på variabeln namnet på modulen som importerats (i vårt fall "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu när vi vet det här kan vi lägga till en if-sats som låter oss köra våra test endast då programmet körs självständigt. Som du ser nedan, är strukturen bekant:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ord_ett"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sista_ordet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"antal_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testar funktionerna"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ett annat test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sanojen_lkm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Postlådan var full med tidningar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du kör modulen självständigt, skrivs testutskrifterna ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det\ntest\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När modulen importeras i ett program, kommer testutskrifterna inte att göras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Polisstationen var full\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"antal_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Polisstationen\nfull\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgifterna under den här kursen har du flera gånger ombetts att ha dina test under ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -block. Nu vet du varför."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Teckenverktyg","tmcname":"osa07-17_teckenverktyg"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"teckenverktyg"}]},{"type":"text","value":" som innehåller de följande funktionerna:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"byt_storlek(strang: str)"}]},{"type":"text","value":": returnerar den givna strängen så att versaler och gemener har konverterats till gemener respektive versaler"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halvera(strang: str)"}]},{"type":"text","value":": returnerar den givna strängen halverad i en tuple, vid behov är den första halvan kortare"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"specialtecken_bort(stang: str)"}]},{"type":"text","value":" returnerar den givna strängen så att andra tecken än a-ö, A-Ö, siffror och mellanslag är avlägsnade."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" teckenverktyg\n\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"teckenverktyg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"byt_storlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\np1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" teckenverktyg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"halvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nm2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" teckenverktyg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specialtecken_bort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett test, men hur går det??00\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"hEJ ALLA!\nHej\nalla!\nDet här är ett test men hur går det00"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät om materialet för den här veckan."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa dina egna moduler
  • \n
  • vet du vad Python-variabeln __name__ och värdet __main__ har för betydelse.
  • \n

Att skapa dina egna Python-moduler är enkelt. Vilken som helst fil som innehåller valid Python-kod kan importeras som en modul. Låt oss säga att vi har filen ord.py, med följande innehåll:

def ord_ett(strang: str):\n    delar = strang.split(\" \")\n    return delar[0]\n\ndef sista_ordet(strang: str):\n    delar = strang.split(\" \")\n    return delar[-1]\n\ndef antal_ord(strang: str):\n    delar = strang.split(\" \")\n    return len(delar)

Funktionerna som definierats i filen kan kommas åt genom att importera filen:

import ord\n\nstrang = \"Polisstationen var full\"\n\nprint(ord.ord_ett(strang))\nprint(ord.sista_ordet(strang))\nprint(ord.antal_ord(strang))

Polisstationen\nfull\n3

Obs! Filen som innehåller Python-modulen måste befinna sig i samma mapp där programmet som importerar modulen finns i. Alternativt kan filen med modulen vara i någon av de andra mappar där Python söker moduler ifrån. I övriga fall kommer Pythontolken inte att hitta modulen när import-satsen körs.

Vi kan använda våra moduler exakt på samma sätt som moduler i Pythons standardbibliotek:

from ord import ord_ett, sista_ordet\n\nmening = input(\"Ange mening: \")\n\nprint(\"Första ordet var: \" + ord_ett(mening))\nprint(\"Sista ordet var: \" + sista_ordet(mening))

Ange mening: Han höll på att svimma i den heta bussen\nFörsta ordet var: Han\nSista ordet var: bussen

Använda typledtrådar

När vi använder moduler är typledtrådar speciellt till nytta. Om du använder en kodeditor som stödjer typledtrådar, kommer användandet av moduler att bli mycket enklare.

Till exempel Visual Studio Code visar typledtrådar medan du skriver kod:

\n \n \n \n \n \n \"7\n \n \n

Kod i modulens huvudfunktion

Om en modul innehåller kod som inte finns inom en funktionsdefinition (dvs. kod i huvudfunktionen), kommer den här koden att köras automatiskt då modulen importeras.

Låt oss se på en situation där ord.py innehåller några testutskrifter:

def ord_ett(strang: str):\n    delar = strang.split(\" \")\n    return delar[0]\n\ndef sista_ordet(strang: str):\n    delar = strang.split(\" \")\n    return delar[-1]\n\ndef antal_ord(strang: str):\n    delar = strang.split(\" \")\n    return len(delar)\n\nprint(ord_ett(\"Det här är ett test\"))\nprint(sista_ordet(\"Ett annat test\"))\nprint(antal_ord(\"Postlådan var full med tidningar\"))

Om vi nu importerar modulen med en import-sats, kommer all kod utanför de definierade funktionerna att köras:

import ord\n\nstrang = \"Polisstationen var full\"\n\nprint(ord.ord_ett(strang))\nprint(ord.sista_ordet(strang))\nprint(ord.antal_ord(strang))

Det\ntest\n5\nPolisstationen\nfull\n3

Som du ser, är det här inte en riktigt bra sak i vår situation, eftersom vår utskrift nu blandas med testutskrifter från modulen.

Till all lycka finns en lösning, och den är bekant sedan tidigare. Vi måste helt enkelt testa om programmet körs självständigt eller om koden har importerats med en import-sats. Python har en inbyggd variabel __name__ som innehåller namnet på programmet som körs. Om programmet körs självständigt är värdet på den nyss nämnda variabeln __main__. Om programmet däremot har importerats är värdet på variabeln namnet på modulen som importerats (i vårt fall ord).

Nu när vi vet det här kan vi lägga till en if-sats som låter oss köra våra test endast då programmet körs självständigt. Som du ser nedan, är strukturen bekant:

def ord_ett(strang: str) -> str:\n    delar = strang.split(\" \")\n    return delar[0]\n\ndef sista_ordet(strang: str) -> str:\n    delar = strang.split(\" \")\n    return delar[-1]\n\ndef antal_ord(strang: str) -> int:\n    delar = strang.split(\" \")\n    return len(delar)\n\nif __name__ == \"__main__\":\n    # testar funktionerna\n    print(ord_ett(\"Det här är ett test\"))\n    print(sista_ordet(\"Ett annat test\"))\n    print(sanojen_lkm(\"Postlådan var full med tidningar\"))

Om du kör modulen självständigt, skrivs testutskrifterna ut:

Det\ntest\n5

När modulen importeras i ett program, kommer testutskrifterna inte att göras:

import ord\n\nstrang = \"Polisstationen var full\"\n\nprint(ord.ord_ett(strang))\nprint(ord.sista_ordet(strang))\nprint(ord.antal_ord(strang))

Polisstationen\nfull\n3

I uppgifterna under den här kursen har du flera gånger ombetts att ha dina test under ett if __name__ == \"__main__\" -block. Nu vet du varför.

Skapa modulen teckenverktyg som innehåller de följande funktionerna:

    \n
  • byt_storlek(strang: str): returnerar den givna strängen så att versaler och gemener har konverterats till gemener respektive versaler
  • \n
  • halvera(strang: str): returnerar den givna strängen halverad i en tuple, vid behov är den första halvan kortare
  • \n
  • specialtecken_bort(stang: str) returnerar den givna strängen så att andra tecken än a-ö, A-Ö, siffror och mellanslag är avlägsnade.
  • \n

Exempel:

import teckenverktyg\n\nstrang = \"Hej alla!\"\n\nprint(teckenverktyg.byt_storlek(strang))\n\np1, p2 = teckenverktyg.halvera(strang)\n\nprint(p1)\nprint(p2)\n\nm2 = teckenverktyg.specialtecken_bort(\"Det här är ett test, men hur går det??00\")\nprint(m2)

hEJ ALLA!\nHej\nalla!\nDet här är ett test men hur går det00

Vänligen svara på en kort enkät om materialet för den här veckan.

","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/5-egna-moduler.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"kan du skapa dina egna moduler"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"vet du vad Python-variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__name__"}]},{"type":"text","value":" och värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__main__"}]},{"type":"text","value":" har för betydelse."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att skapa dina egna Python-moduler är enkelt. Vilken som helst fil som innehåller valid Python-kod kan importeras som en modul. Låt oss säga att vi har filen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord.py"}]},{"type":"text","value":", med följande innehåll:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ord_ett"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sista_ordet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"antal_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionerna som definierats i filen kan kommas åt genom att importera filen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Polisstationen var full\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"antal_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Polisstationen\nfull\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! Filen som innehåller Python-modulen måste befinna sig i samma mapp där programmet som importerar modulen finns i. Alternativt kan filen med modulen vara i någon av de andra mappar där Python söker moduler ifrån. I övriga fall kommer Pythontolken inte att hitta modulen när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satsen körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan använda våra moduler exakt på samma sätt som moduler i Pythons standardbibliotek:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sista_ordet\n\nmening "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"input"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ange mening: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Första ordet var: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sista ordet var: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mening"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange mening: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Han höll på att svimma i den heta bussen"}]},{"type":"text","value":"\nFörsta ordet var: Han\nSista ordet var: bussen"}]}]},{"type":"element","tagName":"h2","properties":{"id":"anvanda-typledtradar","style":"position:relative;"},"children":[{"type":"text","value":"Använda typledtrådar"},{"type":"element","tagName":"a","properties":{"href":"#anvanda-typledtradar","ariaLabel":"anvanda typledtradar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När vi använder moduler är typledtrådar speciellt till nytta. Om du använder en kodeditor som stödjer typledtrådar, kommer användandet av moduler att bli mycket enklare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel Visual Studio Code visar typledtrådar medan du skriver kod:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 519px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 23.91304347826087%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAsklEQVQY032Q2w6DIBBE+Q4DmpSLilwEtG82sf//T9OwxD7VPpzM7IadJcv6vscdQojbPuec/DAMxOXZv0AuOKy12Pcn1jUh54JSCo7jhfN8I4RI/WmaEeMK7wNY3XRH13Wwcwvctp2ooc55Gq5B4zjBmJF8VWaMglIKWmtIKWGMofrq5ZKRc/7+poZJqfB4SNL2VpOvsBAsvHeIMWJZFqRUh1qdcoILrt1McNLKdbdffADLIo82jUnA7AAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7772b5ee03bb1ca30f17560902916121/a0b58/7_vihje.webp 230w","/static/7772b5ee03bb1ca30f17560902916121/bc10c/7_vihje.webp 460w","/static/7772b5ee03bb1ca30f17560902916121/ad2dd/7_vihje.webp 519w"],"sizes":"(max-width: 519px) 100vw, 519px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7772b5ee03bb1ca30f17560902916121/81c8e/7_vihje.png 230w","/static/7772b5ee03bb1ca30f17560902916121/08a84/7_vihje.png 460w","/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png 519w"],"sizes":"(max-width: 519px) 100vw, 519px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png","alt":"7 vihje","title":"7 vihje","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"kod-i-modulens-huvudfunktion","style":"position:relative;"},"children":[{"type":"text","value":"Kod i modulens huvudfunktion"},{"type":"element","tagName":"a","properties":{"href":"#kod-i-modulens-huvudfunktion","ariaLabel":"kod i modulens huvudfunktion permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en modul innehåller kod som inte finns inom en funktionsdefinition (dvs. kod i huvudfunktionen), kommer den här koden att köras automatiskt då modulen importeras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss se på en situation där "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord.py"}]},{"type":"text","value":" innehåller några testutskrifter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ord_ett"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sista_ordet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"antal_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ett annat test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antal_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Postlådan var full med tidningar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om vi nu importerar modulen med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-sats, kommer all kod utanför de definierade funktionerna att köras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Polisstationen var full\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"antal_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det\ntest\n5\nPolisstationen\nfull\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du ser, är det här inte en riktigt bra sak i vår situation, eftersom vår utskrift nu blandas med testutskrifter från modulen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till all lycka finns en lösning, och den är bekant sedan tidigare. Vi måste helt enkelt testa om programmet körs självständigt eller om koden har importerats med en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-sats. Python har en inbyggd variabel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__name__"}]},{"type":"text","value":" som innehåller namnet på programmet som körs. Om programmet körs självständigt är värdet på den nyss nämnda variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__main__"}]},{"type":"text","value":". Om programmet däremot har importerats är värdet på variabeln namnet på modulen som importerats (i vårt fall "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu när vi vet det här kan vi lägga till en if-sats som låter oss köra våra test endast då programmet körs självständigt. Som du ser nedan, är strukturen bekant:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ord_ett"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sista_ordet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"antal_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n delar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"delar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testar funktionerna"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ett annat test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sanojen_lkm"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Postlådan var full med tidningar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du kör modulen självständigt, skrivs testutskrifterna ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det\ntest\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När modulen importeras i ett program, kommer testutskrifterna inte att göras:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Polisstationen var full\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ord_ett"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sista_ordet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"antal_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Polisstationen\nfull\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgifterna under den här kursen har du flera gånger ombetts att ha dina test under ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" -block. Nu vet du varför."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Teckenverktyg","tmcname":"osa07-17_teckenverktyg"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"teckenverktyg"}]},{"type":"text","value":" som innehåller de följande funktionerna:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"byt_storlek(strang: str)"}]},{"type":"text","value":": returnerar den givna strängen så att versaler och gemener har konverterats till gemener respektive versaler"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"halvera(strang: str)"}]},{"type":"text","value":": returnerar den givna strängen halverad i en tuple, vid behov är den första halvan kortare"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"specialtecken_bort(stang: str)"}]},{"type":"text","value":" returnerar den givna strängen så att andra tecken än a-ö, A-Ö, siffror och mellanslag är avlägsnade."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" teckenverktyg\n\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"teckenverktyg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"byt_storlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\np1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" teckenverktyg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"halvera"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nm2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" teckenverktyg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specialtecken_bort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det här är ett test, men hur går det??00\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"hEJ ALLA!\nHej\nalla!\nDet här är ett test men hur går det00"}]}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en kort enkät om materialet för den här veckan."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • kan du skapa dina egna moduler
  • \n
  • vet du vad Python-variabeln __name__ och värdet __main__ har för betydelse.
  • \n

Att skapa dina egna Python-moduler är enkelt. Vilken som helst fil som innehåller valid Python-kod kan importeras som en modul. Låt oss säga att vi har filen ord.py, med följande innehåll:

def ord_ett(strang: str):\n    delar = strang.split(\" \")\n    return delar[0]\n\ndef sista_ordet(strang: str):\n    delar = strang.split(\" \")\n    return delar[-1]\n\ndef antal_ord(strang: str):\n    delar = strang.split(\" \")\n    return len(delar)

Funktionerna som definierats i filen kan kommas åt genom att importera filen:

import ord\n\nstrang = \"Polisstationen var full\"\n\nprint(ord.ord_ett(strang))\nprint(ord.sista_ordet(strang))\nprint(ord.antal_ord(strang))

Polisstationen\nfull\n3

Obs! Filen som innehåller Python-modulen måste befinna sig i samma mapp där programmet som importerar modulen finns i. Alternativt kan filen med modulen vara i någon av de andra mappar där Python söker moduler ifrån. I övriga fall kommer Pythontolken inte att hitta modulen när import-satsen körs.

Vi kan använda våra moduler exakt på samma sätt som moduler i Pythons standardbibliotek:

from ord import ord_ett, sista_ordet\n\nmening = input(\"Ange mening: \")\n\nprint(\"Första ordet var: \" + ord_ett(mening))\nprint(\"Sista ordet var: \" + sista_ordet(mening))

Ange mening: Han höll på att svimma i den heta bussen\nFörsta ordet var: Han\nSista ordet var: bussen

Använda typledtrådar

När vi använder moduler är typledtrådar speciellt till nytta. Om du använder en kodeditor som stödjer typledtrådar, kommer användandet av moduler att bli mycket enklare.

Till exempel Visual Studio Code visar typledtrådar medan du skriver kod:

\n \n \n \n \n \n \"7\n \n \n

Kod i modulens huvudfunktion

Om en modul innehåller kod som inte finns inom en funktionsdefinition (dvs. kod i huvudfunktionen), kommer den här koden att köras automatiskt då modulen importeras.

Låt oss se på en situation där ord.py innehåller några testutskrifter:

def ord_ett(strang: str):\n    delar = strang.split(\" \")\n    return delar[0]\n\ndef sista_ordet(strang: str):\n    delar = strang.split(\" \")\n    return delar[-1]\n\ndef antal_ord(strang: str):\n    delar = strang.split(\" \")\n    return len(delar)\n\nprint(ord_ett(\"Det här är ett test\"))\nprint(sista_ordet(\"Ett annat test\"))\nprint(antal_ord(\"Postlådan var full med tidningar\"))

Om vi nu importerar modulen med en import-sats, kommer all kod utanför de definierade funktionerna att köras:

import ord\n\nstrang = \"Polisstationen var full\"\n\nprint(ord.ord_ett(strang))\nprint(ord.sista_ordet(strang))\nprint(ord.antal_ord(strang))

Det\ntest\n5\nPolisstationen\nfull\n3

Som du ser, är det här inte en riktigt bra sak i vår situation, eftersom vår utskrift nu blandas med testutskrifter från modulen.

Till all lycka finns en lösning, och den är bekant sedan tidigare. Vi måste helt enkelt testa om programmet körs självständigt eller om koden har importerats med en import-sats. Python har en inbyggd variabel __name__ som innehåller namnet på programmet som körs. Om programmet körs självständigt är värdet på den nyss nämnda variabeln __main__. Om programmet däremot har importerats är värdet på variabeln namnet på modulen som importerats (i vårt fall ord).

Nu när vi vet det här kan vi lägga till en if-sats som låter oss köra våra test endast då programmet körs självständigt. Som du ser nedan, är strukturen bekant:

def ord_ett(strang: str) -> str:\n    delar = strang.split(\" \")\n    return delar[0]\n\ndef sista_ordet(strang: str) -> str:\n    delar = strang.split(\" \")\n    return delar[-1]\n\ndef antal_ord(strang: str) -> int:\n    delar = strang.split(\" \")\n    return len(delar)\n\nif __name__ == \"__main__\":\n    # testar funktionerna\n    print(ord_ett(\"Det här är ett test\"))\n    print(sista_ordet(\"Ett annat test\"))\n    print(sanojen_lkm(\"Postlådan var full med tidningar\"))

Om du kör modulen självständigt, skrivs testutskrifterna ut:

Det\ntest\n5

När modulen importeras i ett program, kommer testutskrifterna inte att göras:

import ord\n\nstrang = \"Polisstationen var full\"\n\nprint(ord.ord_ett(strang))\nprint(ord.sista_ordet(strang))\nprint(ord.antal_ord(strang))

Polisstationen\nfull\n3

I uppgifterna under den här kursen har du flera gånger ombetts att ha dina test under ett if __name__ == \"__main__\" -block. Nu vet du varför.

Skapa modulen teckenverktyg som innehåller de följande funktionerna:

    \n
  • byt_storlek(strang: str): returnerar den givna strängen så att versaler och gemener har konverterats till gemener respektive versaler
  • \n
  • halvera(strang: str): returnerar den givna strängen halverad i en tuple, vid behov är den första halvan kortare
  • \n
  • specialtecken_bort(stang: str) returnerar den givna strängen så att andra tecken än a-ö, A-Ö, siffror och mellanslag är avlägsnade.
  • \n

Exempel:

import teckenverktyg\n\nstrang = \"Hej alla!\"\n\nprint(teckenverktyg.byt_storlek(strang))\n\np1, p2 = teckenverktyg.halvera(strang)\n\nprint(p1)\nprint(p2)\n\nm2 = teckenverktyg.specialtecken_bort(\"Det här är ett test, men hur går det??00\")\nprint(m2)

hEJ ALLA!\nHej\nalla!\nDet här är ett test men hur går det00

Vänligen svara på en kort enkät om materialet för den här veckan.

","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/5-egna-moduler.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-7/6-funktionaliteter-python/page-data.json b/page-data/osa-7/6-funktionaliteter-python/page-data.json index 4303f6a2c..622a40fd0 100644 --- a/page-data/osa-7/6-funktionaliteter-python/page-data.json +++ b/page-data/osa-7/6-funktionaliteter-python/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-7/6-funktionaliteter-python", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"har du bekantat dig med några fler funktionaliteter i Python."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att knyta ihop den här kursen, ska du ännu få en lista av nyttiga funktionaliteter som Python har att erbjuda."}]},{"type":"element","tagName":"h2","properties":{"id":"if-satser-pa-en-rad","style":"position:relative;"},"children":[{"type":"text","value":"If-satser på en rad"},{"type":"element","tagName":"a","properties":{"href":"#if-satser-pa-en-rad","ariaLabel":"if satser pa en rad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande satser ger samma resultat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jämn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ojämn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jämn\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ojämn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det senare exemplet har vi en if-sats i enradsformat: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a if [villkor] else b"}]},{"type":"text","value":". Värdet för uttrycket blir "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" då villkoret är sant och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" i övriga fall. På engelska kallas detta ternary operator."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If-satser kan användas då man vill tilldela något värde baserat på ett villkor. Till exempel om du har variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" och vill antingen öka eller ändra på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":":s värde baserat på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" paritet (jämnt/udda tal), kan du använda en normal if-else-sats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eller så kan du göra det hela med en rad kod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"ett-tomt-block","style":"position:relative;"},"children":[{"type":"text","value":"Ett \"tomt\" block"},{"type":"element","tagName":"a","properties":{"href":"#ett-tomt-block","ariaLabel":"ett tomt block permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du minns kanske att block inte kan vara tomma i Python. Om du behöver ett block som inte gör någonting – till exempel då du testar någon annan funktionalitet – kan du använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"-instruktionen. Du kan till exempel skapa en funktion som inte gör någonting:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen kommer att returnera direkt. Om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"-instruktionen lämnas bort, kommer koden att ge ett fel, eftersom block inte kan vara tomma."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"loopar-med-else-block","style":"position:relative;"},"children":[{"type":"text","value":"Loopar med else-block"},{"type":"element","tagName":"a","properties":{"href":"#loopar-med-else-block","ariaLabel":"loopar med else block permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python kan loopar också ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":"-block. Koden inom det här blocket körs då loopen avslutas normalt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel här går vi igenom en lista med siffror. Om det finns ett jämnt tal i listan, kommer ett meddelande att skrivas ut och loopen avslutas. Om det inte finns några jämna tal, kommer loopen att avslutas vanligt, men ett annat meddelande skrivs ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittade jämnt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittade inte jämnt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett mera traditionellt sätt att göra det här är att använda en hjälpvariabel som minns om en viss typ av element har hittats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nhittades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittade jämnt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hittades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" hittades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittade inte jämnt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda en for-else-sats sparar oss den tid som skulle krävas för att skapa en hjälpvariabel och logiken kring den."}]},{"type":"element","tagName":"h2","properties":{"id":"standardvarde-for-en-parameter","style":"position:relative;"},"children":[{"type":"text","value":"Standardvärde för en parameter"},{"type":"element","tagName":"a","properties":{"href":"#standardvarde-for-en-parameter","ariaLabel":"standardvarde for en parameter permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python kan funktioners parametrar ha ett standardvärde. Det används då ett argument inte ges till en funktion då den anropas. Se följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tervehdi"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nimi"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nimi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntervehdi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntervehdi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Elin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntervehdi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej, Emilia\nHej, Elin\nHej, Malin"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! En tom sträng är fortfarande en sträng. Standardvärdet kommer inte att användas om en tom sträng ges som argument till en funktion."}]},{"type":"element","tagName":"h2","properties":{"id":"andrande-antal-parametrar","style":"position:relative;"},"children":[{"type":"text","value":"Ändrande antal parametrar"},{"type":"element","tagName":"a","properties":{"href":"#andrande-antal-parametrar","ariaLabel":"andrande antal parametrar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också definiera att en funktion har ett ändrande antal parametrar, genom att lägga till en asterisk för parameternamnet. Alla argument som ges till funktionen lagras i en tuple som kan kommas åt genom den namngivna parametern."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande funktion räknar antalet argument som getts samt summan av dem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du gav\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"argument\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Deras summa är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du gav 5 argument\nDeras summa är 15"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Eget programmeringsspråk","tmcname":"osa07-18_programmeringssprak"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgifter skapar vi ett program som kan köra vårt eget programmeringsspråk. Du kan använda allt du lärt dig under den här kursen när du utför den här uppgiften."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmen består av rader, som kan följa något av dessa format:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT [värde]"}]},{"type":"text","value":": skriver ut det givna värdet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MOV [variabel] [värde]"}]},{"type":"text","value":": tilldelar variabeln det givna värdet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADD [variabel] [värde]"}]},{"type":"text","value":": adderar till den givna variabeln"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SUB [variabel] [värde]"}]},{"type":"text","value":": subtraherar från den givna variabeln"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MUL [variabel] [värde]"}]},{"type":"text","value":": multiplicerar den givna variabeln"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ställe]:"}]},{"type":"text","value":": definierar ett ställe dit man senare kan förflyttas till"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"JUMP [ställe]"}]},{"type":"text","value":": gå till specificerat ställe"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IF [villkor] JUMP [ställe]"}]},{"type":"text","value":": om villkoret är sant, gå till specificerat ställe"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":": avslutar programmet"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet körs rad för rad från och med den första raden. Programmet avslutas vid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":" eller då den sista raden passeras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet har 26 variabler ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[variabel]"}]},{"type":"text","value":") namngivna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Z"}]},{"type":"text","value":". Alla variabler är 0 då programmet startar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla värden som behandlas är helta. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[värde]"}]},{"type":"text","value":" hänvisar till en variabel eller ett givet heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ställe]"}]},{"type":"text","value":" består av bokstäverna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"z"}]},{"type":"text","value":" och siffrorna 0-9. Olika ställen kan inte ha samma namn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[villkor]"}]},{"type":"text","value":" ges i formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[värde] [jämförelse] [värde]"}]},{"type":"text","value":". "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[jämförelse]"}]},{"type":"text","value":" är något av de följande: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kor(program)"}]},{"type":"text","value":" som tar emot programmet som en lista. Ett element i listan representerar en rad kod. Funktionen ska returnera resultaten från alla "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT"}]},{"type":"text","value":"-instruktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att det program som ges till funktionen har korrekt syntax. Funktionen behöver inte alltså kunna behandla sådana här fel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan få två poäng av den här uppgiften: ett poäng om instruktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MOV"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADD"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SUB"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MUL"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":" fungerar, och ett poäng om resterande instruktioner fungerar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel 1:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"program1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel 2:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"program2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"start:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF A >= B JUMP slut\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SUB B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP start\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"slut:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 10, 2, 9, 3, 8, 4, 7, 5, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel 3 (fakultet):"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"program3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"start:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MUL A B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF B <= 10 JUMP start\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel 4 (primtal):"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"program4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV N 50\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"start:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV Z 0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV C B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nytt:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF C == A JUMP fel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF C > A JUMP skippa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD C B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP nytt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV Z 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP skippa2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"skippa:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF B < A JUMP test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"skippa2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF Z == 1 JUMP skippa3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"skippa3:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF A <= N JUMP start\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på kursfeedbacksenkäten här nedan. Enkätens resultat hjälper oss att utveckla och förbättra den här kursen."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • har du bekantat dig med några fler funktionaliteter i Python.
  • \n

För att knyta ihop den här kursen, ska du ännu få en lista av nyttiga funktionaliteter som Python har att erbjuda.

If-satser på en rad

Följande satser ger samma resultat:

if x%2 == 0:\n    print(\"jämn\")\nelse:\n    print(\"ojämn\")
print(\"jämn\" if x%2 == 0 else \"ojämn\")

I det senare exemplet har vi en if-sats i enradsformat: a if [villkor] else b. Värdet för uttrycket blir a då villkoret är sant och b i övriga fall. På engelska kallas detta ternary operator.

If-satser kan användas då man vill tilldela något värde baserat på ett villkor. Till exempel om du har variablerna x och y och vill antingen öka eller ändra på y:s värde baserat på x paritet (jämnt/udda tal), kan du använda en normal if-else-sats:

if x%2 == 0:\n    y += 1\nelse:\n    y = 0

Eller så kan du göra det hela med en rad kod:

y = y + 1 if x%2 == 0 else 0

Ett \"tomt\" block

Du minns kanske att block inte kan vara tomma i Python. Om du behöver ett block som inte gör någonting – till exempel då du testar någon annan funktionalitet – kan du använda pass-instruktionen. Du kan till exempel skapa en funktion som inte gör någonting:

def test():\n    pass

Funktionen kommer att returnera direkt. Om pass-instruktionen lämnas bort, kommer koden att ge ett fel, eftersom block inte kan vara tomma.

def test():

Loopar med else-block

I Python kan loopar också ett else-block. Koden inom det här blocket körs då loopen avslutas normalt.

Till exempel här går vi igenom en lista med siffror. Om det finns ett jämnt tal i listan, kommer ett meddelande att skrivas ut och loopen avslutas. Om det inte finns några jämna tal, kommer loopen att avslutas vanligt, men ett annat meddelande skrivs ut:

lista = [3,5,2,8,1]\nfor x in lista:\n    if x%2 == 0:\n        print(\"hittade jämnt tal\", x)\n        break\nelse:\n    print(\"hittade inte jämnt tal\")

Ett mera traditionellt sätt att göra det här är att använda en hjälpvariabel som minns om en viss typ av element har hittats:

lista = [3,5,2,8,1]\nhittades = False\nfor x in lista:\n    if x%2 == 0:\n        print(\"hittade jämnt tal\", x)\n        hittades = True\n        break\nif not hittades:\n    print(\"hittade inte jämnt tal\")

Att använda en for-else-sats sparar oss den tid som skulle krävas för att skapa en hjälpvariabel och logiken kring den.

Standardvärde för en parameter

I Python kan funktioners parametrar ha ett standardvärde. Det används då ett argument inte ges till en funktion då den anropas. Se följande exempel:

def tervehdi(nimi=\"Emilia\"):\n    print(\"Hej,\", nimi)\n\ntervehdi()\ntervehdi(\"Elin\")\ntervehdi(\"Malin\")

Hej, Emilia\nHej, Elin\nHej, Malin

Obs! En tom sträng är fortfarande en sträng. Standardvärdet kommer inte att användas om en tom sträng ges som argument till en funktion.

Ändrande antal parametrar

Du kan också definiera att en funktion har ett ändrande antal parametrar, genom att lägga till en asterisk för parameternamnet. Alla argument som ges till funktionen lagras i en tuple som kan kommas åt genom den namngivna parametern.

Följande funktion räknar antalet argument som getts samt summan av dem:

def test(*lista):\n    print(\"Du gav\", len(lista), \"argument\")\n    print(\"Deras summa är\", sum(lista))\n\ntest(1, 2, 3, 4, 5)

Du gav 5 argument\nDeras summa är 15

I den här uppgifter skapar vi ett program som kan köra vårt eget programmeringsspråk. Du kan använda allt du lärt dig under den här kursen när du utför den här uppgiften.

Programmen består av rader, som kan följa något av dessa format:

    \n
  • PRINT [värde]: skriver ut det givna värdet
  • \n
  • MOV [variabel] [värde]: tilldelar variabeln det givna värdet
  • \n
  • ADD [variabel] [värde]: adderar till den givna variabeln
  • \n
  • SUB [variabel] [värde]: subtraherar från den givna variabeln
  • \n
  • MUL [variabel] [värde]: multiplicerar den givna variabeln
  • \n
  • [ställe]:: definierar ett ställe dit man senare kan förflyttas till
  • \n
  • JUMP [ställe]: gå till specificerat ställe
  • \n
  • IF [villkor] JUMP [ställe]: om villkoret är sant, gå till specificerat ställe
  • \n
  • END: avslutar programmet
  • \n

Programmet körs rad för rad från och med den första raden. Programmet avslutas vid END eller då den sista raden passeras.

Programmet har 26 variabler ([variabel]) namngivna A till Z. Alla variabler är 0 då programmet startar.

Alla värden som behandlas är helta. [värde] hänvisar till en variabel eller ett givet heltal.

Ett [ställe] består av bokstäverna a till z och siffrorna 0-9. Olika ställen kan inte ha samma namn.

[villkor] ges i formatet [värde] [jämförelse] [värde]. [jämförelse] är något av de följande: ==, !=, <, <=, > eller >=.

Skapa funktionen kor(program) som tar emot programmet som en lista. Ett element i listan representerar en rad kod. Funktionen ska returnera resultaten från alla PRINT-instruktioner.

Du kan anta att det program som ges till funktionen har korrekt syntax. Funktionen behöver inte alltså kunna behandla sådana här fel.

Du kan få två poäng av den här uppgiften: ett poäng om instruktionerna PRINT, MOV, ADD, SUB, MUL och END fungerar, och ett poäng om resterande instruktioner fungerar.

Exempel 1:

program1 = []\nprogram1.append(\"MOV A 1\")\nprogram1.append(\"MOV B 2\")\nprogram1.append(\"PRINT A\")\nprogram1.append(\"PRINT B\")\nprogram1.append(\"ADD A B\")\nprogram1.append(\"PRINT A\")\nprogram1.append(\"END\")\nresultat = kor(program1)\nprint(resultat)

[1, 2, 3]

Exempel 2:

program2 = []\nprogram2.append(\"MOV A 1\")\nprogram2.append(\"MOV B 10\")\nprogram2.append(\"start:\")\nprogram2.append(\"IF A >= B JUMP slut\")\nprogram2.append(\"PRINT A\")\nprogram2.append(\"PRINT B\")\nprogram2.append(\"ADD A 1\")\nprogram2.append(\"SUB B 1\")\nprogram2.append(\"JUMP start\")\nprogram2.append(\"slut:\")\nprogram2.append(\"END\")\nresultat = kor(program2)\nprint(resultat)

[1, 10, 2, 9, 3, 8, 4, 7, 5, 6]

Exempel 3 (fakultet):

program3 = []\nprogram3.append(\"MOV A 1\")\nprogram3.append(\"MOV B 1\")\nprogram3.append(\"start:\")\nprogram3.append(\"PRINT A\")\nprogram3.append(\"ADD B 1\")\nprogram3.append(\"MUL A B\")\nprogram3.append(\"IF B <= 10 JUMP start\")\nprogram3.append(\"END\")\nresultat = kor(program3)\nprint(resultat)

[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]

Exempel 4 (primtal):

program4 = []\nprogram4.append(\"MOV N 50\")\nprogram4.append(\"PRINT 2\")\nprogram4.append(\"MOV A 3\")\nprogram4.append(\"start:\")\nprogram4.append(\"MOV B 2\")\nprogram4.append(\"MOV Z 0\")\nprogram4.append(\"test:\")\nprogram4.append(\"MOV C B\")\nprogram4.append(\"nytt:\")\nprogram4.append(\"IF C == A JUMP fel\")\nprogram4.append(\"IF C > A JUMP skippa\")\nprogram4.append(\"ADD C B\")\nprogram4.append(\"JUMP nytt\")\nprogram4.append(\"fel:\")\nprogram4.append(\"MOV Z 1\")\nprogram4.append(\"JUMP skippa2\")\nprogram4.append(\"skippa:\")\nprogram4.append(\"ADD B 1\")\nprogram4.append(\"IF B < A JUMP test\")\nprogram4.append(\"skippa2:\")\nprogram4.append(\"IF Z == 1 JUMP skippa3\")\nprogram4.append(\"PRINT A\")\nprogram4.append(\"skippa3:\")\nprogram4.append(\"ADD A 1\")\nprogram4.append(\"IF A <= N JUMP start\")\nresultat = kor(program4)\nprint(resultat)

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Vänligen svara på kursfeedbacksenkäten här nedan. Enkätens resultat hjälper oss att utveckla och förbättra den här kursen.

","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/6-funktionaliteter-python.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Lärandemål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"har du bekantat dig med några fler funktionaliteter i Python."}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För att knyta ihop den här kursen, ska du ännu få en lista av nyttiga funktionaliteter som Python har att erbjuda."}]},{"type":"element","tagName":"h2","properties":{"id":"if-satser-pa-en-rad","style":"position:relative;"},"children":[{"type":"text","value":"If-satser på en rad"},{"type":"element","tagName":"a","properties":{"href":"#if-satser-pa-en-rad","ariaLabel":"if satser pa en rad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande satser ger samma resultat:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jämn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ojämn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"jämn\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ojämn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det senare exemplet har vi en if-sats i enradsformat: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a if [villkor] else b"}]},{"type":"text","value":". Värdet för uttrycket blir "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" då villkoret är sant och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" i övriga fall. På engelska kallas detta ternary operator."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If-satser kan användas då man vill tilldela något värde baserat på ett villkor. Till exempel om du har variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" och vill antingen öka eller ändra på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":":s värde baserat på "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" paritet (jämnt/udda tal), kan du använda en normal if-else-sats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eller så kan du göra det hela med en rad kod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"ett-tomt-block","style":"position:relative;"},"children":[{"type":"text","value":"Ett \"tomt\" block"},{"type":"element","tagName":"a","properties":{"href":"#ett-tomt-block","ariaLabel":"ett tomt block permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du minns kanske att block inte kan vara tomma i Python. Om du behöver ett block som inte gör någonting – till exempel då du testar någon annan funktionalitet – kan du använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"-instruktionen. Du kan till exempel skapa en funktion som inte gör någonting:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen kommer att returnera direkt. Om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"-instruktionen lämnas bort, kommer koden att ge ett fel, eftersom block inte kan vara tomma."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"loopar-med-else-block","style":"position:relative;"},"children":[{"type":"text","value":"Loopar med else-block"},{"type":"element","tagName":"a","properties":{"href":"#loopar-med-else-block","ariaLabel":"loopar med else block permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python kan loopar också ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":"-block. Koden inom det här blocket körs då loopen avslutas normalt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till exempel här går vi igenom en lista med siffror. Om det finns ett jämnt tal i listan, kommer ett meddelande att skrivas ut och loopen avslutas. Om det inte finns några jämna tal, kommer loopen att avslutas vanligt, men ett annat meddelande skrivs ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittade jämnt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittade inte jämnt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett mera traditionellt sätt att göra det här är att använda en hjälpvariabel som minns om en viss typ av element har hittats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nhittades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittade jämnt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hittades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" hittades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hittade inte jämnt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att använda en for-else-sats sparar oss den tid som skulle krävas för att skapa en hjälpvariabel och logiken kring den."}]},{"type":"element","tagName":"h2","properties":{"id":"standardvarde-for-en-parameter","style":"position:relative;"},"children":[{"type":"text","value":"Standardvärde för en parameter"},{"type":"element","tagName":"a","properties":{"href":"#standardvarde-for-en-parameter","ariaLabel":"standardvarde for en parameter permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python kan funktioners parametrar ha ett standardvärde. Det används då ett argument inte ges till en funktion då den anropas. Se följande exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tervehdi"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nimi"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nimi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntervehdi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntervehdi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Elin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntervehdi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hej, Emilia\nHej, Elin\nHej, Malin"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obs! En tom sträng är fortfarande en sträng. Standardvärdet kommer inte att användas om en tom sträng ges som argument till en funktion."}]},{"type":"element","tagName":"h2","properties":{"id":"andrande-antal-parametrar","style":"position:relative;"},"children":[{"type":"text","value":"Ändrande antal parametrar"},{"type":"element","tagName":"a","properties":{"href":"#andrande-antal-parametrar","ariaLabel":"andrande antal parametrar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan också definiera att en funktion har ett ändrande antal parametrar, genom att lägga till en asterisk för parameternamnet. Alla argument som ges till funktionen lagras i en tuple som kan kommas åt genom den namngivna parametern."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande funktion räknar antalet argument som getts samt summan av dem:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"test"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Du gav\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"argument\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Deras summa är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du gav 5 argument\nDeras summa är 15"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Eget programmeringsspråk","tmcname":"osa07-18_programmeringssprak"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgifter skapar vi ett program som kan köra vårt eget programmeringsspråk. Du kan använda allt du lärt dig under den här kursen när du utför den här uppgiften."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmen består av rader, som kan följa något av dessa format:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT [värde]"}]},{"type":"text","value":": skriver ut det givna värdet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MOV [variabel] [värde]"}]},{"type":"text","value":": tilldelar variabeln det givna värdet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADD [variabel] [värde]"}]},{"type":"text","value":": adderar till den givna variabeln"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SUB [variabel] [värde]"}]},{"type":"text","value":": subtraherar från den givna variabeln"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MUL [variabel] [värde]"}]},{"type":"text","value":": multiplicerar den givna variabeln"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ställe]:"}]},{"type":"text","value":": definierar ett ställe dit man senare kan förflyttas till"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"JUMP [ställe]"}]},{"type":"text","value":": gå till specificerat ställe"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IF [villkor] JUMP [ställe]"}]},{"type":"text","value":": om villkoret är sant, gå till specificerat ställe"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":": avslutar programmet"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet körs rad för rad från och med den första raden. Programmet avslutas vid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":" eller då den sista raden passeras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet har 26 variabler ("},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[variabel]"}]},{"type":"text","value":") namngivna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Z"}]},{"type":"text","value":". Alla variabler är 0 då programmet startar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla värden som behandlas är helta. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[värde]"}]},{"type":"text","value":" hänvisar till en variabel eller ett givet heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ställe]"}]},{"type":"text","value":" består av bokstäverna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"z"}]},{"type":"text","value":" och siffrorna 0-9. Olika ställen kan inte ha samma namn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[villkor]"}]},{"type":"text","value":" ges i formatet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[värde] [jämförelse] [värde]"}]},{"type":"text","value":". "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[jämförelse]"}]},{"type":"text","value":" är något av de följande: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kor(program)"}]},{"type":"text","value":" som tar emot programmet som en lista. Ett element i listan representerar en rad kod. Funktionen ska returnera resultaten från alla "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT"}]},{"type":"text","value":"-instruktioner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att det program som ges till funktionen har korrekt syntax. Funktionen behöver inte alltså kunna behandla sådana här fel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan få två poäng av den här uppgiften: ett poäng om instruktionerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MOV"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADD"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SUB"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MUL"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":" fungerar, och ett poäng om resterande instruktioner fungerar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel 1:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"program1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel 2:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"program2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"start:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF A >= B JUMP slut\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SUB B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP start\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"slut:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 10, 2, 9, 3, 8, 4, 7, 5, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel 3 (fakultet):"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"program3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"start:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MUL A B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF B <= 10 JUMP start\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel 4 (primtal):"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"program4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV N 50\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"start:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV Z 0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV C B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nytt:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF C == A JUMP fel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF C > A JUMP skippa\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD C B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP nytt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"fel:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV Z 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP skippa2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"skippa:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF B < A JUMP test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"skippa2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF Z == 1 JUMP skippa3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"skippa3:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF A <= N JUMP start\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på kursfeedbacksenkäten här nedan. Enkätens resultat hjälper oss att utveckla och förbättra den här kursen."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • har du bekantat dig med några fler funktionaliteter i Python.
  • \n

För att knyta ihop den här kursen, ska du ännu få en lista av nyttiga funktionaliteter som Python har att erbjuda.

If-satser på en rad

Följande satser ger samma resultat:

if x%2 == 0:\n    print(\"jämn\")\nelse:\n    print(\"ojämn\")
print(\"jämn\" if x%2 == 0 else \"ojämn\")

I det senare exemplet har vi en if-sats i enradsformat: a if [villkor] else b. Värdet för uttrycket blir a då villkoret är sant och b i övriga fall. På engelska kallas detta ternary operator.

If-satser kan användas då man vill tilldela något värde baserat på ett villkor. Till exempel om du har variablerna x och y och vill antingen öka eller ändra på y:s värde baserat på x paritet (jämnt/udda tal), kan du använda en normal if-else-sats:

if x%2 == 0:\n    y += 1\nelse:\n    y = 0

Eller så kan du göra det hela med en rad kod:

y = y + 1 if x%2 == 0 else 0

Ett \"tomt\" block

Du minns kanske att block inte kan vara tomma i Python. Om du behöver ett block som inte gör någonting – till exempel då du testar någon annan funktionalitet – kan du använda pass-instruktionen. Du kan till exempel skapa en funktion som inte gör någonting:

def test():\n    pass

Funktionen kommer att returnera direkt. Om pass-instruktionen lämnas bort, kommer koden att ge ett fel, eftersom block inte kan vara tomma.

def test():

Loopar med else-block

I Python kan loopar också ett else-block. Koden inom det här blocket körs då loopen avslutas normalt.

Till exempel här går vi igenom en lista med siffror. Om det finns ett jämnt tal i listan, kommer ett meddelande att skrivas ut och loopen avslutas. Om det inte finns några jämna tal, kommer loopen att avslutas vanligt, men ett annat meddelande skrivs ut:

lista = [3,5,2,8,1]\nfor x in lista:\n    if x%2 == 0:\n        print(\"hittade jämnt tal\", x)\n        break\nelse:\n    print(\"hittade inte jämnt tal\")

Ett mera traditionellt sätt att göra det här är att använda en hjälpvariabel som minns om en viss typ av element har hittats:

lista = [3,5,2,8,1]\nhittades = False\nfor x in lista:\n    if x%2 == 0:\n        print(\"hittade jämnt tal\", x)\n        hittades = True\n        break\nif not hittades:\n    print(\"hittade inte jämnt tal\")

Att använda en for-else-sats sparar oss den tid som skulle krävas för att skapa en hjälpvariabel och logiken kring den.

Standardvärde för en parameter

I Python kan funktioners parametrar ha ett standardvärde. Det används då ett argument inte ges till en funktion då den anropas. Se följande exempel:

def tervehdi(nimi=\"Emilia\"):\n    print(\"Hej,\", nimi)\n\ntervehdi()\ntervehdi(\"Elin\")\ntervehdi(\"Malin\")

Hej, Emilia\nHej, Elin\nHej, Malin

Obs! En tom sträng är fortfarande en sträng. Standardvärdet kommer inte att användas om en tom sträng ges som argument till en funktion.

Ändrande antal parametrar

Du kan också definiera att en funktion har ett ändrande antal parametrar, genom att lägga till en asterisk för parameternamnet. Alla argument som ges till funktionen lagras i en tuple som kan kommas åt genom den namngivna parametern.

Följande funktion räknar antalet argument som getts samt summan av dem:

def test(*lista):\n    print(\"Du gav\", len(lista), \"argument\")\n    print(\"Deras summa är\", sum(lista))\n\ntest(1, 2, 3, 4, 5)

Du gav 5 argument\nDeras summa är 15

I den här uppgifter skapar vi ett program som kan köra vårt eget programmeringsspråk. Du kan använda allt du lärt dig under den här kursen när du utför den här uppgiften.

Programmen består av rader, som kan följa något av dessa format:

    \n
  • PRINT [värde]: skriver ut det givna värdet
  • \n
  • MOV [variabel] [värde]: tilldelar variabeln det givna värdet
  • \n
  • ADD [variabel] [värde]: adderar till den givna variabeln
  • \n
  • SUB [variabel] [värde]: subtraherar från den givna variabeln
  • \n
  • MUL [variabel] [värde]: multiplicerar den givna variabeln
  • \n
  • [ställe]:: definierar ett ställe dit man senare kan förflyttas till
  • \n
  • JUMP [ställe]: gå till specificerat ställe
  • \n
  • IF [villkor] JUMP [ställe]: om villkoret är sant, gå till specificerat ställe
  • \n
  • END: avslutar programmet
  • \n

Programmet körs rad för rad från och med den första raden. Programmet avslutas vid END eller då den sista raden passeras.

Programmet har 26 variabler ([variabel]) namngivna A till Z. Alla variabler är 0 då programmet startar.

Alla värden som behandlas är helta. [värde] hänvisar till en variabel eller ett givet heltal.

Ett [ställe] består av bokstäverna a till z och siffrorna 0-9. Olika ställen kan inte ha samma namn.

[villkor] ges i formatet [värde] [jämförelse] [värde]. [jämförelse] är något av de följande: ==, !=, <, <=, > eller >=.

Skapa funktionen kor(program) som tar emot programmet som en lista. Ett element i listan representerar en rad kod. Funktionen ska returnera resultaten från alla PRINT-instruktioner.

Du kan anta att det program som ges till funktionen har korrekt syntax. Funktionen behöver inte alltså kunna behandla sådana här fel.

Du kan få två poäng av den här uppgiften: ett poäng om instruktionerna PRINT, MOV, ADD, SUB, MUL och END fungerar, och ett poäng om resterande instruktioner fungerar.

Exempel 1:

program1 = []\nprogram1.append(\"MOV A 1\")\nprogram1.append(\"MOV B 2\")\nprogram1.append(\"PRINT A\")\nprogram1.append(\"PRINT B\")\nprogram1.append(\"ADD A B\")\nprogram1.append(\"PRINT A\")\nprogram1.append(\"END\")\nresultat = kor(program1)\nprint(resultat)

[1, 2, 3]

Exempel 2:

program2 = []\nprogram2.append(\"MOV A 1\")\nprogram2.append(\"MOV B 10\")\nprogram2.append(\"start:\")\nprogram2.append(\"IF A >= B JUMP slut\")\nprogram2.append(\"PRINT A\")\nprogram2.append(\"PRINT B\")\nprogram2.append(\"ADD A 1\")\nprogram2.append(\"SUB B 1\")\nprogram2.append(\"JUMP start\")\nprogram2.append(\"slut:\")\nprogram2.append(\"END\")\nresultat = kor(program2)\nprint(resultat)

[1, 10, 2, 9, 3, 8, 4, 7, 5, 6]

Exempel 3 (fakultet):

program3 = []\nprogram3.append(\"MOV A 1\")\nprogram3.append(\"MOV B 1\")\nprogram3.append(\"start:\")\nprogram3.append(\"PRINT A\")\nprogram3.append(\"ADD B 1\")\nprogram3.append(\"MUL A B\")\nprogram3.append(\"IF B <= 10 JUMP start\")\nprogram3.append(\"END\")\nresultat = kor(program3)\nprint(resultat)

[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]

Exempel 4 (primtal):

program4 = []\nprogram4.append(\"MOV N 50\")\nprogram4.append(\"PRINT 2\")\nprogram4.append(\"MOV A 3\")\nprogram4.append(\"start:\")\nprogram4.append(\"MOV B 2\")\nprogram4.append(\"MOV Z 0\")\nprogram4.append(\"test:\")\nprogram4.append(\"MOV C B\")\nprogram4.append(\"nytt:\")\nprogram4.append(\"IF C == A JUMP fel\")\nprogram4.append(\"IF C > A JUMP skippa\")\nprogram4.append(\"ADD C B\")\nprogram4.append(\"JUMP nytt\")\nprogram4.append(\"fel:\")\nprogram4.append(\"MOV Z 1\")\nprogram4.append(\"JUMP skippa2\")\nprogram4.append(\"skippa:\")\nprogram4.append(\"ADD B 1\")\nprogram4.append(\"IF B < A JUMP test\")\nprogram4.append(\"skippa2:\")\nprogram4.append(\"IF Z == 1 JUMP skippa3\")\nprogram4.append(\"PRINT A\")\nprogram4.append(\"skippa3:\")\nprogram4.append(\"ADD A 1\")\nprogram4.append(\"IF A <= N JUMP start\")\nresultat = kor(program4)\nprint(resultat)

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Vänligen svara på kursfeedbacksenkäten här nedan. Enkätens resultat hjälper oss att utveckla och förbättra den här kursen.

","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/6-funktionaliteter-python.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-7/page-data.json b/page-data/osa-7/page-data.json index 71b78562f..7e6abbd8e 100644 --- a/page-data/osa-7/page-data.json +++ b/page-data/osa-7/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-7", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-7","title":"Modul 7"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-7","title":"Modul 7"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-7/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-8/1-objekt-och-metoder/page-data.json b/page-data/osa-8/1-objekt-och-metoder/page-data.json index a801aeff5..ded6481e2 100644 --- a/page-data/osa-8/1-objekt-och-metoder/page-data.json +++ b/page-data/osa-8/1-objekt-och-metoder/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-8/1-objekt-och-metoder", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du veta vad ett objekt är i programmering"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du förstå vad som menas med oberoende hos individuella objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du kunna skapa och komma åt objekt"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta är första delen av den Avancerade Kursen inom Programmering. Materialet är designat för att bli använt med Visual Studio Code editeraren, liksom den föregående kursen Introduktion till Programming. Ifall du inte använt Visual Studio Code tidigare, så hittar du installeringsinstruktionerna "},{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/fi/installation/vscode","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"här"}]},{"type":"text","value":" och en introduktion till programmeringsomgivningen från förra kursen "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-4/1-vscode","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"här"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I introduktion till Programmering kursen så lade vi märke till att det ofta är logiskt att gruppera relaterad data tillsammans i våra program. Ifall vi till exemepl skulle förvara information om en bok skulle det vara logiskt att använda oss av en tupel eller en ordlista för att organisera datan till en enskild datastruktur."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lösningen kunde se ut så här när man använder en tupel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kodningsboken\""}]},{"type":"text","value":"\nförfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"text","value":"\nar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1992"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi sammanställer dessa i en ordlista"}]},{"type":"text","value":"\nbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" författare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut bokens namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I ett fall som detta är fördelen med att använda en ordlista att vi kan använda strängar istället för indexar som nycklar. Alltså, kan vi ge deskriptiva namn till sakerna som förvaras datastrukturen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kodningsboken\""}]},{"type":"text","value":"\nforfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"text","value":"\nar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1992"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Slår ihop till en ordlista"}]},{"type":"text","value":"\nbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"författare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut bokens namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I båda fallen så skapar vi ett nytt "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"objekt"}]},{"type":"text","value":". Inom programmering har termen specifikt betydelsen av en oberoende helhet, i detta fall innehållande några bitar av data som på något vis är relaterade. Att vara oberoende betyder att ändringar till ett objekt inte påverkar andra objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall vi skulle skapa två strukturellt identiska representationer av böcker, användandes ordlistor och identiska nycklar, skulle ändringar som görs till en av dem inte påverka den andra:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den gamle och Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"författare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ernest Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nbok2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sju Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"författare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Aleksis Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1894"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbok1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ett nytt namn\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den gamle och Python\nSju Python\nEtt nytt namn\nSju Python"}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 341px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a9810fa9a13874067832dc3cc67de74e/4c0ad/8_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 45.65217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABgUlEQVQoz4WS646iQBCFef9n8QV2sln9MbPKCiITFVERL+gKcgcR8fJNaFezPyaxkpPTle7UOVXVEi8iSRIWiwWr1ZooCplOZ2w3GyxrLthx1ljWjJ3r0Wq1kFbLFX4QcCpLsiwjz/Mn0jRlYo5Ru13e39tYsym2bfP74wOlq6L3NZRulz+dDoZh0m53kAI/4HAoqKqKoig4Ho9PFMUR254zNgwmpkmv/8ntdnsK1gaiKCKOYrhBr6chvWo5y1L8vU+aJsRJIorUwg+UZSm4FlIUBel6vYrkO9RxOBzuLuKYMAzF+YE69zxPIAgClsvla4e1s3uBWLTpuS77vYfv70ni5P7on7im9ZA2jkMYRsJ6vYTHbGquNzydmOiaxo+3n8ymM8zxGK2n0vzVRNc/sefW/QeEIc1mC8l1XbIs53K5iKIPnE6lWMzf7RZn7QioqvrfbHPO5zN5lghD1akS9y9b3u12yLJMX9PoyDK6rjMYDBiNRgyHQ0aGIbj+To1Ggy+AFaTywUP64gAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a9810fa9a13874067832dc3cc67de74e/a0b58/8_1_1.webp 230w","/static/a9810fa9a13874067832dc3cc67de74e/5af35/8_1_1.webp 341w"],"sizes":"(max-width: 341px) 100vw, 341px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a9810fa9a13874067832dc3cc67de74e/81c8e/8_1_1.png 230w","/static/a9810fa9a13874067832dc3cc67de74e/4c0ad/8_1_1.png 341w"],"sizes":"(max-width: 341px) 100vw, 341px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a9810fa9a13874067832dc3cc67de74e/4c0ad/8_1_1.png","alt":"8 1 1","title":"8 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"text-box","properties":{"variant":"info","name":"Python objekt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kanske kommer ihåg från introduktion till programmering kursen att vilket som helst värde i Python internt är behandlat som ett objekt. Detta betyder att värdet lagrat i en variabel är en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"referens till ett objekt"}]},{"type":"text","value":". Själva datan är lagrad inuti ett objekt i datorns minne. Ifall du ger ett värde till en ny variable med kommandot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a = 3"}]},{"type":"text","value":", är värdet som lagras i variabeln "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inte"}]},{"type":"text","value":" 3, utan en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"referens till ett objekt som innehåller värdet 3"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De flesta andra programmeringsspråk (i varje fall de som stöder objekt-orienterad programmering) inkluderar vissa särskilt definierade "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"primitiva datatyper"}]},{"type":"text","value":". Dessa inkluderar ofta åtminstone [integer] nummer, flyttals nummer och boleska sanningsvärden. Primitiver är processerade direkt, vilket betyder att de är lagrade direkt i variabler, inte som referenser. Python har inga sådana primitiver, men att jobba med de grundläggade datatyperna i Python är i praktiken väldigt liknande. Objekt av dessa grundläggande datatyper (såsom nummer, boleska värden och strängar) är "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"oföränderliga"}]},{"type":"text","value":", vilket betyder att de inte kan ändras i minnet. Ifall värdet som lagras i en variabel med en grundläggande datatyp måste ändras så byts hela referensen ut, men själva objektet kvarstår i minnet."}]}]},{"type":"element","tagName":"h2","properties":{"id":"objekt-och-metoder","style":"position:relative;"},"children":[{"type":"text","value":"Objekt och metoder"},{"type":"element","tagName":"a","properties":{"href":"#objekt-och-metoder","ariaLabel":"objekt och metoder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datan som lagras i ett objekt kan kommas åt genom olika "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"metoder"}]},{"type":"text","value":". En metod är en funktion som opererar på ett specifikt objekt som den är kopplad till. Sättet att åtskilja mellan metoder och andra funktioner ligger i hur de är kallade: först skriver man namnet på objektet som avses, sedan en punkt och till sist metodens namn, åtföljt av argument ifall sådana finns. Till exempel returnerar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"values"}]},{"type":"text","value":" alla värden som är lagrade i ett objekt av typen ordlista eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dict"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# detta skapar ett objekt av typen ordlista med namnet bok"}]},{"type":"text","value":"\nbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den gamle och Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"författare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ernest Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut alla värden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metodkallelsen values() skrivs efter namnet på variabeln"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kom ihåg punktnotation!"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den gamel och Python\nErnest Pythonson\n1952"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt opererar en strängmetod på det strängobjekt som den kallas på. Några exempel av strängmetoder är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Påhittige Per\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut mängden P som förekommer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"P\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Mängden P som hittas i en annan sträng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Påhittade Praktiska Prepositioner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"P\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Indexen av delsträngen Per"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Per\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna sträng har ingen matchande delsträng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Helt annan sträng\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Per\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n10\n-1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängmetoder returnerar värden, men ändrar inte innehållet av en sträng. Liksom nämnt ovan är strängar i Python oföränderliga. Detta gäller däremot inte alla metoder; Pythonlistor är föränderliga, alltså kan listmetoder ändra innehållet av listan som de kallas på:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi lägger till några element"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi tar bort ett element från början"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3, 5, 1]\n[2, 3, 5, 1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Minsta medeltalet","tmcname":"osa08-01_minsta_medeltalet"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minsta_medeltalet(person1: dict, person2: dict, person3: dict)"}]},{"type":"text","value":", som får tre ordlistor som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje ordlistsobjekt innehåller värden som refererar till följande nycklar:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"text","value":": tävlarens namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"resultat1\""}]},{"type":"text","value":": tävlarens första resultat (heltal mellan 1...10)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"resultat2\""}]},{"type":"text","value":": tävlarens andra resultat (liksom ovan)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"resultat3\""}]},{"type":"text","value":": tävlarens tredje resultat (liksom ovan)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska beräkna genomsnittet av de tre resultaten för varje tävlande och sedan returnera den tävlande vars genomsnittliga resultat var det minsta. Returvärdet bör vara hela ordlistsobjektet som innehåller information om deltagaren."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att endast en tävlare har det minsta medeltalet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ella\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bella\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Stella\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minsta_medeltalet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'namn': 'Stella', 'resultat1': 3, 'resultat2': 1, 'resultat3': 1}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Radernas summor","tmcname":"osa08-02_radernas_summor "},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python är varje värde som lagras i en variabel en referens till ett objekt, så varje värde som lagras i en lista är också en referens till ett objekt. Detta gäller även vid modellering av en matrisdatastruktur: varje värde i listan på högsta nivån är en referens till en annan lista, som i sin tur innehåller referenser till de objekt som representerar elementen i matrisen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"radernas_summor(matris: list)"}]},{"type":"text","value":", som tar en heltalsmatris som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska lägga till ett nytt element på varje rad i matrisen. Detta element innehåller summan av de andra elementen på den raden. Funktionen har inget returvärde. Den bör modifiera matrisen i parametern."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på funktionen i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nradernas_summor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[1, 2, 3], [3, 4, 7]]"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du veta vad ett objekt är i programmering
  • \n
  • Kommer du förstå vad som menas med oberoende hos individuella objekt
  • \n
  • Kommer du kunna skapa och komma åt objekt
  • \n

Detta är första delen av den Avancerade Kursen inom Programmering. Materialet är designat för att bli använt med Visual Studio Code editeraren, liksom den föregående kursen Introduktion till Programming. Ifall du inte använt Visual Studio Code tidigare, så hittar du installeringsinstruktionerna här och en introduktion till programmeringsomgivningen från förra kursen här.

I introduktion till Programmering kursen så lade vi märke till att det ofta är logiskt att gruppera relaterad data tillsammans i våra program. Ifall vi till exemepl skulle förvara information om en bok skulle det vara logiskt att använda oss av en tupel eller en ordlista för att organisera datan till en enskild datastruktur.

Lösningen kunde se ut så här när man använder en tupel:

namn = \"Kodningsboken\"\nförfattare = \"Peter Python\"\nar = 1992\n\n# Vi sammanställer dessa i en ordlista\nbok = (namn, författare, ar)\n\n# Vi skriver ut bokens namn\nprint(bok[0])

I ett fall som detta är fördelen med att använda en ordlista att vi kan använda strängar istället för indexar som nycklar. Alltså, kan vi ge deskriptiva namn till sakerna som förvaras datastrukturen:

namn = \"Kodningsboken\"\nforfattare = \"Peter Python\"\nar = 1992\n\n# Slår ihop till en ordlista\nbok = {\"namn\": namn, \"författare\": forfattare, \"år\": ar}\n\n# Vi skriver ut bokens namn\nprint(bok[\"namn\"])

I båda fallen så skapar vi ett nytt objekt. Inom programmering har termen specifikt betydelsen av en oberoende helhet, i detta fall innehållande några bitar av data som på något vis är relaterade. Att vara oberoende betyder att ändringar till ett objekt inte påverkar andra objekt.

Ifall vi skulle skapa två strukturellt identiska representationer av böcker, användandes ordlistor och identiska nycklar, skulle ändringar som görs till en av dem inte påverka den andra:

bok1 = {\"namn\": \"Den gamle och Python\", \"författare\": \"Ernest Pythonson\", \"år\": 1952}\nbok2 = {\"namn\": \"Sju Python\", \"författare\": \"Aleksis Python\", \"år\": 1894}\n\nprint(bok1[\"namn\"])\nprint(bok2[\"namn\"])\n\nbok1[\"namn\"] = \"Ett nytt namn\"\n\nprint(bok1[\"namn\"])\nprint(bok2[\"namn\"])

Den gamle och Python\nSju Python\nEtt nytt namn\nSju Python

\n \n \n \n \n \n \"8\n \n \n

Du kanske kommer ihåg från introduktion till programmering kursen att vilket som helst värde i Python internt är behandlat som ett objekt. Detta betyder att värdet lagrat i en variabel är en referens till ett objekt. Själva datan är lagrad inuti ett objekt i datorns minne. Ifall du ger ett värde till en ny variable med kommandot a = 3, är värdet som lagras i variabeln inte 3, utan en referens till ett objekt som innehåller värdet 3.

De flesta andra programmeringsspråk (i varje fall de som stöder objekt-orienterad programmering) inkluderar vissa särskilt definierade primitiva datatyper. Dessa inkluderar ofta åtminstone [integer] nummer, flyttals nummer och boleska sanningsvärden. Primitiver är processerade direkt, vilket betyder att de är lagrade direkt i variabler, inte som referenser. Python har inga sådana primitiver, men att jobba med de grundläggade datatyperna i Python är i praktiken väldigt liknande. Objekt av dessa grundläggande datatyper (såsom nummer, boleska värden och strängar) är oföränderliga, vilket betyder att de inte kan ändras i minnet. Ifall värdet som lagras i en variabel med en grundläggande datatyp måste ändras så byts hela referensen ut, men själva objektet kvarstår i minnet.

Objekt och metoder

Datan som lagras i ett objekt kan kommas åt genom olika metoder. En metod är en funktion som opererar på ett specifikt objekt som den är kopplad till. Sättet att åtskilja mellan metoder och andra funktioner ligger i hur de är kallade: först skriver man namnet på objektet som avses, sedan en punkt och till sist metodens namn, åtföljt av argument ifall sådana finns. Till exempel returnerar metoden values alla värden som är lagrade i ett objekt av typen ordlista eller dict:

# detta skapar ett objekt av typen ordlista med namnet bok\nbok = {\"namn\": \"Den gamle och Python\", \"författare\": \"Ernest Pythonson\", \"år\": 1952}\n\n# Vi skriver ut alla värden\n# Metodkallelsen values() skrivs efter namnet på variabeln\n# Kom ihåg punktnotation!\nfor varde in bok.values():\n    print(varde)

Den gamel och Python\nErnest Pythonson\n1952

På samma sätt opererar en strängmetod på det strängobjekt som den kallas på. Några exempel av strängmetoder är count och find:

namn = \"Påhittige Per\"\n\n# Skriv ut mängden P som förekommer\nprint(namn.count(\"P\"))\n\n# Mängden P som hittas i en annan sträng\nprint(\"Påhittade Praktiska Prepositioner\".count(\"P\"))\n\n# Indexen av delsträngen Per\nprint(namn.find(\"Per\"))\n\n# Denna sträng har ingen matchande delsträng\nprint(\"Helt annan sträng\".find(\"Per\"))

2\n3\n10\n-1

Strängmetoder returnerar värden, men ändrar inte innehållet av en sträng. Liksom nämnt ovan är strängar i Python oföränderliga. Detta gäller däremot inte alla metoder; Pythonlistor är föränderliga, alltså kan listmetoder ändra innehållet av listan som de kallas på:

lista = [1,2,3]\n\n# Vi lägger till några element\nlista.append(5)\nlista.append(1)\n\nprint(lista)\n\n# Vi tar bort ett element från början\nlista.pop(0)\n\nprint(lista)

[1, 2, 3, 5, 1]\n[2, 3, 5, 1]

Skapa funktionen minsta_medeltalet(person1: dict, person2: dict, person3: dict), som får tre ordlistor som argument.

Varje ordlistsobjekt innehåller värden som refererar till följande nycklar:

    \n
  • \"namn\": tävlarens namn
  • \n
  • \"resultat1\": tävlarens första resultat (heltal mellan 1...10)
  • \n
  • \"resultat2\": tävlarens andra resultat (liksom ovan)
  • \n
  • \"resultat3\": tävlarens tredje resultat (liksom ovan)
  • \n

Funktionen ska beräkna genomsnittet av de tre resultaten för varje tävlande och sedan returnera den tävlande vars genomsnittliga resultat var det minsta. Returvärdet bör vara hela ordlistsobjektet som innehåller information om deltagaren.

Du kan anta att endast en tävlare har det minsta medeltalet.

Ett exempel på funktionen:

person1 = {\"namn\": \"Ella\", \"resultat1\": 2, \"resultat2\": 3, \"resultat3\": 3}\nperson2 = {\"namn\": \"Bella\", \"resultat1\": 5, \"resultat2\": 1, \"resultat3\": 8}\nperson3 = {\"namn\": \"Stella\", \"resultat1\": 3, \"resultat2\": 1, \"resultat3\": 1}\n\nprint(minsta_medeltalet(person1, person2, person3))

{'namn': 'Stella', 'resultat1': 3, 'resultat2': 1, 'resultat3': 1}

I Python är varje värde som lagras i en variabel en referens till ett objekt, så varje värde som lagras i en lista är också en referens till ett objekt. Detta gäller även vid modellering av en matrisdatastruktur: varje värde i listan på högsta nivån är en referens till en annan lista, som i sin tur innehåller referenser till de objekt som representerar elementen i matrisen.

Skapa funktionen radernas_summor(matris: list), som tar en heltalsmatris som argument.

Funktionen ska lägga till ett nytt element på varje rad i matrisen. Detta element innehåller summan av de andra elementen på den raden. Funktionen har inget returvärde. Den bör modifiera matrisen i parametern.

Ett exempel på funktionen i användning:

matris = [[1, 2], [3, 4]]\nradernas_summor(matris)\nprint(matris)

[[1, 2, 3], [3, 4, 7]]

","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/1-objekt-och-metoder.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du veta vad ett objekt är i programmering"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du förstå vad som menas med oberoende hos individuella objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du kunna skapa och komma åt objekt"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta är första delen av den Avancerade Kursen inom Programmering. Materialet är designat för att bli använt med Visual Studio Code editeraren, liksom den föregående kursen Introduktion till Programming. Ifall du inte använt Visual Studio Code tidigare, så hittar du installeringsinstruktionerna "},{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/fi/installation/vscode","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"här"}]},{"type":"text","value":" och en introduktion till programmeringsomgivningen från förra kursen "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-4/1-vscode","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"här"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I introduktion till Programmering kursen så lade vi märke till att det ofta är logiskt att gruppera relaterad data tillsammans i våra program. Ifall vi till exemepl skulle förvara information om en bok skulle det vara logiskt att använda oss av en tupel eller en ordlista för att organisera datan till en enskild datastruktur."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lösningen kunde se ut så här när man använder en tupel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kodningsboken\""}]},{"type":"text","value":"\nförfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"text","value":"\nar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1992"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi sammanställer dessa i en ordlista"}]},{"type":"text","value":"\nbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" författare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut bokens namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I ett fall som detta är fördelen med att använda en ordlista att vi kan använda strängar istället för indexar som nycklar. Alltså, kan vi ge deskriptiva namn till sakerna som förvaras datastrukturen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kodningsboken\""}]},{"type":"text","value":"\nforfattare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"text","value":"\nar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1992"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Slår ihop till en ordlista"}]},{"type":"text","value":"\nbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"författare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut bokens namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I båda fallen så skapar vi ett nytt "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"objekt"}]},{"type":"text","value":". Inom programmering har termen specifikt betydelsen av en oberoende helhet, i detta fall innehållande några bitar av data som på något vis är relaterade. Att vara oberoende betyder att ändringar till ett objekt inte påverkar andra objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall vi skulle skapa två strukturellt identiska representationer av böcker, användandes ordlistor och identiska nycklar, skulle ändringar som görs till en av dem inte påverka den andra:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den gamle och Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"författare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ernest Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nbok2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sju Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"författare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Aleksis Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1894"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbok1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ett nytt namn\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den gamle och Python\nSju Python\nEtt nytt namn\nSju Python"}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 341px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a9810fa9a13874067832dc3cc67de74e/4c0ad/8_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 45.65217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABgUlEQVQoz4WS646iQBCFef9n8QV2sln9MbPKCiITFVERL+gKcgcR8fJNaFezPyaxkpPTle7UOVXVEi8iSRIWiwWr1ZooCplOZ2w3GyxrLthx1ljWjJ3r0Wq1kFbLFX4QcCpLsiwjz/Mn0jRlYo5Ru13e39tYsym2bfP74wOlq6L3NZRulz+dDoZh0m53kAI/4HAoqKqKoig4Ho9PFMUR254zNgwmpkmv/8ntdnsK1gaiKCKOYrhBr6chvWo5y1L8vU+aJsRJIorUwg+UZSm4FlIUBel6vYrkO9RxOBzuLuKYMAzF+YE69zxPIAgClsvla4e1s3uBWLTpuS77vYfv70ni5P7on7im9ZA2jkMYRsJ6vYTHbGquNzydmOiaxo+3n8ymM8zxGK2n0vzVRNc/sefW/QeEIc1mC8l1XbIs53K5iKIPnE6lWMzf7RZn7QioqvrfbHPO5zN5lghD1akS9y9b3u12yLJMX9PoyDK6rjMYDBiNRgyHQ0aGIbj+To1Ggy+AFaTywUP64gAAAABJRU5ErkJggg=='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a9810fa9a13874067832dc3cc67de74e/a0b58/8_1_1.webp 230w","/static/a9810fa9a13874067832dc3cc67de74e/5af35/8_1_1.webp 341w"],"sizes":"(max-width: 341px) 100vw, 341px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a9810fa9a13874067832dc3cc67de74e/81c8e/8_1_1.png 230w","/static/a9810fa9a13874067832dc3cc67de74e/4c0ad/8_1_1.png 341w"],"sizes":"(max-width: 341px) 100vw, 341px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a9810fa9a13874067832dc3cc67de74e/4c0ad/8_1_1.png","alt":"8 1 1","title":"8 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"text-box","properties":{"variant":"info","name":"Python objekt"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kanske kommer ihåg från introduktion till programmering kursen att vilket som helst värde i Python internt är behandlat som ett objekt. Detta betyder att värdet lagrat i en variabel är en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"referens till ett objekt"}]},{"type":"text","value":". Själva datan är lagrad inuti ett objekt i datorns minne. Ifall du ger ett värde till en ny variable med kommandot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a = 3"}]},{"type":"text","value":", är värdet som lagras i variabeln "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inte"}]},{"type":"text","value":" 3, utan en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"referens till ett objekt som innehåller värdet 3"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De flesta andra programmeringsspråk (i varje fall de som stöder objekt-orienterad programmering) inkluderar vissa särskilt definierade "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"primitiva datatyper"}]},{"type":"text","value":". Dessa inkluderar ofta åtminstone [integer] nummer, flyttals nummer och boleska sanningsvärden. Primitiver är processerade direkt, vilket betyder att de är lagrade direkt i variabler, inte som referenser. Python har inga sådana primitiver, men att jobba med de grundläggade datatyperna i Python är i praktiken väldigt liknande. Objekt av dessa grundläggande datatyper (såsom nummer, boleska värden och strängar) är "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"oföränderliga"}]},{"type":"text","value":", vilket betyder att de inte kan ändras i minnet. Ifall värdet som lagras i en variabel med en grundläggande datatyp måste ändras så byts hela referensen ut, men själva objektet kvarstår i minnet."}]}]},{"type":"element","tagName":"h2","properties":{"id":"objekt-och-metoder","style":"position:relative;"},"children":[{"type":"text","value":"Objekt och metoder"},{"type":"element","tagName":"a","properties":{"href":"#objekt-och-metoder","ariaLabel":"objekt och metoder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datan som lagras i ett objekt kan kommas åt genom olika "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"metoder"}]},{"type":"text","value":". En metod är en funktion som opererar på ett specifikt objekt som den är kopplad till. Sättet att åtskilja mellan metoder och andra funktioner ligger i hur de är kallade: först skriver man namnet på objektet som avses, sedan en punkt och till sist metodens namn, åtföljt av argument ifall sådana finns. Till exempel returnerar metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"values"}]},{"type":"text","value":" alla värden som är lagrade i ett objekt av typen ordlista eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dict"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# detta skapar ett objekt av typen ordlista med namnet bok"}]},{"type":"text","value":"\nbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Den gamle och Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"författare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ernest Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"år\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut alla värden"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metodkallelsen values() skrivs efter namnet på variabeln"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kom ihåg punktnotation!"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" varde "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"values"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den gamel och Python\nErnest Pythonson\n1952"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt opererar en strängmetod på det strängobjekt som den kallas på. Några exempel av strängmetoder är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Påhittige Per\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut mängden P som förekommer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"P\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Mängden P som hittas i en annan sträng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Påhittade Praktiska Prepositioner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"P\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Indexen av delsträngen Per"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Per\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna sträng har ingen matchande delsträng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Helt annan sträng\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Per\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n10\n-1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängmetoder returnerar värden, men ändrar inte innehållet av en sträng. Liksom nämnt ovan är strängar i Python oföränderliga. Detta gäller däremot inte alla metoder; Pythonlistor är föränderliga, alltså kan listmetoder ändra innehållet av listan som de kallas på:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi lägger till några element"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi tar bort ett element från början"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3, 5, 1]\n[2, 3, 5, 1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Minsta medeltalet","tmcname":"osa08-01_minsta_medeltalet"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minsta_medeltalet(person1: dict, person2: dict, person3: dict)"}]},{"type":"text","value":", som får tre ordlistor som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje ordlistsobjekt innehåller värden som refererar till följande nycklar:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"text","value":": tävlarens namn"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"resultat1\""}]},{"type":"text","value":": tävlarens första resultat (heltal mellan 1...10)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"resultat2\""}]},{"type":"text","value":": tävlarens andra resultat (liksom ovan)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"resultat3\""}]},{"type":"text","value":": tävlarens tredje resultat (liksom ovan)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska beräkna genomsnittet av de tre resultaten för varje tävlande och sedan returnera den tävlande vars genomsnittliga resultat var det minsta. Returvärdet bör vara hela ordlistsobjektet som innehåller information om deltagaren."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att endast en tävlare har det minsta medeltalet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"person1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ella\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bella\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"namn\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Stella\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"resultat3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"minsta_medeltalet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'namn': 'Stella', 'resultat1': 3, 'resultat2': 1, 'resultat3': 1}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Radernas summor","tmcname":"osa08-02_radernas_summor "},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python är varje värde som lagras i en variabel en referens till ett objekt, så varje värde som lagras i en lista är också en referens till ett objekt. Detta gäller även vid modellering av en matrisdatastruktur: varje värde i listan på högsta nivån är en referens till en annan lista, som i sin tur innehåller referenser till de objekt som representerar elementen i matrisen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"radernas_summor(matris: list)"}]},{"type":"text","value":", som tar en heltalsmatris som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska lägga till ett nytt element på varje rad i matrisen. Detta element innehåller summan av de andra elementen på den raden. Funktionen har inget returvärde. Den bör modifiera matrisen i parametern."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på funktionen i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"matris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nradernas_summor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"matris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[1, 2, 3], [3, 4, 7]]"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du veta vad ett objekt är i programmering
  • \n
  • Kommer du förstå vad som menas med oberoende hos individuella objekt
  • \n
  • Kommer du kunna skapa och komma åt objekt
  • \n

Detta är första delen av den Avancerade Kursen inom Programmering. Materialet är designat för att bli använt med Visual Studio Code editeraren, liksom den föregående kursen Introduktion till Programming. Ifall du inte använt Visual Studio Code tidigare, så hittar du installeringsinstruktionerna här och en introduktion till programmeringsomgivningen från förra kursen här.

I introduktion till Programmering kursen så lade vi märke till att det ofta är logiskt att gruppera relaterad data tillsammans i våra program. Ifall vi till exemepl skulle förvara information om en bok skulle det vara logiskt att använda oss av en tupel eller en ordlista för att organisera datan till en enskild datastruktur.

Lösningen kunde se ut så här när man använder en tupel:

namn = \"Kodningsboken\"\nförfattare = \"Peter Python\"\nar = 1992\n\n# Vi sammanställer dessa i en ordlista\nbok = (namn, författare, ar)\n\n# Vi skriver ut bokens namn\nprint(bok[0])

I ett fall som detta är fördelen med att använda en ordlista att vi kan använda strängar istället för indexar som nycklar. Alltså, kan vi ge deskriptiva namn till sakerna som förvaras datastrukturen:

namn = \"Kodningsboken\"\nforfattare = \"Peter Python\"\nar = 1992\n\n# Slår ihop till en ordlista\nbok = {\"namn\": namn, \"författare\": forfattare, \"år\": ar}\n\n# Vi skriver ut bokens namn\nprint(bok[\"namn\"])

I båda fallen så skapar vi ett nytt objekt. Inom programmering har termen specifikt betydelsen av en oberoende helhet, i detta fall innehållande några bitar av data som på något vis är relaterade. Att vara oberoende betyder att ändringar till ett objekt inte påverkar andra objekt.

Ifall vi skulle skapa två strukturellt identiska representationer av böcker, användandes ordlistor och identiska nycklar, skulle ändringar som görs till en av dem inte påverka den andra:

bok1 = {\"namn\": \"Den gamle och Python\", \"författare\": \"Ernest Pythonson\", \"år\": 1952}\nbok2 = {\"namn\": \"Sju Python\", \"författare\": \"Aleksis Python\", \"år\": 1894}\n\nprint(bok1[\"namn\"])\nprint(bok2[\"namn\"])\n\nbok1[\"namn\"] = \"Ett nytt namn\"\n\nprint(bok1[\"namn\"])\nprint(bok2[\"namn\"])

Den gamle och Python\nSju Python\nEtt nytt namn\nSju Python

\n \n \n \n \n \n \"8\n \n \n

Du kanske kommer ihåg från introduktion till programmering kursen att vilket som helst värde i Python internt är behandlat som ett objekt. Detta betyder att värdet lagrat i en variabel är en referens till ett objekt. Själva datan är lagrad inuti ett objekt i datorns minne. Ifall du ger ett värde till en ny variable med kommandot a = 3, är värdet som lagras i variabeln inte 3, utan en referens till ett objekt som innehåller värdet 3.

De flesta andra programmeringsspråk (i varje fall de som stöder objekt-orienterad programmering) inkluderar vissa särskilt definierade primitiva datatyper. Dessa inkluderar ofta åtminstone [integer] nummer, flyttals nummer och boleska sanningsvärden. Primitiver är processerade direkt, vilket betyder att de är lagrade direkt i variabler, inte som referenser. Python har inga sådana primitiver, men att jobba med de grundläggade datatyperna i Python är i praktiken väldigt liknande. Objekt av dessa grundläggande datatyper (såsom nummer, boleska värden och strängar) är oföränderliga, vilket betyder att de inte kan ändras i minnet. Ifall värdet som lagras i en variabel med en grundläggande datatyp måste ändras så byts hela referensen ut, men själva objektet kvarstår i minnet.

Objekt och metoder

Datan som lagras i ett objekt kan kommas åt genom olika metoder. En metod är en funktion som opererar på ett specifikt objekt som den är kopplad till. Sättet att åtskilja mellan metoder och andra funktioner ligger i hur de är kallade: först skriver man namnet på objektet som avses, sedan en punkt och till sist metodens namn, åtföljt av argument ifall sådana finns. Till exempel returnerar metoden values alla värden som är lagrade i ett objekt av typen ordlista eller dict:

# detta skapar ett objekt av typen ordlista med namnet bok\nbok = {\"namn\": \"Den gamle och Python\", \"författare\": \"Ernest Pythonson\", \"år\": 1952}\n\n# Vi skriver ut alla värden\n# Metodkallelsen values() skrivs efter namnet på variabeln\n# Kom ihåg punktnotation!\nfor varde in bok.values():\n    print(varde)

Den gamel och Python\nErnest Pythonson\n1952

På samma sätt opererar en strängmetod på det strängobjekt som den kallas på. Några exempel av strängmetoder är count och find:

namn = \"Påhittige Per\"\n\n# Skriv ut mängden P som förekommer\nprint(namn.count(\"P\"))\n\n# Mängden P som hittas i en annan sträng\nprint(\"Påhittade Praktiska Prepositioner\".count(\"P\"))\n\n# Indexen av delsträngen Per\nprint(namn.find(\"Per\"))\n\n# Denna sträng har ingen matchande delsträng\nprint(\"Helt annan sträng\".find(\"Per\"))

2\n3\n10\n-1

Strängmetoder returnerar värden, men ändrar inte innehållet av en sträng. Liksom nämnt ovan är strängar i Python oföränderliga. Detta gäller däremot inte alla metoder; Pythonlistor är föränderliga, alltså kan listmetoder ändra innehållet av listan som de kallas på:

lista = [1,2,3]\n\n# Vi lägger till några element\nlista.append(5)\nlista.append(1)\n\nprint(lista)\n\n# Vi tar bort ett element från början\nlista.pop(0)\n\nprint(lista)

[1, 2, 3, 5, 1]\n[2, 3, 5, 1]

Skapa funktionen minsta_medeltalet(person1: dict, person2: dict, person3: dict), som får tre ordlistor som argument.

Varje ordlistsobjekt innehåller värden som refererar till följande nycklar:

    \n
  • \"namn\": tävlarens namn
  • \n
  • \"resultat1\": tävlarens första resultat (heltal mellan 1...10)
  • \n
  • \"resultat2\": tävlarens andra resultat (liksom ovan)
  • \n
  • \"resultat3\": tävlarens tredje resultat (liksom ovan)
  • \n

Funktionen ska beräkna genomsnittet av de tre resultaten för varje tävlande och sedan returnera den tävlande vars genomsnittliga resultat var det minsta. Returvärdet bör vara hela ordlistsobjektet som innehåller information om deltagaren.

Du kan anta att endast en tävlare har det minsta medeltalet.

Ett exempel på funktionen:

person1 = {\"namn\": \"Ella\", \"resultat1\": 2, \"resultat2\": 3, \"resultat3\": 3}\nperson2 = {\"namn\": \"Bella\", \"resultat1\": 5, \"resultat2\": 1, \"resultat3\": 8}\nperson3 = {\"namn\": \"Stella\", \"resultat1\": 3, \"resultat2\": 1, \"resultat3\": 1}\n\nprint(minsta_medeltalet(person1, person2, person3))

{'namn': 'Stella', 'resultat1': 3, 'resultat2': 1, 'resultat3': 1}

I Python är varje värde som lagras i en variabel en referens till ett objekt, så varje värde som lagras i en lista är också en referens till ett objekt. Detta gäller även vid modellering av en matrisdatastruktur: varje värde i listan på högsta nivån är en referens till en annan lista, som i sin tur innehåller referenser till de objekt som representerar elementen i matrisen.

Skapa funktionen radernas_summor(matris: list), som tar en heltalsmatris som argument.

Funktionen ska lägga till ett nytt element på varje rad i matrisen. Detta element innehåller summan av de andra elementen på den raden. Funktionen har inget returvärde. Den bör modifiera matrisen i parametern.

Ett exempel på funktionen i användning:

matris = [[1, 2], [3, 4]]\nradernas_summor(matris)\nprint(matris)

[[1, 2, 3], [3, 4, 7]]

","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/1-objekt-och-metoder.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-8/2-klasser-och-objekt/page-data.json b/page-data/osa-8/2-klasser-och-objekt/page-data.json index eca672808..9aa95e288 100644 --- a/page-data/osa-8/2-klasser-och-objekt/page-data.json +++ b/page-data/osa-8/2-klasser-och-objekt/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-8/2-klasser-och-objekt", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter detta avsnitt"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad en klass är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att förstå vad kopplingen mellan en klass och ett objekt är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att veta vad som menas med objektorienterad programmering"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I föregående avsnitt arbetade vi med listor, tuplar, ordlistor och strängar. Dessa är alla ganska speciella fall i Python-programmering. Pythons syntax har en unik, fördefinierad metod för att deklarera ett objekt som tillhör var och en av dessa typer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Listor deklareras med hakparenteser"}]},{"type":"text","value":"\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Strängar deklareras med citationstecken"}]},{"type":"text","value":"\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ordlistor deklareras med klammerparenteser"}]},{"type":"text","value":"\nordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ett\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"två:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# tuplar deklareras med parenteser"}]},{"type":"text","value":"\ntupel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När någon annan typ av objekt deklareras måste vi anropa en speciell initialiseringsfunktion som kallas konstruktor. Låt oss ta en titt på hur man arbetar med bråk genom Fraction-klassen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi använder klassen Fraction från modulen fractions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" fractions "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Fraction\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi skapar några nya bråktal"}]},{"type":"text","value":"\nhalv "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntredjedel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntredje "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Tulostetaan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"halv"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tredjedel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tredje"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Murtoluvuilla voi myös laskea"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"halv "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" tredjedel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1/2\n1/3\n3/11\n5/6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se ovan, ser metodkallningar för konstruktorer lite annorlunda ut än de normala metodkallningar som vi har stött på tidigare. För det första är de inte kopplade till något objekt med punktnotationen (eftersom konstruktoranropet behövs för att skapa ett objekt i första hand). Konstruktorsmetoden skrivs också med stor bokstav: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[half = Fraction(1,2)]"}]},{"type":"text","value":". Låt oss titta närmare på hur objekt konstrueras genom att bekanta oss med begreppet klass."}]},{"type":"element","tagName":"h2","properties":{"id":"en-klass-ar-ritningen-till-ett-objekt","style":"position:relative;"},"children":[{"type":"text","value":"En klass är ritningen till ett objekt"},{"type":"element","tagName":"a","properties":{"href":"#en-klass-ar-ritningen-till-ett-objekt","ariaLabel":"en klass ar ritningen till ett objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan använt termen klass i materialet många gånger. I exemplet ovan importerade vi t.ex. klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" från modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractions"}]},{"type":"text","value":". Nya bråktalsobjekt skapades genom att kalla på konstruktorsmetoden för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassdefinition innehåller strukturen och funktionaliteterna för alla objekt som representerar den. Därför kallas klasser ibland till objektens ritningar. En klassdefinition berättar alltså till oss vilken typ av data ett objekt innehåller och definierar de metoder som kan användas på objektet. Objektorienterad programmering är ett programmeringsparadigm där programmets funktionalitet är knuten till användningen av klasser och objekt som skapas baserat på dessa."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassdefinition kan användas för att skapa flera objekt. Som tidigare nämnts är objekt oberoende av varandra. Ändringar som görs till ett objekt påverkar i allmänhet inte de andra objekten som är samma klass. Varje objekt har sin egen unika uppsättning dataattribut. Det kan vara bra att tänka på denna förenkling av förhållandet mellan klass och objekt:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"En klass definierar variablerna"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"när ett objekt skapas tilldelas dessa variabler värden"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan alltså använda ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" för att komma åt täljaren och nämnaren i ett bråktal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" fractions "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Fraction\n\ntal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut täljaren"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numerator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ... och sedan nämnaren"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"denominator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" innehåller deklarationer för variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numerator"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"denominator"}]},{"type":"text","value":". Varje objekt som skapas baserat på klassen har sina egna specifika värden som tilldelas dessa variabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt innehåller objekt som skapats baserat på klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" sina egna unika värden för datumets år, månad och dag:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\njul "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut bägges månad"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"12\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definitionen av klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" innehåller deklarationer av variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"month"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"day"}]},{"type":"text","value":". När ett nytt datumobjekt skapas baserat på klassen tilldelas dessa variabler värden. Varje objekt har sina egna unika värden som tilldelas dessa variabler."}]},{"type":"element","tagName":"h2","properties":{"id":"funktioner-som-arbetar-med-objekt","style":"position:relative;"},"children":[{"type":"text","value":"Funktioner som arbetar med objekt"},{"type":"element","tagName":"a","properties":{"href":"#funktioner-som-arbetar-med-objekt","ariaLabel":"funktioner som arbetar med objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att passera ett objekt som ett argument till en funktion borde vara bekant för dig vid det här laget eftersom vi har gjort det redan många gånger i den här kursen. Låt oss ta en titt på följande exempel. Här har vi en funktion som kontrollerar om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"-objektet som passeras som argument infaller på en helg:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"redan_veckoslut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna funktion använder metoden "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.date.isoweekday","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"isoweekday"}]},{"type":"text","value":", som definieras i klassdefinitionen för klassen date, och returnerar ett heltalsvärde på det sättet att om det angivna datumet är en måndag returnerar den 1, och om det är en tisdag returnerar den 2, och så vidare."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"jul "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"redan_veckoslut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"redan_veckoslut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"metoder-vs-variabler","style":"position:relative;"},"children":[{"type":"text","value":"Metoder vs variabler"},{"type":"element","tagName":"a","properties":{"href":"#metoder-vs-variabler","ariaLabel":"metoder vs variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du arbetar med ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" kanske du märker att det finns en liten skillnad mellan hur variablerna i objektet åtkoms jämfört med hur metoderna som är kopplade till objekten åtkoms:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi kallar metoden"}]},{"type":"text","value":"\nveckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi använder en variabel"}]},{"type":"text","value":"\nmanad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Veckodag:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Månad:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kuukausi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Veckodag: 4\nMånad: 12"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Veckodagen som datumet infaller på är tillgänglig via metoden isoweekday:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta är en metodkallelse, alltså finns det parenteser efter namnet på metoden. Om du lämnar bort parenteserna uppstår det inte något fel, men resultatet blir konstigt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Veckodag:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Veckodag: "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Månaden av ett date-objekt är en variabel, alltså kan det tillgivna värdet kommas åt med en referens."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"manad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg märke till att det inte finns parenteser här. Att sätta in parenteser skulle orsaka ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"manad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nTypeError: 'int' object is not callable"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lista år","tmcname":"osa08-03_lista_ar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_ar(datum: list)"}]},{"type":"text","value":", som får en lista med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"-objekt som argument. Funktionen returnerar en ny lista, som innehåller "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"åren i den originella listan i kronologisk ordning"}]},{"type":"text","value":", från tidigast till äldst."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel av funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dag1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndag2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2006"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndag3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1993"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista_ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"dag1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1993, 2006, 2019]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Affarslista","tmcname":"osa08-04_affarslista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna övningsbotten finns en färdigt definierad "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":"-klass, som kan användas för att modellera en affärslista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Antagandes att vi har ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":"-objekt som refereras till i en variabel med namnet affarslista, kan objektet hanteras med följande metoder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangden_foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\nBananer\n4\nMjölk\n1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även följande går:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Föremålen på affärslistan är indexerade från 1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangden_foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" st\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bananer 4 st\nMjölk 1 st"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se fungerar en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" ungefär som en vanlig lista, men den nås via de metoder som tillhandahålls av Affarslista-klassen. Till skillnad från vanliga Python-listor börjar indexeringen från 1, inte 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"totala_mangd(lista: Affarslista)"}]},{"type":"text","value":", som tar ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" som sitt argument. Funktionen ska beräkna det totala antalet enheter i listan och returnera värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda följande kod för att testa din funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaaner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"appel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ananas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"totala_mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"16"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" Definitionen av klassen Affarslista ingår redan i övningsmallen. Du behöver inte använda en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-sats för att importera den, till skillnad från i exemplen ovan med Python standardbiblioteksklasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"."}]}]}]},"html":"

Efter detta avsnitt

    \n
  • Vet du vad en klass är
  • \n
  • Kommer du att förstå vad kopplingen mellan en klass och ett objekt är
  • \n
  • Kommer du att veta vad som menas med objektorienterad programmering
  • \n

I föregående avsnitt arbetade vi med listor, tuplar, ordlistor och strängar. Dessa är alla ganska speciella fall i Python-programmering. Pythons syntax har en unik, fördefinierad metod för att deklarera ett objekt som tillhör var och en av dessa typer:

# Listor deklareras med hakparenteser\nlista = [1,2,3]\n\n# Strängar deklareras med citationstecken\nstrang = \"Hej alla!\"\n\n# Ordlistor deklareras med klammerparenteser\nordlista = {\"ett\": 1, \"två:\": 2}\n\n# tuplar deklareras med parenteser\ntupel = (1,2,3)

När någon annan typ av objekt deklareras måste vi anropa en speciell initialiseringsfunktion som kallas konstruktor. Låt oss ta en titt på hur man arbetar med bråk genom Fraction-klassen.

# vi använder klassen Fraction från modulen fractions\nfrom fractions import Fraction\n\n# vi skapar några nya bråktal\nhalv = Fraction(1,2)\n\ntredjedel = Fraction(1,3)\n\ntredje = Fraction(3,11)\n\n# Tulostetaan\nprint(halv)\nprint(tredjedel)\nprint(tredje)\n\n# Murtoluvuilla voi myös laskea\nprint(halv + tredjedel)

1/2\n1/3\n3/11\n5/6

Som du kan se ovan, ser metodkallningar för konstruktorer lite annorlunda ut än de normala metodkallningar som vi har stött på tidigare. För det första är de inte kopplade till något objekt med punktnotationen (eftersom konstruktoranropet behövs för att skapa ett objekt i första hand). Konstruktorsmetoden skrivs också med stor bokstav: [half = Fraction(1,2)]. Låt oss titta närmare på hur objekt konstrueras genom att bekanta oss med begreppet klass.

En klass är ritningen till ett objekt

Vi har redan använt termen klass i materialet många gånger. I exemplet ovan importerade vi t.ex. klassen Fraction från modulen fractions. Nya bråktalsobjekt skapades genom att kalla på konstruktorsmetoden för klassen Fraction.

En klassdefinition innehåller strukturen och funktionaliteterna för alla objekt som representerar den. Därför kallas klasser ibland till objektens ritningar. En klassdefinition berättar alltså till oss vilken typ av data ett objekt innehåller och definierar de metoder som kan användas på objektet. Objektorienterad programmering är ett programmeringsparadigm där programmets funktionalitet är knuten till användningen av klasser och objekt som skapas baserat på dessa.

En klassdefinition kan användas för att skapa flera objekt. Som tidigare nämnts är objekt oberoende av varandra. Ändringar som görs till ett objekt påverkar i allmänhet inte de andra objekten som är samma klass. Varje objekt har sin egen unika uppsättning dataattribut. Det kan vara bra att tänka på denna förenkling av förhållandet mellan klass och objekt:

    \n
  • En klass definierar variablerna
  • \n
  • när ett objekt skapas tilldelas dessa variabler värden
  • \n

Vi kan alltså använda ett objekt av typen Fraction för att komma åt täljaren och nämnaren i ett bråktal:

from fractions import Fraction\n\ntal = Fraction(2,5)\n\n# Skriv ut täljaren\nprint(tal.numerator)\n\n# ... och sedan nämnaren\nprint(tal.denominator)

2\n5

Klassdefinitionen för Fraction innehåller deklarationer för variablerna numerator och denominator. Varje objekt som skapas baserat på klassen har sina egna specifika värden som tilldelas dessa variabler.

På samma sätt innehåller objekt som skapats baserat på klassen date sina egna unika värden för datumets år, månad och dag:

from datetime import date\n\njul = date(2020, 12, 24)\nmidsommar = date(2020, 6, 20)\n\n# Vi skriver ut bägges månad\nprint(jul.month)\nprint(midsommar.month)

12\n6

Definitionen av klassen date innehåller deklarationer av variablerna year, month och day. När ett nytt datumobjekt skapas baserat på klassen tilldelas dessa variabler värden. Varje objekt har sina egna unika värden som tilldelas dessa variabler.

Funktioner som arbetar med objekt

Att passera ett objekt som ett argument till en funktion borde vara bekant för dig vid det här laget eftersom vi har gjort det redan många gånger i den här kursen. Låt oss ta en titt på följande exempel. Här har vi en funktion som kontrollerar om date-objektet som passeras som argument infaller på en helg:

def redan_veckoslut(dag: date):\n    veckodag = dag.isoweekday()\n    return veckodag == 6 or veckodag == 7

Denna funktion använder metoden isoweekday, som definieras i klassdefinitionen för klassen date, och returnerar ett heltalsvärde på det sättet att om det angivna datumet är en måndag returnerar den 1, och om det är en tisdag returnerar den 2, och så vidare.

jul = date(2020, 12, 24)\nmidsommar = date(2020, 6, 20)\n\nprint(redan_veckoslut(jul))\nprint(redan_veckoslut(midsommar))

False\nTrue

Metoder vs variabler

När du arbetar med ett objekt av typen date kanske du märker att det finns en liten skillnad mellan hur variablerna i objektet åtkoms jämfört med hur metoderna som är kopplade till objekten åtkoms:

dag = date(2020, 12, 24)\n\n# vi kallar metoden\nveckodag = dag.isoweekday()\n\n# vi använder en variabel\nmanad = dag.month\n\nprint(\"Veckodag:\", veckodag)\nprint(\"Månad:\", kuukausi)

Veckodag: 4\nMånad: 12

Veckodagen som datumet infaller på är tillgänglig via metoden isoweekday:

veckodag = dag.isoweekday()

Detta är en metodkallelse, alltså finns det parenteser efter namnet på metoden. Om du lämnar bort parenteserna uppstår det inte något fel, men resultatet blir konstigt:

veckodag =  dag.isoweekday\nprint(\"Veckodag:\", veckodag)

Veckodag: <built-in method isoweekday of datetime.date object at 0x10ed66450>

Månaden av ett date-objekt är en variabel, alltså kan det tillgivna värdet kommas åt med en referens.

manad = dag.month

Lägg märke till att det inte finns parenteser här. Att sätta in parenteser skulle orsaka ett fel:

manad = dag.month()
Traceback (most recent call last):\nFile \"\", line 1, in \nTypeError: 'int' object is not callable

Skapa funktionen lista_ar(datum: list), som får en lista med date-objekt som argument. Funktionen returnerar en ny lista, som innehåller åren i den originella listan i kronologisk ordning, från tidigast till äldst.

Ett exempel av funktionen:

dag1 = date(2019, 2, 3)\ndag2 = date(2006, 10, 10)\ndag3 = date(1993, 5, 9)\n\nar = lista_ar([dag1, dag2, dag3])\nprint(ar)

[1993, 2006, 2019]

I denna övningsbotten finns en färdigt definierad Affarslista-klass, som kan användas för att modellera en affärslista.

Antagandes att vi har ett Affarslista-objekt som refereras till i en variabel med namnet affarslista, kan objektet hanteras med följande metoder:

\nprint(affarslista.mangden_foremal())\nprint(affarslista.foremal(1))\nprint(affarslista.mangd(1))\nprint(affarslista.foremal(2))\nprint(affarslista.mangd(2))\n

2\nBananer\n4\nMjölk\n1

Även följande går:

# Föremålen på affärslistan är indexerade från 1\nfor i in range(1, affarslista.mangden_foremal()+1):\n    foremal = affarslista.foremal(i)\n    mangd = affarslista.mangd(i)\n    print(f\"{foremal}: {mangd} st\")

Bananer 4 st\nMjölk 1 st

Som du kan se fungerar en Affarslista ungefär som en vanlig lista, men den nås via de metoder som tillhandahålls av Affarslista-klassen. Till skillnad från vanliga Python-listor börjar indexeringen från 1, inte 0.

Skapa en funktion med namnet totala_mangd(lista: Affarslista), som tar ett objekt av typen Affarslista som sitt argument. Funktionen ska beräkna det totala antalet enheter i listan och returnera värdet.

Du kan använda följande kod för att testa din funktion:

if __name__ == \"__main__\":\n    lista = Affarslista()\n    lista.tillagg(\"banaaner\", 10)\n    lista.tillagg(\"appel\", 5)\n    lista.tillagg(\"ananas\", 1)\n\n    print(totala_mangd(lista))

16

OBS: Definitionen av klassen Affarslista ingår redan i övningsmallen. Du behöver inte använda en import-sats för att importera den, till skillnad från i exemplen ovan med Python standardbiblioteksklasserna Fraction och date.

","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/2-klasser-och-objekt.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter detta avsnitt"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad en klass är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att förstå vad kopplingen mellan en klass och ett objekt är"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att veta vad som menas med objektorienterad programmering"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I föregående avsnitt arbetade vi med listor, tuplar, ordlistor och strängar. Dessa är alla ganska speciella fall i Python-programmering. Pythons syntax har en unik, fördefinierad metod för att deklarera ett objekt som tillhör var och en av dessa typer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Listor deklareras med hakparenteser"}]},{"type":"text","value":"\nlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Strängar deklareras med citationstecken"}]},{"type":"text","value":"\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hej alla!\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ordlistor deklareras med klammerparenteser"}]},{"type":"text","value":"\nordlista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ett\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"två:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# tuplar deklareras med parenteser"}]},{"type":"text","value":"\ntupel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När någon annan typ av objekt deklareras måste vi anropa en speciell initialiseringsfunktion som kallas konstruktor. Låt oss ta en titt på hur man arbetar med bråk genom Fraction-klassen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi använder klassen Fraction från modulen fractions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" fractions "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Fraction\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi skapar några nya bråktal"}]},{"type":"text","value":"\nhalv "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntredjedel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntredje "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Tulostetaan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"halv"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tredjedel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tredje"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Murtoluvuilla voi myös laskea"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"halv "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" tredjedel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1/2\n1/3\n3/11\n5/6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se ovan, ser metodkallningar för konstruktorer lite annorlunda ut än de normala metodkallningar som vi har stött på tidigare. För det första är de inte kopplade till något objekt med punktnotationen (eftersom konstruktoranropet behövs för att skapa ett objekt i första hand). Konstruktorsmetoden skrivs också med stor bokstav: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[half = Fraction(1,2)]"}]},{"type":"text","value":". Låt oss titta närmare på hur objekt konstrueras genom att bekanta oss med begreppet klass."}]},{"type":"element","tagName":"h2","properties":{"id":"en-klass-ar-ritningen-till-ett-objekt","style":"position:relative;"},"children":[{"type":"text","value":"En klass är ritningen till ett objekt"},{"type":"element","tagName":"a","properties":{"href":"#en-klass-ar-ritningen-till-ett-objekt","ariaLabel":"en klass ar ritningen till ett objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan använt termen klass i materialet många gånger. I exemplet ovan importerade vi t.ex. klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" från modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractions"}]},{"type":"text","value":". Nya bråktalsobjekt skapades genom att kalla på konstruktorsmetoden för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassdefinition innehåller strukturen och funktionaliteterna för alla objekt som representerar den. Därför kallas klasser ibland till objektens ritningar. En klassdefinition berättar alltså till oss vilken typ av data ett objekt innehåller och definierar de metoder som kan användas på objektet. Objektorienterad programmering är ett programmeringsparadigm där programmets funktionalitet är knuten till användningen av klasser och objekt som skapas baserat på dessa."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassdefinition kan användas för att skapa flera objekt. Som tidigare nämnts är objekt oberoende av varandra. Ändringar som görs till ett objekt påverkar i allmänhet inte de andra objekten som är samma klass. Varje objekt har sin egen unika uppsättning dataattribut. Det kan vara bra att tänka på denna förenkling av förhållandet mellan klass och objekt:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"En klass definierar variablerna"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"när ett objekt skapas tilldelas dessa variabler värden"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan alltså använda ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" för att komma åt täljaren och nämnaren i ett bråktal:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" fractions "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Fraction\n\ntal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut täljaren"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numerator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ... och sedan nämnaren"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"denominator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" innehåller deklarationer för variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numerator"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"denominator"}]},{"type":"text","value":". Varje objekt som skapas baserat på klassen har sina egna specifika värden som tilldelas dessa variabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På samma sätt innehåller objekt som skapats baserat på klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" sina egna unika värden för datumets år, månad och dag:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\njul "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut bägges månad"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"12\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definitionen av klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" innehåller deklarationer av variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"month"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"day"}]},{"type":"text","value":". När ett nytt datumobjekt skapas baserat på klassen tilldelas dessa variabler värden. Varje objekt har sina egna unika värden som tilldelas dessa variabler."}]},{"type":"element","tagName":"h2","properties":{"id":"funktioner-som-arbetar-med-objekt","style":"position:relative;"},"children":[{"type":"text","value":"Funktioner som arbetar med objekt"},{"type":"element","tagName":"a","properties":{"href":"#funktioner-som-arbetar-med-objekt","ariaLabel":"funktioner som arbetar med objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att passera ett objekt som ett argument till en funktion borde vara bekant för dig vid det här laget eftersom vi har gjort det redan många gånger i den här kursen. Låt oss ta en titt på följande exempel. Här har vi en funktion som kontrollerar om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"-objektet som passeras som argument infaller på en helg:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"redan_veckoslut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna funktion använder metoden "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.date.isoweekday","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"isoweekday"}]},{"type":"text","value":", som definieras i klassdefinitionen för klassen date, och returnerar ett heltalsvärde på det sättet att om det angivna datumet är en måndag returnerar den 1, och om det är en tisdag returnerar den 2, och så vidare."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"jul "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsommar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"redan_veckoslut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"redan_veckoslut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"midsommar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"metoder-vs-variabler","style":"position:relative;"},"children":[{"type":"text","value":"Metoder vs variabler"},{"type":"element","tagName":"a","properties":{"href":"#metoder-vs-variabler","ariaLabel":"metoder vs variabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du arbetar med ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" kanske du märker att det finns en liten skillnad mellan hur variablerna i objektet åtkoms jämfört med hur metoderna som är kopplade till objekten åtkoms:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi kallar metoden"}]},{"type":"text","value":"\nveckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi använder en variabel"}]},{"type":"text","value":"\nmanad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Veckodag:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Månad:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kuukausi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Veckodag: 4\nMånad: 12"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Veckodagen som datumet infaller på är tillgänglig via metoden isoweekday:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta är en metodkallelse, alltså finns det parenteser efter namnet på metoden. Om du lämnar bort parenteserna uppstår det inte något fel, men resultatet blir konstigt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"veckodag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Veckodag:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" veckodag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Veckodag: "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Månaden av ett date-objekt är en variabel, alltså kan det tillgivna värdet kommas åt med en referens."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"manad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg märke till att det inte finns parenteser här. Att sätta in parenteser skulle orsaka ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"manad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nTypeError: 'int' object is not callable"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lista år","tmcname":"osa08-03_lista_ar"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lista_ar(datum: list)"}]},{"type":"text","value":", som får en lista med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"-objekt som argument. Funktionen returnerar en ny lista, som innehåller "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"åren i den originella listan i kronologisk ordning"}]},{"type":"text","value":", från tidigast till äldst."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel av funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dag1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndag2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2006"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndag3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1993"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista_ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"dag1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1993, 2006, 2019]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Affarslista","tmcname":"osa08-04_affarslista"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I denna övningsbotten finns en färdigt definierad "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":"-klass, som kan användas för att modellera en affärslista."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Antagandes att vi har ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":"-objekt som refereras till i en variabel med namnet affarslista, kan objektet hanteras med följande metoder:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangden_foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\nBananer\n4\nMjölk\n1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Även följande går:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Föremålen på affärslistan är indexerade från 1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangden_foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n foremal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"i"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"foremal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" st\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bananer 4 st\nMjölk 1 st"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se fungerar en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" ungefär som en vanlig lista, men den nås via de metoder som tillhandahålls av Affarslista-klassen. Till skillnad från vanliga Python-listor börjar indexeringen från 1, inte 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"totala_mangd(lista: Affarslista)"}]},{"type":"text","value":", som tar ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Affarslista"}]},{"type":"text","value":" som sitt argument. Funktionen ska beräkna det totala antalet enheter i listan och returnera värdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda följande kod för att testa din funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaaner\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"appel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ananas\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"totala_mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"16"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" Definitionen av klassen Affarslista ingår redan i övningsmallen. Du behöver inte använda en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-sats för att importera den, till skillnad från i exemplen ovan med Python standardbiblioteksklasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"."}]}]}]},"html":"

Efter detta avsnitt

    \n
  • Vet du vad en klass är
  • \n
  • Kommer du att förstå vad kopplingen mellan en klass och ett objekt är
  • \n
  • Kommer du att veta vad som menas med objektorienterad programmering
  • \n

I föregående avsnitt arbetade vi med listor, tuplar, ordlistor och strängar. Dessa är alla ganska speciella fall i Python-programmering. Pythons syntax har en unik, fördefinierad metod för att deklarera ett objekt som tillhör var och en av dessa typer:

# Listor deklareras med hakparenteser\nlista = [1,2,3]\n\n# Strängar deklareras med citationstecken\nstrang = \"Hej alla!\"\n\n# Ordlistor deklareras med klammerparenteser\nordlista = {\"ett\": 1, \"två:\": 2}\n\n# tuplar deklareras med parenteser\ntupel = (1,2,3)

När någon annan typ av objekt deklareras måste vi anropa en speciell initialiseringsfunktion som kallas konstruktor. Låt oss ta en titt på hur man arbetar med bråk genom Fraction-klassen.

# vi använder klassen Fraction från modulen fractions\nfrom fractions import Fraction\n\n# vi skapar några nya bråktal\nhalv = Fraction(1,2)\n\ntredjedel = Fraction(1,3)\n\ntredje = Fraction(3,11)\n\n# Tulostetaan\nprint(halv)\nprint(tredjedel)\nprint(tredje)\n\n# Murtoluvuilla voi myös laskea\nprint(halv + tredjedel)

1/2\n1/3\n3/11\n5/6

Som du kan se ovan, ser metodkallningar för konstruktorer lite annorlunda ut än de normala metodkallningar som vi har stött på tidigare. För det första är de inte kopplade till något objekt med punktnotationen (eftersom konstruktoranropet behövs för att skapa ett objekt i första hand). Konstruktorsmetoden skrivs också med stor bokstav: [half = Fraction(1,2)]. Låt oss titta närmare på hur objekt konstrueras genom att bekanta oss med begreppet klass.

En klass är ritningen till ett objekt

Vi har redan använt termen klass i materialet många gånger. I exemplet ovan importerade vi t.ex. klassen Fraction från modulen fractions. Nya bråktalsobjekt skapades genom att kalla på konstruktorsmetoden för klassen Fraction.

En klassdefinition innehåller strukturen och funktionaliteterna för alla objekt som representerar den. Därför kallas klasser ibland till objektens ritningar. En klassdefinition berättar alltså till oss vilken typ av data ett objekt innehåller och definierar de metoder som kan användas på objektet. Objektorienterad programmering är ett programmeringsparadigm där programmets funktionalitet är knuten till användningen av klasser och objekt som skapas baserat på dessa.

En klassdefinition kan användas för att skapa flera objekt. Som tidigare nämnts är objekt oberoende av varandra. Ändringar som görs till ett objekt påverkar i allmänhet inte de andra objekten som är samma klass. Varje objekt har sin egen unika uppsättning dataattribut. Det kan vara bra att tänka på denna förenkling av förhållandet mellan klass och objekt:

    \n
  • En klass definierar variablerna
  • \n
  • när ett objekt skapas tilldelas dessa variabler värden
  • \n

Vi kan alltså använda ett objekt av typen Fraction för att komma åt täljaren och nämnaren i ett bråktal:

from fractions import Fraction\n\ntal = Fraction(2,5)\n\n# Skriv ut täljaren\nprint(tal.numerator)\n\n# ... och sedan nämnaren\nprint(tal.denominator)

2\n5

Klassdefinitionen för Fraction innehåller deklarationer för variablerna numerator och denominator. Varje objekt som skapas baserat på klassen har sina egna specifika värden som tilldelas dessa variabler.

På samma sätt innehåller objekt som skapats baserat på klassen date sina egna unika värden för datumets år, månad och dag:

from datetime import date\n\njul = date(2020, 12, 24)\nmidsommar = date(2020, 6, 20)\n\n# Vi skriver ut bägges månad\nprint(jul.month)\nprint(midsommar.month)

12\n6

Definitionen av klassen date innehåller deklarationer av variablerna year, month och day. När ett nytt datumobjekt skapas baserat på klassen tilldelas dessa variabler värden. Varje objekt har sina egna unika värden som tilldelas dessa variabler.

Funktioner som arbetar med objekt

Att passera ett objekt som ett argument till en funktion borde vara bekant för dig vid det här laget eftersom vi har gjort det redan många gånger i den här kursen. Låt oss ta en titt på följande exempel. Här har vi en funktion som kontrollerar om date-objektet som passeras som argument infaller på en helg:

def redan_veckoslut(dag: date):\n    veckodag = dag.isoweekday()\n    return veckodag == 6 or veckodag == 7

Denna funktion använder metoden isoweekday, som definieras i klassdefinitionen för klassen date, och returnerar ett heltalsvärde på det sättet att om det angivna datumet är en måndag returnerar den 1, och om det är en tisdag returnerar den 2, och så vidare.

jul = date(2020, 12, 24)\nmidsommar = date(2020, 6, 20)\n\nprint(redan_veckoslut(jul))\nprint(redan_veckoslut(midsommar))

False\nTrue

Metoder vs variabler

När du arbetar med ett objekt av typen date kanske du märker att det finns en liten skillnad mellan hur variablerna i objektet åtkoms jämfört med hur metoderna som är kopplade till objekten åtkoms:

dag = date(2020, 12, 24)\n\n# vi kallar metoden\nveckodag = dag.isoweekday()\n\n# vi använder en variabel\nmanad = dag.month\n\nprint(\"Veckodag:\", veckodag)\nprint(\"Månad:\", kuukausi)

Veckodag: 4\nMånad: 12

Veckodagen som datumet infaller på är tillgänglig via metoden isoweekday:

veckodag = dag.isoweekday()

Detta är en metodkallelse, alltså finns det parenteser efter namnet på metoden. Om du lämnar bort parenteserna uppstår det inte något fel, men resultatet blir konstigt:

veckodag =  dag.isoweekday\nprint(\"Veckodag:\", veckodag)

Veckodag: <built-in method isoweekday of datetime.date object at 0x10ed66450>

Månaden av ett date-objekt är en variabel, alltså kan det tillgivna värdet kommas åt med en referens.

manad = dag.month

Lägg märke till att det inte finns parenteser här. Att sätta in parenteser skulle orsaka ett fel:

manad = dag.month()
Traceback (most recent call last):\nFile \"\", line 1, in \nTypeError: 'int' object is not callable

Skapa funktionen lista_ar(datum: list), som får en lista med date-objekt som argument. Funktionen returnerar en ny lista, som innehåller åren i den originella listan i kronologisk ordning, från tidigast till äldst.

Ett exempel av funktionen:

dag1 = date(2019, 2, 3)\ndag2 = date(2006, 10, 10)\ndag3 = date(1993, 5, 9)\n\nar = lista_ar([dag1, dag2, dag3])\nprint(ar)

[1993, 2006, 2019]

I denna övningsbotten finns en färdigt definierad Affarslista-klass, som kan användas för att modellera en affärslista.

Antagandes att vi har ett Affarslista-objekt som refereras till i en variabel med namnet affarslista, kan objektet hanteras med följande metoder:

\nprint(affarslista.mangden_foremal())\nprint(affarslista.foremal(1))\nprint(affarslista.mangd(1))\nprint(affarslista.foremal(2))\nprint(affarslista.mangd(2))\n

2\nBananer\n4\nMjölk\n1

Även följande går:

# Föremålen på affärslistan är indexerade från 1\nfor i in range(1, affarslista.mangden_foremal()+1):\n    foremal = affarslista.foremal(i)\n    mangd = affarslista.mangd(i)\n    print(f\"{foremal}: {mangd} st\")

Bananer 4 st\nMjölk 1 st

Som du kan se fungerar en Affarslista ungefär som en vanlig lista, men den nås via de metoder som tillhandahålls av Affarslista-klassen. Till skillnad från vanliga Python-listor börjar indexeringen från 1, inte 0.

Skapa en funktion med namnet totala_mangd(lista: Affarslista), som tar ett objekt av typen Affarslista som sitt argument. Funktionen ska beräkna det totala antalet enheter i listan och returnera värdet.

Du kan använda följande kod för att testa din funktion:

if __name__ == \"__main__\":\n    lista = Affarslista()\n    lista.tillagg(\"banaaner\", 10)\n    lista.tillagg(\"appel\", 5)\n    lista.tillagg(\"ananas\", 1)\n\n    print(totala_mangd(lista))

16

OBS: Definitionen av klassen Affarslista ingår redan i övningsmallen. Du behöver inte använda en import-sats för att importera den, till skillnad från i exemplen ovan med Python standardbiblioteksklasserna Fraction och date.

","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/2-klasser-och-objekt.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-8/3-egna-klasser/page-data.json b/page-data/osa-8/3-egna-klasser/page-data.json index b48b5f5c3..e5df2e7b9 100644 --- a/page-data/osa-8/3-egna-klasser/page-data.json +++ b/page-data/osa-8/3-egna-klasser/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-8/3-egna-klasser", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du definierar dina egna klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa objekt baserade på klasser som du själv har definierat"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur man skriver en konstruktor"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är du bekant med parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad attribut är och hur de används"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klass definieras med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":". Syntaxen ser ut enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"KlassNamn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Klassdefinitionen"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klasser namnges vanligtvis med kamelnotation. Detta innebär att alla ord i klassnamnet skrivs tillsammans, utan mellanslag, och att varje ord börjar med stor bokstav. Följande exempel på klassnamn följer denna konvention:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Veckodag"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BibliotekDatabas"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PythonKursBetyg"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En enskild klassdefinition bör representera en enskild helhet, vars innehåll bör vara sammanlänkat på något sätt. I mer komplicerade program kan klasser innehålla medlemmar av andra klasser. Till exempel kan klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kurs"}]},{"type":"text","value":" innehålla objekt av klasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lektion"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ÖvningsTillfälle"}]},{"type":"text","value":" osv."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en ram av en klassdefinition. Funktionerna saknas fortfarande vid denna tidpunkt."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kodstycket ovan talar om för Python att vi här definierar en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":". Klassen innehåller ingen funktionalitet ännu, men vi kan fortfarande skapa ett objekt baserat på klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett program där två variabler läggs till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":"-objekt: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ägare"}]},{"type":"text","value":". Alla variabler som är kopplade till ett objekt kallas dess attribut, eller mer specifikt, dataattribut, eller ibland instansvariabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De attribut som är kopplade till ett objekt kan nås via objektet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python\n5.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dataattributen är endast tillgängliga via det objekt som de är kopplade till. Varje Bankkonto-objekt som skapas baserat på Bankkonto-klassen har sina egna värden kopplade till dataattributen. Dessa värden kan nås genom att hänvisa till objektet i fråga:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"155.50"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta refererar till dataattributen saldo, som är kopplad till kontot"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# DETTA ORSAKAR ETT FEL, eftersom det inte finns någon sådan oberoende variabel, och objektreferensen saknas"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-lagga-till-en-konstruktor","style":"position:relative;"},"children":[{"type":"text","value":"Att lägga till en konstruktor"},{"type":"element","tagName":"a","properties":{"href":"#att-lagga-till-en-konstruktor","ariaLabel":"att lagga till en konstruktor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan såg vi att en ny instans av en klass kan skapas genom att anropa klassens konstruktormetod på följande sätt: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"KlassensNamn()"}]},{"type":"text","value":". Ovan kopplade vi sedan dataattribut till objektet separat, men det är ofta bekvämare att skicka dessa initiala värden för attribut direkt när objektet skapas. I exemplet ovan hade vi först ett Bankkonto-objekt utan dessa attribut, och attributen existerade först efter att de uttryckligen hade deklarerats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att deklarera attribut utanför konstruktorn leder till en situation där olika instanser av samma klass kan ha olika attribut. Följande kod ger ett fel eftersom vi nu har ett annat "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto-objekt"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pernillas_konto"}]},{"type":"text","value":", som inte innehåller samma attribut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1400"}]},{"type":"text","value":"\n\npernillas_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# DETTA ORSAKAR ETT FEL"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så istället för att deklarera attribut efter att varje instans av klassen har skapats, är det oftast en bättre idé att initialisera attributens värden när klasskonstruktorn anropas. Eftersom klassdefinitionen Bankkonto för närvarande bara är en ram, antas konstruktormetoden implicit av Python-tolkaren, men det är möjligt att definiera egna konstruktormetoder, och det är precis vad vi kommer att göra nu."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En konstruktormetod är en metoddeklaration med det speciella namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":", som vanligtvis inkluderas i början av en klassdefinition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":"-klass med en konstruktormetod tillagd:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Konstruktorn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Namnet på konstruktorsmetoden är alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":". Lägg märke till de två understrecken på båda sidorna av ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"init"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den första parametern i en konstruktorsdefinition heter alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". Detta refererar till själva objektet och är nödvändigt för att deklarera alla attribut som är knutna till objektet. Tilldelningen"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.saldo = saldo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"tilldelar objektets saldoattribut den balans som mottagits som argument. Det är vanligt att använda samma variabelnamn för parametrarna och dataattributen som definieras i en konstruktor, men variabelnamnen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.saldo"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":" ovan hänvisar till två olika variabler:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.saldo"}]},{"type":"text","value":" är ett attribut för objektet. Varje Bankkonto-objekt har sitt eget saldo."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":" är en parameter i konstruktorsmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":". Dess värde sätts till det värde som skickas som argument till metoden när konstruktorn kallas (dvs. när en ny insctance av klassen skapas)."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu när vi har definierat parametrarna för konstruktorsmetoden kan vi skicka de önskade initiala värdena för dataattributen som argument när ett nytt objekt skapas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Konstruktorn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Parametern self ges inget värde, utan Python ger ett sådant automatiskt"}]},{"type":"text","value":"\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npernillas_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"100\n20000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är nu mycket enklare att arbeta med Bankkonto-objekten, eftersom värdena kan skickas när objektet skapas, och de två separata instanserna kan hanteras på ett mer förutsägbart och enhetligt sätt. Att deklarera dataattribut i konstruktorn säkerställer också att attributen verkligen deklareras, och att de önskade initiala värdena alltid ges av programmeraren som använder klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är fortfarande möjligt att ändra de initiala värdena för dataattributen senare i programmet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Konstruktorn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ändra saldot till 1500"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi lägger till 2000 till saldot"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"100\n1500\n3500"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett annat exempel på klasser och objekt. Vi ska skriva en klass som modellerar en enstaka dragning av lotterinummer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LotteriDragning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vecka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" datum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vecka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vecka\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datum\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar ett nytt LotteriDragning-objekt"}]},{"type":"text","value":"\nrunda1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteriDragning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut resultatet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"runda1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vecka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"runda1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" runda1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2021-01-02\n1\n4\n8\n12\n13\n14\n33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se ovan kan attributerna vara av vilken sort som helst. Här har varje LotteriDragnings objekt attributer av typerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bok","tmcname":"osa08-06_bok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":", som har variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forfattare"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"genre"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar"}]},{"type":"text","value":", tillsammans med en konstruktor som anger ursprungsvärden åt dessa attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska fungera englit följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Luciano Ramalho\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naventyr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Äkta Äventyr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Persson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"självbiografi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Boken "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"aventyr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"s genre är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"aventyr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"genre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Luciano Ramalho: Fluent Python (2015)\nBoken Äkta Äventyrs genre är självbiografi"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Skriv klasser","tmcname":"osa08-07_skriv_klasser"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa de tre klasser som anges nedan. Varje klass ska ha exakt samma namn och typer av attribut som anges i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inkludera också en konstruktor i varje klass. Konstruktorn ska ta de ursprungliga värdena för attributen som sina argument, i den ordning som anges nedan."}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Klass Minneslista"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut rubrik (sträng)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut inlagg (lista)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ol","properties":{"start":2},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Klass Kund"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut id (sträng)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut saldo (decimaltal)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut rabatt (heltal)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ol","properties":{"start":3},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Klass Kabel"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut modell (sträng)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut langd (decimaltal)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut maximal_hastighet (heltal)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut dubbelriktad (Boolesk)"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"anvandning-av-objekt-fran-egengjorda-klasser","style":"position:relative;"},"children":[{"type":"text","value":"Användning av objekt från egengjorda klasser"},{"type":"element","tagName":"a","properties":{"href":"#anvandning-av-objekt-fran-egengjorda-klasser","ariaLabel":"anvandning av objekt fran egengjorda klasser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objekt som bildas från dina egna klassdefinitioner skiljer sig inte från andra Python-objekt. De kan skickas som argument och returnera värden precis som alla andra objekt. Vi kan till exempel skriva hjälpfunktioner för att arbeta med bankkonton:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktionen skapar ett nytt bankkonto-objekt och returnerar det"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oppna_konto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n nytt_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" nytt_konto\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# denna funktion lägger till det belopp som anges som argument till saldot som anges som argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillagg_pengar_pa_kontot"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" summa\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" oppna_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntillagg_pengar_pa_kontot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n500"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Skapa husdjur","tmcname":"osa08-07b_skapa_husdjur"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Husdjur"}]},{"type":"text","value":". Klassen har en konstruktor, som tar värden till attributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ras"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fodelsear"}]},{"type":"text","value":" i den ordningen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa sedan en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nytt_husdjur(namn: str, ras: str, fodelsear: int)"}]},{"type":"text","value":" "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"utanför klassdefinitionen"}]},{"type":"text","value":", som skapar och returnerar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Husdjur"}]},{"type":"text","value":"-objekt (alltså av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Husdjur"}]},{"type":"text","value":"-klassen definierad ovan)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på funktionens användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"molly "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nytt_husdjur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Molly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hund\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2017"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ras"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Molly\nhund\n2017"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Äldre bok","tmcname":"osa08-08_aldre_bok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aldre_bok(bok1: bok, bok2: bok)"}]},{"type":"text","value":", som får som argument två objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":". Funktionen berättar vilkendera bok som är äldre. Ifall båda böckerna är publicerades samma år ska meddelandet som skrivs ut vara annorlunda."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Luciano Ramalho\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naventyr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Äkta Äventyr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Persson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"självbiografi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnorma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Norma\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sofi Oksanen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"brott\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naldre_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aventyr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naldre_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" norma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Äkta Äventyr är äldre, den publicerades 1956\nFluent Python och Norma publicerades 2015"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Genrens böcker","tmcname":"osa08-09_genrens_bocker"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"genrens_bocker(bocker: list, genre: str)"}]},{"type":"text","value":", som får som argument en lista med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":"-objekt såväl som en sträng som representerar genren."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"en ny"}]},{"type":"text","value":" lista, som innehåller böckerna med den specifierade genren från den originella listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen fungerar på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Luciano Ramalho\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ohjelmointi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naventyr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Äkta Äventyr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Persson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"självbiografi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnorma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Norma\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sofi Oksanen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"rikos\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbocker "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aventyr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" norma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Snögubben\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jo Nesbø\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"brott\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2007"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Böcker i brottsgenren:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" genrens_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"brott\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Böcker i brottsgenren:\nSofi Oksanen: Norma\nJo Nesbø: Snögubben"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du hur du definierar dina egna klasser
  • \n
  • Kommer du att kunna skapa objekt baserade på klasser som du själv har definierat
  • \n
  • Vet du hur man skriver en konstruktor
  • \n
  • Är du bekant med parameternamnet self
  • \n
  • Vet du vad attribut är och hur de används
  • \n

En klass definieras med nyckelordet class. Syntaxen ser ut enligt följande:

class KlassNamn:\n    # Klassdefinitionen

Klasser namnges vanligtvis med kamelnotation. Detta innebär att alla ord i klassnamnet skrivs tillsammans, utan mellanslag, och att varje ord börjar med stor bokstav. Följande exempel på klassnamn följer denna konvention:

    \n
  • Veckodag
  • \n
  • Bankkonto
  • \n
  • BibliotekDatabas
  • \n
  • PythonKursBetyg
  • \n

En enskild klassdefinition bör representera en enskild helhet, vars innehåll bör vara sammanlänkat på något sätt. I mer komplicerade program kan klasser innehålla medlemmar av andra klasser. Till exempel kan klassen Kurs innehålla objekt av klasserna Lektion, ÖvningsTillfälle osv.

Låt oss ta en titt på en ram av en klassdefinition. Funktionerna saknas fortfarande vid denna tidpunkt.

class Bankkonto:\n    pass

Kodstycket ovan talar om för Python att vi här definierar en klass med namnet Bankkonto. Klassen innehåller ingen funktionalitet ännu, men vi kan fortfarande skapa ett objekt baserat på klassen.

Låt oss titta på ett program där två variabler läggs till ett Bankkonto-objekt: saldo och ägare. Alla variabler som är kopplade till ett objekt kallas dess attribut, eller mer specifikt, dataattribut, eller ibland instansvariabler.

De attribut som är kopplade till ett objekt kan nås via objektet:

class Bankkonto:\n    pass\n\npeters_konto = Bankkonto()\npeters_konto.agare = \"Peter Python\"\npeters_konto.saldo = 5.0\n\nprint(peters_konto.agare)\nprint(peters_konto.saldo)

Peter Python\n5.0

Dataattributen är endast tillgängliga via det objekt som de är kopplade till. Varje Bankkonto-objekt som skapas baserat på Bankkonto-klassen har sina egna värden kopplade till dataattributen. Dessa värden kan nås genom att hänvisa till objektet i fråga:

konto = Bankkonto()\nkonto.saldo = 155.50\n\nprint(konto.saldo) # Detta refererar till dataattributen saldo, som är kopplad till kontot\nprint(saldo) # DETTA ORSAKAR ETT FEL, eftersom det inte finns någon sådan oberoende variabel, och objektreferensen saknas

Att lägga till en konstruktor

I exemplet ovan såg vi att en ny instans av en klass kan skapas genom att anropa klassens konstruktormetod på följande sätt: KlassensNamn(). Ovan kopplade vi sedan dataattribut till objektet separat, men det är ofta bekvämare att skicka dessa initiala värden för attribut direkt när objektet skapas. I exemplet ovan hade vi först ett Bankkonto-objekt utan dessa attribut, och attributen existerade först efter att de uttryckligen hade deklarerats.

Att deklarera attribut utanför konstruktorn leder till en situation där olika instanser av samma klass kan ha olika attribut. Följande kod ger ett fel eftersom vi nu har ett annat Bankkonto-objekt, pernillas_konto, som inte innehåller samma attribut:

class Bankkonto:\n    pass\n\npeters_konto = Bankkonto()\npeters_konto.agare = \"Peter\"\npeters_konto.saldo = 1400\n\npernillas_konto = Bankkonto()\npernillas_konto.agare = \"Pernilla\"\n\nprint(peters_konto.saldo)\nprint(pernillas_konto.saldo) # DETTA ORSAKAR ETT FEL

Så istället för att deklarera attribut efter att varje instans av klassen har skapats, är det oftast en bättre idé att initialisera attributens värden när klasskonstruktorn anropas. Eftersom klassdefinitionen Bankkonto för närvarande bara är en ram, antas konstruktormetoden implicit av Python-tolkaren, men det är möjligt att definiera egna konstruktormetoder, och det är precis vad vi kommer att göra nu.

En konstruktormetod är en metoddeklaration med det speciella namnet __init__, som vanligtvis inkluderas i början av en klassdefinition.

Låt oss ta en titt på en Bankkonto-klass med en konstruktormetod tillagd:

class Bankkonto:\n\n    # Konstruktorn\n    def __init__(self, saldo: float, agare: str):\n        self.saldo = saldo\n        self.agare = agare

Namnet på konstruktorsmetoden är alltid __init__. Lägg märke till de två understrecken på båda sidorna av ordet init.

Den första parametern i en konstruktorsdefinition heter alltid self. Detta refererar till själva objektet och är nödvändigt för att deklarera alla attribut som är knutna till objektet. Tilldelningen

self.saldo = saldo

tilldelar objektets saldoattribut den balans som mottagits som argument. Det är vanligt att använda samma variabelnamn för parametrarna och dataattributen som definieras i en konstruktor, men variabelnamnen self.saldo och saldo ovan hänvisar till två olika variabler:

    \n
  • \n

    Variabeln self.saldo är ett attribut för objektet. Varje Bankkonto-objekt har sitt eget saldo.

    \n
  • \n
  • \n

    Variabeln saldo är en parameter i konstruktorsmetoden __init__. Dess värde sätts till det värde som skickas som argument till metoden när konstruktorn kallas (dvs. när en ny insctance av klassen skapas).

    \n
  • \n

Nu när vi har definierat parametrarna för konstruktorsmetoden kan vi skicka de önskade initiala värdena för dataattributen som argument när ett nytt objekt skapas:

class Bankkonto:\n\n    # Konstruktorn\n    def __init__(self, saldo: float, agare: str):\n        self.saldo = saldo\n        self.agare = agare\n\n# Parametern self ges inget värde, utan Python ger ett sådant automatiskt\npeters_konto = Bankkonto(100, \"Peter Python\")\npernillas_konto = Bankkonto(20000, \"Pernilla Pythonson\")\n\nprint(peters_konto.saldo)\nprint(pernillas_konto.saldo)

100\n20000

Det är nu mycket enklare att arbeta med Bankkonto-objekten, eftersom värdena kan skickas när objektet skapas, och de två separata instanserna kan hanteras på ett mer förutsägbart och enhetligt sätt. Att deklarera dataattribut i konstruktorn säkerställer också att attributen verkligen deklareras, och att de önskade initiala värdena alltid ges av programmeraren som använder klassen.

Det är fortfarande möjligt att ändra de initiala värdena för dataattributen senare i programmet:

class Bankkonto:\n\n    # Konstruktorn\n    def __init__(self, saldo: float, agare: str):\n        self.saldo = saldo\n        self.agare = agare\n\npeters_konto = Bankkonto(100, \"Peter Python\")\nprint(peters_konto.saldo)\n\n# Ändra saldot till 1500\npeters_konto.saldo = 1500\nprint(peters_konto.saldo)\n\n# Vi lägger till 2000 till saldot\npeters_konto.saldo += 2000\nprint(peters_konto.saldo)

100\n1500\n3500

Låt oss titta på ett annat exempel på klasser och objekt. Vi ska skriva en klass som modellerar en enstaka dragning av lotterinummer:

from datetime import date\n\nclass LotteriDragning:\n\n    def __init__(self, vecka: int, datum: date, nummer: list):\n        self.vecka = vecka\n        self.datum = datum\n        self.nummer = nummer\n\n\n# Vi skapar ett nytt LotteriDragning-objekt\nrunda1 = LotteriDragning(1, date(2021, 1, 2), [1,4,8,12,13,14,33])\n\n# Vi skriver ut resultatet\nprint(runda1.vecka)\nprint(runda1.datum)\n\nfor nummer in runda1.nummer:\n    print(nummer)

1\n2021-01-02\n1\n4\n8\n12\n13\n14\n33

Som du kan se ovan kan attributerna vara av vilken sort som helst. Här har varje LotteriDragnings objekt attributer av typerna list och date.

Skapa klassen Bok, som har variablerna namn, forfattare, genre och ar, tillsammans med en konstruktor som anger ursprungsvärden åt dessa attribut.

Klassen ska fungera englit följande:

python = Bok(\"Fluent Python\", \"Luciano Ramalho\", \"programmering\", 2015)\naventyr = Bok(\"Äkta Äventyr\", \"Petter Persson\", \"självbiografi\", 1956)\n\nprint(f\"{python.forfattare}: {python.namn} ({python.ar})\")\nprint(f\"Boken {aventyr.namn}s genre är {aventyr.genre}\")

Luciano Ramalho: Fluent Python (2015)\nBoken Äkta Äventyrs genre är självbiografi

Skapa de tre klasser som anges nedan. Varje klass ska ha exakt samma namn och typer av attribut som anges i listan.

Inkludera också en konstruktor i varje klass. Konstruktorn ska ta de ursprungliga värdena för attributen som sina argument, i den ordning som anges nedan.

    \n
  1. Klass Minneslista
  2. \n
    \n
  • attribut rubrik (sträng)
  • \n
  • attribut inlagg (lista)
  • \n
    \n
  1. Klass Kund
  2. \n
    \n
  • attribut id (sträng)
  • \n
  • attribut saldo (decimaltal)
  • \n
  • attribut rabatt (heltal)
  • \n
    \n
  1. Klass Kabel
  2. \n
    \n
  • attribut modell (sträng)
  • \n
  • attribut langd (decimaltal)
  • \n
  • attribut maximal_hastighet (heltal)
  • \n
  • attribut dubbelriktad (Boolesk)
  • \n

Användning av objekt från egengjorda klasser

Objekt som bildas från dina egna klassdefinitioner skiljer sig inte från andra Python-objekt. De kan skickas som argument och returnera värden precis som alla andra objekt. Vi kan till exempel skriva hjälpfunktioner för att arbeta med bankkonton:

# funktionen skapar ett nytt bankkonto-objekt och returnerar det\ndef oppna_konto(namn: str):\n    nytt_konto =  Bankkonto(0, namn)\n    return nytt_konto\n\n# denna funktion lägger till det belopp som anges som argument till saldot som anges som argument\ndef tillagg_pengar_pa_kontot(konto: Bankkonto, summa: int):\n    konto.saldo += summa\n\npeters_konto = oppna_konto(\"Peter Python\")\nprint(peters_konto.saldo)\n\ntillagg_pengar_pa_kontot(peters_konto, 500)\n\nprint(peters_konto.saldo)

0\n500

Definiera klassen Husdjur. Klassen har en konstruktor, som tar värden till attributen namn, ras och fodelsear i den ordningen.

Skapa sedan en funktion med namnet nytt_husdjur(namn: str, ras: str, fodelsear: int) utanför klassdefinitionen, som skapar och returnerar ett Husdjur-objekt (alltså av Husdjur-klassen definierad ovan).

Exempel på funktionens användning:

molly = nytt_husdjur(\"Molly\", \"hund\", 2017)\nprint(molly.namn)\nprint(molly.ras)\nprint(molly.fodelsear)

Molly\nhund\n2017

Skapa funktionen aldre_bok(bok1: bok, bok2: bok), som får som argument två objekt av typen Bok. Funktionen berättar vilkendera bok som är äldre. Ifall båda böckerna är publicerades samma år ska meddelandet som skrivs ut vara annorlunda.

Funktionen fungerar enligt följande:

python = Bok(\"Fluent Python\", \"Luciano Ramalho\", \"programmering\", 2015)\naventyr = Bok(\"Äkta Äventyr\", \"Petter Persson\", \"självbiografi\", 1956)\nnorma = Bok(\"Norma\", \"Sofi Oksanen\", \"brott\", 2015)\n\naldre_bok(python, aventyr)\naldre_bok(python, norma)

Äkta Äventyr är äldre, den publicerades 1956\nFluent Python och Norma publicerades 2015

Skapa funktionen genrens_bocker(bocker: list, genre: str), som får som argument en lista med Bok-objekt såväl som en sträng som representerar genren.

Funktionen returnerar en ny lista, som innehåller böckerna med den specifierade genren från den originella listan.

Funktionen fungerar på följande sätt:

python = Bok(\"Fluent Python\", \"Luciano Ramalho\", \"ohjelmointi\", 2015)\naventyr = Bok(\"Äkta Äventyr\", \"Petter Persson\", \"självbiografi\", 1956)\nnorma = Bok(\"Norma\", \"Sofi Oksanen\", \"rikos\", 2015)\n\nbocker = [python, aventyr, norma, Bok(\"Snögubben\", \"Jo Nesbø\", \"brott\", 2007)]\n\nprint(\"Böcker i brottsgenren:\")\nfor bok in genrens_bocker(bocker, \"brott\"):\n    print(f\"{bok.forfattare}: {bok.namn}\")

Böcker i brottsgenren:\nSofi Oksanen: Norma\nJo Nesbø: Snögubben

","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/3-egna-klasser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du definierar dina egna klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa objekt baserade på klasser som du själv har definierat"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur man skriver en konstruktor"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är du bekant med parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad attribut är och hur de används"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klass definieras med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":". Syntaxen ser ut enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"KlassNamn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Klassdefinitionen"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klasser namnges vanligtvis med kamelnotation. Detta innebär att alla ord i klassnamnet skrivs tillsammans, utan mellanslag, och att varje ord börjar med stor bokstav. Följande exempel på klassnamn följer denna konvention:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Veckodag"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BibliotekDatabas"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PythonKursBetyg"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En enskild klassdefinition bör representera en enskild helhet, vars innehåll bör vara sammanlänkat på något sätt. I mer komplicerade program kan klasser innehålla medlemmar av andra klasser. Till exempel kan klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kurs"}]},{"type":"text","value":" innehålla objekt av klasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lektion"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ÖvningsTillfälle"}]},{"type":"text","value":" osv."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en ram av en klassdefinition. Funktionerna saknas fortfarande vid denna tidpunkt."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kodstycket ovan talar om för Python att vi här definierar en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":". Klassen innehåller ingen funktionalitet ännu, men vi kan fortfarande skapa ett objekt baserat på klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett program där två variabler läggs till ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":"-objekt: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ägare"}]},{"type":"text","value":". Alla variabler som är kopplade till ett objekt kallas dess attribut, eller mer specifikt, dataattribut, eller ibland instansvariabler."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De attribut som är kopplade till ett objekt kan nås via objektet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5.0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Python\n5.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dataattributen är endast tillgängliga via det objekt som de är kopplade till. Varje Bankkonto-objekt som skapas baserat på Bankkonto-klassen har sina egna värden kopplade till dataattributen. Dessa värden kan nås genom att hänvisa till objektet i fråga:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"155.50"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta refererar till dataattributen saldo, som är kopplad till kontot"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# DETTA ORSAKAR ETT FEL, eftersom det inte finns någon sådan oberoende variabel, och objektreferensen saknas"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"att-lagga-till-en-konstruktor","style":"position:relative;"},"children":[{"type":"text","value":"Att lägga till en konstruktor"},{"type":"element","tagName":"a","properties":{"href":"#att-lagga-till-en-konstruktor","ariaLabel":"att lagga till en konstruktor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan såg vi att en ny instans av en klass kan skapas genom att anropa klassens konstruktormetod på följande sätt: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"KlassensNamn()"}]},{"type":"text","value":". Ovan kopplade vi sedan dataattribut till objektet separat, men det är ofta bekvämare att skicka dessa initiala värden för attribut direkt när objektet skapas. I exemplet ovan hade vi först ett Bankkonto-objekt utan dessa attribut, och attributen existerade först efter att de uttryckligen hade deklarerats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att deklarera attribut utanför konstruktorn leder till en situation där olika instanser av samma klass kan ha olika attribut. Följande kod ger ett fel eftersom vi nu har ett annat "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto-objekt"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pernillas_konto"}]},{"type":"text","value":", som inte innehåller samma attribut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1400"}]},{"type":"text","value":"\n\npernillas_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# DETTA ORSAKAR ETT FEL"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så istället för att deklarera attribut efter att varje instans av klassen har skapats, är det oftast en bättre idé att initialisera attributens värden när klasskonstruktorn anropas. Eftersom klassdefinitionen Bankkonto för närvarande bara är en ram, antas konstruktormetoden implicit av Python-tolkaren, men det är möjligt att definiera egna konstruktormetoder, och det är precis vad vi kommer att göra nu."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En konstruktormetod är en metoddeklaration med det speciella namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":", som vanligtvis inkluderas i början av en klassdefinition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":"-klass med en konstruktormetod tillagd:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Konstruktorn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Namnet på konstruktorsmetoden är alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":". Lägg märke till de två understrecken på båda sidorna av ordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"init"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den första parametern i en konstruktorsdefinition heter alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". Detta refererar till själva objektet och är nödvändigt för att deklarera alla attribut som är knutna till objektet. Tilldelningen"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.saldo = saldo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"tilldelar objektets saldoattribut den balans som mottagits som argument. Det är vanligt att använda samma variabelnamn för parametrarna och dataattributen som definieras i en konstruktor, men variabelnamnen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.saldo"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":" ovan hänvisar till två olika variabler:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.saldo"}]},{"type":"text","value":" är ett attribut för objektet. Varje Bankkonto-objekt har sitt eget saldo."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":" är en parameter i konstruktorsmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":". Dess värde sätts till det värde som skickas som argument till metoden när konstruktorn kallas (dvs. när en ny insctance av klassen skapas)."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu när vi har definierat parametrarna för konstruktorsmetoden kan vi skicka de önskade initiala värdena för dataattributen som argument när ett nytt objekt skapas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Konstruktorn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Parametern self ges inget värde, utan Python ger ett sådant automatiskt"}]},{"type":"text","value":"\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npernillas_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"100\n20000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är nu mycket enklare att arbeta med Bankkonto-objekten, eftersom värdena kan skickas när objektet skapas, och de två separata instanserna kan hanteras på ett mer förutsägbart och enhetligt sätt. Att deklarera dataattribut i konstruktorn säkerställer också att attributen verkligen deklareras, och att de önskade initiala värdena alltid ges av programmeraren som använder klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är fortfarande möjligt att ändra de initiala värdena för dataattributen senare i programmet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Konstruktorn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ändra saldot till 1500"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi lägger till 2000 till saldot"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"100\n1500\n3500"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett annat exempel på klasser och objekt. Vi ska skriva en klass som modellerar en enstaka dragning av lotterinummer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LotteriDragning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vecka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" datum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vecka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vecka\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datum\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar ett nytt LotteriDragning-objekt"}]},{"type":"text","value":"\nrunda1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteriDragning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut resultatet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"runda1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vecka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"runda1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" runda1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2021-01-02\n1\n4\n8\n12\n13\n14\n33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se ovan kan attributerna vara av vilken sort som helst. Här har varje LotteriDragnings objekt attributer av typerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bok","tmcname":"osa08-06_bok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":", som har variablerna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"forfattare"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"genre"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar"}]},{"type":"text","value":", tillsammans med en konstruktor som anger ursprungsvärden åt dessa attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska fungera englit följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Luciano Ramalho\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naventyr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Äkta Äventyr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Persson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"självbiografi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Boken "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"aventyr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"s genre är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"aventyr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"genre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Luciano Ramalho: Fluent Python (2015)\nBoken Äkta Äventyrs genre är självbiografi"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Skriv klasser","tmcname":"osa08-07_skriv_klasser"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa de tre klasser som anges nedan. Varje klass ska ha exakt samma namn och typer av attribut som anges i listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inkludera också en konstruktor i varje klass. Konstruktorn ska ta de ursprungliga värdena för attributen som sina argument, i den ordning som anges nedan."}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Klass Minneslista"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut rubrik (sträng)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut inlagg (lista)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ol","properties":{"start":2},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Klass Kund"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut id (sträng)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut saldo (decimaltal)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut rabatt (heltal)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ol","properties":{"start":3},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Klass Kabel"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut modell (sträng)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut langd (decimaltal)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut maximal_hastighet (heltal)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribut dubbelriktad (Boolesk)"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"anvandning-av-objekt-fran-egengjorda-klasser","style":"position:relative;"},"children":[{"type":"text","value":"Användning av objekt från egengjorda klasser"},{"type":"element","tagName":"a","properties":{"href":"#anvandning-av-objekt-fran-egengjorda-klasser","ariaLabel":"anvandning av objekt fran egengjorda klasser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objekt som bildas från dina egna klassdefinitioner skiljer sig inte från andra Python-objekt. De kan skickas som argument och returnera värden precis som alla andra objekt. Vi kan till exempel skriva hjälpfunktioner för att arbeta med bankkonton:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# funktionen skapar ett nytt bankkonto-objekt och returnerar det"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"oppna_konto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n nytt_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" nytt_konto\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# denna funktion lägger till det belopp som anges som argument till saldot som anges som argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillagg_pengar_pa_kontot"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" summa\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" oppna_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntillagg_pengar_pa_kontot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n500"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Skapa husdjur","tmcname":"osa08-07b_skapa_husdjur"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Husdjur"}]},{"type":"text","value":". Klassen har en konstruktor, som tar värden till attributen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ras"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fodelsear"}]},{"type":"text","value":" i den ordningen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa sedan en funktion med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nytt_husdjur(namn: str, ras: str, fodelsear: int)"}]},{"type":"text","value":" "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"utanför klassdefinitionen"}]},{"type":"text","value":", som skapar och returnerar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Husdjur"}]},{"type":"text","value":"-objekt (alltså av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Husdjur"}]},{"type":"text","value":"-klassen definierad ovan)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på funktionens användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"molly "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nytt_husdjur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Molly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hund\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2017"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ras"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Molly\nhund\n2017"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Äldre bok","tmcname":"osa08-08_aldre_bok"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aldre_bok(bok1: bok, bok2: bok)"}]},{"type":"text","value":", som får som argument två objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":". Funktionen berättar vilkendera bok som är äldre. Ifall båda böckerna är publicerades samma år ska meddelandet som skrivs ut vara annorlunda."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Luciano Ramalho\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naventyr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Äkta Äventyr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Persson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"självbiografi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnorma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Norma\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sofi Oksanen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"brott\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naldre_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aventyr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naldre_bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" norma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Äkta Äventyr är äldre, den publicerades 1956\nFluent Python och Norma publicerades 2015"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Genrens böcker","tmcname":"osa08-09_genrens_bocker"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"genrens_bocker(bocker: list, genre: str)"}]},{"type":"text","value":", som får som argument en lista med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bok"}]},{"type":"text","value":"-objekt såväl som en sträng som representerar genren."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen returnerar "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"en ny"}]},{"type":"text","value":" lista, som innehåller böckerna med den specifierade genren från den originella listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen fungerar på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Luciano Ramalho\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ohjelmointi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naventyr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Äkta Äventyr\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Persson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"självbiografi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnorma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Norma\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sofi Oksanen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"rikos\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbocker "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" aventyr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" norma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Snögubben\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jo Nesbø\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"brott\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2007"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Böcker i brottsgenren:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" bok "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" genrens_bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bocker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"brott\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"forfattare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Böcker i brottsgenren:\nSofi Oksanen: Norma\nJo Nesbø: Snögubben"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du hur du definierar dina egna klasser
  • \n
  • Kommer du att kunna skapa objekt baserade på klasser som du själv har definierat
  • \n
  • Vet du hur man skriver en konstruktor
  • \n
  • Är du bekant med parameternamnet self
  • \n
  • Vet du vad attribut är och hur de används
  • \n

En klass definieras med nyckelordet class. Syntaxen ser ut enligt följande:

class KlassNamn:\n    # Klassdefinitionen

Klasser namnges vanligtvis med kamelnotation. Detta innebär att alla ord i klassnamnet skrivs tillsammans, utan mellanslag, och att varje ord börjar med stor bokstav. Följande exempel på klassnamn följer denna konvention:

    \n
  • Veckodag
  • \n
  • Bankkonto
  • \n
  • BibliotekDatabas
  • \n
  • PythonKursBetyg
  • \n

En enskild klassdefinition bör representera en enskild helhet, vars innehåll bör vara sammanlänkat på något sätt. I mer komplicerade program kan klasser innehålla medlemmar av andra klasser. Till exempel kan klassen Kurs innehålla objekt av klasserna Lektion, ÖvningsTillfälle osv.

Låt oss ta en titt på en ram av en klassdefinition. Funktionerna saknas fortfarande vid denna tidpunkt.

class Bankkonto:\n    pass

Kodstycket ovan talar om för Python att vi här definierar en klass med namnet Bankkonto. Klassen innehåller ingen funktionalitet ännu, men vi kan fortfarande skapa ett objekt baserat på klassen.

Låt oss titta på ett program där två variabler läggs till ett Bankkonto-objekt: saldo och ägare. Alla variabler som är kopplade till ett objekt kallas dess attribut, eller mer specifikt, dataattribut, eller ibland instansvariabler.

De attribut som är kopplade till ett objekt kan nås via objektet:

class Bankkonto:\n    pass\n\npeters_konto = Bankkonto()\npeters_konto.agare = \"Peter Python\"\npeters_konto.saldo = 5.0\n\nprint(peters_konto.agare)\nprint(peters_konto.saldo)

Peter Python\n5.0

Dataattributen är endast tillgängliga via det objekt som de är kopplade till. Varje Bankkonto-objekt som skapas baserat på Bankkonto-klassen har sina egna värden kopplade till dataattributen. Dessa värden kan nås genom att hänvisa till objektet i fråga:

konto = Bankkonto()\nkonto.saldo = 155.50\n\nprint(konto.saldo) # Detta refererar till dataattributen saldo, som är kopplad till kontot\nprint(saldo) # DETTA ORSAKAR ETT FEL, eftersom det inte finns någon sådan oberoende variabel, och objektreferensen saknas

Att lägga till en konstruktor

I exemplet ovan såg vi att en ny instans av en klass kan skapas genom att anropa klassens konstruktormetod på följande sätt: KlassensNamn(). Ovan kopplade vi sedan dataattribut till objektet separat, men det är ofta bekvämare att skicka dessa initiala värden för attribut direkt när objektet skapas. I exemplet ovan hade vi först ett Bankkonto-objekt utan dessa attribut, och attributen existerade först efter att de uttryckligen hade deklarerats.

Att deklarera attribut utanför konstruktorn leder till en situation där olika instanser av samma klass kan ha olika attribut. Följande kod ger ett fel eftersom vi nu har ett annat Bankkonto-objekt, pernillas_konto, som inte innehåller samma attribut:

class Bankkonto:\n    pass\n\npeters_konto = Bankkonto()\npeters_konto.agare = \"Peter\"\npeters_konto.saldo = 1400\n\npernillas_konto = Bankkonto()\npernillas_konto.agare = \"Pernilla\"\n\nprint(peters_konto.saldo)\nprint(pernillas_konto.saldo) # DETTA ORSAKAR ETT FEL

Så istället för att deklarera attribut efter att varje instans av klassen har skapats, är det oftast en bättre idé att initialisera attributens värden när klasskonstruktorn anropas. Eftersom klassdefinitionen Bankkonto för närvarande bara är en ram, antas konstruktormetoden implicit av Python-tolkaren, men det är möjligt att definiera egna konstruktormetoder, och det är precis vad vi kommer att göra nu.

En konstruktormetod är en metoddeklaration med det speciella namnet __init__, som vanligtvis inkluderas i början av en klassdefinition.

Låt oss ta en titt på en Bankkonto-klass med en konstruktormetod tillagd:

class Bankkonto:\n\n    # Konstruktorn\n    def __init__(self, saldo: float, agare: str):\n        self.saldo = saldo\n        self.agare = agare

Namnet på konstruktorsmetoden är alltid __init__. Lägg märke till de två understrecken på båda sidorna av ordet init.

Den första parametern i en konstruktorsdefinition heter alltid self. Detta refererar till själva objektet och är nödvändigt för att deklarera alla attribut som är knutna till objektet. Tilldelningen

self.saldo = saldo

tilldelar objektets saldoattribut den balans som mottagits som argument. Det är vanligt att använda samma variabelnamn för parametrarna och dataattributen som definieras i en konstruktor, men variabelnamnen self.saldo och saldo ovan hänvisar till två olika variabler:

    \n
  • \n

    Variabeln self.saldo är ett attribut för objektet. Varje Bankkonto-objekt har sitt eget saldo.

    \n
  • \n
  • \n

    Variabeln saldo är en parameter i konstruktorsmetoden __init__. Dess värde sätts till det värde som skickas som argument till metoden när konstruktorn kallas (dvs. när en ny insctance av klassen skapas).

    \n
  • \n

Nu när vi har definierat parametrarna för konstruktorsmetoden kan vi skicka de önskade initiala värdena för dataattributen som argument när ett nytt objekt skapas:

class Bankkonto:\n\n    # Konstruktorn\n    def __init__(self, saldo: float, agare: str):\n        self.saldo = saldo\n        self.agare = agare\n\n# Parametern self ges inget värde, utan Python ger ett sådant automatiskt\npeters_konto = Bankkonto(100, \"Peter Python\")\npernillas_konto = Bankkonto(20000, \"Pernilla Pythonson\")\n\nprint(peters_konto.saldo)\nprint(pernillas_konto.saldo)

100\n20000

Det är nu mycket enklare att arbeta med Bankkonto-objekten, eftersom värdena kan skickas när objektet skapas, och de två separata instanserna kan hanteras på ett mer förutsägbart och enhetligt sätt. Att deklarera dataattribut i konstruktorn säkerställer också att attributen verkligen deklareras, och att de önskade initiala värdena alltid ges av programmeraren som använder klassen.

Det är fortfarande möjligt att ändra de initiala värdena för dataattributen senare i programmet:

class Bankkonto:\n\n    # Konstruktorn\n    def __init__(self, saldo: float, agare: str):\n        self.saldo = saldo\n        self.agare = agare\n\npeters_konto = Bankkonto(100, \"Peter Python\")\nprint(peters_konto.saldo)\n\n# Ändra saldot till 1500\npeters_konto.saldo = 1500\nprint(peters_konto.saldo)\n\n# Vi lägger till 2000 till saldot\npeters_konto.saldo += 2000\nprint(peters_konto.saldo)

100\n1500\n3500

Låt oss titta på ett annat exempel på klasser och objekt. Vi ska skriva en klass som modellerar en enstaka dragning av lotterinummer:

from datetime import date\n\nclass LotteriDragning:\n\n    def __init__(self, vecka: int, datum: date, nummer: list):\n        self.vecka = vecka\n        self.datum = datum\n        self.nummer = nummer\n\n\n# Vi skapar ett nytt LotteriDragning-objekt\nrunda1 = LotteriDragning(1, date(2021, 1, 2), [1,4,8,12,13,14,33])\n\n# Vi skriver ut resultatet\nprint(runda1.vecka)\nprint(runda1.datum)\n\nfor nummer in runda1.nummer:\n    print(nummer)

1\n2021-01-02\n1\n4\n8\n12\n13\n14\n33

Som du kan se ovan kan attributerna vara av vilken sort som helst. Här har varje LotteriDragnings objekt attributer av typerna list och date.

Skapa klassen Bok, som har variablerna namn, forfattare, genre och ar, tillsammans med en konstruktor som anger ursprungsvärden åt dessa attribut.

Klassen ska fungera englit följande:

python = Bok(\"Fluent Python\", \"Luciano Ramalho\", \"programmering\", 2015)\naventyr = Bok(\"Äkta Äventyr\", \"Petter Persson\", \"självbiografi\", 1956)\n\nprint(f\"{python.forfattare}: {python.namn} ({python.ar})\")\nprint(f\"Boken {aventyr.namn}s genre är {aventyr.genre}\")

Luciano Ramalho: Fluent Python (2015)\nBoken Äkta Äventyrs genre är självbiografi

Skapa de tre klasser som anges nedan. Varje klass ska ha exakt samma namn och typer av attribut som anges i listan.

Inkludera också en konstruktor i varje klass. Konstruktorn ska ta de ursprungliga värdena för attributen som sina argument, i den ordning som anges nedan.

    \n
  1. Klass Minneslista
  2. \n
    \n
  • attribut rubrik (sträng)
  • \n
  • attribut inlagg (lista)
  • \n
    \n
  1. Klass Kund
  2. \n
    \n
  • attribut id (sträng)
  • \n
  • attribut saldo (decimaltal)
  • \n
  • attribut rabatt (heltal)
  • \n
    \n
  1. Klass Kabel
  2. \n
    \n
  • attribut modell (sträng)
  • \n
  • attribut langd (decimaltal)
  • \n
  • attribut maximal_hastighet (heltal)
  • \n
  • attribut dubbelriktad (Boolesk)
  • \n

Användning av objekt från egengjorda klasser

Objekt som bildas från dina egna klassdefinitioner skiljer sig inte från andra Python-objekt. De kan skickas som argument och returnera värden precis som alla andra objekt. Vi kan till exempel skriva hjälpfunktioner för att arbeta med bankkonton:

# funktionen skapar ett nytt bankkonto-objekt och returnerar det\ndef oppna_konto(namn: str):\n    nytt_konto =  Bankkonto(0, namn)\n    return nytt_konto\n\n# denna funktion lägger till det belopp som anges som argument till saldot som anges som argument\ndef tillagg_pengar_pa_kontot(konto: Bankkonto, summa: int):\n    konto.saldo += summa\n\npeters_konto = oppna_konto(\"Peter Python\")\nprint(peters_konto.saldo)\n\ntillagg_pengar_pa_kontot(peters_konto, 500)\n\nprint(peters_konto.saldo)

0\n500

Definiera klassen Husdjur. Klassen har en konstruktor, som tar värden till attributen namn, ras och fodelsear i den ordningen.

Skapa sedan en funktion med namnet nytt_husdjur(namn: str, ras: str, fodelsear: int) utanför klassdefinitionen, som skapar och returnerar ett Husdjur-objekt (alltså av Husdjur-klassen definierad ovan).

Exempel på funktionens användning:

molly = nytt_husdjur(\"Molly\", \"hund\", 2017)\nprint(molly.namn)\nprint(molly.ras)\nprint(molly.fodelsear)

Molly\nhund\n2017

Skapa funktionen aldre_bok(bok1: bok, bok2: bok), som får som argument två objekt av typen Bok. Funktionen berättar vilkendera bok som är äldre. Ifall båda böckerna är publicerades samma år ska meddelandet som skrivs ut vara annorlunda.

Funktionen fungerar enligt följande:

python = Bok(\"Fluent Python\", \"Luciano Ramalho\", \"programmering\", 2015)\naventyr = Bok(\"Äkta Äventyr\", \"Petter Persson\", \"självbiografi\", 1956)\nnorma = Bok(\"Norma\", \"Sofi Oksanen\", \"brott\", 2015)\n\naldre_bok(python, aventyr)\naldre_bok(python, norma)

Äkta Äventyr är äldre, den publicerades 1956\nFluent Python och Norma publicerades 2015

Skapa funktionen genrens_bocker(bocker: list, genre: str), som får som argument en lista med Bok-objekt såväl som en sträng som representerar genren.

Funktionen returnerar en ny lista, som innehåller böckerna med den specifierade genren från den originella listan.

Funktionen fungerar på följande sätt:

python = Bok(\"Fluent Python\", \"Luciano Ramalho\", \"ohjelmointi\", 2015)\naventyr = Bok(\"Äkta Äventyr\", \"Petter Persson\", \"självbiografi\", 1956)\nnorma = Bok(\"Norma\", \"Sofi Oksanen\", \"rikos\", 2015)\n\nbocker = [python, aventyr, norma, Bok(\"Snögubben\", \"Jo Nesbø\", \"brott\", 2007)]\n\nprint(\"Böcker i brottsgenren:\")\nfor bok in genrens_bocker(bocker, \"brott\"):\n    print(f\"{bok.forfattare}: {bok.namn}\")

Böcker i brottsgenren:\nSofi Oksanen: Norma\nJo Nesbø: Snögubben

","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/3-egna-klasser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-8/4-metoder-i-egna-klasser/page-data.json b/page-data/osa-8/4-metoder-i-egna-klasser/page-data.json index 4748c5155..19ae5b38b 100644 --- a/page-data/osa-8/4-metoder-i-egna-klasser/page-data.json +++ b/page-data/osa-8/4-metoder-i-egna-klasser/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-8/4-metoder-i-egna-klasser", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur klassmetoder fungerar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kan du skriva nya metoder i dina egna klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att förstå begreppen inkapsling och klient inom objektorienterad programmering"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klasser som endast innehåller dataattribut skiljer sig inte så mycket från ordlistor. Nedan kan du se två sätt att modellera ett bankkonto, först med en klassdefinition och sedan med hjälp av en ordlista."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Exempel 1: bankkoto med klassdefinition"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" arsranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" arsranta\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Exempel2: bankkonto med ordlista"}]},{"type":"text","value":"\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kontonummer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"agare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"saldo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"arsranta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med en ordlista är implementeringen mycket kortare och enklare. Med en klass är strukturen däremot mer \"hårt bunden\", så vi kan förvänta oss att alla "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":"-objekt är strukturellt lika. Dessutom är en klass också namngiven. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":" refereras till när ett nytt bankkonto skapas, och objektets typ är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":", inte dict."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En annan stor fördel med klasser är att de förutom data även kan innehålla funktionalitet. En av de vägledande principerna för objektorienterad programmering är att ett objekt används för att komma åt både den data som är kopplad till ett objekt och funktionaliteten för att bearbeta denna data."}]},{"type":"element","tagName":"h2","properties":{"id":"metoder-i-klasser","style":"position:relative;"},"children":[{"type":"text","value":"Metoder i klasser"},{"type":"element","tagName":"a","properties":{"href":"#metoder-i-klasser","ariaLabel":"metoder i klasser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En metod är ett underprogram eller en funktion som är knuten till en specifik klass. Vanligtvis påverkar en metod bara ett enda objekt. En metod definieras inom klassdefinitionen och den kan komma åt klassens dataattribut precis som vilken annan variabel som helst."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss fortsätta med klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":" som introducerades ovan. Nedan har vi en ny metod som lägger till ränta på kontot:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" arsranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" arsranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden lägger till den årliga räntat till saldot"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta\n\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1522.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"text","value":" multiplicerar saldot på kontot med den årliga ränteprocenten och lägger sedan till resultatet till det aktuella saldot. Metoden verkar bara på det objekt som den anropas på."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss se hur detta fungerar när vi har skapat flera instanser av klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Klassen Bankkonto är definierat såsom i förra exemplet"}]},{"type":"text","value":"\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npernillas_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"99999-999\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.05"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npers_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1111-222\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Per Persson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.001"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi tillsätter ränta till Peters och Pernillas konton, men inte Pers"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut alla"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pers_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1522.5\n1575.0\n1500.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se ovan läggs den årliga räntan endast till på de konton som metoden anropas på. Eftersom den årliga räntan är olika för Peters och Paulas konton, blir resultatet olika för dessa två konton. Saldot på Pernillas konto ändras inte, eftersom metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"text","value":" inte anropas på objektet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pernillas_konto"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"inkapsling","style":"position:relative;"},"children":[{"type":"text","value":"Inkapsling"},{"type":"element","tagName":"a","properties":{"href":"#inkapsling","ariaLabel":"inkapsling permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inom objektorienterad programmering dyker ordet klient upp då och då. Det används för att hänvisa till ett kodavsnitt som skapar ett objekt och använder den tjänst som tillges av dess metoder. När data som finns i ett objekt endast används genom de metoder som det tillges, garanteras objektets interna integritet. I praktiken innebär detta att t.ex. en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":"-klass erbjuder metoder för att hantera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":"-attributet, så att saldot aldrig nås direkt av klienten. Dessa metoder kan sedan verifiera att saldot till exempel inte tillåts gå under noll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på hur detta skulle fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" arsranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" arsranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden tillsätter den årliga räntat till saldot av kontot"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den här metoden \"tar ut\" pengar från kontot"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar True ifall det lyckades, och False ifall det misslyckades"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"uttag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" uttagssumma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" uttagssumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" uttagssumma\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uttag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades, kontots saldo är nu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades inte, saldot var otillräckligt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi försöker på nytt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uttag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades, kontots saldo är nu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades inte, saldot var otillräckligt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uttaget lyckades, kontots saldo är nu 500.0\nUttaget lyckades inte, saldot var otillräckligt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att bibehålla objektets interna integritet och erbjuda lämpliga metoder för att säkerställa detta kallas inkapsling. Tanken är att objektets inre arbete är dolt för klienten, men objektet erbjuder metoder som kan användas för att komma åt den data som lagras i objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att lägga till en metod innebär inte att attributet automatiskt döljs. Även om klassdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":" innehåller metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttag"}]},{"type":"text","value":" för att ta ut pengar, kan klientkoden fortfarande komma åt och ändra attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":" direkt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"peters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi försöker lyfta 2000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uttag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades, kontots saldo är nu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades inte, saldot var otillräckligt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi \"tvingar\" ett uttag på 2000"}]},{"type":"text","value":"\n peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saldot är nu:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är möjligt att dölja dataattributen från klientkoden, vilket kan bidra till att lösa detta problem. Vi återkommer till detta ämne i nästa del."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Minskande räknare","tmcname":"osa08-10_minskande_raknare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna övning har flera delar. Varje del är värt ett poäng och kan lämnas in separat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller en delvis ifylld klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MinskandeRaknare"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"MinskandeRaknare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" borjan_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan_varde\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut_varde"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"minska"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# resten av metoderna här"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen används på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MinskandeRaknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"värde: 10\nvärde: 9\nvärde: 8"}]}]},{"type":"element","tagName":"h3","properties":{"id":"minska-raknarens-varde","style":"position:relative;"},"children":[{"type":"text","value":"Minska räknarens värde"},{"type":"element","tagName":"a","properties":{"href":"#minska-raknarens-varde","ariaLabel":"minska raknarens varde permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Komplettera metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minska"}]},{"type":"text","value":" som definieras i mallen, så att den minskar värdet som lagras i räknaren med ett. Se exemplet ovan för förväntat beteende."}]},{"type":"element","tagName":"h3","properties":{"id":"raknaren-far-inte-ha-ett-negativt-varde","style":"position:relative;"},"children":[{"type":"text","value":"Räknaren får inte ha ett negativt värde"},{"type":"element","tagName":"a","properties":{"href":"#raknaren-far-inte-ha-ett-negativt-varde","ariaLabel":"raknaren far inte ha ett negativt varde permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till funktionalitet till din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minska"}]},{"type":"text","value":"-metod, så att räknarens värde aldrig når negativa värden. Om värdet på räknaren är 0 kommer den inte att minskas ytterligare."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MinskandeRaknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"värde: 2\nvärde: 1\nvärde: 0\nvärde: 0"}]}]},{"type":"element","tagName":"h3","properties":{"id":"nollande-av-raknaren","style":"position:relative;"},"children":[{"type":"text","value":"Nollande av räknaren"},{"type":"element","tagName":"a","properties":{"href":"#nollande-av-raknaren","ariaLabel":"nollande av raknaren permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nolla"}]},{"type":"text","value":" som sätter värdet på räknaren till 0:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MinskandeRaknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nolla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"värde: 100\nvärde: 0"}]}]},{"type":"element","tagName":"h3","properties":{"id":"aterstallning-av-raknaren","style":"position:relative;"},"children":[{"type":"text","value":"Återställning av räknaren"},{"type":"element","tagName":"a","properties":{"href":"#aterstallning-av-raknaren","ariaLabel":"aterstallning av raknaren permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aterstall_ursprungligt_varde()"}]},{"type":"text","value":", som återställer räknaren till sitt ursprungliga tillstånd:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MinskandeRaknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"aterstall_ursprungligt_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"värde: 51\nvärde: 55"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som avslutning på detta avsnitt tittar vi på en klass som modellerar en spelares personliga bästa. Klassdefinitionen innehåller separata valideringsmetoder som kontrollerar att de argument som skickas är giltiga. Metoderna anropas redan i konstruktorn. Detta säkerställer att det skapade objektet är sunt internt."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PersonligtRekord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Standardvärden"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1900"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spelare\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dtm_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" poang\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Hjälparmetoder som kollar att argumenten är giltiga"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Namnet ska vara minst två tecken"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"dtm_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ett undantag ifall datumet inte är giltigt"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"poang_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n resultat1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PersonligtRekord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"235"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Datumet är inte giltigt"}]},{"type":"text","value":"\n resultat2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PersonligtRekord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4555"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriver ut standardvärdet 1900-01-01"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"235\nPeter\n2020-11-01\n4555\nPernilla\n1900-01-01"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan anropades även hjälpmetoderna via parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" när de användes i konstruktorn. Det är också möjligt att inkludera /statiska/ metoddefinitioner i klassdefinitioner. Dessa är metoder som kan anropas utan att det någonsin skapas en instans av klassen. Vi återkommer till detta i nästa del."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" används endast när man hänvisar till /objektets egenskaper som en instans av klassen/. Det gäller både dataattributen och de metoder som är knutna till ett objekt. För att göra terminologin mer förvirrande kallas dataattributen och metoderna tillsammans ibland helt enkelt för objektets attribut, vilket är anledningen till att vi i detta material ofta har angett dataattribut när vi menar de variabler som definieras inom klassen. Det är här terminologin hos vissa Python-programmerare skiljer sig något från den terminologi som mer allmänt används inom objektorienterad programmering, där attribut vanligtvis bara hänvisar till dataattributen hos ett objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att skapa lokala variabler inom metoddefinitioner utan att hänvisa till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". Detta bör du göra om det inte finns något behov av att komma åt variablerna utanför metoden. Lokala variabler inom metoder har inga speciella nyckelord; de används precis som alla vanliga variabler som du har stött på hittills. ."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här skulle det till exempel fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bonuskort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Nu är variabeln bonus en lokal variabel, inte ett"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# data attribut till objektet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den kan inte nås genom ett objekt"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.25"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" bonus\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_superbonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Variabeln superbonus är också en lokal variabel."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vanligtvis är hjälpvariabler lokala variabler eftersom"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det inte finns något behov av att komma åt dem från andra"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metoder i klassen eller direkt via ett objekt."}]},{"type":"text","value":"\n superbonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.5"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" superbonus\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Bonuskort(namn="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", saldo="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"För- och efternamn","tmcname":"osa08-10b_for_och_efternamn"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":", som får "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"endast ett attribut"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", som ges till konstruktorn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa dessutom två metoder:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ge_fornamn"}]},{"type":"text","value":" returnerar personens förnamn och metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ge_efternamn"}]},{"type":"text","value":" på samma sätt personens efternamn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att det namn som skickas till konstruktorn kommer att innehålla endast för- och efternamn åtskilda med ett mellanslag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n peter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ge_fornamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ge_efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pauli "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernilla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ge_fornamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernilla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ge_efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter\nPython\nPernilla\nPythonson"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Nummerstatistik","tmcname":"osa08-11_nummerstatistik"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen ska du skapa ett program för att arbeta med siffror, på samma sätt som i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-8/4-metoder-i-egna-klasser","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"slutet av modul 2"}]},{"type":"text","value":" i kursen Introduktion till Programmering. Den här gången ska du definiera en klass för ändamålet."}]},{"type":"element","tagName":"h3","properties":{"id":"mangden-nummer","style":"position:relative;"},"children":[{"type":"text","value":"Mängden nummer"},{"type":"element","tagName":"a","properties":{"href":"#mangden-nummer","ariaLabel":"mangden nummer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Nummerstatistik"}]},{"type":"text","value":", med följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"text","value":" lägger till ett nytt nummer till statistiken"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mangden_nummer"}]},{"type":"text","value":" returnerar mängden nummer som har tillsatts"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här skedet finns det inget behov av att lagra själva siffrorna i någon datastruktur. Det räcker att bara komma ihåg hur många som har lagts till. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"text","value":" tar emot ett argument, men det finns inget behov av att bearbeta det faktiska värdet på något sätt ännu."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen borde ha följande struktur:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"mangden_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"statistik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" NummerStatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden nummer:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" statistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangden_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mängden nummer: 4"}]}]},{"type":"element","tagName":"h3","properties":{"id":"summa-och-medeltal","style":"position:relative;"},"children":[{"type":"text","value":"Summa och medeltal"},{"type":"element","tagName":"a","properties":{"href":"#summa-och-medeltal","ariaLabel":"summa och medeltal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tillsätt följande metoder till klassdefinitionen:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" returnerar summan av talen som satts till (en tom statistik returnerar 0)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"medeltat"}]},{"type":"text","value":" returnerar medeltalet av numren (en tom statistiks medeltal är 0)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"statistik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" NummerStatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden nummer:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" statistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangden_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summa:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" statistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medeltal:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" statistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mängd: 4\nSumma: 11\nMedeltal: 2.75"}]}]},{"type":"element","tagName":"h3","properties":{"id":"anvandarinmatning","style":"position:relative;"},"children":[{"type":"text","value":"Användarinmatning"},{"type":"element","tagName":"a","properties":{"href":"#anvandarinmatning","ariaLabel":"anvandarinmatning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett huvudprogram som fortsätter att fråga användaren om heltal tills användaren skriver in -1. Programmet ska sedan skriva ut summan och medelvärdet av de inmatade talen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ditt program ska använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"text","value":"-objekt för att hålla koll på numren som läggs till."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Du behöver inte ändra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"text","value":"-klassen, i denna del, använd en instans av klassen för att slutföra denna del."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS2: Ditt huvudprogram ska inte vara inuti ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":"-block, annars fungerar inte testen."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange nummer:\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSumma: 13\nMedeltal: 3.25"}]}]},{"type":"element","tagName":"h3","properties":{"id":"flera-summor","style":"position:relative;"},"children":[{"type":"text","value":"Flera summor"},{"type":"element","tagName":"a","properties":{"href":"#flera-summor","ariaLabel":"flera summor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bygg på ditt huvudprogram så att det också separat räknar summan av de jämna och udda tal som läggs till."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Ändra inte din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"text","value":"-klassdefinition i denna del av övningen heller. Definiera i stället tre "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"text","value":"-objekt. Ett av dem ska hålla reda på alla siffror, ett annat ska hålla reda på de jämna siffrorna och det tredje ska hålla reda på de udda siffror som skrivs in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS2: Ditt huvudprogram ska inte vara inuti ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":"-block, annars fungerar inte testen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange nummer:\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSumma: 13\nMedeltal: 3.25\nJämna talens summa: 8\nUdda talens summa: 5"}]}]}]}]},"html":"

Efter den här delen:

    \n
  • Vet du hur klassmetoder fungerar
  • \n
  • Kan du skriva nya metoder i dina egna klasser
  • \n
  • Kommer du att förstå begreppen inkapsling och klient inom objektorienterad programmering
  • \n

Klasser som endast innehåller dataattribut skiljer sig inte så mycket från ordlistor. Nedan kan du se två sätt att modellera ett bankkonto, först med en klassdefinition och sedan med hjälp av en ordlista.

# Exempel 1: bankkoto med klassdefinition\nclass Bankkonto:\n\n    def __init__(self, kontonummer: str, agare: str, saldo: float, arsranta: float):\n        self.kontonummer = kontonummer\n        self.agare = agare\n        self.saldo = saldo\n        self.arsranta = arsranta\n\npeters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)
# Exempel2: bankkonto med ordlista\npeters_konto = {\"kontonummer\": \"12345-678\", \"agare\": \"Peter Python\", \"saldo\": 1500.0, \"arsranta\": 0.0}

Med en ordlista är implementeringen mycket kortare och enklare. Med en klass är strukturen däremot mer \"hårt bunden\", så vi kan förvänta oss att alla Bankkonto-objekt är strukturellt lika. Dessutom är en klass också namngiven. Klassen Bankkonto refereras till när ett nytt bankkonto skapas, och objektets typ är Bankkonto, inte dict.

En annan stor fördel med klasser är att de förutom data även kan innehålla funktionalitet. En av de vägledande principerna för objektorienterad programmering är att ett objekt används för att komma åt både den data som är kopplad till ett objekt och funktionaliteten för att bearbeta denna data.

Metoder i klasser

En metod är ett underprogram eller en funktion som är knuten till en specifik klass. Vanligtvis påverkar en metod bara ett enda objekt. En metod definieras inom klassdefinitionen och den kan komma åt klassens dataattribut precis som vilken annan variabel som helst.

Låt oss fortsätta med klassen Bankkonto som introducerades ovan. Nedan har vi en ny metod som lägger till ränta på kontot:

class Bankkonto:\n\n    def __init__(self, kontonummer: str, agare: str, saldo: float, arsranta: float):\n        self.kontonummer = kontonummer\n        self.agare = agare\n        self.saldo = saldo\n        self.arsranta = arsranta\n\n    # Metoden lägger till den årliga räntat till saldot\n    def tillsatt_ranta(self):\n        self.saldo += self.saldo * self.arsranta\n\n\npeters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\npeters_konto.tillsatt_ranta()\nprint(peters_konto.saldo)

1522.5

Metoden tillsatt_ranta multiplicerar saldot på kontot med den årliga ränteprocenten och lägger sedan till resultatet till det aktuella saldot. Metoden verkar bara på det objekt som den anropas på.

Låt oss se hur detta fungerar när vi har skapat flera instanser av klassen:

# Klassen Bankkonto är definierat såsom i förra exemplet\n\npeters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\npernillas_konto = Bankkonto(\"99999-999\", \"Pernilla Pythonson\", 1500.0, 0.05)\npers_konto = Bankkonto(\"1111-222\", \"Per Persson\", 1500.0, 0.001)\n\n# Vi tillsätter ränta till Peters och Pernillas konton, men inte Pers\npeters_konto.tillsatt_ranta()\npernillas_konto.tillsatt_ranta()\n\n# Vi skriver ut alla\nprint(peters_konto.saldo)\nprint(pernillas_konto.saldo)\nprint(pers_konto.saldo)

1522.5\n1575.0\n1500.0

Som du kan se ovan läggs den årliga räntan endast till på de konton som metoden anropas på. Eftersom den årliga räntan är olika för Peters och Paulas konton, blir resultatet olika för dessa två konton. Saldot på Pernillas konto ändras inte, eftersom metoden tillsatt_ranta inte anropas på objektet pernillas_konto.

Inkapsling

Inom objektorienterad programmering dyker ordet klient upp då och då. Det används för att hänvisa till ett kodavsnitt som skapar ett objekt och använder den tjänst som tillges av dess metoder. När data som finns i ett objekt endast används genom de metoder som det tillges, garanteras objektets interna integritet. I praktiken innebär detta att t.ex. en Bankkonto-klass erbjuder metoder för att hantera saldo-attributet, så att saldot aldrig nås direkt av klienten. Dessa metoder kan sedan verifiera att saldot till exempel inte tillåts gå under noll.

Ett exempel på hur detta skulle fungera:

class Bankkonto:\n\n    def __init__(self, kontonummer: str, agare: str, saldo: float, arsranta: float):\n        self.kontonummer = kontonummer\n        self.agare = agare\n        self.saldo = saldo\n        self.arsranta = arsranta\n\n    # Metoden tillsätter den årliga räntat till saldot av kontot\n    def tillsatt_ranta(self):\n        self.saldo += self.saldo * self.arsranta\n\n    # Den här metoden \"tar ut\" pengar från kontot\n    # Metoden returnerar True ifall det lyckades, och False ifall det misslyckades\n    def uttag(self, uttagssumma: float):\n        if uttagssumma <= self.saldo:\n            self.saldo -= uttagssumma\n            return True\n\n        return False\n\npeters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\n\nif peters_konto.uttag(1000):\n    print(\"Uttaget lyckades, kontots saldo är nu\", peters_konto.saldo)\nelse:\n    print(\"Uttaget lyckades inte, saldot var otillräckligt\")\n\n# Vi försöker på nytt\nif peters_konto.uttag(1000):\n    print(\"Uttaget lyckades, kontots saldo är nu\", peters_konto.saldo)\nelse:\n    print(\"Uttaget lyckades inte, saldot var otillräckligt\")

Uttaget lyckades, kontots saldo är nu 500.0\nUttaget lyckades inte, saldot var otillräckligt

Att bibehålla objektets interna integritet och erbjuda lämpliga metoder för att säkerställa detta kallas inkapsling. Tanken är att objektets inre arbete är dolt för klienten, men objektet erbjuder metoder som kan användas för att komma åt den data som lagras i objektet.

Att lägga till en metod innebär inte att attributet automatiskt döljs. Även om klassdefinitionen Bankkonto innehåller metoden uttag för att ta ut pengar, kan klientkoden fortfarande komma åt och ändra attributet saldo direkt:

peters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\n\n# Vi försöker lyfta 2000\nif peters_konto.uttag(2000):\n    print(\"Uttaget lyckades, kontots saldo är nu\", peters_konto.saldo)\nelse:\n    print(\"Uttaget lyckades inte, saldot var otillräckligt\")\n\n    # Vi \"tvingar\" ett uttag på 2000\n    peters_konto.saldo -= 2000\n\nprint(\"Saldot är nu:\", peters_konto.saldo)

Det är möjligt att dölja dataattributen från klientkoden, vilket kan bidra till att lösa detta problem. Vi återkommer till detta ämne i nästa del.

Denna övning har flera delar. Varje del är värt ett poäng och kan lämnas in separat.

Övningsmallen innehåller en delvis ifylld klass MinskandeRaknare:

class MinskandeRaknare:\n    def __init__(self, borjan_varde: int):\n        self.varde = borjan_varde\n\n    def skriv_ut_varde(self):\n        print(\"värde:\", self.varde)\n\n    def minska(self):\n        pass\n\n    # resten av metoderna här

Klassen används på följande sätt:

raknare = MinskandeRaknare(10)\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()

värde: 10\nvärde: 9\nvärde: 8

Minska räknarens värde

Komplettera metoden minska som definieras i mallen, så att den minskar värdet som lagras i räknaren med ett. Se exemplet ovan för förväntat beteende.

Räknaren får inte ha ett negativt värde

Lägg till funktionalitet till din minska-metod, så att räknarens värde aldrig når negativa värden. Om värdet på räknaren är 0 kommer den inte att minskas ytterligare.

raknare = MinskandeRaknare(2)\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()

värde: 2\nvärde: 1\nvärde: 0\nvärde: 0

Nollande av räknaren

Skapa en metod nolla som sätter värdet på räknaren till 0:

raknare = MinskandeRaknare(100)\nraknare.skriv_ut_varde()\nraknare.nolla()\nraknare.skriv_ut_varde()

värde: 100\nvärde: 0

Återställning av räknaren

Skapa en metod aterstall_ursprungligt_varde(), som återställer räknaren till sitt ursprungliga tillstånd:

raknare = MinskandeRaknare(55)\nraknare.minska()\nraknare.minska()\nraknare.minska()\nraknare.minska()\nraknare.skriv_ut_varde()\nraknare.aterstall_ursprungligt_varde()\nraknare.skriv_ut_varde()

värde: 51\nvärde: 55

Som avslutning på detta avsnitt tittar vi på en klass som modellerar en spelares personliga bästa. Klassdefinitionen innehåller separata valideringsmetoder som kontrollerar att de argument som skickas är giltiga. Metoderna anropas redan i konstruktorn. Detta säkerställer att det skapade objektet är sunt internt.

from datetime import date\n\nclass PersonligtRekord:\n\n    def __init__(self, spelare: str, dag: int, manad: int, ar: int, poang: int):\n        # Standardvärden\n        self.spelare = \"\"\n        self.datum = date(1900, 1, 1)\n        self.poang = 0\n\n        if self.namn_ok(spelare):\n            self.spelare = spelare\n\n        if self.dtm_ok(dag, manad, ar):\n            self.datum = date(ar, manad, dag)\n\n        if self.poang_ok(poang):\n            self.poang = poang\n\n    # Hjälparmetoder som kollar att argumenten är giltiga\n    def namn_ok(self, namn: str):\n        return len(namn) >= 2 # Namnet ska vara minst två tecken\n\n    def dtm_ok(self, dag, manad, ar):\n        try:\n            date(ar, manad, dag)\n            return True\n        except:\n            # Ett undantag ifall datumet inte är giltigt\n            return False\n\n    def poang_ok(self, poang):\n        return poang >= 0\n\nif __name__ == \"__main__\":\n    resultat1 = PersonligtRekord(\"Peter\", 1, 11, 2020, 235)\n    print(resultat1.poang)\n    print(resultat1.spelare)\n    print(resultat1.datum)\n\n    # Datumet är inte giltigt\n    resultat2 = PersonligtRekord(\"Pernilla\", 4, 13, 2019, 4555)\n    print(resultat2.poang)\n    print(resultat2.spelare)\n    print(resultat2.datum) # Skriver ut standardvärdet 1900-01-01

235\nPeter\n2020-11-01\n4555\nPernilla\n1900-01-01

I exemplet ovan anropades även hjälpmetoderna via parameternamnet self när de användes i konstruktorn. Det är också möjligt att inkludera /statiska/ metoddefinitioner i klassdefinitioner. Dessa är metoder som kan anropas utan att det någonsin skapas en instans av klassen. Vi återkommer till detta i nästa del.

Parameternamnet self används endast när man hänvisar till /objektets egenskaper som en instans av klassen/. Det gäller både dataattributen och de metoder som är knutna till ett objekt. För att göra terminologin mer förvirrande kallas dataattributen och metoderna tillsammans ibland helt enkelt för objektets attribut, vilket är anledningen till att vi i detta material ofta har angett dataattribut när vi menar de variabler som definieras inom klassen. Det är här terminologin hos vissa Python-programmerare skiljer sig något från den terminologi som mer allmänt används inom objektorienterad programmering, där attribut vanligtvis bara hänvisar till dataattributen hos ett objekt.

Det är också möjligt att skapa lokala variabler inom metoddefinitioner utan att hänvisa till self. Detta bör du göra om det inte finns något behov av att komma åt variablerna utanför metoden. Lokala variabler inom metoder har inga speciella nyckelord; de används precis som alla vanliga variabler som du har stött på hittills. .

Så här skulle det till exempel fungera:

class Bonuskort:\n    def __init__(self, namn: str, saldo: float):\n        self.namn = namn\n        self.saldo = saldo\n\n    def tillsatt_bonus(self):\n        # Nu är variabeln bonus en lokal variabel, inte ett\n        # data attribut till objektet\n        # Den kan inte nås genom ett objekt\n        bonus = self.saldo * 0.25\n        self.saldo += bonus\n\n    def tillsatt_superbonus(self):\n        # Variabeln superbonus är också en lokal variabel.\n        # Vanligtvis är hjälpvariabler lokala variabler eftersom\n        # det inte finns något behov av att komma åt dem från andra\n        # metoder i klassen eller direkt via ett objekt.\n        superbonus = self.saldo * 0.5\n        self.saldo += superbonus\n\n    def __str__(self):\n        return f\"Bonuskort(namn={self.namn}, saldo={self.saldo})\"

Skapa en klass Person, som får endast ett attribut namn, som ges till konstruktorn.

Skapa dessutom två metoder:

Metoden ge_fornamn returnerar personens förnamn och metoden ge_efternamn på samma sätt personens efternamn.

Du kan anta att det namn som skickas till konstruktorn kommer att innehålla endast för- och efternamn åtskilda med ett mellanslag.

Exempel på användning:

if __name__ == \"__main__\":\n    peter = Person(\"Peter Python\")\n    print(peter.ge_fornamn())\n    print(peter.ge_efternamn())\n\n    pauli = Person(\"Pernilla Pythonson\")\n    print(pernilla.ge_fornamn())\n    print(pernilla.ge_efternamn())

Peter\nPython\nPernilla\nPythonson

I den här övningen ska du skapa ett program för att arbeta med siffror, på samma sätt som i slutet av modul 2 i kursen Introduktion till Programmering. Den här gången ska du definiera en klass för ändamålet.

Mängden nummer

Skapa en klass med namnet Nummerstatistik, med följande metoder:

    \n
  • metoden tillsatt_nummer lägger till ett nytt nummer till statistiken
  • \n
  • metoden mangden_nummer returnerar mängden nummer som har tillsatts
  • \n

I det här skedet finns det inget behov av att lagra själva siffrorna i någon datastruktur. Det räcker att bara komma ihåg hur många som har lagts till. Metoden tillsatt_nummer tar emot ett argument, men det finns inget behov av att bearbeta det faktiska värdet på något sätt ännu.

Funktionen borde ha följande struktur:

class  NummerStatistik:\n    def __init__(self):\n        self.nummer = 0\n\n    def tillsatt_nummer(self, nummer:int):\n        pass\n\n    def mangden_nummer(self):\n        pass
statistik = NummerStatistik()\nstatistik.tillsatt_nummer(3)\nstatistik.tillsatt_nummer(5)\nstatistik.tillsatt_nummer(1)\nstatistik.tillsatt_nummer(2)\nprint(\"Mängden nummer:\", statistik.mangden_nummer())

Mängden nummer: 4

Summa och medeltal

Tillsätt följande metoder till klassdefinitionen:

    \n
  • metoden summa returnerar summan av talen som satts till (en tom statistik returnerar 0)
  • \n
  • metoden medeltat returnerar medeltalet av numren (en tom statistiks medeltal är 0)
  • \n
statistik = NummerStatistik()\nstatistik.tillsatt_nummer(3)\nstatistik.tillsatt_nummer(5)\nstatistik.tillsatt_nummer(1)\nstatistik.tillsatt_nummer(2)\nprint(\"Mängden nummer:\", statistik.mangden_nummer())\nprint(\"Summa:\", statistik.summa())\nprint(\"Medeltal:\", statistik.medeltal())

Mängd: 4\nSumma: 11\nMedeltal: 2.75

Användarinmatning

Skriv ett huvudprogram som fortsätter att fråga användaren om heltal tills användaren skriver in -1. Programmet ska sedan skriva ut summan och medelvärdet av de inmatade talen.

Ditt program ska använda NummerStatistik-objekt för att hålla koll på numren som läggs till.

OBS: Du behöver inte ändra NummerStatistik-klassen, i denna del, använd en instans av klassen för att slutföra denna del.

OBS2: Ditt huvudprogram ska inte vara inuti ett if __name__ == \"__main__\"-block, annars fungerar inte testen.

Ange nummer:\n4\n2\n5\n2\n-1\nSumma: 13\nMedeltal: 3.25

Flera summor

Bygg på ditt huvudprogram så att det också separat räknar summan av de jämna och udda tal som läggs till.

OBS: Ändra inte din NummerStatistik-klassdefinition i denna del av övningen heller. Definiera i stället tre NummerStatistik-objekt. Ett av dem ska hålla reda på alla siffror, ett annat ska hålla reda på de jämna siffrorna och det tredje ska hålla reda på de udda siffror som skrivs in.

OBS2: Ditt huvudprogram ska inte vara inuti ett if __name__ == \"__main__\"-block, annars fungerar inte testen.

Programmet ska fungera så här:

Ange nummer:\n4\n2\n5\n2\n-1\nSumma: 13\nMedeltal: 3.25\nJämna talens summa: 8\nUdda talens summa: 5

","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/4-metoder-i-egna-klasser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur klassmetoder fungerar"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kan du skriva nya metoder i dina egna klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att förstå begreppen inkapsling och klient inom objektorienterad programmering"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klasser som endast innehåller dataattribut skiljer sig inte så mycket från ordlistor. Nedan kan du se två sätt att modellera ett bankkonto, först med en klassdefinition och sedan med hjälp av en ordlista."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Exempel 1: bankkoto med klassdefinition"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" arsranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" arsranta\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Exempel2: bankkonto med ordlista"}]},{"type":"text","value":"\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kontonummer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"agare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"saldo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"arsranta\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Med en ordlista är implementeringen mycket kortare och enklare. Med en klass är strukturen däremot mer \"hårt bunden\", så vi kan förvänta oss att alla "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":"-objekt är strukturellt lika. Dessutom är en klass också namngiven. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":" refereras till när ett nytt bankkonto skapas, och objektets typ är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":", inte dict."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En annan stor fördel med klasser är att de förutom data även kan innehålla funktionalitet. En av de vägledande principerna för objektorienterad programmering är att ett objekt används för att komma åt både den data som är kopplad till ett objekt och funktionaliteten för att bearbeta denna data."}]},{"type":"element","tagName":"h2","properties":{"id":"metoder-i-klasser","style":"position:relative;"},"children":[{"type":"text","value":"Metoder i klasser"},{"type":"element","tagName":"a","properties":{"href":"#metoder-i-klasser","ariaLabel":"metoder i klasser permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En metod är ett underprogram eller en funktion som är knuten till en specifik klass. Vanligtvis påverkar en metod bara ett enda objekt. En metod definieras inom klassdefinitionen och den kan komma åt klassens dataattribut precis som vilken annan variabel som helst."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss fortsätta med klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":" som introducerades ovan. Nedan har vi en ny metod som lägger till ränta på kontot:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" arsranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" arsranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden lägger till den årliga räntat till saldot"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta\n\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1522.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"text","value":" multiplicerar saldot på kontot med den årliga ränteprocenten och lägger sedan till resultatet till det aktuella saldot. Metoden verkar bara på det objekt som den anropas på."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss se hur detta fungerar när vi har skapat flera instanser av klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Klassen Bankkonto är definierat såsom i förra exemplet"}]},{"type":"text","value":"\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npernillas_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"99999-999\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.05"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npers_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1111-222\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Per Persson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.001"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi tillsätter ränta till Peters och Pernillas konton, men inte Pers"}]},{"type":"text","value":"\npeters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut alla"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernillas_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pers_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1522.5\n1575.0\n1500.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som du kan se ovan läggs den årliga räntan endast till på de konton som metoden anropas på. Eftersom den årliga räntan är olika för Peters och Paulas konton, blir resultatet olika för dessa två konton. Saldot på Pernillas konto ändras inte, eftersom metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"text","value":" inte anropas på objektet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pernillas_konto"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"inkapsling","style":"position:relative;"},"children":[{"type":"text","value":"Inkapsling"},{"type":"element","tagName":"a","properties":{"href":"#inkapsling","ariaLabel":"inkapsling permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inom objektorienterad programmering dyker ordet klient upp då och då. Det används för att hänvisa till ett kodavsnitt som skapar ett objekt och använder den tjänst som tillges av dess metoder. När data som finns i ett objekt endast används genom de metoder som det tillges, garanteras objektets interna integritet. I praktiken innebär detta att t.ex. en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":"-klass erbjuder metoder för att hantera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":"-attributet, så att saldot aldrig nås direkt av klienten. Dessa metoder kan sedan verifiera att saldot till exempel inte tillåts gå under noll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på hur detta skulle fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" arsranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" arsranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden tillsätter den årliga räntat till saldot av kontot"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"arsranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den här metoden \"tar ut\" pengar från kontot"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar True ifall det lyckades, och False ifall det misslyckades"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"uttag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" uttagssumma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" uttagssumma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" uttagssumma\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\npeters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uttag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades, kontots saldo är nu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades inte, saldot var otillräckligt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi försöker på nytt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uttag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades, kontots saldo är nu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades inte, saldot var otillräckligt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uttaget lyckades, kontots saldo är nu 500.0\nUttaget lyckades inte, saldot var otillräckligt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att bibehålla objektets interna integritet och erbjuda lämpliga metoder för att säkerställa detta kallas inkapsling. Tanken är att objektets inre arbete är dolt för klienten, men objektet erbjuder metoder som kan användas för att komma åt den data som lagras i objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att lägga till en metod innebär inte att attributet automatiskt döljs. Även om klassdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":" innehåller metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttag"}]},{"type":"text","value":" för att ta ut pengar, kan klientkoden fortfarande komma åt och ändra attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":" direkt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"peters_konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1500.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi försöker lyfta 2000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uttag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades, kontots saldo är nu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Uttaget lyckades inte, saldot var otillräckligt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi \"tvingar\" ett uttag på 2000"}]},{"type":"text","value":"\n peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saldot är nu:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är möjligt att dölja dataattributen från klientkoden, vilket kan bidra till att lösa detta problem. Vi återkommer till detta ämne i nästa del."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Minskande räknare","tmcname":"osa08-10_minskande_raknare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Denna övning har flera delar. Varje del är värt ett poäng och kan lämnas in separat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller en delvis ifylld klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MinskandeRaknare"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"MinskandeRaknare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" borjan_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan_varde\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut_varde"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"värde:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"minska"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# resten av metoderna här"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen används på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MinskandeRaknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"värde: 10\nvärde: 9\nvärde: 8"}]}]},{"type":"element","tagName":"h3","properties":{"id":"minska-raknarens-varde","style":"position:relative;"},"children":[{"type":"text","value":"Minska räknarens värde"},{"type":"element","tagName":"a","properties":{"href":"#minska-raknarens-varde","ariaLabel":"minska raknarens varde permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Komplettera metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minska"}]},{"type":"text","value":" som definieras i mallen, så att den minskar värdet som lagras i räknaren med ett. Se exemplet ovan för förväntat beteende."}]},{"type":"element","tagName":"h3","properties":{"id":"raknaren-far-inte-ha-ett-negativt-varde","style":"position:relative;"},"children":[{"type":"text","value":"Räknaren får inte ha ett negativt värde"},{"type":"element","tagName":"a","properties":{"href":"#raknaren-far-inte-ha-ett-negativt-varde","ariaLabel":"raknaren far inte ha ett negativt varde permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till funktionalitet till din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minska"}]},{"type":"text","value":"-metod, så att räknarens värde aldrig når negativa värden. Om värdet på räknaren är 0 kommer den inte att minskas ytterligare."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MinskandeRaknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"värde: 2\nvärde: 1\nvärde: 0\nvärde: 0"}]}]},{"type":"element","tagName":"h3","properties":{"id":"nollande-av-raknaren","style":"position:relative;"},"children":[{"type":"text","value":"Nollande av räknaren"},{"type":"element","tagName":"a","properties":{"href":"#nollande-av-raknaren","ariaLabel":"nollande av raknaren permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"nolla"}]},{"type":"text","value":" som sätter värdet på räknaren till 0:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MinskandeRaknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nolla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"värde: 100\nvärde: 0"}]}]},{"type":"element","tagName":"h3","properties":{"id":"aterstallning-av-raknaren","style":"position:relative;"},"children":[{"type":"text","value":"Återställning av räknaren"},{"type":"element","tagName":"a","properties":{"href":"#aterstallning-av-raknaren","ariaLabel":"aterstallning av raknaren permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aterstall_ursprungligt_varde()"}]},{"type":"text","value":", som återställer räknaren till sitt ursprungliga tillstånd:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"raknare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" MinskandeRaknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"aterstall_ursprungligt_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nraknare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_varde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"värde: 51\nvärde: 55"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som avslutning på detta avsnitt tittar vi på en klass som modellerar en spelares personliga bästa. Klassdefinitionen innehåller separata valideringsmetoder som kontrollerar att de argument som skickas är giltiga. Metoderna anropas redan i konstruktorn. Detta säkerställer att det skapade objektet är sunt internt."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PersonligtRekord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Standardvärden"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1900"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spelare\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dtm_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" poang\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Hjälparmetoder som kollar att argumenten är giltiga"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Namnet ska vara minst två tecken"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"dtm_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" manad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" dag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ett undantag ifall datumet inte är giltigt"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"poang_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" poang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n resultat1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PersonligtRekord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"235"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Datumet är inte giltigt"}]},{"type":"text","value":"\n resultat2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PersonligtRekord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4555"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resultat2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"datum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriver ut standardvärdet 1900-01-01"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"235\nPeter\n2020-11-01\n4555\nPernilla\n1900-01-01"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan anropades även hjälpmetoderna via parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" när de användes i konstruktorn. Det är också möjligt att inkludera /statiska/ metoddefinitioner i klassdefinitioner. Dessa är metoder som kan anropas utan att det någonsin skapas en instans av klassen. Vi återkommer till detta i nästa del."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" används endast när man hänvisar till /objektets egenskaper som en instans av klassen/. Det gäller både dataattributen och de metoder som är knutna till ett objekt. För att göra terminologin mer förvirrande kallas dataattributen och metoderna tillsammans ibland helt enkelt för objektets attribut, vilket är anledningen till att vi i detta material ofta har angett dataattribut när vi menar de variabler som definieras inom klassen. Det är här terminologin hos vissa Python-programmerare skiljer sig något från den terminologi som mer allmänt används inom objektorienterad programmering, där attribut vanligtvis bara hänvisar till dataattributen hos ett objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att skapa lokala variabler inom metoddefinitioner utan att hänvisa till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". Detta bör du göra om det inte finns något behov av att komma åt variablerna utanför metoden. Lokala variabler inom metoder har inga speciella nyckelord; de används precis som alla vanliga variabler som du har stött på hittills. ."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så här skulle det till exempel fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bonuskort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Nu är variabeln bonus en lokal variabel, inte ett"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# data attribut till objektet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den kan inte nås genom ett objekt"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.25"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" bonus\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_superbonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Variabeln superbonus är också en lokal variabel."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vanligtvis är hjälpvariabler lokala variabler eftersom"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# det inte finns något behov av att komma åt dem från andra"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# metoder i klassen eller direkt via ett objekt."}]},{"type":"text","value":"\n superbonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.5"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" superbonus\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Bonuskort(namn="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", saldo="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"För- och efternamn","tmcname":"osa08-10b_for_och_efternamn"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":", som får "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"endast ett attribut"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", som ges till konstruktorn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa dessutom två metoder:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ge_fornamn"}]},{"type":"text","value":" returnerar personens förnamn och metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ge_efternamn"}]},{"type":"text","value":" på samma sätt personens efternamn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan anta att det namn som skickas till konstruktorn kommer att innehålla endast för- och efternamn åtskilda med ett mellanslag."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n peter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ge_fornamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ge_efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n pauli "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernilla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ge_fornamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pernilla"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ge_efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter\nPython\nPernilla\nPythonson"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Nummerstatistik","tmcname":"osa08-11_nummerstatistik"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen ska du skapa ett program för att arbeta med siffror, på samma sätt som i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-8/4-metoder-i-egna-klasser","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"slutet av modul 2"}]},{"type":"text","value":" i kursen Introduktion till Programmering. Den här gången ska du definiera en klass för ändamålet."}]},{"type":"element","tagName":"h3","properties":{"id":"mangden-nummer","style":"position:relative;"},"children":[{"type":"text","value":"Mängden nummer"},{"type":"element","tagName":"a","properties":{"href":"#mangden-nummer","ariaLabel":"mangden nummer permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Nummerstatistik"}]},{"type":"text","value":", med följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"text","value":" lägger till ett nytt nummer till statistiken"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mangden_nummer"}]},{"type":"text","value":" returnerar mängden nummer som har tillsatts"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det här skedet finns det inget behov av att lagra själva siffrorna i någon datastruktur. Det räcker att bara komma ihåg hur många som har lagts till. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"text","value":" tar emot ett argument, men det finns inget behov av att bearbeta det faktiska värdet på något sätt ännu."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen borde ha följande struktur:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"mangden_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"statistik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" NummerStatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden nummer:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" statistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangden_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mängden nummer: 4"}]}]},{"type":"element","tagName":"h3","properties":{"id":"summa-och-medeltal","style":"position:relative;"},"children":[{"type":"text","value":"Summa och medeltal"},{"type":"element","tagName":"a","properties":{"href":"#summa-och-medeltal","ariaLabel":"summa och medeltal permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tillsätt följande metoder till klassdefinitionen:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summa"}]},{"type":"text","value":" returnerar summan av talen som satts till (en tom statistik returnerar 0)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"medeltat"}]},{"type":"text","value":" returnerar medeltalet av numren (en tom statistiks medeltal är 0)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"statistik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" NummerStatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstatistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden nummer:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" statistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mangden_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Summa:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" statistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Medeltal:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" statistik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"medeltal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mängd: 4\nSumma: 11\nMedeltal: 2.75"}]}]},{"type":"element","tagName":"h3","properties":{"id":"anvandarinmatning","style":"position:relative;"},"children":[{"type":"text","value":"Användarinmatning"},{"type":"element","tagName":"a","properties":{"href":"#anvandarinmatning","ariaLabel":"anvandarinmatning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skriv ett huvudprogram som fortsätter att fråga användaren om heltal tills användaren skriver in -1. Programmet ska sedan skriva ut summan och medelvärdet av de inmatade talen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ditt program ska använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"text","value":"-objekt för att hålla koll på numren som läggs till."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Du behöver inte ändra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"text","value":"-klassen, i denna del, använd en instans av klassen för att slutföra denna del."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS2: Ditt huvudprogram ska inte vara inuti ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":"-block, annars fungerar inte testen."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange nummer:\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSumma: 13\nMedeltal: 3.25"}]}]},{"type":"element","tagName":"h3","properties":{"id":"flera-summor","style":"position:relative;"},"children":[{"type":"text","value":"Flera summor"},{"type":"element","tagName":"a","properties":{"href":"#flera-summor","ariaLabel":"flera summor permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bygg på ditt huvudprogram så att det också separat räknar summan av de jämna och udda tal som läggs till."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Ändra inte din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"text","value":"-klassdefinition i denna del av övningen heller. Definiera i stället tre "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NummerStatistik"}]},{"type":"text","value":"-objekt. Ett av dem ska hålla reda på alla siffror, ett annat ska hålla reda på de jämna siffrorna och det tredje ska hålla reda på de udda siffror som skrivs in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS2: Ditt huvudprogram ska inte vara inuti ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":"-block, annars fungerar inte testen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ska fungera så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ange nummer:\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSumma: 13\nMedeltal: 3.25\nJämna talens summa: 8\nUdda talens summa: 5"}]}]}]}]},"html":"

Efter den här delen:

    \n
  • Vet du hur klassmetoder fungerar
  • \n
  • Kan du skriva nya metoder i dina egna klasser
  • \n
  • Kommer du att förstå begreppen inkapsling och klient inom objektorienterad programmering
  • \n

Klasser som endast innehåller dataattribut skiljer sig inte så mycket från ordlistor. Nedan kan du se två sätt att modellera ett bankkonto, först med en klassdefinition och sedan med hjälp av en ordlista.

# Exempel 1: bankkoto med klassdefinition\nclass Bankkonto:\n\n    def __init__(self, kontonummer: str, agare: str, saldo: float, arsranta: float):\n        self.kontonummer = kontonummer\n        self.agare = agare\n        self.saldo = saldo\n        self.arsranta = arsranta\n\npeters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)
# Exempel2: bankkonto med ordlista\npeters_konto = {\"kontonummer\": \"12345-678\", \"agare\": \"Peter Python\", \"saldo\": 1500.0, \"arsranta\": 0.0}

Med en ordlista är implementeringen mycket kortare och enklare. Med en klass är strukturen däremot mer \"hårt bunden\", så vi kan förvänta oss att alla Bankkonto-objekt är strukturellt lika. Dessutom är en klass också namngiven. Klassen Bankkonto refereras till när ett nytt bankkonto skapas, och objektets typ är Bankkonto, inte dict.

En annan stor fördel med klasser är att de förutom data även kan innehålla funktionalitet. En av de vägledande principerna för objektorienterad programmering är att ett objekt används för att komma åt både den data som är kopplad till ett objekt och funktionaliteten för att bearbeta denna data.

Metoder i klasser

En metod är ett underprogram eller en funktion som är knuten till en specifik klass. Vanligtvis påverkar en metod bara ett enda objekt. En metod definieras inom klassdefinitionen och den kan komma åt klassens dataattribut precis som vilken annan variabel som helst.

Låt oss fortsätta med klassen Bankkonto som introducerades ovan. Nedan har vi en ny metod som lägger till ränta på kontot:

class Bankkonto:\n\n    def __init__(self, kontonummer: str, agare: str, saldo: float, arsranta: float):\n        self.kontonummer = kontonummer\n        self.agare = agare\n        self.saldo = saldo\n        self.arsranta = arsranta\n\n    # Metoden lägger till den årliga räntat till saldot\n    def tillsatt_ranta(self):\n        self.saldo += self.saldo * self.arsranta\n\n\npeters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\npeters_konto.tillsatt_ranta()\nprint(peters_konto.saldo)

1522.5

Metoden tillsatt_ranta multiplicerar saldot på kontot med den årliga ränteprocenten och lägger sedan till resultatet till det aktuella saldot. Metoden verkar bara på det objekt som den anropas på.

Låt oss se hur detta fungerar när vi har skapat flera instanser av klassen:

# Klassen Bankkonto är definierat såsom i förra exemplet\n\npeters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\npernillas_konto = Bankkonto(\"99999-999\", \"Pernilla Pythonson\", 1500.0, 0.05)\npers_konto = Bankkonto(\"1111-222\", \"Per Persson\", 1500.0, 0.001)\n\n# Vi tillsätter ränta till Peters och Pernillas konton, men inte Pers\npeters_konto.tillsatt_ranta()\npernillas_konto.tillsatt_ranta()\n\n# Vi skriver ut alla\nprint(peters_konto.saldo)\nprint(pernillas_konto.saldo)\nprint(pers_konto.saldo)

1522.5\n1575.0\n1500.0

Som du kan se ovan läggs den årliga räntan endast till på de konton som metoden anropas på. Eftersom den årliga räntan är olika för Peters och Paulas konton, blir resultatet olika för dessa två konton. Saldot på Pernillas konto ändras inte, eftersom metoden tillsatt_ranta inte anropas på objektet pernillas_konto.

Inkapsling

Inom objektorienterad programmering dyker ordet klient upp då och då. Det används för att hänvisa till ett kodavsnitt som skapar ett objekt och använder den tjänst som tillges av dess metoder. När data som finns i ett objekt endast används genom de metoder som det tillges, garanteras objektets interna integritet. I praktiken innebär detta att t.ex. en Bankkonto-klass erbjuder metoder för att hantera saldo-attributet, så att saldot aldrig nås direkt av klienten. Dessa metoder kan sedan verifiera att saldot till exempel inte tillåts gå under noll.

Ett exempel på hur detta skulle fungera:

class Bankkonto:\n\n    def __init__(self, kontonummer: str, agare: str, saldo: float, arsranta: float):\n        self.kontonummer = kontonummer\n        self.agare = agare\n        self.saldo = saldo\n        self.arsranta = arsranta\n\n    # Metoden tillsätter den årliga räntat till saldot av kontot\n    def tillsatt_ranta(self):\n        self.saldo += self.saldo * self.arsranta\n\n    # Den här metoden \"tar ut\" pengar från kontot\n    # Metoden returnerar True ifall det lyckades, och False ifall det misslyckades\n    def uttag(self, uttagssumma: float):\n        if uttagssumma <= self.saldo:\n            self.saldo -= uttagssumma\n            return True\n\n        return False\n\npeters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\n\nif peters_konto.uttag(1000):\n    print(\"Uttaget lyckades, kontots saldo är nu\", peters_konto.saldo)\nelse:\n    print(\"Uttaget lyckades inte, saldot var otillräckligt\")\n\n# Vi försöker på nytt\nif peters_konto.uttag(1000):\n    print(\"Uttaget lyckades, kontots saldo är nu\", peters_konto.saldo)\nelse:\n    print(\"Uttaget lyckades inte, saldot var otillräckligt\")

Uttaget lyckades, kontots saldo är nu 500.0\nUttaget lyckades inte, saldot var otillräckligt

Att bibehålla objektets interna integritet och erbjuda lämpliga metoder för att säkerställa detta kallas inkapsling. Tanken är att objektets inre arbete är dolt för klienten, men objektet erbjuder metoder som kan användas för att komma åt den data som lagras i objektet.

Att lägga till en metod innebär inte att attributet automatiskt döljs. Även om klassdefinitionen Bankkonto innehåller metoden uttag för att ta ut pengar, kan klientkoden fortfarande komma åt och ändra attributet saldo direkt:

peters_konto = Bankkonto(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\n\n# Vi försöker lyfta 2000\nif peters_konto.uttag(2000):\n    print(\"Uttaget lyckades, kontots saldo är nu\", peters_konto.saldo)\nelse:\n    print(\"Uttaget lyckades inte, saldot var otillräckligt\")\n\n    # Vi \"tvingar\" ett uttag på 2000\n    peters_konto.saldo -= 2000\n\nprint(\"Saldot är nu:\", peters_konto.saldo)

Det är möjligt att dölja dataattributen från klientkoden, vilket kan bidra till att lösa detta problem. Vi återkommer till detta ämne i nästa del.

Denna övning har flera delar. Varje del är värt ett poäng och kan lämnas in separat.

Övningsmallen innehåller en delvis ifylld klass MinskandeRaknare:

class MinskandeRaknare:\n    def __init__(self, borjan_varde: int):\n        self.varde = borjan_varde\n\n    def skriv_ut_varde(self):\n        print(\"värde:\", self.varde)\n\n    def minska(self):\n        pass\n\n    # resten av metoderna här

Klassen används på följande sätt:

raknare = MinskandeRaknare(10)\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()

värde: 10\nvärde: 9\nvärde: 8

Minska räknarens värde

Komplettera metoden minska som definieras i mallen, så att den minskar värdet som lagras i räknaren med ett. Se exemplet ovan för förväntat beteende.

Räknaren får inte ha ett negativt värde

Lägg till funktionalitet till din minska-metod, så att räknarens värde aldrig når negativa värden. Om värdet på räknaren är 0 kommer den inte att minskas ytterligare.

raknare = MinskandeRaknare(2)\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()\nraknare.minska()\nraknare.skriv_ut_varde()

värde: 2\nvärde: 1\nvärde: 0\nvärde: 0

Nollande av räknaren

Skapa en metod nolla som sätter värdet på räknaren till 0:

raknare = MinskandeRaknare(100)\nraknare.skriv_ut_varde()\nraknare.nolla()\nraknare.skriv_ut_varde()

värde: 100\nvärde: 0

Återställning av räknaren

Skapa en metod aterstall_ursprungligt_varde(), som återställer räknaren till sitt ursprungliga tillstånd:

raknare = MinskandeRaknare(55)\nraknare.minska()\nraknare.minska()\nraknare.minska()\nraknare.minska()\nraknare.skriv_ut_varde()\nraknare.aterstall_ursprungligt_varde()\nraknare.skriv_ut_varde()

värde: 51\nvärde: 55

Som avslutning på detta avsnitt tittar vi på en klass som modellerar en spelares personliga bästa. Klassdefinitionen innehåller separata valideringsmetoder som kontrollerar att de argument som skickas är giltiga. Metoderna anropas redan i konstruktorn. Detta säkerställer att det skapade objektet är sunt internt.

from datetime import date\n\nclass PersonligtRekord:\n\n    def __init__(self, spelare: str, dag: int, manad: int, ar: int, poang: int):\n        # Standardvärden\n        self.spelare = \"\"\n        self.datum = date(1900, 1, 1)\n        self.poang = 0\n\n        if self.namn_ok(spelare):\n            self.spelare = spelare\n\n        if self.dtm_ok(dag, manad, ar):\n            self.datum = date(ar, manad, dag)\n\n        if self.poang_ok(poang):\n            self.poang = poang\n\n    # Hjälparmetoder som kollar att argumenten är giltiga\n    def namn_ok(self, namn: str):\n        return len(namn) >= 2 # Namnet ska vara minst två tecken\n\n    def dtm_ok(self, dag, manad, ar):\n        try:\n            date(ar, manad, dag)\n            return True\n        except:\n            # Ett undantag ifall datumet inte är giltigt\n            return False\n\n    def poang_ok(self, poang):\n        return poang >= 0\n\nif __name__ == \"__main__\":\n    resultat1 = PersonligtRekord(\"Peter\", 1, 11, 2020, 235)\n    print(resultat1.poang)\n    print(resultat1.spelare)\n    print(resultat1.datum)\n\n    # Datumet är inte giltigt\n    resultat2 = PersonligtRekord(\"Pernilla\", 4, 13, 2019, 4555)\n    print(resultat2.poang)\n    print(resultat2.spelare)\n    print(resultat2.datum) # Skriver ut standardvärdet 1900-01-01

235\nPeter\n2020-11-01\n4555\nPernilla\n1900-01-01

I exemplet ovan anropades även hjälpmetoderna via parameternamnet self när de användes i konstruktorn. Det är också möjligt att inkludera /statiska/ metoddefinitioner i klassdefinitioner. Dessa är metoder som kan anropas utan att det någonsin skapas en instans av klassen. Vi återkommer till detta i nästa del.

Parameternamnet self används endast när man hänvisar till /objektets egenskaper som en instans av klassen/. Det gäller både dataattributen och de metoder som är knutna till ett objekt. För att göra terminologin mer förvirrande kallas dataattributen och metoderna tillsammans ibland helt enkelt för objektets attribut, vilket är anledningen till att vi i detta material ofta har angett dataattribut när vi menar de variabler som definieras inom klassen. Det är här terminologin hos vissa Python-programmerare skiljer sig något från den terminologi som mer allmänt används inom objektorienterad programmering, där attribut vanligtvis bara hänvisar till dataattributen hos ett objekt.

Det är också möjligt att skapa lokala variabler inom metoddefinitioner utan att hänvisa till self. Detta bör du göra om det inte finns något behov av att komma åt variablerna utanför metoden. Lokala variabler inom metoder har inga speciella nyckelord; de används precis som alla vanliga variabler som du har stött på hittills. .

Så här skulle det till exempel fungera:

class Bonuskort:\n    def __init__(self, namn: str, saldo: float):\n        self.namn = namn\n        self.saldo = saldo\n\n    def tillsatt_bonus(self):\n        # Nu är variabeln bonus en lokal variabel, inte ett\n        # data attribut till objektet\n        # Den kan inte nås genom ett objekt\n        bonus = self.saldo * 0.25\n        self.saldo += bonus\n\n    def tillsatt_superbonus(self):\n        # Variabeln superbonus är också en lokal variabel.\n        # Vanligtvis är hjälpvariabler lokala variabler eftersom\n        # det inte finns något behov av att komma åt dem från andra\n        # metoder i klassen eller direkt via ett objekt.\n        superbonus = self.saldo * 0.5\n        self.saldo += superbonus\n\n    def __str__(self):\n        return f\"Bonuskort(namn={self.namn}, saldo={self.saldo})\"

Skapa en klass Person, som får endast ett attribut namn, som ges till konstruktorn.

Skapa dessutom två metoder:

Metoden ge_fornamn returnerar personens förnamn och metoden ge_efternamn på samma sätt personens efternamn.

Du kan anta att det namn som skickas till konstruktorn kommer att innehålla endast för- och efternamn åtskilda med ett mellanslag.

Exempel på användning:

if __name__ == \"__main__\":\n    peter = Person(\"Peter Python\")\n    print(peter.ge_fornamn())\n    print(peter.ge_efternamn())\n\n    pauli = Person(\"Pernilla Pythonson\")\n    print(pernilla.ge_fornamn())\n    print(pernilla.ge_efternamn())

Peter\nPython\nPernilla\nPythonson

I den här övningen ska du skapa ett program för att arbeta med siffror, på samma sätt som i slutet av modul 2 i kursen Introduktion till Programmering. Den här gången ska du definiera en klass för ändamålet.

Mängden nummer

Skapa en klass med namnet Nummerstatistik, med följande metoder:

    \n
  • metoden tillsatt_nummer lägger till ett nytt nummer till statistiken
  • \n
  • metoden mangden_nummer returnerar mängden nummer som har tillsatts
  • \n

I det här skedet finns det inget behov av att lagra själva siffrorna i någon datastruktur. Det räcker att bara komma ihåg hur många som har lagts till. Metoden tillsatt_nummer tar emot ett argument, men det finns inget behov av att bearbeta det faktiska värdet på något sätt ännu.

Funktionen borde ha följande struktur:

class  NummerStatistik:\n    def __init__(self):\n        self.nummer = 0\n\n    def tillsatt_nummer(self, nummer:int):\n        pass\n\n    def mangden_nummer(self):\n        pass
statistik = NummerStatistik()\nstatistik.tillsatt_nummer(3)\nstatistik.tillsatt_nummer(5)\nstatistik.tillsatt_nummer(1)\nstatistik.tillsatt_nummer(2)\nprint(\"Mängden nummer:\", statistik.mangden_nummer())

Mängden nummer: 4

Summa och medeltal

Tillsätt följande metoder till klassdefinitionen:

    \n
  • metoden summa returnerar summan av talen som satts till (en tom statistik returnerar 0)
  • \n
  • metoden medeltat returnerar medeltalet av numren (en tom statistiks medeltal är 0)
  • \n
statistik = NummerStatistik()\nstatistik.tillsatt_nummer(3)\nstatistik.tillsatt_nummer(5)\nstatistik.tillsatt_nummer(1)\nstatistik.tillsatt_nummer(2)\nprint(\"Mängden nummer:\", statistik.mangden_nummer())\nprint(\"Summa:\", statistik.summa())\nprint(\"Medeltal:\", statistik.medeltal())

Mängd: 4\nSumma: 11\nMedeltal: 2.75

Användarinmatning

Skriv ett huvudprogram som fortsätter att fråga användaren om heltal tills användaren skriver in -1. Programmet ska sedan skriva ut summan och medelvärdet av de inmatade talen.

Ditt program ska använda NummerStatistik-objekt för att hålla koll på numren som läggs till.

OBS: Du behöver inte ändra NummerStatistik-klassen, i denna del, använd en instans av klassen för att slutföra denna del.

OBS2: Ditt huvudprogram ska inte vara inuti ett if __name__ == \"__main__\"-block, annars fungerar inte testen.

Ange nummer:\n4\n2\n5\n2\n-1\nSumma: 13\nMedeltal: 3.25

Flera summor

Bygg på ditt huvudprogram så att det också separat räknar summan av de jämna och udda tal som läggs till.

OBS: Ändra inte din NummerStatistik-klassdefinition i denna del av övningen heller. Definiera i stället tre NummerStatistik-objekt. Ett av dem ska hålla reda på alla siffror, ett annat ska hålla reda på de jämna siffrorna och det tredje ska hålla reda på de udda siffror som skrivs in.

OBS2: Ditt huvudprogram ska inte vara inuti ett if __name__ == \"__main__\"-block, annars fungerar inte testen.

Programmet ska fungera så här:

Ange nummer:\n4\n2\n5\n2\n-1\nSumma: 13\nMedeltal: 3.25\nJämna talens summa: 8\nUdda talens summa: 5

","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/4-metoder-i-egna-klasser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-8/5-fler-exempel/page-data.json b/page-data/osa-8/5-fler-exempel/page-data.json index 5ce5379c1..e1aaeae21 100644 --- a/page-data/osa-8/5-fler-exempel/page-data.json +++ b/page-data/osa-8/5-fler-exempel/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-8/5-fler-exempel", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa mer mångsidiga klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du lägger till en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod i dina klassdefinitioner"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"exempel-1-klassen-rektangel","style":"position:relative;"},"children":[{"type":"text","value":"Exempel 1: klassen Rektangel"},{"type":"element","tagName":"a","properties":{"href":"#exempel-1-klassen-rektangel","ariaLabel":"exempel 1 klassen rektangel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en klass som modellerar en rektangel i ett tvådimensionellt rum:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vanster_ovre\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hoger_nedre\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"area"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"omkrets"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"flytta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n horn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"horn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"x_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" horn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"y_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n horn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"horn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"x_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" horn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"y_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En ny "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":" skapas med två tuplar som argument. Dessa tuplar innehåller x- och y-koordinaterna för det övre vänstra hörnet och det nedre högra hörnet. Konstruktören beräknar rektangelns höjd och bredd baserat på dessa värden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"area"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"omkrets"}]},{"type":"text","value":" beräknar rektangelns area och omkrets baserat på höjd och bredd. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":" flyttar rektangeln med de x- och y-värden som anges som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rektangeln representeras i ett koordinatsystem där x-koordinaterna ökar från vänster till höger och y-koordinaterna ökar från topp till botten. Detta är ett vanligt sätt att hantera koordinater i programmering eftersom det ofta är enklare och mer naturligt att betrakta datorskärmens övre vänstra hörn som den punkt där x och y är lika med noll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program testar klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"omkrets"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(1, 1)\n(4, 3)\n3\n2\n10\n6\n(4, 4)\n(7, 6)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"skriva-ut-ett-objekt","style":"position:relative;"},"children":[{"type":"text","value":"Skriva ut ett objekt"},{"type":"element","tagName":"a","properties":{"href":"#skriva-ut-ett-objekt","ariaLabel":"skriva ut ett objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du har ett objekt som skapats från en klass som du själv definierat, är standardreaktionen på att anropa instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" med objektet som argument inte särskilt informativt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde se ut någorlunda så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"<"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"main"}]},{"type":"text","value":".Rektangel object at 0x000002D7BF148A90>"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi vill självklart ha mer kontroll över vad som skrivs ut. Det enklaste sättet att göra detta är att lägga till en speciell "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod i klassdefinitionen. Dess syfte är att returnera en ögonblicksbild av objektets tillstånd i strängformat. Om klassdefinitionen innehåller en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod är det värde som returneras av metoden, det som skrivs ut när instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så låt oss lägga till en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metoddefinition i vår "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":"-klass:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Resten av klassen kommer här såsom ovan"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar objektets tillstånd i strängformat"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"rektangel "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ... "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu borde "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" instruktionen producera nånting mer användarvänligt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"rektangel (1, 1) ... (4, 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" används kanske oftare för att formulera en strängrepresentation av objektet med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":"-funktionen, som i följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbeskrivning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"beskrivning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"rektangel (1, 1) ... (4, 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns många fler speciella understrukna metoder som kan definieras för klasser. En metod som liknar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metoden är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":"-metoden. Dess syfte är att ge en teknisk representation av objektets tillstånd. Vi kommer att stöta på denna metod senare."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Stoppur","tmcname":"osa08-11a_stoppur"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbotten innehåller följande ram för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stoppur"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Stoppur"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sekunder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minuter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bygg ut på klassdefinitionen så att den fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"klocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Stoppur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3600"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"00:00\n00:01\n00:02\n... många fler rader utskrivna\n00:59\n01:00\n01:01\n... många, många fler rader utskrivna\n59:58\n59:59\n00:00\n00:01"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" innebär alltså att en sekund läggs till i stoppuret. Det maximala värdet för både sekunder och minuter är 59. Din klassdefinition bör också innehålla en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod, som returnerar en strängrepresentation av stoppurets tillstånd, som visas i exemplet ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" Det kan göra det lättare att testa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":"-metoden om du tillfälligt ställer in startvärdena för sekunder och minuter till något värde närmare 59 i konstruktorn. Om du ändrar de ursprungliga värdena, kom ihåg att ändra tillbaka dem innan du skickar in."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Klocka","tmcname":"osa08-12_klocka"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en ny klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Klocka"}]},{"type":"text","value":", som bygger på Stoppur klassen. Den ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"klocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"59"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"installning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n23:59:55\n23:59:56\n23:59:57\n23:59:58\n23:59:59\n00:00:00\n00:00:01\n12:05:00\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstruktorn tar alltså ursprungliga värden för timmar, minuter och sekunder som argument. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" för klockan framåt en sekond och metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"installning"}]},{"type":"text","value":" ställer in klockans timmar och minuter och "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"nollar sekunderna"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lunchkort","tmcname":"osa08-13_lunchkort"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På Unicafe, studentkafeterian vid Helsingfors universitet, kan studenterna betala för sin lunch med ett särskilt betalkort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen kommer du att skriva en klass som heter Lunchkort, med syftet att emulera de funktioner som tillhandahålls av Unicafes betalkort."}]},{"type":"element","tagName":"h3","properties":{"id":"strukturen-av-klassen","style":"position:relative;"},"children":[{"type":"text","value":"Strukturen av klassen"},{"type":"element","tagName":"a","properties":{"href":"#strukturen-av-klassen","ariaLabel":"strukturen av klassen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en ny klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa först konstruktorn för klassen. Den ska ta det ursprungliga saldot som finns på kortet som ett argument och spara det som ett attribut. Skapa sedan en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod som returnerar en sträng som innehåller saldot: ”Saldot är X euro”. Det tillgängliga saldot ska skrivas ut med en decimals noggrannhet. Se exemplet nedan för användning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande är en ram av klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel av användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ovan borde producera följande utskrift:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo är 50.0 euro"}]}]},{"type":"element","tagName":"h3","properties":{"id":"betalning-av-lunch","style":"position:relative;"},"children":[{"type":"text","value":"Betalning av lunch"},{"type":"element","tagName":"a","properties":{"href":"#betalning-av-lunch","ariaLabel":"betalning av lunch permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera följande metoder till din Lunchkort klass:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"at_formanligt"}]},{"type":"text","value":" som minskar saldot med 2.60 euro"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"at_special"}]},{"type":"text","value":" som minskar saldot med 4.60 euro"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande huvudfunktion testar din klass:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde ge följande utskrift:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo är 50.0 euro\nKortets saldo är 47.4 euro\nKortets saldo är 40.2 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Se till att kortets saldo inte kan bli under 0:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo är 4.0 euro\nKortets saldo är 1.4 euro\nKortets saldo är 1.4 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo får alltså inte minska ifall det inte finns tillräckligt med pengar på det."}]},{"type":"element","tagName":"h3","properties":{"id":"satta-in-pengar-pa-kortet","style":"position:relative;"},"children":[{"type":"text","value":"Sätta in pengar på kortet"},{"type":"element","tagName":"a","properties":{"href":"#satta-in-pengar-pa-kortet","ariaLabel":"satta in pengar pa kortet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":"-klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden ska öka saldot på kortet med mängden angivet som argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo är 10.0 euro\nKortets saldo är 25.0 euro\nKortets saldo är 35.0 euro\nKortets saldo är 235.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall kortet försöker laddas med en negativ mängd ska metoden "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/3-fel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"åstadkomma ett undantag"}]},{"type":"text","value":" av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"File \"testi.py\", line 3, in lunchkort\nValueError: Det går inte att lägga till mindre än noll"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" metoden ska "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"åstadkomma"}]},{"type":"text","value":" ett undantag, se "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/3-fel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":" i materialet hur man gör. Metoden får under inga omständigheter själv skriva ut nånting!"}]},{"type":"element","tagName":"h3","properties":{"id":"flera-kort","style":"position:relative;"},"children":[{"type":"text","value":"Flera kort"},{"type":"element","tagName":"a","properties":{"href":"#flera-kort","ariaLabel":"flera kort permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett huvudprogram, som innehåller följande händelser:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Skapa Peters kort. Kortets ursprungliga saldo är 20 euro"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Skapa Mattes kort. Kortets ursprungliga saldo är 30 euro"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter äter en special"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Matte äter förmånligt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter tillsätter 20 euro på kortet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Matte äter en special"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter äter förmånligt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter äter förmånligt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Matte tillsätter 50 euro på kortet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Huvudprogrammets ram:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"peters_kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmattes_kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# resten av huvudprogrammet"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter: Kortets saldo är 15.4 euro\nMatte: Kortets saldo är 27.4 euro\nPeter: Kortets saldo är 35.4 euro\nMatte: Kortets saldo är 22.8 euro\nPeter: Kortets saldo är 30.2 euro\nMatte: Kortets saldo är 72.8 euro"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"exempel-2-uppgiftslista","style":"position:relative;"},"children":[{"type":"text","value":"Exempel 2: Uppgiftslista"},{"type":"element","tagName":"a","properties":{"href":"#exempel-2-uppgiftslista","ariaLabel":"exempel 2 uppgiftslista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftslista"}]},{"type":"text","value":" modellerar en lista med uppgifter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Uppgiftslista"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" prioritet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prioritet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_nasta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden pop tar bort och returnerar sista föremålet på listan"}]},{"type":"text","value":"\n uppgift "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Returnerar det andra föremålet i tupeln, alltså uppgiftens namn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" uppgift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sammanlagt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rensa_uppgifter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillagg_uppgift"}]},{"type":"text","value":" lägger till en ny uppgift på listan. Varje uppgift har också en prioritet, som används för att sortera uppgifterna. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_nasta"}]},{"type":"text","value":" tar bort och returnerar den uppgift som har högst prioritet i listan. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sammanlagt"}]},{"type":"text","value":" finns också, som returnerar antalet uppgifter i listan, och slutligen metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rensa_uppgifter"}]},{"type":"text","value":", som rensar uppgiftslistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inom objektet lagras uppgifterna i en lista. Varje uppgift består av en tupel som innehåller uppgiftens prioritet och dess namn. Prioritetsvärdet lagras först, så att den uppgift som har högst prioritet hamnar sist i listan då den sorteras. Därför kan vi sedan helt enkelt använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"-metoden för att hämta och ta bort det högst prioriterade objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ta en titt på följande program med uppgiftslistan i handling:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studier\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"motion\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"städning\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"dejt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rensa_uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\nmotion\n2\n3\ndejt\nstudier\n1\n0"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sarja","tmcname":"osa08-14_sarja"},"children":[{"type":"element","tagName":"h3","properties":{"id":"klassen-serie","style":"position:relative;"},"children":[{"type":"text","value":"Klassen Serie"},{"type":"element","tagName":"a","properties":{"href":"#klassen-serie","ariaLabel":"klassen serie permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Serie"}]},{"type":"text","value":", som fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dexter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Crime\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drama\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mystery\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"dexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dexter (8 säsonger)\ngenret: Crime, Drama, Mystery, Thriller\ninga betygsättningar"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstruktorn ska ta titeln, antalet säsonger och en lista med genrer för serien som sina argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" när du behöver en sträng från en lista med strängar, där du vill separera med en karaktär av ditt val, kan du använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" metoden på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Crime\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drama\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mystery\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Crime, Drama, Mystery, Thriller"}]}]},{"type":"element","tagName":"h3","properties":{"id":"betygsattningar","style":"position:relative;"},"children":[{"type":"text","value":"Betygsättningar"},{"type":"element","tagName":"a","properties":{"href":"#betygsattningar","ariaLabel":"betygsattningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"betygsatt(betyg: int)"}]},{"type":"text","value":", med vilken man kan ge ett betyg mellan heltalen 0 och 5. Också metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" ska ändras som så, att ifall det finns några betyg ska metoden skriva ut mängden betyg och deras medeltal med en decimals noggranhet."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dexter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Crime\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drama\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mystery\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"dexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dexter (8 esityskautta)\ngenret: Crime, Drama, Mystery, Thriller\nBetyg 5, medeltal 3.4 poäng"}]}]},{"type":"element","tagName":"h3","properties":{"id":"sokning-av-serier","style":"position:relative;"},"children":[{"type":"text","value":"Sökning av serier"},{"type":"element","tagName":"a","properties":{"href":"#sokning-av-serier","ariaLabel":"sokning av serier permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa två funktioner "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"betyg_minst(betyg: float, serier: list)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"innehallar_genren(genre: str, serier: list)"}]},{"type":"text","value":", med vilka det är möjligt att hitta serier på listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoderna fungerar på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Crime\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drama\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mystery\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"South Park\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Animation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Comedy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Friends\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Romance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Comedy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nserier "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg minst 4.5:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" serie "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" betyg_minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" serier"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"genre Comedy:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" serie "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" innehallar_genren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Comedy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" serier"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"betyg minst 4.5:\nDexter"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"genre Comedy:\nSouth Park\nFriends"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att ovanstående kod och testerna för denna övning antar att din klass innehåller attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":". Ifall du använder något annat attribut för att hänvisa till namnet på serien, bör du ändra det innan du skickar in uppgifterna."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en snabb enkät om veckans material:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"a02d084c-8f0c-5918-8740-6282910a2b33"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att kunna skapa mer mångsidiga klasser
  • \n
  • Vet du hur du lägger till en __str__-metod i dina klassdefinitioner
  • \n

Exempel 1: klassen Rektangel

Låt oss ta en titt på en klass som modellerar en rektangel i ett tvådimensionellt rum:

class Rektangel:\n    def __init__(self, vanster_ovre: tuple, hoger_nedre: tuple):\n        self.vanster_ovre = vanster_ovre\n        self.hoger_nedre = hoger_nedre\n        self.bredd = hoger_nedre[0]-vanster_ovre[0]\n        self.hojd = hoger_nedre[1]-vanster_ovre[1]\n\n    def area(self):\n        return self.bredd * self.hojd\n\n    def omkrets(self):\n        return self.bredd * 2 + self.hojd * 2\n\n    def flytta(self, x_andring: int, y_andring: int):\n        horn = self.vanster_ovre\n        self.vanster_ovre = (horn[0]+x_andring, horn[1]+y_andring)\n        horn = self.hoger_nedre\n        self.hoger_nedre = (horn[0]+x_andring, horn[1]+y_andring)

En ny Rektangel skapas med två tuplar som argument. Dessa tuplar innehåller x- och y-koordinaterna för det övre vänstra hörnet och det nedre högra hörnet. Konstruktören beräknar rektangelns höjd och bredd baserat på dessa värden.

Metoderna area och omkrets beräknar rektangelns area och omkrets baserat på höjd och bredd. Metoden flytta flyttar rektangeln med de x- och y-värden som anges som argument.

Rektangeln representeras i ett koordinatsystem där x-koordinaterna ökar från vänster till höger och y-koordinaterna ökar från topp till botten. Detta är ett vanligt sätt att hantera koordinater i programmering eftersom det ofta är enklare och mer naturligt att betrakta datorskärmens övre vänstra hörn som den punkt där x och y är lika med noll.

Följande program testar klassen Rektangel:

rektangel = Rektangel((1, 1), (4, 3))\nprint(rektangel.vanster_ovre)\nprint(rektangel.hoger_nedre)\nprint(rektangel.bredd)\nprint(rektangel.hojd)\nprint(rektangel.omkrets())\nprint(rektangel.area())\n\nrektangel.flytta(3, 3)\nprint(rektangel.vanster_ovre)\nprint(rektangel.hoger_nedre)

(1, 1)\n(4, 3)\n3\n2\n10\n6\n(4, 4)\n(7, 6)

Skriva ut ett objekt

När du har ett objekt som skapats från en klass som du själv definierat, är standardreaktionen på att anropa instruktionen print med objektet som argument inte särskilt informativt:

rektangel = Rektangel((1, 1), (4, 3))\nprint(rektangel)

Utskriften borde se ut någorlunda så här:

<main.Rektangel object at 0x000002D7BF148A90>

Vi vill självklart ha mer kontroll över vad som skrivs ut. Det enklaste sättet att göra detta är att lägga till en speciell __str__-metod i klassdefinitionen. Dess syfte är att returnera en ögonblicksbild av objektets tillstånd i strängformat. Om klassdefinitionen innehåller en __str__-metod är det värde som returneras av metoden, det som skrivs ut när instruktionen print körs.

Så låt oss lägga till en __str__-metoddefinition i vår Rektangel-klass:

class Rektangel:\n\n    # Resten av klassen kommer här såsom ovan\n\n    # Metoden returnerar objektets tillstånd i strängformat\n    def __str__(self):\n        return f\"rektangel {self.vanster_ovre} ... {self.hoger_nedre}\"

Nu borde print instruktionen producera nånting mer användarvänligt:

rektangel = Rektangel((1, 1), (4, 3))\nprint(rektangel)

rektangel (1, 1) ... (4, 3)

Metoden __str__ används kanske oftare för att formulera en strängrepresentation av objektet med str-funktionen, som i följande program:

rektangel = Rektangel((1, 1), (4, 3))\nbeskrivning = str(rektangel)\nprint(beskrivning)

rektangel (1, 1) ... (4, 3)

Det finns många fler speciella understrukna metoder som kan definieras för klasser. En metod som liknar __str__-metoden är __repr__-metoden. Dess syfte är att ge en teknisk representation av objektets tillstånd. Vi kommer att stöta på denna metod senare.

Uppgiftsbotten innehåller följande ram för klassen Stoppur:

class Stoppur:\n    def __init__(self):\n        self.sekunder = 0\n        self.minuter = 0

Bygg ut på klassdefinitionen så att den fungerar enligt följande:

klocka = Stoppur()\nfor i in range(3600):\n    print(klocka)\n    klocka.tick()

00:00\n00:01\n00:02\n... många fler rader utskrivna\n00:59\n01:00\n01:01\n... många, många fler rader utskrivna\n59:58\n59:59\n00:00\n00:01

Metoden tick innebär alltså att en sekund läggs till i stoppuret. Det maximala värdet för både sekunder och minuter är 59. Din klassdefinition bör också innehålla en __str__-metod, som returnerar en strängrepresentation av stoppurets tillstånd, som visas i exemplet ovan.

Tips: Det kan göra det lättare att testa tick-metoden om du tillfälligt ställer in startvärdena för sekunder och minuter till något värde närmare 59 i konstruktorn. Om du ändrar de ursprungliga värdena, kom ihåg att ändra tillbaka dem innan du skickar in.

Definiera en ny klass Klocka, som bygger på Stoppur klassen. Den ska fungera enligt följande:

klocka = Klocka(23, 59, 55)\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\n\nklocka.installning(12, 5)\nprint(klocka)
\n23:59:55\n23:59:56\n23:59:57\n23:59:58\n23:59:59\n00:00:00\n00:00:01\n12:05:00\n

Konstruktorn tar alltså ursprungliga värden för timmar, minuter och sekunder som argument. Metoden tick för klockan framåt en sekond och metoden installning ställer in klockans timmar och minuter och nollar sekunderna.

På Unicafe, studentkafeterian vid Helsingfors universitet, kan studenterna betala för sin lunch med ett särskilt betalkort.

I den här övningen kommer du att skriva en klass som heter Lunchkort, med syftet att emulera de funktioner som tillhandahålls av Unicafes betalkort.

Strukturen av klassen

Skapa en ny klass med namnet Lunchkort.

Skapa först konstruktorn för klassen. Den ska ta det ursprungliga saldot som finns på kortet som ett argument och spara det som ett attribut. Skapa sedan en __str__-metod som returnerar en sträng som innehåller saldot: ”Saldot är X euro”. Det tillgängliga saldot ska skrivas ut med en decimals noggrannhet. Se exemplet nedan för användning.

Följande är en ram av klassen:

class Lunchkort:\n    def __init__(self, saldo: float):\n        self.saldo = saldo\n\n    def __str__(self):\n        pass

Exempel av användning:

kort = Lunchkort(50)\nprint(kort)

Programmet ovan borde producera följande utskrift:

Kortets saldo är 50.0 euro

Betalning av lunch

Implementera följande metoder till din Lunchkort klass:

    \n
  • at_formanligt som minskar saldot med 2.60 euro
  • \n
  • at_special som minskar saldot med 4.60 euro
  • \n

Följande huvudfunktion testar din klass:

kort = Lunchkort(50)\nprint(kort)\n\nkort.at_formanligt()\nprint(kort)\n\nkort.at_special()\nkort.at_formanligt()\nprint(kort)

Detta borde ge följande utskrift:

Kortets saldo är 50.0 euro\nKortets saldo är 47.4 euro\nKortets saldo är 40.2 euro

Se till att kortets saldo inte kan bli under 0:

kort = Lunchkort(4)\nprint(kort)\n\nkort.at_formanligt()\nprint(kort)\n\nkort.at_formanligt()\nprint(kort)

Kortets saldo är 4.0 euro\nKortets saldo är 1.4 euro\nKortets saldo är 1.4 euro

Kortets saldo får alltså inte minska ifall det inte finns tillräckligt med pengar på det.

Sätta in pengar på kortet

Lägg till metoden tillsatt_pengar till Lunchkort-klassen.

Metoden ska öka saldot på kortet med mängden angivet som argument.

kort = Lunchkort(10)\nprint(kort)\nkort.tillsatt_pengar(15)\nprint(kort)\nkort.tillsatt_pengar(10)\nprint(kort)\nkort.tillsatt_pengar(200)\nprint(kort)

Kortets saldo är 10.0 euro\nKortets saldo är 25.0 euro\nKortets saldo är 35.0 euro\nKortets saldo är 235.0 euro

Ifall kortet försöker laddas med en negativ mängd ska metoden åstadkomma ett undantag av typen ValueError:

kort = Lunchkort(10)\nkort.tillsatt_pengar(-10)

File \"testi.py\", line 3, in lunchkort\nValueError: Det går inte att lägga till mindre än noll

OBS: metoden ska åstadkomma ett undantag, se modul 6 i materialet hur man gör. Metoden får under inga omständigheter själv skriva ut nånting!

Flera kort

Skapa ett huvudprogram, som innehåller följande händelser:

    \n
  • Skapa Peters kort. Kortets ursprungliga saldo är 20 euro
  • \n
  • Skapa Mattes kort. Kortets ursprungliga saldo är 30 euro
  • \n
  • Peter äter en special
  • \n
  • Matte äter förmånligt
  • \n
  • Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)
  • \n
  • Peter tillsätter 20 euro på kortet
  • \n
  • Matte äter en special
  • \n
  • Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)
  • \n
  • Peter äter förmånligt
  • \n
  • Peter äter förmånligt
  • \n
  • Matte tillsätter 50 euro på kortet
  • \n
  • Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)
  • \n

Huvudprogrammets ram:

peters_kort = Lunchkort(20)\nmattes_kort = Lunchkort(30)\n# resten av huvudprogrammet

Utskriften borde vara följande:

Peter: Kortets saldo är 15.4 euro\nMatte: Kortets saldo är 27.4 euro\nPeter: Kortets saldo är 35.4 euro\nMatte: Kortets saldo är 22.8 euro\nPeter: Kortets saldo är 30.2 euro\nMatte: Kortets saldo är 72.8 euro

Exempel 2: Uppgiftslista

Följande klass Uppgiftslista modellerar en lista med uppgifter:

class Uppgiftslista:\n    def __init__(self):\n        self.uppgifter = []\n\n    def tillsatt(self, namn: str, prioritet: int):\n        self.uppgifter.append((prioritet, namn))\n\n    def hamta_nasta(self):\n        self.uppgifter.sort()\n        # Metoden pop tar bort och returnerar sista föremålet på listan\n        uppgift = self.uppgifter.pop()\n        # Returnerar det andra föremålet i tupeln, alltså uppgiftens namn\n        return uppgift[1]\n\n    def sammanlagt(self):\n        return len(self.uppgifter)\n\n    def rensa_uppgifter(self):\n        self.uppgifter = []

Metoden tillagg_uppgift lägger till en ny uppgift på listan. Varje uppgift har också en prioritet, som används för att sortera uppgifterna. Metoden hamta_nasta tar bort och returnerar den uppgift som har högst prioritet i listan. Metoden sammanlagt finns också, som returnerar antalet uppgifter i listan, och slutligen metoden rensa_uppgifter, som rensar uppgiftslistan.

Inom objektet lagras uppgifterna i en lista. Varje uppgift består av en tupel som innehåller uppgiftens prioritet och dess namn. Prioritetsvärdet lagras först, så att den uppgift som har högst prioritet hamnar sist i listan då den sorteras. Därför kan vi sedan helt enkelt använda pop-metoden för att hämta och ta bort det högst prioriterade objektet.

Ta en titt på följande program med uppgiftslistan i handling:

lista = Uppgiftslista()\nlista.tillsatt(\"studier\", 50)\nlista.tillsatt(\"motion\", 60)\nlista.tillsatt(\"städning\", 10)\nprint(lista.sammanlagt())\nprint(lista.hamta_nasta())\nprint(lista.sammanlagt())\nlista.tillsatt(\"dejt\", 100)\nprint(lista.sammanlagt())\nprint(lista.hamta_nasta())\nprint(lista.hamta_nasta())\nprint(lista.sammanlagt())\nlista.rensa_uppgifter()\nprint(lista.sammanlagt())

3\nmotion\n2\n3\ndejt\nstudier\n1\n0

Klassen Serie

Skapa klassen Serie, som fungerar enligt följande:

dexter = Serie(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\nprint(dexter)

Dexter (8 säsonger)\ngenret: Crime, Drama, Mystery, Thriller\ninga betygsättningar

Konstruktorn ska ta titeln, antalet säsonger och en lista med genrer för serien som sina argument.

Tips: när du behöver en sträng från en lista med strängar, där du vill separera med en karaktär av ditt val, kan du använda join metoden på följande sätt:

lista = [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"]\nstrang = \", \".join(lista)\nprint(strang)

Crime, Drama, Mystery, Thriller

Betygsättningar

Skapa en metod betygsatt(betyg: int), med vilken man kan ge ett betyg mellan heltalen 0 och 5. Också metoden __str__ ska ändras som så, att ifall det finns några betyg ska metoden skriva ut mängden betyg och deras medeltal med en decimals noggranhet.

dexter = Serie(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\ndexter.betygsatt(4)\ndexter.betygsatt(5)\ndexter.betygsatt(5)\ndexter.betygsatt(3)\ndexter.betygsatt(0)\nprint(dexter)

Dexter (8 esityskautta)\ngenret: Crime, Drama, Mystery, Thriller\nBetyg 5, medeltal 3.4 poäng

Sökning av serier

Skapa två funktioner betyg_minst(betyg: float, serier: list) och innehallar_genren(genre: str, serier: list), med vilka det är möjligt att hitta serier på listan.

Metoderna fungerar på följande sätt:

s1 = Serie(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\ns1.betygsatt(5)\n\ns2 = Serie(\"South Park\", 24, [\"Animation\", \"Comedy\"])\ns2.betygsatt(3)\n\ns3 = Serie(\"Friends\", 10, [\"Romance\", \"Comedy\"])\ns3.betygsatt(2)\n\nserier = [s1, s2, s3]\n\nprint(\"betyg minst 4.5:\")\nfor serie in betyg_minst(4.5, serier):\n    print(serie.namn)\n\nprint(\"genre Comedy:\")\nfor serie in innehallar_genren(\"Comedy\", serier):\n    print(serie.namn)

betyg minst 4.5:\nDexter

genre Comedy:\nSouth Park\nFriends

Observera att ovanstående kod och testerna för denna övning antar att din klass innehåller attributet namn. Ifall du använder något annat attribut för att hänvisa till namnet på serien, bör du ändra det innan du skickar in uppgifterna.

Vänligen svara på en snabb enkät om veckans material:

","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/5-fler-exempel.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa mer mångsidiga klasser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du lägger till en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod i dina klassdefinitioner"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"exempel-1-klassen-rektangel","style":"position:relative;"},"children":[{"type":"text","value":"Exempel 1: klassen Rektangel"},{"type":"element","tagName":"a","properties":{"href":"#exempel-1-klassen-rektangel","ariaLabel":"exempel 1 klassen rektangel permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en klass som modellerar en rektangel i ett tvådimensionellt rum:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vanster_ovre\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hoger_nedre\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"area"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"omkrets"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"flytta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n horn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"horn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"x_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" horn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"y_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n horn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"horn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"x_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" horn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"y_andring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En ny "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":" skapas med två tuplar som argument. Dessa tuplar innehåller x- och y-koordinaterna för det övre vänstra hörnet och det nedre högra hörnet. Konstruktören beräknar rektangelns höjd och bredd baserat på dessa värden."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"area"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"omkrets"}]},{"type":"text","value":" beräknar rektangelns area och omkrets baserat på höjd och bredd. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"flytta"}]},{"type":"text","value":" flyttar rektangeln med de x- och y-värden som anges som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rektangeln representeras i ett koordinatsystem där x-koordinaterna ökar från vänster till höger och y-koordinaterna ökar från topp till botten. Detta är ett vanligt sätt att hantera koordinater i programmering eftersom det ofta är enklare och mer naturligt att betrakta datorskärmens övre vänstra hörn som den punkt där x och y är lika med noll."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande program testar klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bredd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hojd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"omkrets"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flytta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(1, 1)\n(4, 3)\n3\n2\n10\n6\n(4, 4)\n(7, 6)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"skriva-ut-ett-objekt","style":"position:relative;"},"children":[{"type":"text","value":"Skriva ut ett objekt"},{"type":"element","tagName":"a","properties":{"href":"#skriva-ut-ett-objekt","ariaLabel":"skriva ut ett objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När du har ett objekt som skapats från en klass som du själv definierat, är standardreaktionen på att anropa instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" med objektet som argument inte särskilt informativt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde se ut någorlunda så här:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"<"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"main"}]},{"type":"text","value":".Rektangel object at 0x000002D7BF148A90>"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi vill självklart ha mer kontroll över vad som skrivs ut. Det enklaste sättet att göra detta är att lägga till en speciell "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod i klassdefinitionen. Dess syfte är att returnera en ögonblicksbild av objektets tillstånd i strängformat. Om klassdefinitionen innehåller en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod är det värde som returneras av metoden, det som skrivs ut när instruktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" körs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Så låt oss lägga till en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metoddefinition i vår "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"text","value":"-klass:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rektangel"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Resten av klassen kommer här såsom ovan"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar objektets tillstånd i strängformat"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"rektangel "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vanster_ovre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ... "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hoger_nedre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nu borde "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" instruktionen producera nånting mer användarvänligt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"rektangel (1, 1) ... (4, 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" används kanske oftare för att formulera en strängrepresentation av objektet med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":"-funktionen, som i följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rektangel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbeskrivning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rektangel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"beskrivning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"rektangel (1, 1) ... (4, 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns många fler speciella understrukna metoder som kan definieras för klasser. En metod som liknar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metoden är "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":"-metoden. Dess syfte är att ge en teknisk representation av objektets tillstånd. Vi kommer att stöta på denna metod senare."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Stoppur","tmcname":"osa08-11a_stoppur"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbotten innehåller följande ram för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stoppur"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Stoppur"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sekunder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minuter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bygg ut på klassdefinitionen så att den fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"klocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Stoppur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3600"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"00:00\n00:01\n00:02\n... många fler rader utskrivna\n00:59\n01:00\n01:01\n... många, många fler rader utskrivna\n59:58\n59:59\n00:00\n00:01"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" innebär alltså att en sekund läggs till i stoppuret. Det maximala värdet för både sekunder och minuter är 59. Din klassdefinition bör också innehålla en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod, som returnerar en strängrepresentation av stoppurets tillstånd, som visas i exemplet ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" Det kan göra det lättare att testa "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":"-metoden om du tillfälligt ställer in startvärdena för sekunder och minuter till något värde närmare 59 i konstruktorn. Om du ändrar de ursprungliga värdena, kom ihåg att ändra tillbaka dem innan du skickar in."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Klocka","tmcname":"osa08-12_klocka"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera en ny klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Klocka"}]},{"type":"text","value":", som bygger på Stoppur klassen. Den ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"klocka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"59"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nklocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"installning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klocka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n23:59:55\n23:59:56\n23:59:57\n23:59:58\n23:59:59\n00:00:00\n00:00:01\n12:05:00\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstruktorn tar alltså ursprungliga värden för timmar, minuter och sekunder som argument. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" för klockan framåt en sekond och metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"installning"}]},{"type":"text","value":" ställer in klockans timmar och minuter och "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"nollar sekunderna"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lunchkort","tmcname":"osa08-13_lunchkort"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På Unicafe, studentkafeterian vid Helsingfors universitet, kan studenterna betala för sin lunch med ett särskilt betalkort."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen kommer du att skriva en klass som heter Lunchkort, med syftet att emulera de funktioner som tillhandahålls av Unicafes betalkort."}]},{"type":"element","tagName":"h3","properties":{"id":"strukturen-av-klassen","style":"position:relative;"},"children":[{"type":"text","value":"Strukturen av klassen"},{"type":"element","tagName":"a","properties":{"href":"#strukturen-av-klassen","ariaLabel":"strukturen av klassen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en ny klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa först konstruktorn för klassen. Den ska ta det ursprungliga saldot som finns på kortet som ett argument och spara det som ett attribut. Skapa sedan en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod som returnerar en sträng som innehåller saldot: ”Saldot är X euro”. Det tillgängliga saldot ska skrivas ut med en decimals noggrannhet. Se exemplet nedan för användning."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande är en ram av klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel av användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmet ovan borde producera följande utskrift:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo är 50.0 euro"}]}]},{"type":"element","tagName":"h3","properties":{"id":"betalning-av-lunch","style":"position:relative;"},"children":[{"type":"text","value":"Betalning av lunch"},{"type":"element","tagName":"a","properties":{"href":"#betalning-av-lunch","ariaLabel":"betalning av lunch permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera följande metoder till din Lunchkort klass:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"at_formanligt"}]},{"type":"text","value":" som minskar saldot med 2.60 euro"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"at_special"}]},{"type":"text","value":" som minskar saldot med 4.60 euro"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande huvudfunktion testar din klass:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta borde ge följande utskrift:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo är 50.0 euro\nKortets saldo är 47.4 euro\nKortets saldo är 40.2 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Se till att kortets saldo inte kan bli under 0:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo är 4.0 euro\nKortets saldo är 1.4 euro\nKortets saldo är 1.4 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo får alltså inte minska ifall det inte finns tillräckligt med pengar på det."}]},{"type":"element","tagName":"h3","properties":{"id":"satta-in-pengar-pa-kortet","style":"position:relative;"},"children":[{"type":"text","value":"Sätta in pengar på kortet"},{"type":"element","tagName":"a","properties":{"href":"#satta-in-pengar-pa-kortet","ariaLabel":"satta in pengar pa kortet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"text","value":" till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":"-klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden ska öka saldot på kortet med mängden angivet som argument."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo är 10.0 euro\nKortets saldo är 25.0 euro\nKortets saldo är 35.0 euro\nKortets saldo är 235.0 euro"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall kortet försöker laddas med en negativ mängd ska metoden "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/3-fel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"åstadkomma ett undantag"}]},{"type":"text","value":" av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"File \"testi.py\", line 3, in lunchkort\nValueError: Det går inte att lägga till mindre än noll"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" metoden ska "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"åstadkomma"}]},{"type":"text","value":" ett undantag, se "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/3-fel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":" i materialet hur man gör. Metoden får under inga omständigheter själv skriva ut nånting!"}]},{"type":"element","tagName":"h3","properties":{"id":"flera-kort","style":"position:relative;"},"children":[{"type":"text","value":"Flera kort"},{"type":"element","tagName":"a","properties":{"href":"#flera-kort","ariaLabel":"flera kort permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa ett huvudprogram, som innehåller följande händelser:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Skapa Peters kort. Kortets ursprungliga saldo är 20 euro"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Skapa Mattes kort. Kortets ursprungliga saldo är 30 euro"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter äter en special"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Matte äter förmånligt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter tillsätter 20 euro på kortet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Matte äter en special"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter äter förmånligt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter äter förmånligt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Matte tillsätter 50 euro på kortet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Huvudprogrammets ram:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"peters_kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmattes_kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# resten av huvudprogrammet"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter: Kortets saldo är 15.4 euro\nMatte: Kortets saldo är 27.4 euro\nPeter: Kortets saldo är 35.4 euro\nMatte: Kortets saldo är 22.8 euro\nPeter: Kortets saldo är 30.2 euro\nMatte: Kortets saldo är 72.8 euro"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"exempel-2-uppgiftslista","style":"position:relative;"},"children":[{"type":"text","value":"Exempel 2: Uppgiftslista"},{"type":"element","tagName":"a","properties":{"href":"#exempel-2-uppgiftslista","ariaLabel":"exempel 2 uppgiftslista permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Uppgiftslista"}]},{"type":"text","value":" modellerar en lista med uppgifter:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Uppgiftslista"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" prioritet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prioritet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_nasta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden pop tar bort och returnerar sista föremålet på listan"}]},{"type":"text","value":"\n uppgift "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Returnerar det andra föremålet i tupeln, alltså uppgiftens namn"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" uppgift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sammanlagt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"rensa_uppgifter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uppgifter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillagg_uppgift"}]},{"type":"text","value":" lägger till en ny uppgift på listan. Varje uppgift har också en prioritet, som används för att sortera uppgifterna. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hamta_nasta"}]},{"type":"text","value":" tar bort och returnerar den uppgift som har högst prioritet i listan. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sammanlagt"}]},{"type":"text","value":" finns också, som returnerar antalet uppgifter i listan, och slutligen metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rensa_uppgifter"}]},{"type":"text","value":", som rensar uppgiftslistan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inom objektet lagras uppgifterna i en lista. Varje uppgift består av en tupel som innehåller uppgiftens prioritet och dess namn. Prioritetsvärdet lagras först, så att den uppgift som har högst prioritet hamnar sist i listan då den sorteras. Därför kan vi sedan helt enkelt använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"-metoden för att hämta och ta bort det högst prioriterade objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ta en titt på följande program med uppgiftslistan i handling:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Uppgiftslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studier\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"motion\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"städning\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"dejt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_nasta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rensa_uppgifter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanlagt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\nmotion\n2\n3\ndejt\nstudier\n1\n0"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sarja","tmcname":"osa08-14_sarja"},"children":[{"type":"element","tagName":"h3","properties":{"id":"klassen-serie","style":"position:relative;"},"children":[{"type":"text","value":"Klassen Serie"},{"type":"element","tagName":"a","properties":{"href":"#klassen-serie","ariaLabel":"klassen serie permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Serie"}]},{"type":"text","value":", som fungerar enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dexter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Crime\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drama\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mystery\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"dexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dexter (8 säsonger)\ngenret: Crime, Drama, Mystery, Thriller\ninga betygsättningar"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Konstruktorn ska ta titeln, antalet säsonger och en lista med genrer för serien som sina argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips:"}]},{"type":"text","value":" när du behöver en sträng från en lista med strängar, där du vill separera med en karaktär av ditt val, kan du använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" metoden på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Crime\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drama\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mystery\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstrang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\", \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Crime, Drama, Mystery, Thriller"}]}]},{"type":"element","tagName":"h3","properties":{"id":"betygsattningar","style":"position:relative;"},"children":[{"type":"text","value":"Betygsättningar"},{"type":"element","tagName":"a","properties":{"href":"#betygsattningar","ariaLabel":"betygsattningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"betygsatt(betyg: int)"}]},{"type":"text","value":", med vilken man kan ge ett betyg mellan heltalen 0 och 5. Också metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" ska ändras som så, att ifall det finns några betyg ska metoden skriva ut mängden betyg och deras medeltal med en decimals noggranhet."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dexter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Crime\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drama\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mystery\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"dexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dexter (8 esityskautta)\ngenret: Crime, Drama, Mystery, Thriller\nBetyg 5, medeltal 3.4 poäng"}]}]},{"type":"element","tagName":"h3","properties":{"id":"sokning-av-serier","style":"position:relative;"},"children":[{"type":"text","value":"Sökning av serier"},{"type":"element","tagName":"a","properties":{"href":"#sokning-av-serier","ariaLabel":"sokning av serier permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa två funktioner "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"betyg_minst(betyg: float, serier: list)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"innehallar_genren(genre: str, serier: list)"}]},{"type":"text","value":", med vilka det är möjligt att hitta serier på listan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoderna fungerar på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Crime\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Drama\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mystery\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"South Park\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Animation\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Comedy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Friends\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Romance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Comedy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"betygsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nserier "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"betyg minst 4.5:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" serie "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" betyg_minst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" serier"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"genre Comedy:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" serie "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" innehallar_genren"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Comedy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" serier"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"serie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"betyg minst 4.5:\nDexter"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"genre Comedy:\nSouth Park\nFriends"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Observera att ovanstående kod och testerna för denna övning antar att din klass innehåller attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":". Ifall du använder något annat attribut för att hänvisa till namnet på serien, bör du ändra det innan du skickar in uppgifterna."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vänligen svara på en snabb enkät om veckans material:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"a02d084c-8f0c-5918-8740-6282910a2b33"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att kunna skapa mer mångsidiga klasser
  • \n
  • Vet du hur du lägger till en __str__-metod i dina klassdefinitioner
  • \n

Exempel 1: klassen Rektangel

Låt oss ta en titt på en klass som modellerar en rektangel i ett tvådimensionellt rum:

class Rektangel:\n    def __init__(self, vanster_ovre: tuple, hoger_nedre: tuple):\n        self.vanster_ovre = vanster_ovre\n        self.hoger_nedre = hoger_nedre\n        self.bredd = hoger_nedre[0]-vanster_ovre[0]\n        self.hojd = hoger_nedre[1]-vanster_ovre[1]\n\n    def area(self):\n        return self.bredd * self.hojd\n\n    def omkrets(self):\n        return self.bredd * 2 + self.hojd * 2\n\n    def flytta(self, x_andring: int, y_andring: int):\n        horn = self.vanster_ovre\n        self.vanster_ovre = (horn[0]+x_andring, horn[1]+y_andring)\n        horn = self.hoger_nedre\n        self.hoger_nedre = (horn[0]+x_andring, horn[1]+y_andring)

En ny Rektangel skapas med två tuplar som argument. Dessa tuplar innehåller x- och y-koordinaterna för det övre vänstra hörnet och det nedre högra hörnet. Konstruktören beräknar rektangelns höjd och bredd baserat på dessa värden.

Metoderna area och omkrets beräknar rektangelns area och omkrets baserat på höjd och bredd. Metoden flytta flyttar rektangeln med de x- och y-värden som anges som argument.

Rektangeln representeras i ett koordinatsystem där x-koordinaterna ökar från vänster till höger och y-koordinaterna ökar från topp till botten. Detta är ett vanligt sätt att hantera koordinater i programmering eftersom det ofta är enklare och mer naturligt att betrakta datorskärmens övre vänstra hörn som den punkt där x och y är lika med noll.

Följande program testar klassen Rektangel:

rektangel = Rektangel((1, 1), (4, 3))\nprint(rektangel.vanster_ovre)\nprint(rektangel.hoger_nedre)\nprint(rektangel.bredd)\nprint(rektangel.hojd)\nprint(rektangel.omkrets())\nprint(rektangel.area())\n\nrektangel.flytta(3, 3)\nprint(rektangel.vanster_ovre)\nprint(rektangel.hoger_nedre)

(1, 1)\n(4, 3)\n3\n2\n10\n6\n(4, 4)\n(7, 6)

Skriva ut ett objekt

När du har ett objekt som skapats från en klass som du själv definierat, är standardreaktionen på att anropa instruktionen print med objektet som argument inte särskilt informativt:

rektangel = Rektangel((1, 1), (4, 3))\nprint(rektangel)

Utskriften borde se ut någorlunda så här:

<main.Rektangel object at 0x000002D7BF148A90>

Vi vill självklart ha mer kontroll över vad som skrivs ut. Det enklaste sättet att göra detta är att lägga till en speciell __str__-metod i klassdefinitionen. Dess syfte är att returnera en ögonblicksbild av objektets tillstånd i strängformat. Om klassdefinitionen innehåller en __str__-metod är det värde som returneras av metoden, det som skrivs ut när instruktionen print körs.

Så låt oss lägga till en __str__-metoddefinition i vår Rektangel-klass:

class Rektangel:\n\n    # Resten av klassen kommer här såsom ovan\n\n    # Metoden returnerar objektets tillstånd i strängformat\n    def __str__(self):\n        return f\"rektangel {self.vanster_ovre} ... {self.hoger_nedre}\"

Nu borde print instruktionen producera nånting mer användarvänligt:

rektangel = Rektangel((1, 1), (4, 3))\nprint(rektangel)

rektangel (1, 1) ... (4, 3)

Metoden __str__ används kanske oftare för att formulera en strängrepresentation av objektet med str-funktionen, som i följande program:

rektangel = Rektangel((1, 1), (4, 3))\nbeskrivning = str(rektangel)\nprint(beskrivning)

rektangel (1, 1) ... (4, 3)

Det finns många fler speciella understrukna metoder som kan definieras för klasser. En metod som liknar __str__-metoden är __repr__-metoden. Dess syfte är att ge en teknisk representation av objektets tillstånd. Vi kommer att stöta på denna metod senare.

Uppgiftsbotten innehåller följande ram för klassen Stoppur:

class Stoppur:\n    def __init__(self):\n        self.sekunder = 0\n        self.minuter = 0

Bygg ut på klassdefinitionen så att den fungerar enligt följande:

klocka = Stoppur()\nfor i in range(3600):\n    print(klocka)\n    klocka.tick()

00:00\n00:01\n00:02\n... många fler rader utskrivna\n00:59\n01:00\n01:01\n... många, många fler rader utskrivna\n59:58\n59:59\n00:00\n00:01

Metoden tick innebär alltså att en sekund läggs till i stoppuret. Det maximala värdet för både sekunder och minuter är 59. Din klassdefinition bör också innehålla en __str__-metod, som returnerar en strängrepresentation av stoppurets tillstånd, som visas i exemplet ovan.

Tips: Det kan göra det lättare att testa tick-metoden om du tillfälligt ställer in startvärdena för sekunder och minuter till något värde närmare 59 i konstruktorn. Om du ändrar de ursprungliga värdena, kom ihåg att ändra tillbaka dem innan du skickar in.

Definiera en ny klass Klocka, som bygger på Stoppur klassen. Den ska fungera enligt följande:

klocka = Klocka(23, 59, 55)\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\nklocka.tick()\nprint(klocka)\n\nklocka.installning(12, 5)\nprint(klocka)
\n23:59:55\n23:59:56\n23:59:57\n23:59:58\n23:59:59\n00:00:00\n00:00:01\n12:05:00\n

Konstruktorn tar alltså ursprungliga värden för timmar, minuter och sekunder som argument. Metoden tick för klockan framåt en sekond och metoden installning ställer in klockans timmar och minuter och nollar sekunderna.

På Unicafe, studentkafeterian vid Helsingfors universitet, kan studenterna betala för sin lunch med ett särskilt betalkort.

I den här övningen kommer du att skriva en klass som heter Lunchkort, med syftet att emulera de funktioner som tillhandahålls av Unicafes betalkort.

Strukturen av klassen

Skapa en ny klass med namnet Lunchkort.

Skapa först konstruktorn för klassen. Den ska ta det ursprungliga saldot som finns på kortet som ett argument och spara det som ett attribut. Skapa sedan en __str__-metod som returnerar en sträng som innehåller saldot: ”Saldot är X euro”. Det tillgängliga saldot ska skrivas ut med en decimals noggrannhet. Se exemplet nedan för användning.

Följande är en ram av klassen:

class Lunchkort:\n    def __init__(self, saldo: float):\n        self.saldo = saldo\n\n    def __str__(self):\n        pass

Exempel av användning:

kort = Lunchkort(50)\nprint(kort)

Programmet ovan borde producera följande utskrift:

Kortets saldo är 50.0 euro

Betalning av lunch

Implementera följande metoder till din Lunchkort klass:

    \n
  • at_formanligt som minskar saldot med 2.60 euro
  • \n
  • at_special som minskar saldot med 4.60 euro
  • \n

Följande huvudfunktion testar din klass:

kort = Lunchkort(50)\nprint(kort)\n\nkort.at_formanligt()\nprint(kort)\n\nkort.at_special()\nkort.at_formanligt()\nprint(kort)

Detta borde ge följande utskrift:

Kortets saldo är 50.0 euro\nKortets saldo är 47.4 euro\nKortets saldo är 40.2 euro

Se till att kortets saldo inte kan bli under 0:

kort = Lunchkort(4)\nprint(kort)\n\nkort.at_formanligt()\nprint(kort)\n\nkort.at_formanligt()\nprint(kort)

Kortets saldo är 4.0 euro\nKortets saldo är 1.4 euro\nKortets saldo är 1.4 euro

Kortets saldo får alltså inte minska ifall det inte finns tillräckligt med pengar på det.

Sätta in pengar på kortet

Lägg till metoden tillsatt_pengar till Lunchkort-klassen.

Metoden ska öka saldot på kortet med mängden angivet som argument.

kort = Lunchkort(10)\nprint(kort)\nkort.tillsatt_pengar(15)\nprint(kort)\nkort.tillsatt_pengar(10)\nprint(kort)\nkort.tillsatt_pengar(200)\nprint(kort)

Kortets saldo är 10.0 euro\nKortets saldo är 25.0 euro\nKortets saldo är 35.0 euro\nKortets saldo är 235.0 euro

Ifall kortet försöker laddas med en negativ mängd ska metoden åstadkomma ett undantag av typen ValueError:

kort = Lunchkort(10)\nkort.tillsatt_pengar(-10)

File \"testi.py\", line 3, in lunchkort\nValueError: Det går inte att lägga till mindre än noll

OBS: metoden ska åstadkomma ett undantag, se modul 6 i materialet hur man gör. Metoden får under inga omständigheter själv skriva ut nånting!

Flera kort

Skapa ett huvudprogram, som innehåller följande händelser:

    \n
  • Skapa Peters kort. Kortets ursprungliga saldo är 20 euro
  • \n
  • Skapa Mattes kort. Kortets ursprungliga saldo är 30 euro
  • \n
  • Peter äter en special
  • \n
  • Matte äter förmånligt
  • \n
  • Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)
  • \n
  • Peter tillsätter 20 euro på kortet
  • \n
  • Matte äter en special
  • \n
  • Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)
  • \n
  • Peter äter förmånligt
  • \n
  • Peter äter förmånligt
  • \n
  • Matte tillsätter 50 euro på kortet
  • \n
  • Kortens saldon skrivs ut (På varsin rad, med ägarens namn i början av raden)
  • \n

Huvudprogrammets ram:

peters_kort = Lunchkort(20)\nmattes_kort = Lunchkort(30)\n# resten av huvudprogrammet

Utskriften borde vara följande:

Peter: Kortets saldo är 15.4 euro\nMatte: Kortets saldo är 27.4 euro\nPeter: Kortets saldo är 35.4 euro\nMatte: Kortets saldo är 22.8 euro\nPeter: Kortets saldo är 30.2 euro\nMatte: Kortets saldo är 72.8 euro

Exempel 2: Uppgiftslista

Följande klass Uppgiftslista modellerar en lista med uppgifter:

class Uppgiftslista:\n    def __init__(self):\n        self.uppgifter = []\n\n    def tillsatt(self, namn: str, prioritet: int):\n        self.uppgifter.append((prioritet, namn))\n\n    def hamta_nasta(self):\n        self.uppgifter.sort()\n        # Metoden pop tar bort och returnerar sista föremålet på listan\n        uppgift = self.uppgifter.pop()\n        # Returnerar det andra föremålet i tupeln, alltså uppgiftens namn\n        return uppgift[1]\n\n    def sammanlagt(self):\n        return len(self.uppgifter)\n\n    def rensa_uppgifter(self):\n        self.uppgifter = []

Metoden tillagg_uppgift lägger till en ny uppgift på listan. Varje uppgift har också en prioritet, som används för att sortera uppgifterna. Metoden hamta_nasta tar bort och returnerar den uppgift som har högst prioritet i listan. Metoden sammanlagt finns också, som returnerar antalet uppgifter i listan, och slutligen metoden rensa_uppgifter, som rensar uppgiftslistan.

Inom objektet lagras uppgifterna i en lista. Varje uppgift består av en tupel som innehåller uppgiftens prioritet och dess namn. Prioritetsvärdet lagras först, så att den uppgift som har högst prioritet hamnar sist i listan då den sorteras. Därför kan vi sedan helt enkelt använda pop-metoden för att hämta och ta bort det högst prioriterade objektet.

Ta en titt på följande program med uppgiftslistan i handling:

lista = Uppgiftslista()\nlista.tillsatt(\"studier\", 50)\nlista.tillsatt(\"motion\", 60)\nlista.tillsatt(\"städning\", 10)\nprint(lista.sammanlagt())\nprint(lista.hamta_nasta())\nprint(lista.sammanlagt())\nlista.tillsatt(\"dejt\", 100)\nprint(lista.sammanlagt())\nprint(lista.hamta_nasta())\nprint(lista.hamta_nasta())\nprint(lista.sammanlagt())\nlista.rensa_uppgifter()\nprint(lista.sammanlagt())

3\nmotion\n2\n3\ndejt\nstudier\n1\n0

Klassen Serie

Skapa klassen Serie, som fungerar enligt följande:

dexter = Serie(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\nprint(dexter)

Dexter (8 säsonger)\ngenret: Crime, Drama, Mystery, Thriller\ninga betygsättningar

Konstruktorn ska ta titeln, antalet säsonger och en lista med genrer för serien som sina argument.

Tips: när du behöver en sträng från en lista med strängar, där du vill separera med en karaktär av ditt val, kan du använda join metoden på följande sätt:

lista = [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"]\nstrang = \", \".join(lista)\nprint(strang)

Crime, Drama, Mystery, Thriller

Betygsättningar

Skapa en metod betygsatt(betyg: int), med vilken man kan ge ett betyg mellan heltalen 0 och 5. Också metoden __str__ ska ändras som så, att ifall det finns några betyg ska metoden skriva ut mängden betyg och deras medeltal med en decimals noggranhet.

dexter = Serie(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\ndexter.betygsatt(4)\ndexter.betygsatt(5)\ndexter.betygsatt(5)\ndexter.betygsatt(3)\ndexter.betygsatt(0)\nprint(dexter)

Dexter (8 esityskautta)\ngenret: Crime, Drama, Mystery, Thriller\nBetyg 5, medeltal 3.4 poäng

Sökning av serier

Skapa två funktioner betyg_minst(betyg: float, serier: list) och innehallar_genren(genre: str, serier: list), med vilka det är möjligt att hitta serier på listan.

Metoderna fungerar på följande sätt:

s1 = Serie(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\ns1.betygsatt(5)\n\ns2 = Serie(\"South Park\", 24, [\"Animation\", \"Comedy\"])\ns2.betygsatt(3)\n\ns3 = Serie(\"Friends\", 10, [\"Romance\", \"Comedy\"])\ns3.betygsatt(2)\n\nserier = [s1, s2, s3]\n\nprint(\"betyg minst 4.5:\")\nfor serie in betyg_minst(4.5, serier):\n    print(serie.namn)\n\nprint(\"genre Comedy:\")\nfor serie in innehallar_genren(\"Comedy\", serier):\n    print(serie.namn)

betyg minst 4.5:\nDexter

genre Comedy:\nSouth Park\nFriends

Observera att ovanstående kod och testerna för denna övning antar att din klass innehåller attributet namn. Ifall du använder något annat attribut för att hänvisa till namnet på serien, bör du ändra det innan du skickar in uppgifterna.

Vänligen svara på en snabb enkät om veckans material:

","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/5-fler-exempel.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-8/page-data.json b/page-data/osa-8/page-data.json index 29e7905b8..e6b05bc17 100644 --- a/page-data/osa-8/page-data.json +++ b/page-data/osa-8/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-8", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-8","title":"Modul 8"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-8","title":"Modul 8"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-8/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-9/1-objekt-och-referenser/page-data.json b/page-data/osa-9/1-objekt-och-referenser/page-data.json index 3c419fc38..9f918354c 100644 --- a/page-data/osa-9/1-objekt-och-referenser/page-data.json +++ b/page-data/osa-9/1-objekt-och-referenser/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-9/1-objekt-och-referenser", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda olika datastrukturer för att hantera objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur objekt kan bli passerade som argument"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje värde i Python är ett objekt. Alla objekt som du skapar baserat på en klass som du själv har definierat fungerar exakt på samma sätt som alla \"vanliga\" Python-objekt. Objekt kan till exempel lagras i en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" slutforsdatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kurs\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slutforsdatum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" slutforsdatum\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar några slutförda kurser och lägger dessa i en lista"}]},{"type":"text","value":"\n prestationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n mat1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matematik 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n prg1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programmering 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prg1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi lägger några till rakt till listan"}]},{"type":"text","value":"\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fysik 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programmering 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi går igenom alla slutförda kurser, skriver ut deras namn och räknar ihop den totala mängden studiepoäng"}]},{"type":"text","value":"\n studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studiepoäng totalt:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Matematik 1\nProgrammering 1\nFysik 2\nProgrammering 2\nStudiepoäng totalt: 20"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Snabbaste bilen","tmcname":"osa09-01_snabbaste_bil"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbotten har en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bil"}]},{"type":"text","value":" som representerar en bil genom två attribut: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"marke (str)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"topphastighet (int)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"snabbaste_bil(bilar: list)"}]},{"type":"text","value":", som får en lista av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bil"}]},{"type":"text","value":"-objekt som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera märket på den snabbaste bilen. Du kan anta att det alltid kommer att finnas en enda bil med den högsta topphastigheten. Ändra inte listan som ges som argument, gör heller inte några ändringar i klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bil"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda följande kod för att testa din funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bil1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mersu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"195"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bil2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lada\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bil3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ferrari\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"280"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bil4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Trabant\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n bilar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"bil1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bil2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bil3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bil4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"snabbaste_bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bilar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ferrari"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Godkända prestationer","tmcname":"osa09-02_godkanda_prestationer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten hittas klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Provprestation"}]},{"type":"text","value":", som, liksom namnet anger, tar modell av en provtagares prestation i ett prov. Den har två attribut, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"presterare (str)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"poang (int)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"godkanda(prestationer: list, poanggrans: int)"}]},{"type":"text","value":", som får som parametrar en lista av provprestationer och ett heltal som representerar det minsta poängantalet för att bli godkänd."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska skapa och returnera en ny lista, som innehåller endast de prov som kom över poänggränsen i den ursprungliga listan. Ändra inte listan som ges som argument, ändra heller inte definitionen för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Provprestation"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda följande kod för att testa funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Per\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n klarade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" godkanda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" godkand "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" klarade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klarade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Provprestation (presterare: Pernilla, poang: 19)\nProvprestation (presterare: Per, poang: 15)\nProvprestation (presterare: Petra, poang: 17)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kanske minns att listor inte innehåller några objekt i sig själva. De innehåller referenser till objekt. Exakt samma objekt kan förekomma flera gånger i en och samma lista, och det kan refereras till flera gånger i listan eller utanför den. Låt oss ta en titt på ett exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" enhet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"enhet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" enhet\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n affarslista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n mjolk "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mjölk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"liter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mjolk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mjolk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gurka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"st\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 313px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a3722ba9326d57861bd87ed8593aab50/80977/9_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 64.34782608695653%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABi0lEQVQ4y22TfW+CQAzG+f7faokhbH9MBgOdC5NXMcZ35VX0WZ5m5w60CTnu2v7aa3sG/uR2u8ma5zm22y0OhwP2+718u90Oq9VKmaKqKiyXSxyPx7u+LEt4ngdjCKTBM6GTEjrzG0qapo/AxSLHp+MiTlJMPA/j8QfquukBm6bBTxDAdT4RhhHs9zGSJEWSJE+AWYrRy4sYv1oWoiiWc5ZBBwbfXzBHJmazGdJsIedRFD0CN5uNXKdtWlRVjev1KjodSD1r3HUd2rYVG0ocx0+AmqMuDKQDT6fTg00Yho9A1orO6/Uavu8jCAJZ2Vkll8tF9OzydDoVm/P53M9Qh/43aCFOXNW50imhrq7r+95QRjpM/bP4Us+2FfBQeO2iKHp+PeAwQxadg84MuNKZQbjnvxp23c8YRtXBzGA+n4sjC84a8YWwztxnWdYDypXfrFd82A58f4KyrCQ6R4QQ27Z7s6c6rCCq03oSRlkUYsQudt21F5GDSijfqGma0lHLsuA4DlzXlaEeZvgL31rp5spmjHgAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a3722ba9326d57861bd87ed8593aab50/a0b58/9_1_1.webp 230w","/static/a3722ba9326d57861bd87ed8593aab50/12766/9_1_1.webp 313w"],"sizes":"(max-width: 313px) 100vw, 313px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a3722ba9326d57861bd87ed8593aab50/81c8e/9_1_1.png 230w","/static/a3722ba9326d57861bd87ed8593aab50/80977/9_1_1.png 313w"],"sizes":"(max-width: 313px) 100vw, 313px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a3722ba9326d57861bd87ed8593aab50/80977/9_1_1.png","alt":"9 1 1","title":"9 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det finns mer än en referens till samma objekt spelar det ingen roll vilken av referenserna som används:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Hund"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn\n\nhundar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nmolly "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Molly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Molly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hundar i början:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" hund "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hunden på index 0 får ett nytt namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rex\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" hund "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hunden på index 2 får ett nytt namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fifi\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" hund "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koirat alussa:\nMolly\nMolly\nMolly\nHunden på index 0 får ett nytt namn::\nRex\nRex\nMolly\nHunden på index 2 får ett nytt namn:\nRex\nRex\nFifi"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Referenserna på index 0 och 1 i listan hänvisar till samma objekt. Var och en av referenserna kan användas för att komma åt objektet. Referensen på index 2 hänvisar till ett annat objekt, men med till synes samma innehåll. Om innehållet i det senare objektet ändras påverkas inte det andra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" används för att kontrollera om de två referenserna hänvisar till exakt samma objekt, medan operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" talar om för dig om innehållet i objekten är detsamma. Följande exempel gör förhoppningsvis skillnaden tydlig:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista1\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" lista3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" lista3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" lista3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" lista3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue\nFalse"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nTrue"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla Python-objekt kan också lagras i en ordlista eller någon annan datastruktur. Detta gäller även objekt som är av en klass som du själv har definierat."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sp\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi använder studerandenummer som nyckel och värdet som fås är ett objekt av typen Studerande"}]},{"type":"text","value":"\n studeranden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olle Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ove Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"67"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" kan hjälpa dig att förstå exemplet ovan:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d20c7f9a5570bf04d75c3836868a8870/15171/9_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 43.04347826086957%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABPklEQVQoz2WR20rEMBBA/f+/8k0RFUVExdVda7ubtnHbSSfN3FLtVlHWA4E85DBnyEkYUow44Oj36PexB4wRpx9yzvPJ2cymf5xQSuM4ElHre1fvmSX/IGqA4xBHFgkBzPRYHgZ0BwIAMxFRSslynqbJeTi9uC9cr6ptceHr8zBcxXiNeKXazzIz9wcAYMkzs3yQY6Jy14ZILAJ+ReNONIqgCJrJLHddt1q9FEVRVVXT1AAQQmDmpXzJU5WuOuu7x3nv/CcbADabTVG873aurhvnnPfeOdc0zSHh9+lIXL7ddPvbMNwzl7OMiCmlnKcwUB+Sqi3xfycvmJl3dxHvRB5Ui28ZIxIRM5uZqorIcjn6HjMt15feX0K4IXqb5cQWQ6jK8nW93m63bdsy8zLnaPI05fDxHOOTyLNq9SV/Av8dBuHbnwNlAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d20c7f9a5570bf04d75c3836868a8870/a0b58/9_1_2.webp 230w","/static/d20c7f9a5570bf04d75c3836868a8870/bc10c/9_1_2.webp 460w","/static/d20c7f9a5570bf04d75c3836868a8870/966d8/9_1_2.webp 920w","/static/d20c7f9a5570bf04d75c3836868a8870/445df/9_1_2.webp 1380w","/static/d20c7f9a5570bf04d75c3836868a8870/78de1/9_1_2.webp 1840w","/static/d20c7f9a5570bf04d75c3836868a8870/995bd/9_1_2.webp 2230w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d20c7f9a5570bf04d75c3836868a8870/81c8e/9_1_2.png 230w","/static/d20c7f9a5570bf04d75c3836868a8870/08a84/9_1_2.png 460w","/static/d20c7f9a5570bf04d75c3836868a8870/c0255/9_1_2.png 920w","/static/d20c7f9a5570bf04d75c3836868a8870/b1001/9_1_2.png 1380w","/static/d20c7f9a5570bf04d75c3836868a8870/161ec/9_1_2.png 1840w","/static/d20c7f9a5570bf04d75c3836868a8870/15171/9_1_2.png 2230w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d20c7f9a5570bf04d75c3836868a8870/c0255/9_1_2.png","alt":"9 1 2","title":"9 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"self-eller-inget-self","style":"position:relative;"},"children":[{"type":"text","value":"Self eller inget self?"},{"type":"element","tagName":"a","properties":{"href":"#self-eller-inget-self","ariaLabel":"self eller inget self permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi bara snuddat vid ytan när det gäller att använda parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". Låt oss titta närmare på när det bör eller inte bör användas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan har vi en enkel klass som låter oss skapa ett ordförråd-objekt som innehåller några ord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nordforrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Ordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"objekt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"objekt-orienterad programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"objekt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nörd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"nörd\nobjekt\nobjekt-orienterad programmering\npython"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan med ord lagras i ett attribut med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.ord"}]},{"type":"text","value":". I det här fallet är parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" obligatoriskt både i klassens konstruktormetod och i alla andra metoder som använder variabeln. Om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" utelämnas kommer de olika metoderna inte att få tillgång till samma lista med ord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss lägga till en ny metod i vår klassdefinition. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta_ord(self)"}]},{"type":"text","value":" returnerar (ett av) de längsta orden i ordförrådet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande är ett sätt att utföra denna uppgift, men vi kommer snart att se att det inte är ett särskilt bra sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langsta_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi definierar två hjälpvariabler"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta_langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här metoden använder två hjälpvariabler som deklareras med parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". Kom ihåg att namnen på variablerna inte spelar någon roll i funktionell mening, så dessa variabler kan också namnges mer förvirrande som till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hjalpare"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hjalpare2"}]},{"type":"text","value":". Koden börjar se lite kryptisk ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langsta_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi definierar två hjälpvariabler"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När en variabel deklareras med parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" blir den ett attribut till objektet. Detta innebär att variabeln kommer att existera så länge objektet existerar. Specifikt kommer variabeln att fortsätta existera även efter att metoden som deklarerar den har avslutat sin exekvering (engelska “Execution”). I exemplet ovan är detta helt onödigt, eftersom hjälpvariablerna endast är avsedda att användas inom metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"longest_word(self)"}]},{"type":"text","value":". Så att deklarera hjälpvariabler med parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" är inte en särskilt bra idé här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förutom att variabler kan existera efter sitt \"utgångsdatum\" kan användning av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" för att skapa nya attribut där de inte är nödvändiga orsaka svåra buggar i din kod. Särskilt generiskt namngivna attribut som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.hjalpare"}]},{"type":"text","value":", som sedan används i flera olika metoder, kan orsaka oväntade beteenden som är svåra att spåra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om t.ex. en hjälpvariabel deklareras som ett attribut och tilldelas ett ursprungligt värde i konstruktorn, men variabeln sedan används i ett orelaterat sammanhang i en annan metod, blir resultatet ofta oförutsägbart:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi definierar hjälparvariabler"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langsta_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# detta fungerar inte eftersom hjalpare2 har fel typ"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man skulle kunna tro att detta skulle lösas genom att bara deklarera attributen där de används, utanför konstruktorn, men detta resulterar i en situation där de attribut som är tillgängliga via ett objekt är beroende av vilka metoder som har utförts. I föregående del såg vi att fördelen med att deklarera attribut i konstruktorn är att alla instanser av klassen då kommer att ha exakt samma attribut. Om så inte är fallet kan det lätt leda till fel om man använder olika instanser av klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sammanfattningsvis, om du behöver hjälpvariabler för användning inom en enda metod, är det korrekta sättet att göra det utan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". För att göra din kod lättare att förstå, använd också informativa variabelnamn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langsta_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# detta är det korrekta sättet att definiera"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpvariabler för användning i en enda metod"}]},{"type":"text","value":"\n langsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n langsta_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" langsta_langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n langsta_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n langsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" langsta"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I implementeringen ovan är hjälpvariablerna endast tillgängliga när metoden utförs. De värden som lagras i dem kan inte orsaka komplikationer i andra delar av programmet."}]},{"type":"element","tagName":"h2","properties":{"id":"objekt-som-argument-till-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Objekt som argument till funktioner"},{"type":"element","tagName":"a","properties":{"href":"#objekt-som-argument-till-funktioner","ariaLabel":"objekt som argument till funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De objekt som skapas baserat på våra egna klasser är vanligtvis mutabla. Du kanske kommer ihåg att till exempel Python-listor är föränderliga: när de passeras som argument till funktioner kan deras innehåll ändras som ett resultat av exekveringen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett enkelt exempel där en funktion får en referens till ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" som sitt argument. Funktionen ändrar sedan namnet på studenten. Både funktionen och huvudfunktionen som anropar den har åtkomst till samma objekt, så ändringen syns även i huvudfunktionen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# observera att typledtråden använder namnet på klassen definierad ovan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"andra_namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olle Studerande\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skapa ett Studerande-objekt"}]},{"type":"text","value":"\nolle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olle Elev\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"olle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nandra_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"olle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"olle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olle Elev (12345)\nOlle Studerande (12345)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att skapa objekt inom funktioner. Om en funktion returnerar en referens till det nyskapade objektet är det också åtkomligt inom huvudfunktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" choice\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna funktion skapar och returnerar ett nytt Studerande-objekt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den väljer slumpmässigt värden för namnet och studerandenumret."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ny_studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fornamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Atte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maria\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n efternamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Virtanen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lahtinen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Leinonen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Slumpmässigt namn"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fornamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Slumpmässigt studerandenummer"}]},{"type":"text","value":"\n studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99999"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skapa och returnera ett Studerande-objekt"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kalla funktionen fem gånger och spara resultatet i en lista"}]},{"type":"text","value":"\n studeranden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ny_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut resultatet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du kör ovanstående kan det resultera i följande utskrift (OBS: eftersom slumpen är inblandad kommer resultaten sannolikt att bli annorlunda om du testar koden själv)."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Maria Lahtinen (36213)\nAtte Virtanen (11859)\nMaria Pythonen (77330)\nAtte Pythonson (86451)\nMinna Pythonson (86211)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"objekt-som-argument-till-metoder","style":"position:relative;"},"children":[{"type":"text","value":"Objekt som argument till metoder"},{"type":"element","tagName":"a","properties":{"href":"#objekt-som-argument-till-metoder","ariaLabel":"objekt som argument till metoder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På liknande sätt kan objekt fungera som argument till metoder. Låt oss ta en titt på ett exempel från en nöjespark:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Akattraktion"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" langdgrans"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"besokare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langdgrans "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langdgrans\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ta_ombord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langdgrans"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"besokare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kom ombord\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" var för kort :(\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"besokare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" besökare)\""}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Attraktionen innehåller en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"motta_besökare"}]},{"type":"text","value":", som tar ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" som argument. Om besökaren är tillräckligt lång släpps denne ombord och antalet besökare ökas. Klasserna kan testas på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"berg_och_dalbana "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Akattraktion"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Berg_och_dalbana\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"120"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njakob "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jakob\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvilma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilma\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"105"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nberg_och_dalbana"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ta_ombord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jakob"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nberg_och_dalbana"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ta_ombord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vilma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berg_och_dalbana"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jakob kom ombord\nVenla var för kort :(\nBerg_och_dalbana (1 besökare)"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bebiscenter","tmcname":"osa09-03_bebiscenter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns färdigt redan en klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" såväl som en ram för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":". Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":"-objekt utför olika handlingar såsom mäter eller matar personer. I denna uppgift implementerar du resten av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":"-klassen. Koden för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" ska inte ändras överhuvudtaget!"}]},{"type":"element","tagName":"h2","properties":{"id":"vagning-av-person","style":"position:relative;"},"children":[{"type":"text","value":"Vägning av person"},{"type":"element","tagName":"a","properties":{"href":"#vagning-av-person","ariaLabel":"vagning av person permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":"-klassens definitione finns en ram för funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vagning"}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"vagning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# returnera vikten av personen som passerades som argument"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden tar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":"-objekt som sitt argument. Den ska returnera personens vikt. Du kan komma åt en persons vikt genom attributet som definieras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":"-klassen. Vänligen fyll i resten av implementationen för metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vagning"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan finns ett exempel på en huvudfunktion där ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":" väger två olika "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":"-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"haga_barnvard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BebisCenter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nerik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"176"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik väger 7 kg\nPeter väger 85 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"matning","style":"position:relative;"},"children":[{"type":"text","value":"Matning"},{"type":"element","tagName":"a","properties":{"href":"#matning","ariaLabel":"matning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är möjligt att ändra tillståndet för ett objekt som skickas som ett argument. Implementera metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mata(person: Person)"}]},{"type":"text","value":" som ökar vikten på den person som skickas som argument med ett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel vägs två personer, och sedan matas en av dem tre gånger. Därefter vägs personerna igen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"haga_barnvard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCenter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nerik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"176"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde visa att Eriks vikt har stigit med tre:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik väger 7 kg\nPeter väger 85 kg"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik väger 10 kg\nPeter väger 85 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"raknande-av-vagningar","style":"position:relative;"},"children":[{"type":"text","value":"Räknande av vägningar"},{"type":"element","tagName":"a","properties":{"href":"#raknande-av-vagningar","ariaLabel":"raknande av vagningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vagningar"}]},{"type":"text","value":"() som returnerar det totala antalet vägningar som ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCenter"}]},{"type":"text","value":"-objekt har utfört. OBS: Du kommer att behöva ett nytt attribut för att hålla reda på antalet vägningar. Du kan använda följande kod för att testa din metod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"haga_barnvard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCenter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nerik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"176"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Vägningar utförda "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Vägningar utförda "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Vägningar utförda "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vägningar utförda 0\nVägningar utförda 2\nVägningar utförda 6"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lunchkort och betalterminal","tmcname":"osa09-04_lunchkort_och_betalterminal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I föregående del fanns en "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-8/5-fler-exempel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"övning"}]},{"type":"text","value":" där du implementerade klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":". Kortet hade separata metoder för att äta en vanlig och en speciell lunch, samt en metod för att sätta in pengar på kortet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":", så som du ombads implementera den, har dock vissa problem. Kortet självt hade kunskap om priserna på de olika lunchalternativen och visste att subtrahera rätt mängd pengar från saldot baserat på dessa. Men tänk dig att priserna ändrades eller att nya varor infördes i systemet, men att flera kort redan var registrerade i systemet. Detta skulle innebära att alla befintliga kort skulle behöva ersättas med versioner med kunskap om de nya priserna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En bättre lösning skulle vara att göra korten ”dumma”, okunniga om priserna på olika produkter. Syftet med kortet skulle vara att helt enkelt hålla reda på det tillgängliga saldot. Alla mer komplicerade funktioner bör rymmas inom en annan klass: betalterminalen."}]},{"type":"element","tagName":"h2","properties":{"id":"ett-simplare-lunchkort","style":"position:relative;"},"children":[{"type":"text","value":"Ett simplare Lunchkort"},{"type":"element","tagName":"a","properties":{"href":"#ett-simplare-lunchkort","ariaLabel":"ett simplare lunchkort permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi förverkligar först en dummare version av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":"-klassen. Kortet borde endast ha funktioner för att få reda på det tillfälliga saldot, lägga till pengar på kortet och subtrahera pengar från saldot. Fyll i metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"subtrahera_saldo(mangd)"}]},{"type":"text","value":" enligt kommentarerna i övningsfältet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"subtrahera_saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden ska endast kunna subtrahera från saldot"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ifall det finns tillräckligt med pengar på kortet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Lyckad betalning returnerar True, icke-lyckad False."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Huvudtestprogram:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saldo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subtrahera_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saldo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subtrahera_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckads:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saldo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Saldo 10\nBetalningen lyckades: True\nSaldo 2\nBetalningen lyckades: False\nSaldo 2"}]}]},{"type":"element","tagName":"h2","properties":{"id":"betalningsterminalen-och-hanterandet-av-kontantbetalning","style":"position:relative;"},"children":[{"type":"text","value":"Betalningsterminalen och hanterandet av kontantbetalning"},{"type":"element","tagName":"a","properties":{"href":"#betalningsterminalen-och-hanterandet-av-kontantbetalning","ariaLabel":"betalningsterminalen och hanterandet av kontantbetalning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I studentkafeterian går det att betala med kontanter eller Lunchkort. En betalterminal används för att hantera både kontant- och korttransaktioner. Låt oss börja med kontanttransaktionerna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en ram för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Betalterminal"}]},{"type":"text","value":"-klass. Implementera metoderna enligt beskrivningen i kommentarerna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Betalterminal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# I kassan finns det ursprungligen 1000 euro i kontanter"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"formanliga "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"special "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"at_formanligt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" betalning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En formånlig lunch kostar 2.50 euro"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Öka värdet på pengarna som finns i terminalen med en förmånlig lunchs pris,"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# öka mängden förmånliga luncher och returnera rätt mängd växel."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall betalningen inte är tillräckligt stor säljs ingen lunch och hela summan returneras"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"at_special"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" betalning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En special lunch kostar 4.30 euro"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Öka värdet på pengarna som finns i terminalen med en special lunchs pris,"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# öka mängden special luncher och returnera rätt mängd växel."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall betalningen inte är tillräckligt stor säljs ingen lunch och hela summan returneras"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Betalterminal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvaxel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden växel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vaxel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvaxel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden växel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vaxel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvaxel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden växel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vaxel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pengar i kassan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Förmånliga luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"formanliga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Special luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mängden växel 7.5\nMängden växel 2.5\nMängden växel 0.0\nPengar i kassan 1009.3\nFörmånliga luncher sålda 2\nSpecial luncher sålda 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"betalning-med-kort","style":"position:relative;"},"children":[{"type":"text","value":"Betalning med kort"},{"type":"element","tagName":"a","properties":{"href":"#betalning-med-kort","ariaLabel":"betalning med kort permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss nu implementera korttransaktioner. Vi kommer att behöva metoder som tar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":" som argument och minskar saldot på kortet med lunchens pris. Nedan hittar du ramar av dessa funktioner. Vänligen fyll i metoderna enligt beskrivningen i kommentarerna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Betalterminal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"at_formanligt_kort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En förmånlig lunch kostar 2.50 euro."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Om kortet har tillräckligt med pengar, subtrahera lunchens pris och returnera True."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Annars returnera False"}]},{"type":"text","value":"\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"at_special_kort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En special lunch kostar 4.30 euro."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Om kortet har tillräckligt med pengar, subtrahera lunchens pris och returnera True."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Annars returnera False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" betalningar med Lunchkort ändrar inte på mängden pengar i terminalen. Luncher säljes däremot fortfarande så länge pengarna på saldot räcker till, alltså kom ihåg att öka mängden luncher som sålts."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande kod kan du använda för att testa din klass:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Betalterminal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvaxel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden växel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vaxel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pengar i kassan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Förmånliga luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"formanliga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"special luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mängden växel 7.5\nBetalningen lyckades: True\nBetalningen lyckades: False\nBetalningen lyckades: True\nPengar i kassan 1002.5\nFörmånliga luncher sålda 2\nspecial luncher sålda 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"tillsattning-av-pengar-pa-kortet","style":"position:relative;"},"children":[{"type":"text","value":"Tillsättning av pengar på kortet"},{"type":"element","tagName":"a","properties":{"href":"#tillsattning-av-pengar-pa-kortet","ariaLabel":"tillsattning av pengar pa kortet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Slutligen lägger vi till en metod som gör att du kan sätta in pengar på kortet. Kortägaren betalar detta kontant, så att den insatta summan läggs till de pengar som finns tillgängliga i terminalen. Här är en ram för metoden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar_kortet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda följande kod för att testa din metod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Betalterminal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nantons_kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Kortets saldo "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nexactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar_kortet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Kortets saldo "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Kortets saldo "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pengar i kassan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Förmånliga luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"formanliga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"special luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo 2 euro\nBetalningen lyckades: False\nKortets saldo 102 euro\nBetalningen lyckades: True\nKortets saldo 97.7 euro\nPengar i kassan 1100\nFörmånliga luncher sålda 0\nspecial luncher sålda 1"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-instans-av-samma-klass-som-argument-till-en-metod","style":"position:relative;"},"children":[{"type":"text","value":"En instans av samma klass som argument till en metod"},{"type":"element","tagName":"a","properties":{"href":"#en-instans-av-samma-klass-som-argument-till-en-metod","ariaLabel":"en instans av samma klass som argument till en metod permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan har vi ytterligare en version av klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fodelsear"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi vill skriva ett program som jämför åldern på objekt av typen Person. Vi kan skriva en separat funktion för detta ändamål:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nmuhammad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Muhammad ibn Musa al-Khwarizmi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"780"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npascal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Blaise Pascal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1623"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngrace "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grace Hopper\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1906"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" aldre_an"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är inte äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" aldre_an"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är inte äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Muhammad ibn Musa al-Khwarizmi är äldre än Blaise Pascal\nGrace Hopper är inte äldre än Blaise Pascal"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En av principerna för objektorienterad programmering är att all funktionalitet som hanterar objekt av en viss typ ska inkluderas i klassdefinitionen, som metoder. I stället för en funktion kan vi alltså skriva en metod som gör det möjligt att jämföra åldern på ett Person-objekt med ett annat Person-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fodelsear\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# OBS! Typledtrådar måste vara inom citationstecken ifall parametern är av samma typ som klassen självt!"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Person\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här kallas det objekt som metoden anropas på för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":", medan det andra Person-objektet kallas för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"annat"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att anrop av en metod skiljer sig från anrop av en funktion. En metod är kopplad till ett objekt med punktnotationen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"muhammad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Muhammad ibn Musa al-Khwarizmi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"780"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npascal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Blaise Pascal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1623"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngrace "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grace Hopper\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1906"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"aldre_an"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är inte äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"aldre_an"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är inte äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till vänster om punkten finns själva objektet, som kallas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" i metoddefinitionen. Inom parentes står argumentet till metoden, vilket är det objekt som kallas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"annat"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften från programmet är exakt densamma som med funktionsimplementeringen ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till sist, en ganska kosmetisk punkt: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if...else"}]},{"type":"text","value":"-strukturen i metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"text","value":" är i stort sett onödig. Värdet på det booleska uttrycket i villkoret är redan exakt samma sanningsvärde som returneras. Metoden kan alltså förenklas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fodelsear\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# OBS! Typledtrådar måste vara inom citationstecken ifall parametern är av samma typ som klassen självt!"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Person\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Liksom det framkommer av kommentarerna i exemplen ovan, så måste typledtråden omslutas av citattecken ifall parametern i en metoddefinition är av samma typ som klassen själv. Om citattecknen utelämnas uppstår ett fel, vilket du kommer att se om du försöker med följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta fungerar inte, Person måste vara innanför citationstecken"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bostadsjämförelse","tmcname":"osa09-05_bostadsjamforelse"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Databasen hos en fastighetsmäklare innehåller register över tillgängliga fastigheter med objekt som definieras av följande klass:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kvadratmeter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kvadratpris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rum\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kvadratmeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kvadratmeter\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kvadratpris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kvadratpris"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din uppgift är att implementera metoder som gör det möjligt att jämföra tillgängliga egenskaper."}]},{"type":"element","tagName":"h2","properties":{"id":"ar-den-storre","style":"position:relative;"},"children":[{"type":"text","value":"Är den större?"},{"type":"element","tagName":"a","properties":{"href":"#ar-den-storre","ariaLabel":"ar den storre permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storre(self, jamforelse)"}]},{"type":"text","value":", som returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", ifall "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"text","value":"-objektet är större än objektet det jämförs med."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen borde fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"eira_etta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nberghall_tvaa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njakobacka_trea "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eira_etta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"storre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jakobacka_trea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"storre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"prisskillnad","style":"position:relative;"},"children":[{"type":"text","value":"Prisskillnad"},{"type":"element","tagName":"a","properties":{"href":"#prisskillnad","ariaLabel":"prisskillnad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prisskillnad(self, jamforelse)"}]},{"type":"text","value":", som returnerar skillnaden i pris mellan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"text","value":"-objektet självt och det som det jämförs med. Prisskillnaden är det absoluta värdet av skillnaden mellan de två fastigheternas totalpriser. Det totala priset för en fastighet är dess kvadratmeterpris multiplicerat med antalet kvadratmeter i fastigheten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen borde fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"eira_etta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nberghall_tvaa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njakobacka_trea "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eira_etta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"prisskillnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jakobacka_trea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"prisskillnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"71600\n35400"}]}]},{"type":"element","tagName":"h2","properties":{"id":"dyrare","style":"position:relative;"},"children":[{"type":"text","value":"Dyrare?"},{"type":"element","tagName":"a","properties":{"href":"#dyrare","ariaLabel":"dyrare permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dyrare(self, jamforelse)"}]},{"type":"text","value":" som returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", ifall "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"text","value":"-objektet är dyrare än objektet som det jämförs med."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen borde fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"eira_etta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nberghall_tvaa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njakobacka_trea "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eira_etta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dyrare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jakobacka_trea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dyrare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att kunna använda olika datastrukturer för att hantera objekt
  • \n
  • Vet du hur objekt kan bli passerade som argument
  • \n

Varje värde i Python är ett objekt. Alla objekt som du skapar baserat på en klass som du själv har definierat fungerar exakt på samma sätt som alla \"vanliga\" Python-objekt. Objekt kan till exempel lagras i en lista:

from datetime import date\n\nclass SlutfordKurs:\n\n    def __init__(self, kurs: str, studiepoang: int, slutforsdatum: date):\n        self.kurs = kurs\n        self.studiepoang = studiepoang\n        self.slutforsdatum = slutforsdatum\n\n\nif __name__ == \"__main__\":\n    # Vi skapar några slutförda kurser och lägger dessa i en lista\n    prestationer = []\n\n    mat1 = SlutfordKurs(\"Matematik 1\", 5, date(2020, 3, 11))\n    prg1 = SlutfordKurs(\"Programmering 1\", 6, date(2019, 12, 17))\n\n    prestationer.append(mat1)\n    prestationer.append(prg1)\n\n    # Vi lägger några till rakt till listan\n    prestationer.append(SlutfordKurs(\"Fysik 2\", 4, date(2019, 11, 10)))\n    prestationer.append(SlutfordKurs(\"Programmering 2\", 5, date(2020, 5, 19)))\n\n    # Vi går igenom alla slutförda kurser, skriver ut deras namn och räknar ihop den totala mängden studiepoäng\n    studiepoang = 0\n    for prestation in prestationer:\n        print(prestation.kurs)\n        studiepoang += prestation.studiepoang\n\n    print(\"Studiepoäng totalt:\", studiepoang)

Matematik 1\nProgrammering 1\nFysik 2\nProgrammering 2\nStudiepoäng totalt: 20

Uppgiftsbotten har en klass med namnet Bil som representerar en bil genom två attribut: marke (str) och topphastighet (int).

Skapa funktionen snabbaste_bil(bilar: list), som får en lista av Bil-objekt som argument.

Funktionen ska returnera märket på den snabbaste bilen. Du kan anta att det alltid kommer att finnas en enda bil med den högsta topphastigheten. Ändra inte listan som ges som argument, gör heller inte några ändringar i klassdefinitionen för Bil.

Du kan använda följande kod för att testa din funktion:

if __name__ == \"__main__\":\n    bil1 = Bil(\"Mersu\", 195)\n    bil2 = Bil(\"Lada\", 110)\n    bil3 = Bil(\"Ferrari\", 280)\n    bil4 = Bil(\"Trabant\", 85)\n\n    bilar = [bil1, bil2, bil3, bil4]\n    print(snabbaste_bil(bilar))

Ferrari

I uppgiftsbotten hittas klassen Provprestation, som, liksom namnet anger, tar modell av en provtagares prestation i ett prov. Den har två attribut, presterare (str) och poang (int).

Skapa funktionen godkanda(prestationer: list, poanggrans: int), som får som parametrar en lista av provprestationer och ett heltal som representerar det minsta poängantalet för att bli godkänd.

Funktionen ska skapa och returnera en ny lista, som innehåller endast de prov som kom över poänggränsen i den ursprungliga listan. Ändra inte listan som ges som argument, ändra heller inte definitionen för klassen Provprestation.

Du kan använda följande kod för att testa funktionen:

if __name__ == \"__main__\":\n    s1 = Provprestation(\"Peter\", 12)\n    s2 = Provprestation(\"Pernilla\", 19)\n    s3 = Provprestation(\"Per\", 15)\n    s4 = Provprestation(\"Pia\", 9)\n    s5 = Provprestation(\"Petra\", 17)\n\n    klarade = godkanda([s1, s2, s3, s4, s5], 15)\n    for godkand in klarade:\n        print(klarade)

Provprestation (presterare: Pernilla, poang: 19)\nProvprestation (presterare: Per, poang: 15)\nProvprestation (presterare: Petra, poang: 17)

Du kanske minns att listor inte innehåller några objekt i sig själva. De innehåller referenser till objekt. Exakt samma objekt kan förekomma flera gånger i en och samma lista, och det kan refereras till flera gånger i listan eller utanför den. Låt oss ta en titt på ett exempel:

class Produkt:\n    def __init__(self, namn: int, enhet: str):\n        self.namn = namn\n        self.enhet = enhet\n\n\nif __name__ == \"__main__\":\n    affarslista = []\n    mjolk = Produkt(\"Mjölk\", \"liter\")\n\n    affarslista.append(mjolk)\n    affarslista.append(mjolk)\n    affarslista.append(Produkt(\"Gurka\", \"st\"))
\n \n \n \n \n \n \"9\n \n \n

Self eller inget self?

Hittills har vi bara snuddat vid ytan när det gäller att använda parameternamnet self. Låt oss titta närmare på när det bör eller inte bör användas.

Nedan har vi en enkel klass som låter oss skapa ett ordförråd-objekt som innehåller några ord:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n\n    def tillsatt_ord(self, ord: str):\n        if not ord in self.ord:\n            self.ord.append(ord)\n\n    def utskrift(self):\n        for ord in sorted(self.ord):\n            print(ord)\n\nordforrad = Ordforrad()\nordforrad.tillsatt_ord(\"python\")\nordforrad.tillsatt_ord(\"objekt\")\nordforrad.tillsatt_ord(\"objekt-orienterad programmering\")\nordforrad.tillsatt_ord(\"objekt\")\nordforrad.tillsatt_ord(\"nörd\")\n\nordforrad.utskrift()

nörd\nobjekt\nobjekt-orienterad programmering\npython

Listan med ord lagras i ett attribut med namnet self.ord. I det här fallet är parameternamnet self obligatoriskt både i klassens konstruktormetod och i alla andra metoder som använder variabeln. Om self utelämnas kommer de olika metoderna inte att få tillgång till samma lista med ord.

Låt oss lägga till en ny metod i vår klassdefinition. Metoden langsta_ord(self) returnerar (ett av) de längsta orden i ordförrådet.

Följande är ett sätt att utföra denna uppgift, men vi kommer snart att se att det inte är ett särskilt bra sätt:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n\n    # ...\n\n    def langsta_ord(self):\n        # vi definierar två hjälpvariabler\n        self.langsta = \"\"\n        self.langsta_langd = 0\n\n        for ord in self.ord:\n            if len(ord) > self.langsta_langd:\n                self.langsta_langd = len(ord)\n                self.langsta = ord\n\n        return self.langsta

Den här metoden använder två hjälpvariabler som deklareras med parameternamnet self. Kom ihåg att namnen på variablerna inte spelar någon roll i funktionell mening, så dessa variabler kan också namnges mer förvirrande som till exempel hjalpare och hjalpare2. Koden börjar se lite kryptisk ut:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n\n    # ...\n\n    def langsta_ord(self):\n        # vi definierar två hjälpvariabler\n        self.hjalpare = \"\"\n        self.hjalpare2 = 0\n\n        for ord in self.ord:\n            if len(ord) > self.hjalpare2:\n                self.hjalpare2 = len(ord)\n                self.hjalpare = ord\n\n        return self.hjalpare

När en variabel deklareras med parameternamnet self blir den ett attribut till objektet. Detta innebär att variabeln kommer att existera så länge objektet existerar. Specifikt kommer variabeln att fortsätta existera även efter att metoden som deklarerar den har avslutat sin exekvering (engelska “Execution”). I exemplet ovan är detta helt onödigt, eftersom hjälpvariablerna endast är avsedda att användas inom metoden longest_word(self). Så att deklarera hjälpvariabler med parameternamnet self är inte en särskilt bra idé här.

Förutom att variabler kan existera efter sitt \"utgångsdatum\" kan användning av self för att skapa nya attribut där de inte är nödvändiga orsaka svåra buggar i din kod. Särskilt generiskt namngivna attribut som self.hjalpare, som sedan används i flera olika metoder, kan orsaka oväntade beteenden som är svåra att spåra.

Om t.ex. en hjälpvariabel deklareras som ett attribut och tilldelas ett ursprungligt värde i konstruktorn, men variabeln sedan används i ett orelaterat sammanhang i en annan metod, blir resultatet ofta oförutsägbart:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n        # vi definierar hjälparvariabler\n        self.hjalpare = \"\"\n        self.hjalpare2 = \"\"\n        self.hjalpare3 = \"\"\n        self.hjalpare4 = \"\"\n\n    # ...\n\n    def langsta_ord(self):\n        for ord in self.ord:\n            # detta fungerar inte eftersom hjalpare2 har fel typ\n            if len(ord) > self.hjalpare2:\n                self.hjalpare2 = len(ord)\n                self.hjalpare = ord\n\n        return self.hjalpare

Man skulle kunna tro att detta skulle lösas genom att bara deklarera attributen där de används, utanför konstruktorn, men detta resulterar i en situation där de attribut som är tillgängliga via ett objekt är beroende av vilka metoder som har utförts. I föregående del såg vi att fördelen med att deklarera attribut i konstruktorn är att alla instanser av klassen då kommer att ha exakt samma attribut. Om så inte är fallet kan det lätt leda till fel om man använder olika instanser av klassen.

Sammanfattningsvis, om du behöver hjälpvariabler för användning inom en enda metod, är det korrekta sättet att göra det utan self. För att göra din kod lättare att förstå, använd också informativa variabelnamn:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n\n    # ...\n\n    def langsta_ord(self):\n        # detta är det korrekta sättet att definiera\n        # hjälpvariabler för användning i en enda metod\n        langsta = \"\"\n        langsta_langd = 0\n\n        for ord in self.ord:\n            if len(ord) > langsta_langd:\n                langsta_langd = len(ord)\n                langsta = ord\n\n        return langsta

I implementeringen ovan är hjälpvariablerna endast tillgängliga när metoden utförs. De värden som lagras i dem kan inte orsaka komplikationer i andra delar av programmet.

Objekt som argument till funktioner

De objekt som skapas baserat på våra egna klasser är vanligtvis mutabla. Du kanske kommer ihåg att till exempel Python-listor är föränderliga: när de passeras som argument till funktioner kan deras innehåll ändras som ett resultat av exekveringen.

Låt oss titta på ett enkelt exempel där en funktion får en referens till ett objekt av typen Studerande som sitt argument. Funktionen ändrar sedan namnet på studenten. Både funktionen och huvudfunktionen som anropar den har åtkomst till samma objekt, så ändringen syns även i huvudfunktionen.

class Studerande:\n    def __init__(self, namn: str, studerandenummer: str):\n        self.namn = namn\n        self.studerandenummer = studerandenummer\n\n    def __str__(self):\n        return f\"{self.namn} ({self.studerandenummer})\"\n\n# observera att typledtråden använder namnet på klassen definierad ovan\ndef andra_namn(studerande: Studerande):\n    studerande.namn = \"Olle Studerande\"\n\n# skapa ett Studerande-objekt\nolle = Studerande(\"Olle Elev\", \"12345\")\n\nprint(olle)\nandra_namn(olle)\nprint(olle)

Olle Elev (12345)\nOlle Studerande (12345)

Det är också möjligt att skapa objekt inom funktioner. Om en funktion returnerar en referens till det nyskapade objektet är det också åtkomligt inom huvudfunktionen:

from random import randint, choice\n\nclass Studerande:\n    def __init__(self, namn: str, studerandenummer: str):\n        self.namn = namn\n        self.studerandenummer = studerandenummer\n\n    def __str__(self):\n        return f\"{self.namn} ({self.studerandenummer})\"\n\n\n# Denna funktion skapar och returnerar ett nytt Studerande-objekt.\n# Den väljer slumpmässigt värden för namnet och studerandenumret.\ndef ny_studerande():\n    fornamn = [\"Atte\",\"Peter\",\"Minna\",\"Maria\"]\n    efternamn = [\"Virtanen\", \"Lahtinen\", \"Leinonen\", \"Pythonson\"]\n\n    # Slumpmässigt namn\n    namn = choice(fornamn) + \" \" + choice(efternamn)\n\n    # Slumpmässigt studerandenummer\n    studerandenummer = str(randint(10000,99999))\n\n    # Skapa och returnera ett Studerande-objekt\n    return Studerande(namn, studerandenummer)\n\nif __name__ == \"__main__\":\n    # Kalla funktionen fem gånger och spara resultatet i en lista\n    studeranden = []\n    for i in range(5):\n        studeranden.append(ny_studerande())\n\n    # Skriv ut resultatet\n    for studerande in studeranden:\n        print(studerande)

Om du kör ovanstående kan det resultera i följande utskrift (OBS: eftersom slumpen är inblandad kommer resultaten sannolikt att bli annorlunda om du testar koden själv).

Maria Lahtinen (36213)\nAtte Virtanen (11859)\nMaria Pythonen (77330)\nAtte Pythonson (86451)\nMinna Pythonson (86211)

Objekt som argument till metoder

På liknande sätt kan objekt fungera som argument till metoder. Låt oss ta en titt på ett exempel från en nöjespark:

class Person:\n    def __init__(self, namn: str, langd: int):\n        self.namn = namn\n        self.langd = langd\n\nclass Akattraktion:\n    def __init__(self, namn: str, langdgrans: int):\n        self.besokare = 0\n        self.namn = namn\n        self.langdgrans = langdgrans\n\n    def ta_ombord(self, person: Person):\n        if person.langd >= self.langdgrans:\n            self.besokare += 1\n            print(f\"{person.namn} kom ombord\")\n        else:\n            print(f\"{person.namn} var för kort :(\")\n\n    def __str__(self):\n        return f\"{self.namn} ({self.besokare} besökare)\"

Attraktionen innehåller en metod motta_besökare, som tar ett objekt av typen Person som argument. Om besökaren är tillräckligt lång släpps denne ombord och antalet besökare ökas. Klasserna kan testas på följande sätt:

berg_och_dalbana = Akattraktion(\"Berg_och_dalbana\", 120)\njakob = Person(\"Jakob\", 172)\nvilma = Person(\"Vilma\", 105)\n\nberg_och_dalbana.ta_ombord(jakob)\nberg_och_dalbana.ta_ombord(vilma)\n\nprint(berg_och_dalbana)

Jakob kom ombord\nVenla var för kort :(\nBerg_och_dalbana (1 besökare)

I uppgiftsbotten finns färdigt redan en klass Person såväl som en ram för klassen BebisCenter. Ett BebisCenter-objekt utför olika handlingar såsom mäter eller matar personer. I denna uppgift implementerar du resten av BebisCenter-klassen. Koden för klassen Person ska inte ändras överhuvudtaget!

Vägning av person

I BebisCenter-klassens definitione finns en ram för funktionen vagning

class BebisCenter:\n    def vagning(self, person: Person):\n        # returnera vikten av personen som passerades som argument\n        return -1

Metoden tar ett Person-objekt som sitt argument. Den ska returnera personens vikt. Du kan komma åt en persons vikt genom attributet som definieras i Person-klassen. Vänligen fyll i resten av implementationen för metoden vagning.

Nedan finns ett exempel på en huvudfunktion där ett BebisCenter väger två olika Person-objekt:

haga_barnvard = BebisCenter()\n\nerik = Person(\"Erik\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"{erik.namn} väger {haga_barnvard.vagning(erik)} kg\")\nprint(f\"{peter.namn} väger {haga_barnvard.vagning(peter)} kg\")

Erik väger 7 kg\nPeter väger 85 kg

Matning

Det är möjligt att ändra tillståndet för ett objekt som skickas som ett argument. Implementera metoden mata(person: Person) som ökar vikten på den person som skickas som argument med ett.

I följande exempel vägs två personer, och sedan matas en av dem tre gånger. Därefter vägs personerna igen:

haga_barnvard = BabyCenter()\n\nerik = Person(\"Erik\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"{erik.namn} väger {haga_barnvard.vagning(erik)} kg\")\nprint(f\"{peter.namn} väger {haga_barnvard.vagning(peter)} kg\")\nprint()\n\nhaga_barnvard.mata(erik)\nhaga_barnvard.mata(erik)\nhaga_barnvard.mata(erik)\n\nprint(f\"{erik.namn} väger {haga_barnvard.vagning(erik)} kg\")\nprint(f\"{peter.namn} väger {haga_barnvard.vagning(peter)} kg\")

Utskriften borde visa att Eriks vikt har stigit med tre:

Erik väger 7 kg\nPeter väger 85 kg

Erik väger 10 kg\nPeter väger 85 kg

Räknande av vägningar

Implementera metoden vagningar() som returnerar det totala antalet vägningar som ett BabyCenter-objekt har utfört. OBS: Du kommer att behöva ett nytt attribut för att hålla reda på antalet vägningar. Du kan använda följande kod för att testa din metod:

haga_barnvard = BabyCenter()\n\nerik = Person(\"Erik\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"Vägningar utförda {haga_barnvard.vagningar()}\")\n\nhaga_barnvard.vagning(erik)\nhaga_barnvard.vagning(erik)\n\nprint(f\"Vägningar utförda {haga_barnvard.vagningar()}\")\n\nhaga_barnvard.vagning(erik)\nhaga_barnvard.vagning(erik)\nhaga_barnvard.vagning(erik)\nhaga_barnvard.vagning(erik)\n\nprint(f\"Vägningar utförda {haga_barnvard.vagningar()}\")

Vägningar utförda 0\nVägningar utförda 2\nVägningar utförda 6

I föregående del fanns en övning där du implementerade klassen Lunchkort. Kortet hade separata metoder för att äta en vanlig och en speciell lunch, samt en metod för att sätta in pengar på kortet.

Klassen Lunchkort, så som du ombads implementera den, har dock vissa problem. Kortet självt hade kunskap om priserna på de olika lunchalternativen och visste att subtrahera rätt mängd pengar från saldot baserat på dessa. Men tänk dig att priserna ändrades eller att nya varor infördes i systemet, men att flera kort redan var registrerade i systemet. Detta skulle innebära att alla befintliga kort skulle behöva ersättas med versioner med kunskap om de nya priserna.

En bättre lösning skulle vara att göra korten ”dumma”, okunniga om priserna på olika produkter. Syftet med kortet skulle vara att helt enkelt hålla reda på det tillgängliga saldot. Alla mer komplicerade funktioner bör rymmas inom en annan klass: betalterminalen.

Ett simplare Lunchkort

Vi förverkligar först en dummare version av Lunchkort-klassen. Kortet borde endast ha funktioner för att få reda på det tillfälliga saldot, lägga till pengar på kortet och subtrahera pengar från saldot. Fyll i metoden subtrahera_saldo(mangd) enligt kommentarerna i övningsfältet:

class Lunchkort:\n    def __init__(self, saldo: float):\n        self.saldo = saldo\n\n    def tillsatt_pengar(self, mangd: float):\n        self.saldo += mangd\n\n    def subtrahera_saldo(self, mangd: float):\n        pass\n        # Metoden ska endast kunna subtrahera från saldot\n        # ifall det finns tillräckligt med pengar på kortet\n        # Lyckad betalning returnerar True, icke-lyckad False.

Huvudtestprogram:

if __name__ == \"__main__\":\n    kort = Lunchkort(10)\n    print(\"Saldo\", kort.saldo)\n    resultat = kort.subtrahera_saldo(8)\n    print(\"Betalningen lyckades:\", resultat)\n    print(\"Saldo\", kort.saldo)\n    resultat = kort.subtrahera_saldo(4)\n    print(\"Betalningen lyckads:\", resultat)\n    print(\"Saldo\", kort.saldo)

Saldo 10\nBetalningen lyckades: True\nSaldo 2\nBetalningen lyckades: False\nSaldo 2

Betalningsterminalen och hanterandet av kontantbetalning

I studentkafeterian går det att betala med kontanter eller Lunchkort. En betalterminal används för att hantera både kontant- och korttransaktioner. Låt oss börja med kontanttransaktionerna.

Här har vi en ram för en Betalterminal-klass. Implementera metoderna enligt beskrivningen i kommentarerna:

class Betalterminal:\n    def __init__(self):\n        # I kassan finns det ursprungligen 1000 euro i kontanter\n        self.pengar = 1000\n        self.formanliga = 0\n        self.special = 0\n\n    def at_formanligt(self, betalning: float):\n        # En formånlig lunch kostar 2.50 euro\n        # Öka värdet på pengarna som finns i terminalen med en förmånlig lunchs pris,\n        # öka mängden förmånliga luncher och returnera rätt mängd växel.\n        # Ifall betalningen inte är tillräckligt stor säljs ingen lunch och hela summan returneras\n\n    def at_special(self, betalning: float):\n        # En special lunch kostar 4.30 euro\n        # Öka värdet på pengarna som finns i terminalen med en special lunchs pris,\n        # öka mängden special luncher och returnera rätt mängd växel.\n        # Ifall betalningen inte är tillräckligt stor säljs ingen lunch och hela summan returneras

Exempel på användning:

exactum = Betalterminal()\n\nvaxel = exactum.at_formanligt(10)\nprint(\"Mängden växel\", vaxel)\n\nvaxel = exactum.at_formanligt(5)\nprint(\"Mängden växel\", vaxel)\n\nvaxel = exactum.at_special(4.3)\nprint(\"Mängden växel\", vaxel)\n\nprint(\"Pengar i kassan\", exactum.pengar)\nprint(\"Förmånliga luncher sålda\", exactum.formanliga)\nprint(\"Special luncher sålda\", exactum.special)

Mängden växel 7.5\nMängden växel 2.5\nMängden växel 0.0\nPengar i kassan 1009.3\nFörmånliga luncher sålda 2\nSpecial luncher sålda 1

Betalning med kort

Låt oss nu implementera korttransaktioner. Vi kommer att behöva metoder som tar ett Lunchkort som argument och minskar saldot på kortet med lunchens pris. Nedan hittar du ramar av dessa funktioner. Vänligen fyll i metoderna enligt beskrivningen i kommentarerna:

class Betalterminal:\n    # ...\n\n    def at_formanligt_kort(self, kort: Lunchkort):\n        # En förmånlig lunch kostar 2.50 euro.\n        # Om kortet har tillräckligt med pengar, subtrahera lunchens pris och returnera True.\n        # Annars returnera False\n\n\n    def at_special_kort(self, kort: Lunchkort):\n        # En special lunch kostar 4.30 euro.\n        # Om kortet har tillräckligt med pengar, subtrahera lunchens pris och returnera True.\n        # Annars returnera False

OBS: betalningar med Lunchkort ändrar inte på mängden pengar i terminalen. Luncher säljes däremot fortfarande så länge pengarna på saldot räcker till, alltså kom ihåg att öka mängden luncher som sålts.

Följande kod kan du använda för att testa din klass:

exactum = Betalterminal()\n\nvaxel = exactum.at_formanligt(10)\nprint(\"Mängden växel\", vaxel)\n\nkort = Lunchkort(7)\n\nresultat = exactum.at_special_kort(kort)\nprint(\"Betalningen lyckades:\", resultat)\nresultat = exactum.at_special_kort(kort)\nprint(\"Betalningen lyckades:\", resultat)\nresultat = exactum.at_formanligt_kort(kort)\nprint(\"Betalningen lyckades:\", resultat)\n\nprint(\"Pengar i kassan\", exactum.pengar)\nprint(\"Förmånliga luncher sålda\", exactum.formanliga)\nprint(\"special luncher sålda\", exactum.special)

Mängden växel 7.5\nBetalningen lyckades: True\nBetalningen lyckades: False\nBetalningen lyckades: True\nPengar i kassan 1002.5\nFörmånliga luncher sålda 2\nspecial luncher sålda 1

Tillsättning av pengar på kortet

Slutligen lägger vi till en metod som gör att du kan sätta in pengar på kortet. Kortägaren betalar detta kontant, så att den insatta summan läggs till de pengar som finns tillgängliga i terminalen. Här är en ram för metoden:

def tillsatt_pengar_kortet(self, kort: Lunchkort, summa: float):\n    pass

Du kan använda följande kod för att testa din metod:

exactum = Betalterminal()\n\nantons_kort = Lunchkort(2)\nprint(f\"Kortets saldo {antons_kort.saldo} euro\")\n\nresultat = exactum.at_special_kort(antons_kort)\nprint(\"Betalningen lyckades:\", resultat)\n\nexactum.tillsatt_pengar_kortet(antons_kort, 100)\nprint(f\"Kortets saldo {antons_kort.saldo} euro\")\n\nresultat = exactum.at_special_kort(antons_kort)\nprint(\"Betalningen lyckades:\", resultat)\nprint(f\"Kortets saldo {antons_kort.saldo} euro\")\n\nprint(\"Pengar i kassan\", exactum.pengar)\nprint(\"Förmånliga luncher sålda\", exactum.formanliga)\nprint(\"special luncher sålda\", exactum.special)

Kortets saldo 2 euro\nBetalningen lyckades: False\nKortets saldo 102 euro\nBetalningen lyckades: True\nKortets saldo 97.7 euro\nPengar i kassan 1100\nFörmånliga luncher sålda 0\nspecial luncher sålda 1

En instans av samma klass som argument till en metod

Nedan har vi ytterligare en version av klassen Person:

class Person:\n    def __init__(self, namn: str, fodelsear: int):\n        self.namn = namn\n        self.fodelsear = fodelsear

Låt oss anta att vi vill skriva ett program som jämför åldern på objekt av typen Person. Vi kan skriva en separat funktion för detta ändamål:

def aldre_an(person1: Person, person2: Person):\n    if person1.fodelsear < person2.fodelsear:\n        return True\n    else:\n        return False\n\nmuhammad = Person(\"Muhammad ibn Musa al-Khwarizmi\", 780)\npascal = Person(\"Blaise Pascal\", 1623)\ngrace = Person(\"Grace Hopper\", 1906)\n\nif aldre_an(muhammad, pascal):\n    print(f\"{muhammad} är äldre än {pascal}\")\nelse:\n    print(f\"{muhammad} är inte äldre än {pascal}\")\n\nif aldre_an(grace, pascal):\n    print(f\"{grace} är äldre än {pascal}\")\nelse:\n    print(f\"{grace} är inte äldre än {pascal}\")

Muhammad ibn Musa al-Khwarizmi är äldre än Blaise Pascal\nGrace Hopper är inte äldre än Blaise Pascal

En av principerna för objektorienterad programmering är att all funktionalitet som hanterar objekt av en viss typ ska inkluderas i klassdefinitionen, som metoder. I stället för en funktion kan vi alltså skriva en metod som gör det möjligt att jämföra åldern på ett Person-objekt med ett annat Person-objekt:

class Person:\n    def __init__(self, namn: str, fodelsear: int):\n        self.namn = namn\n        self.fodelsear = fodelsear\n\n    # OBS! Typledtrådar måste vara inom citationstecken ifall parametern är av samma typ som klassen självt!\n    def aldre_an(self, annat: \"Person\"):\n        if self.fodelsear < annat.fodelsear:\n            return True\n        else:\n            return False

Här kallas det objekt som metoden anropas på för self, medan det andra Person-objektet kallas för annat.

Kom ihåg att anrop av en metod skiljer sig från anrop av en funktion. En metod är kopplad till ett objekt med punktnotationen:

muhammad = Person(\"Muhammad ibn Musa al-Khwarizmi\", 780)\npascal = Person(\"Blaise Pascal\", 1623)\ngrace = Person(\"Grace Hopper\", 1906)\n\nif muhammad.aldre_an(pascal):\n    print(f\"{muhammad.namn} är äldre än {pascal.namn}\")\nelse:\n    print(f\"{muhammad.namn} är inte äldre än {pascal.namn}\")\n\nif grace.aldre_an(pascal):\n    print(f\"{grace.namn} är äldre än {pascal.namn}\")\nelse:\n    print(f\"{grace.namn} är inte äldre än {pascal.namn}\")

Till vänster om punkten finns själva objektet, som kallas self i metoddefinitionen. Inom parentes står argumentet till metoden, vilket är det objekt som kallas annat.

Utskriften från programmet är exakt densamma som med funktionsimplementeringen ovan.

Till sist, en ganska kosmetisk punkt: if...else-strukturen i metoden aldre_an är i stort sett onödig. Värdet på det booleska uttrycket i villkoret är redan exakt samma sanningsvärde som returneras. Metoden kan alltså förenklas:

class Person:\n    def __init__(self, namn: str, fodelsear: int):\n        self.namn = namn\n        self.fodelsear = fodelsear\n\n    # OBS! Typledtrådar måste vara inom citationstecken ifall parametern är av samma typ som klassen självt!\n    def aldre_an(self, annat: \"Person\"):\n        return self.fodelsear < annat.fodelsear:

Liksom det framkommer av kommentarerna i exemplen ovan, så måste typledtråden omslutas av citattecken ifall parametern i en metoddefinition är av samma typ som klassen själv. Om citattecknen utelämnas uppstår ett fel, vilket du kommer att se om du försöker med följande:

class Person:\n    # ...\n\n    # Detta fungerar inte, Person måste vara innanför citationstecken\n    def aldre_an(self, annat: Person):\n        return self.fodelsear < annat.fodelsear:

Databasen hos en fastighetsmäklare innehåller register över tillgängliga fastigheter med objekt som definieras av följande klass:

class Bostad:\n    def __init__(self, rum: int, kvadratmeter: int, kvadratpris: int):\n        self.rum = rum\n        self.kvadratmeter = kvadratmeter\n        self.kvadratpris = kvadratpris

Din uppgift är att implementera metoder som gör det möjligt att jämföra tillgängliga egenskaper.

Är den större?

Skapa metoden storre(self, jamforelse), som returnerar True, ifall Bostad-objektet är större än objektet det jämförs med.

Exempel på hur funktionen borde fungera:

eira_etta = Bostad(1, 16, 5500)\nberghall_tvaa = Bostad(2, 38, 4200)\njakobacka_trea = Bostad(3, 78, 2500)\n\nprint(eira_etta.storre(berghall_tvaa))\nprint(jakobacka_trea.storre(berghall_tvaa))

False\nTrue

Prisskillnad

Skapa metoden prisskillnad(self, jamforelse), som returnerar skillnaden i pris mellan Bostad-objektet självt och det som det jämförs med. Prisskillnaden är det absoluta värdet av skillnaden mellan de två fastigheternas totalpriser. Det totala priset för en fastighet är dess kvadratmeterpris multiplicerat med antalet kvadratmeter i fastigheten.

Exempel på hur funktionen borde fungera:

eira_etta = Bostad(1, 16, 5500)\nberghall_tvaa = Bostad(2, 38, 4200)\njakobacka_trea = Bostad(3, 78, 2500)\n\nprint(eira_etta.prisskillnad(berghall_tvaa))\nprint(jakobacka_trea.prisskillnad(berghall_tvaa))

71600\n35400

Dyrare?

Skapa metoden dyrare(self, jamforelse) som returnerar True, ifall Bostad-objektet är dyrare än objektet som det jämförs med.

Exempel på hur funktionen borde fungera:

eira_etta = Bostad(1, 16, 5500)\nberghall_tvaa = Bostad(2, 38, 4200)\njakobacka_trea = Bostad(3, 78, 2500)\n\nprint(eira_etta.dyrare(berghall_tvaa))\nprint(jakobacka_trea.dyrare(berghall_tvaa))

False\nTrue

","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/1-objekt-och-referenser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna använda olika datastrukturer för att hantera objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur objekt kan bli passerade som argument"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje värde i Python är ett objekt. Alla objekt som du skapar baserat på en klass som du själv har definierat fungerar exakt på samma sätt som alla \"vanliga\" Python-objekt. Objekt kan till exempel lagras i en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" slutforsdatum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kurs\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slutforsdatum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" slutforsdatum\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar några slutförda kurser och lägger dessa i en lista"}]},{"type":"text","value":"\n prestationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n mat1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matematik 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n prg1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programmering 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mat1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prg1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi lägger några till rakt till listan"}]},{"type":"text","value":"\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fysik 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programmering 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi går igenom alla slutförda kurser, skriver ut deras namn och räknar ihop den totala mängden studiepoäng"}]},{"type":"text","value":"\n studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studiepoäng totalt:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Matematik 1\nProgrammering 1\nFysik 2\nProgrammering 2\nStudiepoäng totalt: 20"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Snabbaste bilen","tmcname":"osa09-01_snabbaste_bil"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Uppgiftsbotten har en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bil"}]},{"type":"text","value":" som representerar en bil genom två attribut: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"marke (str)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"topphastighet (int)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"snabbaste_bil(bilar: list)"}]},{"type":"text","value":", som får en lista av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bil"}]},{"type":"text","value":"-objekt som argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska returnera märket på den snabbaste bilen. Du kan anta att det alltid kommer att finnas en enda bil med den högsta topphastigheten. Ändra inte listan som ges som argument, gör heller inte några ändringar i klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bil"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda följande kod för att testa din funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bil1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mersu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"195"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bil2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lada\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bil3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ferrari\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"280"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n bil4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Trabant\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n bilar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"bil1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bil2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bil3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bil4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"snabbaste_bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bilar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ferrari"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Godkända prestationer","tmcname":"osa09-02_godkanda_prestationer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten hittas klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Provprestation"}]},{"type":"text","value":", som, liksom namnet anger, tar modell av en provtagares prestation i ett prov. Den har två attribut, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"presterare (str)"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"poang (int)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"godkanda(prestationer: list, poanggrans: int)"}]},{"type":"text","value":", som får som parametrar en lista av provprestationer och ett heltal som representerar det minsta poängantalet för att bli godkänd."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Funktionen ska skapa och returnera en ny lista, som innehåller endast de prov som kom över poänggränsen i den ursprungliga listan. Ändra inte listan som ges som argument, ändra heller inte definitionen för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Provprestation"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda följande kod för att testa funktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Per\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Provprestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petra\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n klarade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" godkanda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" godkand "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" klarade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"klarade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Provprestation (presterare: Pernilla, poang: 19)\nProvprestation (presterare: Per, poang: 15)\nProvprestation (presterare: Petra, poang: 17)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kanske minns att listor inte innehåller några objekt i sig själva. De innehåller referenser till objekt. Exakt samma objekt kan förekomma flera gånger i en och samma lista, och det kan refereras till flera gånger i listan eller utanför den. Låt oss ta en titt på ett exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Produkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" enhet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"enhet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" enhet\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n affarslista "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n mjolk "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mjölk\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"liter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mjolk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"mjolk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n affarslista"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Produkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gurka\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"st\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 313px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a3722ba9326d57861bd87ed8593aab50/80977/9_1_1.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 64.34782608695653%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABi0lEQVQ4y22TfW+CQAzG+f7faokhbH9MBgOdC5NXMcZ35VX0WZ5m5w60CTnu2v7aa3sG/uR2u8ma5zm22y0OhwP2+718u90Oq9VKmaKqKiyXSxyPx7u+LEt4ngdjCKTBM6GTEjrzG0qapo/AxSLHp+MiTlJMPA/j8QfquukBm6bBTxDAdT4RhhHs9zGSJEWSJE+AWYrRy4sYv1oWoiiWc5ZBBwbfXzBHJmazGdJsIedRFD0CN5uNXKdtWlRVjev1KjodSD1r3HUd2rYVG0ocx0+AmqMuDKQDT6fTg00Yho9A1orO6/Uavu8jCAJZ2Vkll8tF9OzydDoVm/P53M9Qh/43aCFOXNW50imhrq7r+95QRjpM/bP4Us+2FfBQeO2iKHp+PeAwQxadg84MuNKZQbjnvxp23c8YRtXBzGA+n4sjC84a8YWwztxnWdYDypXfrFd82A58f4KyrCQ6R4QQ27Z7s6c6rCCq03oSRlkUYsQudt21F5GDSijfqGma0lHLsuA4DlzXlaEeZvgL31rp5spmjHgAAAAASUVORK5CYII='); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a3722ba9326d57861bd87ed8593aab50/a0b58/9_1_1.webp 230w","/static/a3722ba9326d57861bd87ed8593aab50/12766/9_1_1.webp 313w"],"sizes":"(max-width: 313px) 100vw, 313px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a3722ba9326d57861bd87ed8593aab50/81c8e/9_1_1.png 230w","/static/a3722ba9326d57861bd87ed8593aab50/80977/9_1_1.png 313w"],"sizes":"(max-width: 313px) 100vw, 313px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a3722ba9326d57861bd87ed8593aab50/80977/9_1_1.png","alt":"9 1 1","title":"9 1 1","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om det finns mer än en referens till samma objekt spelar det ingen roll vilken av referenserna som används:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Hund"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn\n\nhundar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nmolly "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Molly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"molly"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Molly\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hundar i början:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" hund "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hunden på index 0 får ett nytt namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rex\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" hund "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hunden på index 2 får ett nytt namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fifi\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" hund "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" hundar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hund"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Koirat alussa:\nMolly\nMolly\nMolly\nHunden på index 0 får ett nytt namn::\nRex\nRex\nMolly\nHunden på index 2 får ett nytt namn:\nRex\nRex\nFifi"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Referenserna på index 0 och 1 i listan hänvisar till samma objekt. Var och en av referenserna kan användas för att komma åt objektet. Referensen på index 2 hänvisar till ett annat objekt, men med till synes samma innehåll. Om innehållet i det senare objektet ändras påverkas inte det andra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" används för att kontrollera om de två referenserna hänvisar till exakt samma objekt, medan operatorn "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" talar om för dig om innehållet i objekten är detsamma. Följande exempel gör förhoppningsvis skillnaden tydlig:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nlista3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lista1\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" lista3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" lista3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" lista2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" lista3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lista2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" lista3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue\nFalse"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nTrue"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla Python-objekt kan också lagras i en ordlista eller någon annan datastruktur. Detta gäller även objekt som är av en klass som du själv har definierat."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sp\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi använder studerandenummer som nyckel och värdet som fås är ett objekt av typen Studerande"}]},{"type":"text","value":"\n studeranden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olle Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ove Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"67"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visualiseringsverktyget"}]},{"type":"text","value":" kan hjälpa dig att förstå exemplet ovan:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d20c7f9a5570bf04d75c3836868a8870/15171/9_1_2.png","style":"display: block","target":"_blank","rel":["noopener"]},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 43.04347826086957%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABPklEQVQoz2WR20rEMBBA/f+/8k0RFUVExdVda7ubtnHbSSfN3FLtVlHWA4E85DBnyEkYUow44Oj36PexB4wRpx9yzvPJ2cymf5xQSuM4ElHre1fvmSX/IGqA4xBHFgkBzPRYHgZ0BwIAMxFRSslynqbJeTi9uC9cr6ptceHr8zBcxXiNeKXazzIz9wcAYMkzs3yQY6Jy14ZILAJ+ReNONIqgCJrJLHddt1q9FEVRVVXT1AAQQmDmpXzJU5WuOuu7x3nv/CcbADabTVG873aurhvnnPfeOdc0zSHh9+lIXL7ddPvbMNwzl7OMiCmlnKcwUB+Sqi3xfycvmJl3dxHvRB5Ui28ZIxIRM5uZqorIcjn6HjMt15feX0K4IXqb5cQWQ6jK8nW93m63bdsy8zLnaPI05fDxHOOTyLNq9SV/Av8dBuHbnwNlAAAAAElFTkSuQmCC'); background-size: cover; display: block;"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"picture","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d20c7f9a5570bf04d75c3836868a8870/a0b58/9_1_2.webp 230w","/static/d20c7f9a5570bf04d75c3836868a8870/bc10c/9_1_2.webp 460w","/static/d20c7f9a5570bf04d75c3836868a8870/966d8/9_1_2.webp 920w","/static/d20c7f9a5570bf04d75c3836868a8870/445df/9_1_2.webp 1380w","/static/d20c7f9a5570bf04d75c3836868a8870/78de1/9_1_2.webp 1840w","/static/d20c7f9a5570bf04d75c3836868a8870/995bd/9_1_2.webp 2230w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d20c7f9a5570bf04d75c3836868a8870/81c8e/9_1_2.png 230w","/static/d20c7f9a5570bf04d75c3836868a8870/08a84/9_1_2.png 460w","/static/d20c7f9a5570bf04d75c3836868a8870/c0255/9_1_2.png 920w","/static/d20c7f9a5570bf04d75c3836868a8870/b1001/9_1_2.png 1380w","/static/d20c7f9a5570bf04d75c3836868a8870/161ec/9_1_2.png 1840w","/static/d20c7f9a5570bf04d75c3836868a8870/15171/9_1_2.png 2230w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d20c7f9a5570bf04d75c3836868a8870/c0255/9_1_2.png","alt":"9 1 2","title":"9 1 2","loading":"lazy","decoding":"async","style":"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;"},"children":[]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"element","tagName":"h2","properties":{"id":"self-eller-inget-self","style":"position:relative;"},"children":[{"type":"text","value":"Self eller inget self?"},{"type":"element","tagName":"a","properties":{"href":"#self-eller-inget-self","ariaLabel":"self eller inget self permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi bara snuddat vid ytan när det gäller att använda parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". Låt oss titta närmare på när det bör eller inte bör användas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan har vi en enkel klass som låter oss skapa ett ordförråd-objekt som innehåller några ord:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"utskrift"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nordforrad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Ordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"objekt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"objekt-orienterad programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"objekt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nörd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nordforrad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"utskrift"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"nörd\nobjekt\nobjekt-orienterad programmering\npython"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Listan med ord lagras i ett attribut med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.ord"}]},{"type":"text","value":". I det här fallet är parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" obligatoriskt både i klassens konstruktormetod och i alla andra metoder som använder variabeln. Om "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" utelämnas kommer de olika metoderna inte att få tillgång till samma lista med ord."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss lägga till en ny metod i vår klassdefinition. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langsta_ord(self)"}]},{"type":"text","value":" returnerar (ett av) de längsta orden i ordförrådet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande är ett sätt att utföra denna uppgift, men vi kommer snart att se att det inte är ett särskilt bra sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langsta_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi definierar två hjälpvariabler"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta_langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langsta"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Den här metoden använder två hjälpvariabler som deklareras med parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". Kom ihåg att namnen på variablerna inte spelar någon roll i funktionell mening, så dessa variabler kan också namnges mer förvirrande som till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hjalpare"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hjalpare2"}]},{"type":"text","value":". Koden börjar se lite kryptisk ut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langsta_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi definierar två hjälpvariabler"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När en variabel deklareras med parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" blir den ett attribut till objektet. Detta innebär att variabeln kommer att existera så länge objektet existerar. Specifikt kommer variabeln att fortsätta existera även efter att metoden som deklarerar den har avslutat sin exekvering (engelska “Execution”). I exemplet ovan är detta helt onödigt, eftersom hjälpvariablerna endast är avsedda att användas inom metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"longest_word(self)"}]},{"type":"text","value":". Så att deklarera hjälpvariabler med parameternamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" är inte en särskilt bra idé här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Förutom att variabler kan existera efter sitt \"utgångsdatum\" kan användning av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" för att skapa nya attribut där de inte är nödvändiga orsaka svåra buggar i din kod. Särskilt generiskt namngivna attribut som "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.hjalpare"}]},{"type":"text","value":", som sedan används i flera olika metoder, kan orsaka oväntade beteenden som är svåra att spåra."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om t.ex. en hjälpvariabel deklareras som ett attribut och tilldelas ett ursprungligt värde i konstruktorn, men variabeln sedan används i ett orelaterat sammanhang i en annan metod, blir resultatet ofta oförutsägbart:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# vi definierar hjälparvariabler"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langsta_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# detta fungerar inte eftersom hjalpare2 har fel typ"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hjalpare"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Man skulle kunna tro att detta skulle lösas genom att bara deklarera attributen där de används, utanför konstruktorn, men detta resulterar i en situation där de attribut som är tillgängliga via ett objekt är beroende av vilka metoder som har utförts. I föregående del såg vi att fördelen med att deklarera attribut i konstruktorn är att alla instanser av klassen då kommer att ha exakt samma attribut. Om så inte är fallet kan det lätt leda till fel om man använder olika instanser av klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sammanfattningsvis, om du behöver hjälpvariabler för användning inom en enda metod, är det korrekta sättet att göra det utan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". För att göra din kod lättare att förstå, använd också informativa variabelnamn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Ordforrad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langsta_ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# detta är det korrekta sättet att definiera"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hjälpvariabler för användning i en enda metod"}]},{"type":"text","value":"\n langsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":"\n langsta_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" langsta_langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n langsta_langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n langsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"ord"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" langsta"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I implementeringen ovan är hjälpvariablerna endast tillgängliga när metoden utförs. De värden som lagras i dem kan inte orsaka komplikationer i andra delar av programmet."}]},{"type":"element","tagName":"h2","properties":{"id":"objekt-som-argument-till-funktioner","style":"position:relative;"},"children":[{"type":"text","value":"Objekt som argument till funktioner"},{"type":"element","tagName":"a","properties":{"href":"#objekt-som-argument-till-funktioner","ariaLabel":"objekt som argument till funktioner permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De objekt som skapas baserat på våra egna klasser är vanligtvis mutabla. Du kanske kommer ihåg att till exempel Python-listor är föränderliga: när de passeras som argument till funktioner kan deras innehåll ändras som ett resultat av exekveringen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss titta på ett enkelt exempel där en funktion får en referens till ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" som sitt argument. Funktionen ändrar sedan namnet på studenten. Både funktionen och huvudfunktionen som anropar den har åtkomst till samma objekt, så ändringen syns även i huvudfunktionen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# observera att typledtråden använder namnet på klassen definierad ovan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"andra_namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olle Studerande\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# skapa ett Studerande-objekt"}]},{"type":"text","value":"\nolle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olle Elev\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"olle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nandra_namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"olle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"olle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olle Elev (12345)\nOlle Studerande (12345)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är också möjligt att skapa objekt inom funktioner. Om en funktion returnerar en referens till det nyskapade objektet är det också åtkomligt inom huvudfunktionen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" choice\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna funktion skapar och returnerar ett nytt Studerande-objekt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den väljer slumpmässigt värden för namnet och studerandenumret."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ny_studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n fornamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Atte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Minna\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Maria\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n efternamn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Virtanen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lahtinen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Leinonen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Slumpmässigt namn"}]},{"type":"text","value":"\n namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"fornamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"efternamn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Slumpmässigt studerandenummer"}]},{"type":"text","value":"\n studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99999"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skapa och returnera ett Studerande-objekt"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kalla funktionen fem gånger och spara resultatet i en lista"}]},{"type":"text","value":"\n studeranden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ny_studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skriv ut resultatet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du kör ovanstående kan det resultera i följande utskrift (OBS: eftersom slumpen är inblandad kommer resultaten sannolikt att bli annorlunda om du testar koden själv)."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Maria Lahtinen (36213)\nAtte Virtanen (11859)\nMaria Pythonen (77330)\nAtte Pythonson (86451)\nMinna Pythonson (86211)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"objekt-som-argument-till-metoder","style":"position:relative;"},"children":[{"type":"text","value":"Objekt som argument till metoder"},{"type":"element","tagName":"a","properties":{"href":"#objekt-som-argument-till-metoder","ariaLabel":"objekt som argument till metoder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"På liknande sätt kan objekt fungera som argument till metoder. Låt oss ta en titt på ett exempel från en nöjespark:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langd\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Akattraktion"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" langdgrans"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"besokare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langdgrans "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" langdgrans\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ta_ombord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langdgrans"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"besokare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kom ombord\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" var för kort :(\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"besokare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" besökare)\""}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Attraktionen innehåller en metod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"motta_besökare"}]},{"type":"text","value":", som tar ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" som argument. Om besökaren är tillräckligt lång släpps denne ombord och antalet besökare ökas. Klasserna kan testas på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"berg_och_dalbana "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Akattraktion"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Berg_och_dalbana\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"120"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njakob "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jakob\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvilma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Vilma\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"105"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nberg_och_dalbana"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ta_ombord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jakob"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nberg_och_dalbana"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ta_ombord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vilma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berg_och_dalbana"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Jakob kom ombord\nVenla var för kort :(\nBerg_och_dalbana (1 besökare)"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bebiscenter","tmcname":"osa09-03_bebiscenter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns färdigt redan en klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" såväl som en ram för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":". Ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":"-objekt utför olika handlingar såsom mäter eller matar personer. I denna uppgift implementerar du resten av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":"-klassen. Koden för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" ska inte ändras överhuvudtaget!"}]},{"type":"element","tagName":"h2","properties":{"id":"vagning-av-person","style":"position:relative;"},"children":[{"type":"text","value":"Vägning av person"},{"type":"element","tagName":"a","properties":{"href":"#vagning-av-person","ariaLabel":"vagning av person permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":"-klassens definitione finns en ram för funktionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vagning"}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"vagning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# returnera vikten av personen som passerades som argument"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Metoden tar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":"-objekt som sitt argument. Den ska returnera personens vikt. Du kan komma åt en persons vikt genom attributet som definieras i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":"-klassen. Vänligen fyll i resten av implementationen för metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vagning"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan finns ett exempel på en huvudfunktion där ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BebisCenter"}]},{"type":"text","value":" väger två olika "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":"-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"haga_barnvard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BebisCenter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nerik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"176"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik väger 7 kg\nPeter väger 85 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"matning","style":"position:relative;"},"children":[{"type":"text","value":"Matning"},{"type":"element","tagName":"a","properties":{"href":"#matning","ariaLabel":"matning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är möjligt att ändra tillståndet för ett objekt som skickas som ett argument. Implementera metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mata(person: Person)"}]},{"type":"text","value":" som ökar vikten på den person som skickas som argument med ett."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel vägs två personer, och sedan matas en av dem tre gånger. Därefter vägs personerna igen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"haga_barnvard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCenter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nerik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"176"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mata"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" väger "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde visa att Eriks vikt har stigit med tre:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik väger 7 kg\nPeter väger 85 kg"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Erik väger 10 kg\nPeter väger 85 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"raknande-av-vagningar","style":"position:relative;"},"children":[{"type":"text","value":"Räknande av vägningar"},{"type":"element","tagName":"a","properties":{"href":"#raknande-av-vagningar","ariaLabel":"raknande av vagningar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vagningar"}]},{"type":"text","value":"() som returnerar det totala antalet vägningar som ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCenter"}]},{"type":"text","value":"-objekt har utfört. OBS: Du kommer att behöva ett nytt attribut för att hålla reda på antalet vägningar. Du kan använda följande kod för att testa din metod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"haga_barnvard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCenter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nerik "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"176"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Vägningar utförda "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Vägningar utförda "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhaga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"erik"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Vägningar utförda "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"haga_barnvard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vagningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vägningar utförda 0\nVägningar utförda 2\nVägningar utförda 6"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lunchkort och betalterminal","tmcname":"osa09-04_lunchkort_och_betalterminal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I föregående del fanns en "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-8/5-fler-exempel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"övning"}]},{"type":"text","value":" där du implementerade klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":". Kortet hade separata metoder för att äta en vanlig och en speciell lunch, samt en metod för att sätta in pengar på kortet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":", så som du ombads implementera den, har dock vissa problem. Kortet självt hade kunskap om priserna på de olika lunchalternativen och visste att subtrahera rätt mängd pengar från saldot baserat på dessa. Men tänk dig att priserna ändrades eller att nya varor infördes i systemet, men att flera kort redan var registrerade i systemet. Detta skulle innebära att alla befintliga kort skulle behöva ersättas med versioner med kunskap om de nya priserna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En bättre lösning skulle vara att göra korten ”dumma”, okunniga om priserna på olika produkter. Syftet med kortet skulle vara att helt enkelt hålla reda på det tillgängliga saldot. Alla mer komplicerade funktioner bör rymmas inom en annan klass: betalterminalen."}]},{"type":"element","tagName":"h2","properties":{"id":"ett-simplare-lunchkort","style":"position:relative;"},"children":[{"type":"text","value":"Ett simplare Lunchkort"},{"type":"element","tagName":"a","properties":{"href":"#ett-simplare-lunchkort","ariaLabel":"ett simplare lunchkort permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi förverkligar först en dummare version av "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":"-klassen. Kortet borde endast ha funktioner för att få reda på det tillfälliga saldot, lägga till pengar på kortet och subtrahera pengar från saldot. Fyll i metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"subtrahera_saldo(mangd)"}]},{"type":"text","value":" enligt kommentarerna i övningsfältet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"subtrahera_saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden ska endast kunna subtrahera från saldot"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ifall det finns tillräckligt med pengar på kortet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Lyckad betalning returnerar True, icke-lyckad False."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Huvudtestprogram:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saldo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subtrahera_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saldo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n resultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subtrahera_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckads:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saldo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Saldo 10\nBetalningen lyckades: True\nSaldo 2\nBetalningen lyckades: False\nSaldo 2"}]}]},{"type":"element","tagName":"h2","properties":{"id":"betalningsterminalen-och-hanterandet-av-kontantbetalning","style":"position:relative;"},"children":[{"type":"text","value":"Betalningsterminalen och hanterandet av kontantbetalning"},{"type":"element","tagName":"a","properties":{"href":"#betalningsterminalen-och-hanterandet-av-kontantbetalning","ariaLabel":"betalningsterminalen och hanterandet av kontantbetalning permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I studentkafeterian går det att betala med kontanter eller Lunchkort. En betalterminal används för att hantera både kontant- och korttransaktioner. Låt oss börja med kontanttransaktionerna."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en ram för en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Betalterminal"}]},{"type":"text","value":"-klass. Implementera metoderna enligt beskrivningen i kommentarerna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Betalterminal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# I kassan finns det ursprungligen 1000 euro i kontanter"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"formanliga "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"special "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"at_formanligt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" betalning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En formånlig lunch kostar 2.50 euro"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Öka värdet på pengarna som finns i terminalen med en förmånlig lunchs pris,"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# öka mängden förmånliga luncher och returnera rätt mängd växel."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall betalningen inte är tillräckligt stor säljs ingen lunch och hela summan returneras"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"at_special"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" betalning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En special lunch kostar 4.30 euro"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Öka värdet på pengarna som finns i terminalen med en special lunchs pris,"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# öka mängden special luncher och returnera rätt mängd växel."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Ifall betalningen inte är tillräckligt stor säljs ingen lunch och hela summan returneras"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Betalterminal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvaxel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden växel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vaxel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvaxel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden växel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vaxel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvaxel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4.3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden växel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vaxel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pengar i kassan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Förmånliga luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"formanliga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Special luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mängden växel 7.5\nMängden växel 2.5\nMängden växel 0.0\nPengar i kassan 1009.3\nFörmånliga luncher sålda 2\nSpecial luncher sålda 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"betalning-med-kort","style":"position:relative;"},"children":[{"type":"text","value":"Betalning med kort"},{"type":"element","tagName":"a","properties":{"href":"#betalning-med-kort","ariaLabel":"betalning med kort permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss nu implementera korttransaktioner. Vi kommer att behöva metoder som tar ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lunchkort"}]},{"type":"text","value":" som argument och minskar saldot på kortet med lunchens pris. Nedan hittar du ramar av dessa funktioner. Vänligen fyll i metoderna enligt beskrivningen i kommentarerna:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Betalterminal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"at_formanligt_kort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En förmånlig lunch kostar 2.50 euro."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Om kortet har tillräckligt med pengar, subtrahera lunchens pris och returnera True."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Annars returnera False"}]},{"type":"text","value":"\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"at_special_kort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En special lunch kostar 4.30 euro."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Om kortet har tillräckligt med pengar, subtrahera lunchens pris och returnera True."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Annars returnera False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" betalningar med Lunchkort ändrar inte på mängden pengar i terminalen. Luncher säljes däremot fortfarande så länge pengarna på saldot räcker till, alltså kom ihåg att öka mängden luncher som sålts."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande kod kan du använda för att testa din klass:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Betalterminal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvaxel "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden växel\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vaxel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_formanligt_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pengar i kassan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Förmånliga luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"formanliga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"special luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mängden växel 7.5\nBetalningen lyckades: True\nBetalningen lyckades: False\nBetalningen lyckades: True\nPengar i kassan 1002.5\nFörmånliga luncher sålda 2\nspecial luncher sålda 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"tillsattning-av-pengar-pa-kortet","style":"position:relative;"},"children":[{"type":"text","value":"Tillsättning av pengar på kortet"},{"type":"element","tagName":"a","properties":{"href":"#tillsattning-av-pengar-pa-kortet","ariaLabel":"tillsattning av pengar pa kortet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Slutligen lägger vi till en metod som gör att du kan sätta in pengar på kortet. Kortägaren betalar detta kontant, så att den insatta summan läggs till de pengar som finns tillgängliga i terminalen. Här är en ram för metoden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar_kortet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Du kan använda följande kod för att testa din metod:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Betalterminal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nantons_kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lunchkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Kortets saldo "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nexactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar_kortet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Kortets saldo "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresultat "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"at_special_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betalningen lyckades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" resultat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Kortets saldo "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"antons_kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euro\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pengar i kassan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Förmånliga luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"formanliga"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"special luncher sålda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"special"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kortets saldo 2 euro\nBetalningen lyckades: False\nKortets saldo 102 euro\nBetalningen lyckades: True\nKortets saldo 97.7 euro\nPengar i kassan 1100\nFörmånliga luncher sålda 0\nspecial luncher sålda 1"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-instans-av-samma-klass-som-argument-till-en-metod","style":"position:relative;"},"children":[{"type":"text","value":"En instans av samma klass som argument till en metod"},{"type":"element","tagName":"a","properties":{"href":"#en-instans-av-samma-klass-som-argument-till-en-metod","ariaLabel":"en instans av samma klass som argument till en metod permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nedan har vi ytterligare en version av klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fodelsear"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss anta att vi vill skriva ett program som jämför åldern på objekt av typen Person. Vi kan skriva en separat funktion för detta ändamål:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nmuhammad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Muhammad ibn Musa al-Khwarizmi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"780"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npascal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Blaise Pascal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1623"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngrace "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grace Hopper\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1906"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" aldre_an"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är inte äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" aldre_an"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är inte äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Muhammad ibn Musa al-Khwarizmi är äldre än Blaise Pascal\nGrace Hopper är inte äldre än Blaise Pascal"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En av principerna för objektorienterad programmering är att all funktionalitet som hanterar objekt av en viss typ ska inkluderas i klassdefinitionen, som metoder. I stället för en funktion kan vi alltså skriva en metod som gör det möjligt att jämföra åldern på ett Person-objekt med ett annat Person-objekt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fodelsear\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# OBS! Typledtrådar måste vara inom citationstecken ifall parametern är av samma typ som klassen självt!"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Person\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här kallas det objekt som metoden anropas på för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":", medan det andra Person-objektet kallas för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"annat"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Kom ihåg att anrop av en metod skiljer sig från anrop av en funktion. En metod är kopplad till ett objekt med punktnotationen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"muhammad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Muhammad ibn Musa al-Khwarizmi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"780"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npascal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Blaise Pascal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1623"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngrace "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grace Hopper\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1906"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"aldre_an"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är inte äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"aldre_an"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är inte äldre än "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till vänster om punkten finns själva objektet, som kallas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" i metoddefinitionen. Inom parentes står argumentet till metoden, vilket är det objekt som kallas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"annat"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften från programmet är exakt densamma som med funktionsimplementeringen ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till sist, en ganska kosmetisk punkt: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if...else"}]},{"type":"text","value":"-strukturen i metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"text","value":" är i stort sett onödig. Värdet på det booleska uttrycket i villkoret är redan exakt samma sanningsvärde som returneras. Metoden kan alltså förenklas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" fodelsear\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# OBS! Typledtrådar måste vara inom citationstecken ifall parametern är av samma typ som klassen självt!"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Person\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Liksom det framkommer av kommentarerna i exemplen ovan, så måste typledtråden omslutas av citattecken ifall parametern i en metoddefinition är av samma typ som klassen själv. Om citattecknen utelämnas uppstår ett fel, vilket du kommer att se om du försöker med följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta fungerar inte, Person måste vara innanför citationstecken"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"aldre_an"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" annat"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fodelsear"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bostadsjämförelse","tmcname":"osa09-05_bostadsjamforelse"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Databasen hos en fastighetsmäklare innehåller register över tillgängliga fastigheter med objekt som definieras av följande klass:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kvadratmeter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kvadratpris"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rum\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kvadratmeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kvadratmeter\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kvadratpris "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kvadratpris"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din uppgift är att implementera metoder som gör det möjligt att jämföra tillgängliga egenskaper."}]},{"type":"element","tagName":"h2","properties":{"id":"ar-den-storre","style":"position:relative;"},"children":[{"type":"text","value":"Är den större?"},{"type":"element","tagName":"a","properties":{"href":"#ar-den-storre","ariaLabel":"ar den storre permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storre(self, jamforelse)"}]},{"type":"text","value":", som returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", ifall "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"text","value":"-objektet är större än objektet det jämförs med."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen borde fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"eira_etta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nberghall_tvaa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njakobacka_trea "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eira_etta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"storre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jakobacka_trea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"storre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"prisskillnad","style":"position:relative;"},"children":[{"type":"text","value":"Prisskillnad"},{"type":"element","tagName":"a","properties":{"href":"#prisskillnad","ariaLabel":"prisskillnad permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prisskillnad(self, jamforelse)"}]},{"type":"text","value":", som returnerar skillnaden i pris mellan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"text","value":"-objektet självt och det som det jämförs med. Prisskillnaden är det absoluta värdet av skillnaden mellan de två fastigheternas totalpriser. Det totala priset för en fastighet är dess kvadratmeterpris multiplicerat med antalet kvadratmeter i fastigheten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen borde fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"eira_etta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nberghall_tvaa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njakobacka_trea "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eira_etta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"prisskillnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jakobacka_trea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"prisskillnad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"71600\n35400"}]}]},{"type":"element","tagName":"h2","properties":{"id":"dyrare","style":"position:relative;"},"children":[{"type":"text","value":"Dyrare?"},{"type":"element","tagName":"a","properties":{"href":"#dyrare","ariaLabel":"dyrare permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dyrare(self, jamforelse)"}]},{"type":"text","value":" som returnerar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", ifall "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bostad"}]},{"type":"text","value":"-objektet är dyrare än objektet som det jämförs med."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur funktionen borde fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"eira_etta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nberghall_tvaa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njakobacka_trea "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bostad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eira_etta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dyrare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jakobacka_trea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dyrare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"berghall_tvaa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Kommer du att kunna använda olika datastrukturer för att hantera objekt
  • \n
  • Vet du hur objekt kan bli passerade som argument
  • \n

Varje värde i Python är ett objekt. Alla objekt som du skapar baserat på en klass som du själv har definierat fungerar exakt på samma sätt som alla \"vanliga\" Python-objekt. Objekt kan till exempel lagras i en lista:

from datetime import date\n\nclass SlutfordKurs:\n\n    def __init__(self, kurs: str, studiepoang: int, slutforsdatum: date):\n        self.kurs = kurs\n        self.studiepoang = studiepoang\n        self.slutforsdatum = slutforsdatum\n\n\nif __name__ == \"__main__\":\n    # Vi skapar några slutförda kurser och lägger dessa i en lista\n    prestationer = []\n\n    mat1 = SlutfordKurs(\"Matematik 1\", 5, date(2020, 3, 11))\n    prg1 = SlutfordKurs(\"Programmering 1\", 6, date(2019, 12, 17))\n\n    prestationer.append(mat1)\n    prestationer.append(prg1)\n\n    # Vi lägger några till rakt till listan\n    prestationer.append(SlutfordKurs(\"Fysik 2\", 4, date(2019, 11, 10)))\n    prestationer.append(SlutfordKurs(\"Programmering 2\", 5, date(2020, 5, 19)))\n\n    # Vi går igenom alla slutförda kurser, skriver ut deras namn och räknar ihop den totala mängden studiepoäng\n    studiepoang = 0\n    for prestation in prestationer:\n        print(prestation.kurs)\n        studiepoang += prestation.studiepoang\n\n    print(\"Studiepoäng totalt:\", studiepoang)

Matematik 1\nProgrammering 1\nFysik 2\nProgrammering 2\nStudiepoäng totalt: 20

Uppgiftsbotten har en klass med namnet Bil som representerar en bil genom två attribut: marke (str) och topphastighet (int).

Skapa funktionen snabbaste_bil(bilar: list), som får en lista av Bil-objekt som argument.

Funktionen ska returnera märket på den snabbaste bilen. Du kan anta att det alltid kommer att finnas en enda bil med den högsta topphastigheten. Ändra inte listan som ges som argument, gör heller inte några ändringar i klassdefinitionen för Bil.

Du kan använda följande kod för att testa din funktion:

if __name__ == \"__main__\":\n    bil1 = Bil(\"Mersu\", 195)\n    bil2 = Bil(\"Lada\", 110)\n    bil3 = Bil(\"Ferrari\", 280)\n    bil4 = Bil(\"Trabant\", 85)\n\n    bilar = [bil1, bil2, bil3, bil4]\n    print(snabbaste_bil(bilar))

Ferrari

I uppgiftsbotten hittas klassen Provprestation, som, liksom namnet anger, tar modell av en provtagares prestation i ett prov. Den har två attribut, presterare (str) och poang (int).

Skapa funktionen godkanda(prestationer: list, poanggrans: int), som får som parametrar en lista av provprestationer och ett heltal som representerar det minsta poängantalet för att bli godkänd.

Funktionen ska skapa och returnera en ny lista, som innehåller endast de prov som kom över poänggränsen i den ursprungliga listan. Ändra inte listan som ges som argument, ändra heller inte definitionen för klassen Provprestation.

Du kan använda följande kod för att testa funktionen:

if __name__ == \"__main__\":\n    s1 = Provprestation(\"Peter\", 12)\n    s2 = Provprestation(\"Pernilla\", 19)\n    s3 = Provprestation(\"Per\", 15)\n    s4 = Provprestation(\"Pia\", 9)\n    s5 = Provprestation(\"Petra\", 17)\n\n    klarade = godkanda([s1, s2, s3, s4, s5], 15)\n    for godkand in klarade:\n        print(klarade)

Provprestation (presterare: Pernilla, poang: 19)\nProvprestation (presterare: Per, poang: 15)\nProvprestation (presterare: Petra, poang: 17)

Du kanske minns att listor inte innehåller några objekt i sig själva. De innehåller referenser till objekt. Exakt samma objekt kan förekomma flera gånger i en och samma lista, och det kan refereras till flera gånger i listan eller utanför den. Låt oss ta en titt på ett exempel:

class Produkt:\n    def __init__(self, namn: int, enhet: str):\n        self.namn = namn\n        self.enhet = enhet\n\n\nif __name__ == \"__main__\":\n    affarslista = []\n    mjolk = Produkt(\"Mjölk\", \"liter\")\n\n    affarslista.append(mjolk)\n    affarslista.append(mjolk)\n    affarslista.append(Produkt(\"Gurka\", \"st\"))
\n \n \n \n \n \n \"9\n \n \n

Self eller inget self?

Hittills har vi bara snuddat vid ytan när det gäller att använda parameternamnet self. Låt oss titta närmare på när det bör eller inte bör användas.

Nedan har vi en enkel klass som låter oss skapa ett ordförråd-objekt som innehåller några ord:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n\n    def tillsatt_ord(self, ord: str):\n        if not ord in self.ord:\n            self.ord.append(ord)\n\n    def utskrift(self):\n        for ord in sorted(self.ord):\n            print(ord)\n\nordforrad = Ordforrad()\nordforrad.tillsatt_ord(\"python\")\nordforrad.tillsatt_ord(\"objekt\")\nordforrad.tillsatt_ord(\"objekt-orienterad programmering\")\nordforrad.tillsatt_ord(\"objekt\")\nordforrad.tillsatt_ord(\"nörd\")\n\nordforrad.utskrift()

nörd\nobjekt\nobjekt-orienterad programmering\npython

Listan med ord lagras i ett attribut med namnet self.ord. I det här fallet är parameternamnet self obligatoriskt både i klassens konstruktormetod och i alla andra metoder som använder variabeln. Om self utelämnas kommer de olika metoderna inte att få tillgång till samma lista med ord.

Låt oss lägga till en ny metod i vår klassdefinition. Metoden langsta_ord(self) returnerar (ett av) de längsta orden i ordförrådet.

Följande är ett sätt att utföra denna uppgift, men vi kommer snart att se att det inte är ett särskilt bra sätt:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n\n    # ...\n\n    def langsta_ord(self):\n        # vi definierar två hjälpvariabler\n        self.langsta = \"\"\n        self.langsta_langd = 0\n\n        for ord in self.ord:\n            if len(ord) > self.langsta_langd:\n                self.langsta_langd = len(ord)\n                self.langsta = ord\n\n        return self.langsta

Den här metoden använder två hjälpvariabler som deklareras med parameternamnet self. Kom ihåg att namnen på variablerna inte spelar någon roll i funktionell mening, så dessa variabler kan också namnges mer förvirrande som till exempel hjalpare och hjalpare2. Koden börjar se lite kryptisk ut:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n\n    # ...\n\n    def langsta_ord(self):\n        # vi definierar två hjälpvariabler\n        self.hjalpare = \"\"\n        self.hjalpare2 = 0\n\n        for ord in self.ord:\n            if len(ord) > self.hjalpare2:\n                self.hjalpare2 = len(ord)\n                self.hjalpare = ord\n\n        return self.hjalpare

När en variabel deklareras med parameternamnet self blir den ett attribut till objektet. Detta innebär att variabeln kommer att existera så länge objektet existerar. Specifikt kommer variabeln att fortsätta existera även efter att metoden som deklarerar den har avslutat sin exekvering (engelska “Execution”). I exemplet ovan är detta helt onödigt, eftersom hjälpvariablerna endast är avsedda att användas inom metoden longest_word(self). Så att deklarera hjälpvariabler med parameternamnet self är inte en särskilt bra idé här.

Förutom att variabler kan existera efter sitt \"utgångsdatum\" kan användning av self för att skapa nya attribut där de inte är nödvändiga orsaka svåra buggar i din kod. Särskilt generiskt namngivna attribut som self.hjalpare, som sedan används i flera olika metoder, kan orsaka oväntade beteenden som är svåra att spåra.

Om t.ex. en hjälpvariabel deklareras som ett attribut och tilldelas ett ursprungligt värde i konstruktorn, men variabeln sedan används i ett orelaterat sammanhang i en annan metod, blir resultatet ofta oförutsägbart:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n        # vi definierar hjälparvariabler\n        self.hjalpare = \"\"\n        self.hjalpare2 = \"\"\n        self.hjalpare3 = \"\"\n        self.hjalpare4 = \"\"\n\n    # ...\n\n    def langsta_ord(self):\n        for ord in self.ord:\n            # detta fungerar inte eftersom hjalpare2 har fel typ\n            if len(ord) > self.hjalpare2:\n                self.hjalpare2 = len(ord)\n                self.hjalpare = ord\n\n        return self.hjalpare

Man skulle kunna tro att detta skulle lösas genom att bara deklarera attributen där de används, utanför konstruktorn, men detta resulterar i en situation där de attribut som är tillgängliga via ett objekt är beroende av vilka metoder som har utförts. I föregående del såg vi att fördelen med att deklarera attribut i konstruktorn är att alla instanser av klassen då kommer att ha exakt samma attribut. Om så inte är fallet kan det lätt leda till fel om man använder olika instanser av klassen.

Sammanfattningsvis, om du behöver hjälpvariabler för användning inom en enda metod, är det korrekta sättet att göra det utan self. För att göra din kod lättare att förstå, använd också informativa variabelnamn:

class Ordforrad:\n    def __init__(self):\n        self.ord = []\n\n    # ...\n\n    def langsta_ord(self):\n        # detta är det korrekta sättet att definiera\n        # hjälpvariabler för användning i en enda metod\n        langsta = \"\"\n        langsta_langd = 0\n\n        for ord in self.ord:\n            if len(ord) > langsta_langd:\n                langsta_langd = len(ord)\n                langsta = ord\n\n        return langsta

I implementeringen ovan är hjälpvariablerna endast tillgängliga när metoden utförs. De värden som lagras i dem kan inte orsaka komplikationer i andra delar av programmet.

Objekt som argument till funktioner

De objekt som skapas baserat på våra egna klasser är vanligtvis mutabla. Du kanske kommer ihåg att till exempel Python-listor är föränderliga: när de passeras som argument till funktioner kan deras innehåll ändras som ett resultat av exekveringen.

Låt oss titta på ett enkelt exempel där en funktion får en referens till ett objekt av typen Studerande som sitt argument. Funktionen ändrar sedan namnet på studenten. Både funktionen och huvudfunktionen som anropar den har åtkomst till samma objekt, så ändringen syns även i huvudfunktionen.

class Studerande:\n    def __init__(self, namn: str, studerandenummer: str):\n        self.namn = namn\n        self.studerandenummer = studerandenummer\n\n    def __str__(self):\n        return f\"{self.namn} ({self.studerandenummer})\"\n\n# observera att typledtråden använder namnet på klassen definierad ovan\ndef andra_namn(studerande: Studerande):\n    studerande.namn = \"Olle Studerande\"\n\n# skapa ett Studerande-objekt\nolle = Studerande(\"Olle Elev\", \"12345\")\n\nprint(olle)\nandra_namn(olle)\nprint(olle)

Olle Elev (12345)\nOlle Studerande (12345)

Det är också möjligt att skapa objekt inom funktioner. Om en funktion returnerar en referens till det nyskapade objektet är det också åtkomligt inom huvudfunktionen:

from random import randint, choice\n\nclass Studerande:\n    def __init__(self, namn: str, studerandenummer: str):\n        self.namn = namn\n        self.studerandenummer = studerandenummer\n\n    def __str__(self):\n        return f\"{self.namn} ({self.studerandenummer})\"\n\n\n# Denna funktion skapar och returnerar ett nytt Studerande-objekt.\n# Den väljer slumpmässigt värden för namnet och studerandenumret.\ndef ny_studerande():\n    fornamn = [\"Atte\",\"Peter\",\"Minna\",\"Maria\"]\n    efternamn = [\"Virtanen\", \"Lahtinen\", \"Leinonen\", \"Pythonson\"]\n\n    # Slumpmässigt namn\n    namn = choice(fornamn) + \" \" + choice(efternamn)\n\n    # Slumpmässigt studerandenummer\n    studerandenummer = str(randint(10000,99999))\n\n    # Skapa och returnera ett Studerande-objekt\n    return Studerande(namn, studerandenummer)\n\nif __name__ == \"__main__\":\n    # Kalla funktionen fem gånger och spara resultatet i en lista\n    studeranden = []\n    for i in range(5):\n        studeranden.append(ny_studerande())\n\n    # Skriv ut resultatet\n    for studerande in studeranden:\n        print(studerande)

Om du kör ovanstående kan det resultera i följande utskrift (OBS: eftersom slumpen är inblandad kommer resultaten sannolikt att bli annorlunda om du testar koden själv).

Maria Lahtinen (36213)\nAtte Virtanen (11859)\nMaria Pythonen (77330)\nAtte Pythonson (86451)\nMinna Pythonson (86211)

Objekt som argument till metoder

På liknande sätt kan objekt fungera som argument till metoder. Låt oss ta en titt på ett exempel från en nöjespark:

class Person:\n    def __init__(self, namn: str, langd: int):\n        self.namn = namn\n        self.langd = langd\n\nclass Akattraktion:\n    def __init__(self, namn: str, langdgrans: int):\n        self.besokare = 0\n        self.namn = namn\n        self.langdgrans = langdgrans\n\n    def ta_ombord(self, person: Person):\n        if person.langd >= self.langdgrans:\n            self.besokare += 1\n            print(f\"{person.namn} kom ombord\")\n        else:\n            print(f\"{person.namn} var för kort :(\")\n\n    def __str__(self):\n        return f\"{self.namn} ({self.besokare} besökare)\"

Attraktionen innehåller en metod motta_besökare, som tar ett objekt av typen Person som argument. Om besökaren är tillräckligt lång släpps denne ombord och antalet besökare ökas. Klasserna kan testas på följande sätt:

berg_och_dalbana = Akattraktion(\"Berg_och_dalbana\", 120)\njakob = Person(\"Jakob\", 172)\nvilma = Person(\"Vilma\", 105)\n\nberg_och_dalbana.ta_ombord(jakob)\nberg_och_dalbana.ta_ombord(vilma)\n\nprint(berg_och_dalbana)

Jakob kom ombord\nVenla var för kort :(\nBerg_och_dalbana (1 besökare)

I uppgiftsbotten finns färdigt redan en klass Person såväl som en ram för klassen BebisCenter. Ett BebisCenter-objekt utför olika handlingar såsom mäter eller matar personer. I denna uppgift implementerar du resten av BebisCenter-klassen. Koden för klassen Person ska inte ändras överhuvudtaget!

Vägning av person

I BebisCenter-klassens definitione finns en ram för funktionen vagning

class BebisCenter:\n    def vagning(self, person: Person):\n        # returnera vikten av personen som passerades som argument\n        return -1

Metoden tar ett Person-objekt som sitt argument. Den ska returnera personens vikt. Du kan komma åt en persons vikt genom attributet som definieras i Person-klassen. Vänligen fyll i resten av implementationen för metoden vagning.

Nedan finns ett exempel på en huvudfunktion där ett BebisCenter väger två olika Person-objekt:

haga_barnvard = BebisCenter()\n\nerik = Person(\"Erik\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"{erik.namn} väger {haga_barnvard.vagning(erik)} kg\")\nprint(f\"{peter.namn} väger {haga_barnvard.vagning(peter)} kg\")

Erik väger 7 kg\nPeter väger 85 kg

Matning

Det är möjligt att ändra tillståndet för ett objekt som skickas som ett argument. Implementera metoden mata(person: Person) som ökar vikten på den person som skickas som argument med ett.

I följande exempel vägs två personer, och sedan matas en av dem tre gånger. Därefter vägs personerna igen:

haga_barnvard = BabyCenter()\n\nerik = Person(\"Erik\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"{erik.namn} väger {haga_barnvard.vagning(erik)} kg\")\nprint(f\"{peter.namn} väger {haga_barnvard.vagning(peter)} kg\")\nprint()\n\nhaga_barnvard.mata(erik)\nhaga_barnvard.mata(erik)\nhaga_barnvard.mata(erik)\n\nprint(f\"{erik.namn} väger {haga_barnvard.vagning(erik)} kg\")\nprint(f\"{peter.namn} väger {haga_barnvard.vagning(peter)} kg\")

Utskriften borde visa att Eriks vikt har stigit med tre:

Erik väger 7 kg\nPeter väger 85 kg

Erik väger 10 kg\nPeter väger 85 kg

Räknande av vägningar

Implementera metoden vagningar() som returnerar det totala antalet vägningar som ett BabyCenter-objekt har utfört. OBS: Du kommer att behöva ett nytt attribut för att hålla reda på antalet vägningar. Du kan använda följande kod för att testa din metod:

haga_barnvard = BabyCenter()\n\nerik = Person(\"Erik\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"Vägningar utförda {haga_barnvard.vagningar()}\")\n\nhaga_barnvard.vagning(erik)\nhaga_barnvard.vagning(erik)\n\nprint(f\"Vägningar utförda {haga_barnvard.vagningar()}\")\n\nhaga_barnvard.vagning(erik)\nhaga_barnvard.vagning(erik)\nhaga_barnvard.vagning(erik)\nhaga_barnvard.vagning(erik)\n\nprint(f\"Vägningar utförda {haga_barnvard.vagningar()}\")

Vägningar utförda 0\nVägningar utförda 2\nVägningar utförda 6

I föregående del fanns en övning där du implementerade klassen Lunchkort. Kortet hade separata metoder för att äta en vanlig och en speciell lunch, samt en metod för att sätta in pengar på kortet.

Klassen Lunchkort, så som du ombads implementera den, har dock vissa problem. Kortet självt hade kunskap om priserna på de olika lunchalternativen och visste att subtrahera rätt mängd pengar från saldot baserat på dessa. Men tänk dig att priserna ändrades eller att nya varor infördes i systemet, men att flera kort redan var registrerade i systemet. Detta skulle innebära att alla befintliga kort skulle behöva ersättas med versioner med kunskap om de nya priserna.

En bättre lösning skulle vara att göra korten ”dumma”, okunniga om priserna på olika produkter. Syftet med kortet skulle vara att helt enkelt hålla reda på det tillgängliga saldot. Alla mer komplicerade funktioner bör rymmas inom en annan klass: betalterminalen.

Ett simplare Lunchkort

Vi förverkligar först en dummare version av Lunchkort-klassen. Kortet borde endast ha funktioner för att få reda på det tillfälliga saldot, lägga till pengar på kortet och subtrahera pengar från saldot. Fyll i metoden subtrahera_saldo(mangd) enligt kommentarerna i övningsfältet:

class Lunchkort:\n    def __init__(self, saldo: float):\n        self.saldo = saldo\n\n    def tillsatt_pengar(self, mangd: float):\n        self.saldo += mangd\n\n    def subtrahera_saldo(self, mangd: float):\n        pass\n        # Metoden ska endast kunna subtrahera från saldot\n        # ifall det finns tillräckligt med pengar på kortet\n        # Lyckad betalning returnerar True, icke-lyckad False.

Huvudtestprogram:

if __name__ == \"__main__\":\n    kort = Lunchkort(10)\n    print(\"Saldo\", kort.saldo)\n    resultat = kort.subtrahera_saldo(8)\n    print(\"Betalningen lyckades:\", resultat)\n    print(\"Saldo\", kort.saldo)\n    resultat = kort.subtrahera_saldo(4)\n    print(\"Betalningen lyckads:\", resultat)\n    print(\"Saldo\", kort.saldo)

Saldo 10\nBetalningen lyckades: True\nSaldo 2\nBetalningen lyckades: False\nSaldo 2

Betalningsterminalen och hanterandet av kontantbetalning

I studentkafeterian går det att betala med kontanter eller Lunchkort. En betalterminal används för att hantera både kontant- och korttransaktioner. Låt oss börja med kontanttransaktionerna.

Här har vi en ram för en Betalterminal-klass. Implementera metoderna enligt beskrivningen i kommentarerna:

class Betalterminal:\n    def __init__(self):\n        # I kassan finns det ursprungligen 1000 euro i kontanter\n        self.pengar = 1000\n        self.formanliga = 0\n        self.special = 0\n\n    def at_formanligt(self, betalning: float):\n        # En formånlig lunch kostar 2.50 euro\n        # Öka värdet på pengarna som finns i terminalen med en förmånlig lunchs pris,\n        # öka mängden förmånliga luncher och returnera rätt mängd växel.\n        # Ifall betalningen inte är tillräckligt stor säljs ingen lunch och hela summan returneras\n\n    def at_special(self, betalning: float):\n        # En special lunch kostar 4.30 euro\n        # Öka värdet på pengarna som finns i terminalen med en special lunchs pris,\n        # öka mängden special luncher och returnera rätt mängd växel.\n        # Ifall betalningen inte är tillräckligt stor säljs ingen lunch och hela summan returneras

Exempel på användning:

exactum = Betalterminal()\n\nvaxel = exactum.at_formanligt(10)\nprint(\"Mängden växel\", vaxel)\n\nvaxel = exactum.at_formanligt(5)\nprint(\"Mängden växel\", vaxel)\n\nvaxel = exactum.at_special(4.3)\nprint(\"Mängden växel\", vaxel)\n\nprint(\"Pengar i kassan\", exactum.pengar)\nprint(\"Förmånliga luncher sålda\", exactum.formanliga)\nprint(\"Special luncher sålda\", exactum.special)

Mängden växel 7.5\nMängden växel 2.5\nMängden växel 0.0\nPengar i kassan 1009.3\nFörmånliga luncher sålda 2\nSpecial luncher sålda 1

Betalning med kort

Låt oss nu implementera korttransaktioner. Vi kommer att behöva metoder som tar ett Lunchkort som argument och minskar saldot på kortet med lunchens pris. Nedan hittar du ramar av dessa funktioner. Vänligen fyll i metoderna enligt beskrivningen i kommentarerna:

class Betalterminal:\n    # ...\n\n    def at_formanligt_kort(self, kort: Lunchkort):\n        # En förmånlig lunch kostar 2.50 euro.\n        # Om kortet har tillräckligt med pengar, subtrahera lunchens pris och returnera True.\n        # Annars returnera False\n\n\n    def at_special_kort(self, kort: Lunchkort):\n        # En special lunch kostar 4.30 euro.\n        # Om kortet har tillräckligt med pengar, subtrahera lunchens pris och returnera True.\n        # Annars returnera False

OBS: betalningar med Lunchkort ändrar inte på mängden pengar i terminalen. Luncher säljes däremot fortfarande så länge pengarna på saldot räcker till, alltså kom ihåg att öka mängden luncher som sålts.

Följande kod kan du använda för att testa din klass:

exactum = Betalterminal()\n\nvaxel = exactum.at_formanligt(10)\nprint(\"Mängden växel\", vaxel)\n\nkort = Lunchkort(7)\n\nresultat = exactum.at_special_kort(kort)\nprint(\"Betalningen lyckades:\", resultat)\nresultat = exactum.at_special_kort(kort)\nprint(\"Betalningen lyckades:\", resultat)\nresultat = exactum.at_formanligt_kort(kort)\nprint(\"Betalningen lyckades:\", resultat)\n\nprint(\"Pengar i kassan\", exactum.pengar)\nprint(\"Förmånliga luncher sålda\", exactum.formanliga)\nprint(\"special luncher sålda\", exactum.special)

Mängden växel 7.5\nBetalningen lyckades: True\nBetalningen lyckades: False\nBetalningen lyckades: True\nPengar i kassan 1002.5\nFörmånliga luncher sålda 2\nspecial luncher sålda 1

Tillsättning av pengar på kortet

Slutligen lägger vi till en metod som gör att du kan sätta in pengar på kortet. Kortägaren betalar detta kontant, så att den insatta summan läggs till de pengar som finns tillgängliga i terminalen. Här är en ram för metoden:

def tillsatt_pengar_kortet(self, kort: Lunchkort, summa: float):\n    pass

Du kan använda följande kod för att testa din metod:

exactum = Betalterminal()\n\nantons_kort = Lunchkort(2)\nprint(f\"Kortets saldo {antons_kort.saldo} euro\")\n\nresultat = exactum.at_special_kort(antons_kort)\nprint(\"Betalningen lyckades:\", resultat)\n\nexactum.tillsatt_pengar_kortet(antons_kort, 100)\nprint(f\"Kortets saldo {antons_kort.saldo} euro\")\n\nresultat = exactum.at_special_kort(antons_kort)\nprint(\"Betalningen lyckades:\", resultat)\nprint(f\"Kortets saldo {antons_kort.saldo} euro\")\n\nprint(\"Pengar i kassan\", exactum.pengar)\nprint(\"Förmånliga luncher sålda\", exactum.formanliga)\nprint(\"special luncher sålda\", exactum.special)

Kortets saldo 2 euro\nBetalningen lyckades: False\nKortets saldo 102 euro\nBetalningen lyckades: True\nKortets saldo 97.7 euro\nPengar i kassan 1100\nFörmånliga luncher sålda 0\nspecial luncher sålda 1

En instans av samma klass som argument till en metod

Nedan har vi ytterligare en version av klassen Person:

class Person:\n    def __init__(self, namn: str, fodelsear: int):\n        self.namn = namn\n        self.fodelsear = fodelsear

Låt oss anta att vi vill skriva ett program som jämför åldern på objekt av typen Person. Vi kan skriva en separat funktion för detta ändamål:

def aldre_an(person1: Person, person2: Person):\n    if person1.fodelsear < person2.fodelsear:\n        return True\n    else:\n        return False\n\nmuhammad = Person(\"Muhammad ibn Musa al-Khwarizmi\", 780)\npascal = Person(\"Blaise Pascal\", 1623)\ngrace = Person(\"Grace Hopper\", 1906)\n\nif aldre_an(muhammad, pascal):\n    print(f\"{muhammad} är äldre än {pascal}\")\nelse:\n    print(f\"{muhammad} är inte äldre än {pascal}\")\n\nif aldre_an(grace, pascal):\n    print(f\"{grace} är äldre än {pascal}\")\nelse:\n    print(f\"{grace} är inte äldre än {pascal}\")

Muhammad ibn Musa al-Khwarizmi är äldre än Blaise Pascal\nGrace Hopper är inte äldre än Blaise Pascal

En av principerna för objektorienterad programmering är att all funktionalitet som hanterar objekt av en viss typ ska inkluderas i klassdefinitionen, som metoder. I stället för en funktion kan vi alltså skriva en metod som gör det möjligt att jämföra åldern på ett Person-objekt med ett annat Person-objekt:

class Person:\n    def __init__(self, namn: str, fodelsear: int):\n        self.namn = namn\n        self.fodelsear = fodelsear\n\n    # OBS! Typledtrådar måste vara inom citationstecken ifall parametern är av samma typ som klassen självt!\n    def aldre_an(self, annat: \"Person\"):\n        if self.fodelsear < annat.fodelsear:\n            return True\n        else:\n            return False

Här kallas det objekt som metoden anropas på för self, medan det andra Person-objektet kallas för annat.

Kom ihåg att anrop av en metod skiljer sig från anrop av en funktion. En metod är kopplad till ett objekt med punktnotationen:

muhammad = Person(\"Muhammad ibn Musa al-Khwarizmi\", 780)\npascal = Person(\"Blaise Pascal\", 1623)\ngrace = Person(\"Grace Hopper\", 1906)\n\nif muhammad.aldre_an(pascal):\n    print(f\"{muhammad.namn} är äldre än {pascal.namn}\")\nelse:\n    print(f\"{muhammad.namn} är inte äldre än {pascal.namn}\")\n\nif grace.aldre_an(pascal):\n    print(f\"{grace.namn} är äldre än {pascal.namn}\")\nelse:\n    print(f\"{grace.namn} är inte äldre än {pascal.namn}\")

Till vänster om punkten finns själva objektet, som kallas self i metoddefinitionen. Inom parentes står argumentet till metoden, vilket är det objekt som kallas annat.

Utskriften från programmet är exakt densamma som med funktionsimplementeringen ovan.

Till sist, en ganska kosmetisk punkt: if...else-strukturen i metoden aldre_an är i stort sett onödig. Värdet på det booleska uttrycket i villkoret är redan exakt samma sanningsvärde som returneras. Metoden kan alltså förenklas:

class Person:\n    def __init__(self, namn: str, fodelsear: int):\n        self.namn = namn\n        self.fodelsear = fodelsear\n\n    # OBS! Typledtrådar måste vara inom citationstecken ifall parametern är av samma typ som klassen självt!\n    def aldre_an(self, annat: \"Person\"):\n        return self.fodelsear < annat.fodelsear:

Liksom det framkommer av kommentarerna i exemplen ovan, så måste typledtråden omslutas av citattecken ifall parametern i en metoddefinition är av samma typ som klassen själv. Om citattecknen utelämnas uppstår ett fel, vilket du kommer att se om du försöker med följande:

class Person:\n    # ...\n\n    # Detta fungerar inte, Person måste vara innanför citationstecken\n    def aldre_an(self, annat: Person):\n        return self.fodelsear < annat.fodelsear:

Databasen hos en fastighetsmäklare innehåller register över tillgängliga fastigheter med objekt som definieras av följande klass:

class Bostad:\n    def __init__(self, rum: int, kvadratmeter: int, kvadratpris: int):\n        self.rum = rum\n        self.kvadratmeter = kvadratmeter\n        self.kvadratpris = kvadratpris

Din uppgift är att implementera metoder som gör det möjligt att jämföra tillgängliga egenskaper.

Är den större?

Skapa metoden storre(self, jamforelse), som returnerar True, ifall Bostad-objektet är större än objektet det jämförs med.

Exempel på hur funktionen borde fungera:

eira_etta = Bostad(1, 16, 5500)\nberghall_tvaa = Bostad(2, 38, 4200)\njakobacka_trea = Bostad(3, 78, 2500)\n\nprint(eira_etta.storre(berghall_tvaa))\nprint(jakobacka_trea.storre(berghall_tvaa))

False\nTrue

Prisskillnad

Skapa metoden prisskillnad(self, jamforelse), som returnerar skillnaden i pris mellan Bostad-objektet självt och det som det jämförs med. Prisskillnaden är det absoluta värdet av skillnaden mellan de två fastigheternas totalpriser. Det totala priset för en fastighet är dess kvadratmeterpris multiplicerat med antalet kvadratmeter i fastigheten.

Exempel på hur funktionen borde fungera:

eira_etta = Bostad(1, 16, 5500)\nberghall_tvaa = Bostad(2, 38, 4200)\njakobacka_trea = Bostad(3, 78, 2500)\n\nprint(eira_etta.prisskillnad(berghall_tvaa))\nprint(jakobacka_trea.prisskillnad(berghall_tvaa))

71600\n35400

Dyrare?

Skapa metoden dyrare(self, jamforelse) som returnerar True, ifall Bostad-objektet är dyrare än objektet som det jämförs med.

Exempel på hur funktionen borde fungera:

eira_etta = Bostad(1, 16, 5500)\nberghall_tvaa = Bostad(2, 38, 4200)\njakobacka_trea = Bostad(3, 78, 2500)\n\nprint(eira_etta.dyrare(berghall_tvaa))\nprint(jakobacka_trea.dyrare(berghall_tvaa))

False\nTrue

","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/1-objekt-och-referenser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-9/2-objekt-som-attribut/page-data.json b/page-data/osa-9/2-objekt-som-attribut/page-data.json index b76aba8fc..428a38cb2 100644 --- a/page-data/osa-9/2-objekt-som-attribut/page-data.json +++ b/page-data/osa-9/2-objekt-som-attribut/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-9/2-objekt-som-attribut", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur man använder objekt som attribut i andra objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du vara bekant med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan sett exempel på klasser som har listor som attribut. Eftersom det alltså inte finns något som hindrar oss från att inkludera mutabla objekt som attribut i våra klasser, kan vi lika gärna använda instanser av våra egna klasser som attribut i andra klasser som vi själva har definierat. I följande exempel kommer vi att definiera klasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kurs"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"text","value":". En slutförd kurs använder sig av de två första klasserna. Klassdefinitionerna är mycket korta och enkla för att vi bättre ska kunna koncentrera oss på tekniken att använda instanser av våra egna klasser som attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kommer att anta att varje klass definieras i en separat fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först definierar vi klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kurs"}]},{"type":"text","value":" i en fil med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kurs.py"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Kurs"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kod\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till näst, klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" i en fil med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande.py"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Slutligen är klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"text","value":" definierad i en fil med namned "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs.py"}]},{"type":"text","value":". Eftersom den använder de två andra klasserna, måste de importeras innan de kan användas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" kurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Kurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Studerande\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerande\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kurs\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vitsord"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel av en huvudfunktion som lägger till några slutförda kurser i en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" slutfordkurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" SlutfordKurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" kurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Kurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Studerande\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar en lista av studeranden"}]},{"type":"text","value":"\nstuderanden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstuderanden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olle\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderanden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderanden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"9999\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"43"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderanden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"3333\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kurssen Introduktion till Programmering"}]},{"type":"text","value":"\nitp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till Programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"itp1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi ger prestationer för varje student, med vitsordet 3 till alla"}]},{"type":"text","value":"\nprestationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" itp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut studerandenas namn för varje avklarad kurs"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olle\nPeter\nLena\nTina"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad exakt händer med alla prickar på raden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print(kurs.studerande.namn)"}]},{"type":"text","value":"?"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kurs"}]},{"type":"text","value":" är en instans av klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande"}]},{"type":"text","value":" refererar till ett attribut i objektet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"text","value":", som är ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attributnamnet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":"-objektet innehåller namnet på studenten"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"nar-ar-en-import-nodvandig","style":"position:relative;"},"children":[{"type":"text","value":"När är en import nödvändig?"},{"type":"element","tagName":"a","properties":{"href":"#nar-ar-en-import-nodvandig","ariaLabel":"nar ar en import nodvandig permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan förekommer en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-sats ganska många gånger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" slutfordkurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" SlutfordKurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" kurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Kurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Studerande\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En importsats är bara nödvändig när man använder kod som är definierad någonstans utanför den aktuella filen (eller Python-tolksessionen). Detta inkluderar situationer där vi vill använda något som är definierat i Pythons standardbibliotek. Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" innehåller till exempel vissa matematiska operationer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"kvadratroten av "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan antog vi att de tre klasserna definierades i var sin fil och att huvudfunktionen kördes från ytterligare en fil. Det var därför "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satserna var nödvändiga."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om all programkod skrivs i samma fil, vilket de flesta övningarna i den här kursen rekommenderar, behöver du "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"inte"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satser för att använda de klasser du har definierat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall du märker dig själv skriva något i stil med"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Person\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"är det sannolikt att du förstått nånting felaktigt. Ifall du behöver en uppfriskare så introducerades "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" deklarationen för första gången i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-7/1-moduler","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 7"}]},{"type":"text","value":" av kursmaterialet."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Husdjur","tmcname":"osa09-06_husdjur"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns två klasser, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Husdjur"}]},{"type":"text","value":". Varje person har ett djur. Ändra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metoden i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" klassen, så att metoden returnerar en sträng som berättar personens namn plus husdjurets namn och ras enligt exemplet nedan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängen som returneras av metoden "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"måste följa det format som anges nedan exakt"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hulda "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Husdjur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hulda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"blandrasig hund\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nludde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ludde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hulda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ludde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ludde, vars kompis är Hulda, en blandrasig hund"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" alla klassdefinitioner finns i samma textfil. Du ska inte behöva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"era något."}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-lista-med-objekt-som-attribut-till-ett-objekt","style":"position:relative;"},"children":[{"type":"text","value":"En lista med objekt som attribut till ett objekt"},{"type":"element","tagName":"a","properties":{"href":"#en-lista-med-objekt-som-attribut-till-ett-objekt","ariaLabel":"en lista med objekt som attribut till ett objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan använde vi enstaka instanser av andra klasser som attribut: en Person har ett enda Husdjur som attribut, och en SlutfordKurs har en Studerande och en Kurs som attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I objektorienterad programmering är det ofta så att vi vill ha en samling objekt som attribut. Till exempel följer relationen mellan ett idrottslag och dess spelare detta mönster:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" mal\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (mål "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Lag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sammanfattning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" spelare "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lag:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Spelare:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Spelarnas målmängd:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på hur vår klass fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gumboll "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkts boll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lag: Gumtäkts boll\nSpelare: 3\nSpelarnas målmängd: [10, 22, 1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Paketpackning","tmcname":"osa09-07_paketpackning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen får du öva på att slå in presenter. Du kommer att skriva två klasser: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Present"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lada"}]},{"type":"text","value":". En present har ett namn och en vikt, och en låda innehåller presenter."}]},{"type":"element","tagName":"h2","properties":{"id":"presentklassen","style":"position:relative;"},"children":[{"type":"text","value":"Presentklassen"},{"type":"element","tagName":"a","properties":{"href":"#presentklassen","ariaLabel":"presentklassen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Present"}]},{"type":"text","value":" som kan användas för att representera olika typer av presenter. Klassdefinitionen bör innehålla attribut för presentens namn och vikt (i kg). Instanser av klassen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Presentens namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Presentens vikt:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Present:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde vara"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Presentens namn: ABC bok\nPresentens vikt: 2\nPresent: ABC bok (2 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ladklassen","style":"position:relative;"},"children":[{"type":"text","value":"Lådklassen"},{"type":"element","tagName":"a","properties":{"href":"#ladklassen","ariaLabel":"ladklassen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lada"}]},{"type":"text","value":". Du ska kunna lägga presenter i lådan och lådan ska hålla reda på den sammanlagda vikten av presenterna i den. Klassdefinitionen bör innehålla dessa metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_present(self, present: Present)"}]},{"type":"text","value":", som tillsätter present givet som argument till lådan. Metoden returnerar inget värde."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"totalvikt(self)"}]},{"type":"text","value":", som returnerar den totala vikten av presenterna i lådan."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande är ett exempel av koden i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlada "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalvikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncd_skiva "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pink Floyd: Dark side of the moon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cd_skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalvikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"none-en-referens-till-ingenting","style":"position:relative;"},"children":[{"type":"text","value":"None: en referens till ingenting"},{"type":"element","tagName":"a","properties":{"href":"#none-en-referens-till-ingenting","ariaLabel":"none en referens till ingenting permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python-programmering refererar alla initialiserade variabler till ett objekt. Det finns dock oundvikligen situationer där vi måste referera till något som inte existerar, utan att orsaka fel. Nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" representerar just ett sådant \"tomt\" objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss fortsätta från exemplet med lag och spelare ovan och anta att vi vill lägga till en metod för att söka efter spelare i laget med hjälp av spelarens namn. Om ingen sådan spelare hittas kan det vara vettigt att returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" mal\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (mål "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Lag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" spelare "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" spelare\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss testa vår funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gumboll "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkts boll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nspelare1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nspelare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Johan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anton (mål 1)\nNone"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Var dock försiktig med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":". Det kan ibland orsaka mer problem än det löser. Det är ett vanligt programmeringsfel att försöka komma åt en metod eller ett attribut via en referens som utvärderas till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gumboll "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkts boll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nspelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Johan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Johans målmängd "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekverande av ovanstående skulle orsaka ett fel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nAttributeError: 'NoneType' object has no attribute 'mal'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är en god idé att kontrollera om det finns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" innan du försöker komma åt några attribut eller metoder för returvärden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gumboll "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkts boll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nspelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Johan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spelare "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Johans målmängd "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"p"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Johan spelar inte i Gumtäkts boll :(\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Johan spelar inte i Gumtäkts boll :("}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Rummets kortaste","tmcname":"osa09-08_rummets_kortaste"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":". En person har ett namn och en höjd. I denna övning kommer du att implementera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rum"}]},{"type":"text","value":". Du kan lägga till valfritt antal personer i ett rum, och du kan också söka efter och ta bort den kortaste personen i rummet."}]},{"type":"element","tagName":"h2","properties":{"id":"rum","style":"position:relative;"},"children":[{"type":"text","value":"Rum"},{"type":"element","tagName":"a","properties":{"href":"#rum","ariaLabel":"rum permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rum"}]},{"type":"text","value":", som har en lista på personer som attribut och har följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt(person: Person)"}]},{"type":"text","value":" lägger till personen som anges som argument till rummet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar_tomt()"}]},{"type":"text","value":" - Teturnerar värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", beroende på om rummet är tomt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_personer()"}]},{"type":"text","value":" skriver ut innehållet av listan på personerna i rummet"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande är ett exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rummet tomt?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_tomt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"183"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"182"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alex\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"186"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tom\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"185"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rummet tomt?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_tomt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rummet tomt? True\nRummet tomt? False\nI rummet finns 5 personer, totallängd 908 cm\nLena (183 cm)\nKenya (182 cm)\nAlex (186 cm)\nNina (172 cm)\nTom (185 cm)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"kortaste-personen","style":"position:relative;"},"children":[{"type":"text","value":"Kortaste personen"},{"type":"element","tagName":"a","properties":{"href":"#kortaste-personen","ariaLabel":"kortaste personen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera metoden kortast() inom klassdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rum"}]},{"type":"text","value":". Metoden ska returnera den kortaste personen i det rum som den anropas i. Om rummet är tomt ska metoden returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":". Metoden ska "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inte"}]},{"type":"text","value":" ta bort personen från rummet."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rummet tomt?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_tomt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kortast:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"183"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"182"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alex\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"186"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rummet tomt?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_tomt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kortast:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rummet tomt? True\nKortast: None"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rummet tomt? False\nKortast: Nina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I rummet finns 4 personer, totallängd 723 cm\nLena (183 cm)\nKenya (182 cm)\nNina (172 cm)\nAlex (186 cm)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ta-bort-en-person-fran-rummet","style":"position:relative;"},"children":[{"type":"text","value":"Ta bort en person från rummet"},{"type":"element","tagName":"a","properties":{"href":"#ta-bort-en-person-fran-rummet","ariaLabel":"ta bort en person fran rummet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ta_bort_kortaste"}]},{"type":"text","value":"() inom klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rum"}]},{"type":"text","value":". Metoden ska ta bort det kortaste "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":"-objektet från rummet och returnera referensen till objektet. Om rummet är tomt ska metoden returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"183"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"182"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alex\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"186"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nborttagen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ta_bort_kortaste"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Borttagen från rummet: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"borttagen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I rummet finns 4 personer, totallängd 723 cm\nLena (183 cm)\nKenya (182 cm)\nNina (172 cm)\nAlex (186 cm)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Borttagen från rummet: Nina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I rummet finns 3 personer, totallängd 551 cm\nLena (183 cm)\nKenya (182 cm)\nAlex (186 cm)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips"}]},{"type":"text","value":": "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-4/3-listor","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"i modul 4"}]},{"type":"text","value":" hittar du instruktioner för att ta bort föremål från en lista"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips2"}]},{"type":"text","value":": det är alltid möjligt att kalla en annan metod från samma klass inuti en metod. Följande borde fungera helt bra:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kortast"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ta_bort_kortaste"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kortast_person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du hur man använder objekt som attribut i andra objekt
  • \n
  • Kommer du vara bekant med nyckelordet None
  • \n

Vi har redan sett exempel på klasser som har listor som attribut. Eftersom det alltså inte finns något som hindrar oss från att inkludera mutabla objekt som attribut i våra klasser, kan vi lika gärna använda instanser av våra egna klasser som attribut i andra klasser som vi själva har definierat. I följande exempel kommer vi att definiera klasserna Kurs, Studerande och SlutfordKurs. En slutförd kurs använder sig av de två första klasserna. Klassdefinitionerna är mycket korta och enkla för att vi bättre ska kunna koncentrera oss på tekniken att använda instanser av våra egna klasser som attribut.

Vi kommer att anta att varje klass definieras i en separat fil.

Först definierar vi klassen Kurs i en fil med namnet kurs.py:

class Kurs:\n    def __init__(self, namn: str, kod: str, studiepoang: int):\n        self.namn = namn\n        self.kod = kod\n        self.studiepoang = studiepoang

Till näst, klassen Studerande i en fil med namnet studerande.py:

class Studerande:\n    def __init__(self, namn: str, studerandenummer: str, studiepoang: int):\n        self.namn = namn\n        self.studerandenummer = studerandenummer\n        self.studiepoang = studiepoang

Slutligen är klassen SlutfordKurs definierad i en fil med namned SlutfordKurs.py. Eftersom den använder de två andra klasserna, måste de importeras innan de kan användas:

from kurs import Kurs\nfrom studerande import Studerande\n\nclass SlutfordKurs:\n    def __init__(self, studerande: Studerande, kurs: Kurs, vitsord: int):\n        self.studerande = studerande\n        self.kurs = kurs\n        self.vitsord = vitsord

Här är ett exempel av en huvudfunktion som lägger till några slutförda kurser i en lista:

from slutfordkurs import SlutfordKurs\nfrom kurs import Kurs\nfrom studerande import Studerande\n\n# Vi skapar en lista av studeranden\nstuderanden = []\nstuderanden.append(Studerande(\"Olle\", \"1234\", 10))\nstuderanden.append(Studerande(\"Peter\", \"3210\", 23))\nstuderanden.append(Studerande(\"Lena\", \"9999\", 43))\nstuderanden.append(Studerande(\"Tina\", \"3333\", 8))\n\n# Kurssen Introduktion till Programmering\nitp = Kurs(\"Introduktion till Programmering\", \"itp1\", 5)\n\n# Vi ger prestationer för varje student, med vitsordet 3 till alla\nprestationer = []\nfor studerande in studeranden:\n    prestationer.append(SlutfordKurs(studerande, itp, 3))\n\n# Vi skriver ut studerandenas namn för varje avklarad kurs\nfor prestation in prestationer:\n    print(prestation.studerande.namn)

Olle\nPeter\nLena\nTina

Vad exakt händer med alla prickar på raden print(kurs.studerande.namn)?

    \n
  • kurs är en instans av klassen SlutfordKurs
  • \n
  • studerande refererar till ett attribut i objektet SlutfordKurs, som är ett objekt av typen Studerande
  • \n
  • attributnamnet i Studerande-objektet innehåller namnet på studenten
  • \n

När är en import nödvändig?

I exemplen ovan förekommer en import-sats ganska många gånger:

from slutfordkurs import SlutfordKurs\nfrom kurs import Kurs\nfrom studerande import Studerande\n\n# kod

En importsats är bara nödvändig när man använder kod som är definierad någonstans utanför den aktuella filen (eller Python-tolksessionen). Detta inkluderar situationer där vi vill använda något som är definierat i Pythons standardbibliotek. Modulen math innehåller till exempel vissa matematiska operationer:

import math\n\nx = 10\nprint(f\"kvadratroten av {x} är {math.sqrt(x)}\")

I exemplet ovan antog vi att de tre klasserna definierades i var sin fil och att huvudfunktionen kördes från ytterligare en fil. Det var därför import-satserna var nödvändiga.

Om all programkod skrivs i samma fil, vilket de flesta övningarna i den här kursen rekommenderar, behöver du inte import-satser för att använda de klasser du har definierat.

Ifall du märker dig själv skriva något i stil med

from person import Person\n\n# kod

är det sannolikt att du förstått nånting felaktigt. Ifall du behöver en uppfriskare så introducerades import deklarationen för första gången i modul 7 av kursmaterialet.

I uppgiftsbotten finns två klasser, Person och Husdjur. Varje person har ett djur. Ändra __str__-metoden i Person klassen, så att metoden returnerar en sträng som berättar personens namn plus husdjurets namn och ras enligt exemplet nedan.

Strängen som returneras av metoden måste följa det format som anges nedan exakt.

hulda = Husdjur(\"Hulda\", \"blandrasig hund\")\nludde = Person(\"Ludde\", hulda)\n\nprint(ludde)

Ludde, vars kompis är Hulda, en blandrasig hund

OBS: alla klassdefinitioner finns i samma textfil. Du ska inte behöva importera något.

En lista med objekt som attribut till ett objekt

I exemplen ovan använde vi enstaka instanser av andra klasser som attribut: en Person har ett enda Husdjur som attribut, och en SlutfordKurs har en Studerande och en Kurs som attribut.

I objektorienterad programmering är det ofta så att vi vill ha en samling objekt som attribut. Till exempel följer relationen mellan ett idrottslag och dess spelare detta mönster:

class Spelare:\n    def __init__(self, namn: str, mal: int):\n        self.namn = namn\n        self.mal = mal\n\n    def __str__(self):\n        return f\"{self.namn} (mål {self.mal})\"\n\nclass Lag:\n    def __init__(self, namn: str):\n        self.namn = namn\n        self.spelare = []\n\n    def tillsatt_spelare(self, spelare: Spelare):\n        self.spelare.append(spelare)\n\n    def sammanfattning(self):\n        mal = []\n        for spelare in self.spelare:\n            mal.append(spelare.mal)\n        print(\"Lag:\", self.namn)\n        print(\"Spelare:\", len(self.spelare))\n        print(\"Spelarnas målmängd:\", mal)

Ett exempel på hur vår klass fungerar:

gumboll = Lag(\"Gumtäkts boll\")\ngumboll.tillsatt_spelare(Spelare(\"Erik\", 10))\ngumboll.tillsatt_spelare(Spelare(\"Emilia\", 22))\ngumboll.tillsatt_spelare(Spelare(\"Anton\", 1))\ngumboll.sammanfattning()

Lag: Gumtäkts boll\nSpelare: 3\nSpelarnas målmängd: [10, 22, 1]

I den här övningen får du öva på att slå in presenter. Du kommer att skriva två klasser: Present och Lada. En present har ett namn och en vikt, och en låda innehåller presenter.

Presentklassen

Definiera klassen Present som kan användas för att representera olika typer av presenter. Klassdefinitionen bör innehålla attribut för presentens namn och vikt (i kg). Instanser av klassen ska fungera på följande sätt:

bok = Present(\"ABC bok\", 2)\n\nprint(\"Presentens namn:\", bok.namn)\nprint(\"Presentens vikt:\", bok.vikt)\nprint(\"Present:\", bok)

Utskriften borde vara

Presentens namn: ABC bok\nPresentens vikt: 2\nPresent: ABC bok (2 kg)

Lådklassen

Definiera klassen Lada. Du ska kunna lägga presenter i lådan och lådan ska hålla reda på den sammanlagda vikten av presenterna i den. Klassdefinitionen bör innehålla dessa metoder:

    \n
  • tillsatt_present(self, present: Present), som tillsätter present givet som argument till lådan. Metoden returnerar inget värde.
  • \n
  • totalvikt(self), som returnerar den totala vikten av presenterna i lådan.
  • \n

Följande är ett exempel av koden i användning:

bok = Present(\"ABC bok\", 2)\n\nlada = Lada()\nlada.tillsatt_present(bok)\nprint(lada.totalvikt())\n\ncd_skiva = Present(\"Pink Floyd: Dark side of the moon\", 1)\nlada.tillsatt_present(cd_skiva)\nprint(lada.totalvikt())

2\n3

None: en referens till ingenting

I Python-programmering refererar alla initialiserade variabler till ett objekt. Det finns dock oundvikligen situationer där vi måste referera till något som inte existerar, utan att orsaka fel. Nyckelordet None representerar just ett sådant \"tomt\" objekt.

Låt oss fortsätta från exemplet med lag och spelare ovan och anta att vi vill lägga till en metod för att söka efter spelare i laget med hjälp av spelarens namn. Om ingen sådan spelare hittas kan det vara vettigt att returnera None:

class Spelare:\n    def __init__(self, namn: str, mal: int):\n        self.namn = namn\n        self.mal = mal\n\n    def __str__(self):\n        return f\"{self.namn} (mål {self.mal})\"\n\nclass Lag:\n    def __init__(self, namn: str):\n        self.namn = namn\n        self.spelare = []\n\n    def tillsatt_spelare(self, spelare: Spelare):\n        self.spelare.append(spelare)\n\n    def sok(self, namn: str):\n        for spelare in self.spelare:\n            if spelare.namn == namn:\n                return spelare\n        return None

Låt oss testa vår funktion:

gumboll = Lag(\"Gumtäkts boll\")\ngumboll.tillsatt_spelare(Spelare(\"Erik\", 10))\ngumboll.tillsatt_spelare(Spelare(\"Emilia\", 22))\ngumboll.tillsatt_spelare(Spelare(\"Anton\", 1))\n\nspelare1 = gumboll.sok(\"Anton\")\nprint(spelare1)\nspelare2 = gumboll.sok(\"Johan\")\nprint(spelare2)

Anton (mål 1)\nNone

Var dock försiktig med None. Det kan ibland orsaka mer problem än det löser. Det är ett vanligt programmeringsfel att försöka komma åt en metod eller ett attribut via en referens som utvärderas till None:

gumboll = Lag(\"Gumtäkts boll\")\ngumboll.tillsatt_spelare(Spelare(\"Erik\", 10))\n\nspelare = gumboll.sok(\"Johan\")\nprint(f\"Johans målmängd {spelare.mal}\")

Exekverande av ovanstående skulle orsaka ett fel:

Traceback (most recent call last):\nFile \"\", line 1, in \nAttributeError: 'NoneType' object has no attribute 'mal'

Det är en god idé att kontrollera om det finns None innan du försöker komma åt några attribut eller metoder för returvärden:

gumboll = Lag(\"Gumtäkts boll\")\ngumboll.tillsatt_spelare(Spelare(\"Erik\", 10))\n\nspelare = gumboll.sok(\"Johan\")\nif spelare is not None:\n    print(f\"Johans målmängd {p.mal}\")\nelse:\n    print(f\"Johan spelar inte i Gumtäkts boll :(\")

Johan spelar inte i Gumtäkts boll :(

Övningsmallen innehåller klassen Person. En person har ett namn och en höjd. I denna övning kommer du att implementera klassen Rum. Du kan lägga till valfritt antal personer i ett rum, och du kan också söka efter och ta bort den kortaste personen i rummet.

Rum

Skapa klassen Rum, som har en lista på personer som attribut och har följande metoder:

    \n
  • tillsatt(person: Person) lägger till personen som anges som argument till rummet.
  • \n
  • ar_tomt() - Teturnerar värdet True eller False, beroende på om rummet är tomt.
  • \n
  • skriv_ut_personer() skriver ut innehållet av listan på personerna i rummet
  • \n

Följande är ett exempel:

rum = Rum()\nprint(\"Rummet tomt?\", rum.ar_tomt())\nrum.tillsatt(Person(\"Lena\", 183))\nrum.tillsatt(Person(\"Kenya\", 182))\nrum.tillsatt(Person(\"Alex\", 186))\nrum.tillsatt(Person(\"Nina\", 172))\nrum.tillsatt(Person(\"Tom\", 185))\nprint(\"Rummet tomt?\", rum.ar_tomt())\nrum.skriv_ut_personer()

Rummet tomt? True\nRummet tomt? False\nI rummet finns 5 personer, totallängd 908 cm\nLena (183 cm)\nKenya (182 cm)\nAlex (186 cm)\nNina (172 cm)\nTom (185 cm)

Kortaste personen

Definiera metoden kortast() inom klassdefinitionen Rum. Metoden ska returnera den kortaste personen i det rum som den anropas i. Om rummet är tomt ska metoden returnera None. Metoden ska inte ta bort personen från rummet.

rum = Rum()\n\nprint(\"Rummet tomt?\", rum.ar_tomt())\nprint(\"Kortast:\", rum.kortast())\n\nrum.tillsatt(Person(\"Lena\", 183))\nrum.tillsatt(Person(\"Kenya\", 182))\nrum.tillsatt(Person(\"Nina\", 172))\nrum.tillsatt(Person(\"Alex\", 186))\n\nprint()\n\nprint(\"Rummet tomt?\", rum.ar_tomt())\nprint(\"Kortast:\", rum.kortast())\n\nprint()\n\nrum.skriv_ut_personer()

Rummet tomt? True\nKortast: None

Rummet tomt? False\nKortast: Nina

I rummet finns 4 personer, totallängd 723 cm\nLena (183 cm)\nKenya (182 cm)\nNina (172 cm)\nAlex (186 cm)

Ta bort en person från rummet

Definiera metoden ta_bort_kortaste() inom klassdefinitionen för Rum. Metoden ska ta bort det kortaste Person-objektet från rummet och returnera referensen till objektet. Om rummet är tomt ska metoden returnera None.

rum = Rum()\n\nrum.tillsatt(Person(\"Lena\", 183))\nrum.tillsatt(Person(\"Kenya\", 182))\nrum.tillsatt(Person(\"Nina\", 172))\nrum.tillsatt(Person(\"Alex\", 186))\nrum.skriv_ut_personer()\n\nprint()\n\nborttagen = rum.ta_bort_kortaste()\nprint(f\"Borttagen från rummet: {borttagen.namn}\")\n\nprint()\n\nrum.skriv_ut_personer()

I rummet finns 4 personer, totallängd 723 cm\nLena (183 cm)\nKenya (182 cm)\nNina (172 cm)\nAlex (186 cm)

Borttagen från rummet: Nina

I rummet finns 3 personer, totallängd 551 cm\nLena (183 cm)\nKenya (182 cm)\nAlex (186 cm)

Tips: i modul 4 hittar du instruktioner för att ta bort föremål från en lista

Tips2: det är alltid möjligt att kalla en annan metod från samma klass inuti en metod. Följande borde fungera helt bra:

class Rum:\n    # ...\n    def kortast(self):\n        # kod\n\n    def ta_bort_kortaste(self):\n        kortast_person = self.kortast()\n        # ...
","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/2-objekt-som-attribut.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur man använder objekt som attribut i andra objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du vara bekant med nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi har redan sett exempel på klasser som har listor som attribut. Eftersom det alltså inte finns något som hindrar oss från att inkludera mutabla objekt som attribut i våra klasser, kan vi lika gärna använda instanser av våra egna klasser som attribut i andra klasser som vi själva har definierat. I följande exempel kommer vi att definiera klasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kurs"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"text","value":". En slutförd kurs använder sig av de två första klasserna. Klassdefinitionerna är mycket korta och enkla för att vi bättre ska kunna koncentrera oss på tekniken att använda instanser av våra egna klasser som attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kommer att anta att varje klass definieras i en separat fil."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först definierar vi klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kurs"}]},{"type":"text","value":" i en fil med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kurs.py"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Kurs"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kod"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kod "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kod\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Till näst, klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" i en fil med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande.py"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Slutligen är klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"text","value":" definierad i en fil med namned "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs.py"}]},{"type":"text","value":". Eftersom den använder de två andra klasserna, måste de importeras innan de kan användas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" kurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Kurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Studerande\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" vitsord"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerande "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerande\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kurs "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kurs\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vitsord "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" vitsord"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här är ett exempel av en huvudfunktion som lägger till några slutförda kurser i en lista:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" slutfordkurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" SlutfordKurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" kurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Kurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Studerande\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skapar en lista av studeranden"}]},{"type":"text","value":"\nstuderanden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstuderanden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olle\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderanden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderanden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"9999\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"43"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderanden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"3333\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kurssen Introduktion till Programmering"}]},{"type":"text","value":"\nitp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduktion till Programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"itp1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi ger prestationer för varje student, med vitsordet 3 till alla"}]},{"type":"text","value":"\nprestationer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" studeranden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"SlutfordKurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" itp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi skriver ut studerandenas namn för varje avklarad kurs"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" prestation "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" prestationer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olle\nPeter\nLena\nTina"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vad exakt händer med alla prickar på raden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print(kurs.studerande.namn)"}]},{"type":"text","value":"?"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kurs"}]},{"type":"text","value":" är en instans av klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande"}]},{"type":"text","value":" refererar till ett attribut i objektet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SlutfordKurs"}]},{"type":"text","value":", som är ett objekt av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attributnamnet i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":"-objektet innehåller namnet på studenten"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"nar-ar-en-import-nodvandig","style":"position:relative;"},"children":[{"type":"text","value":"När är en import nödvändig?"},{"type":"element","tagName":"a","properties":{"href":"#nar-ar-en-import-nodvandig","ariaLabel":"nar ar en import nodvandig permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan förekommer en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-sats ganska många gånger:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" slutfordkurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" SlutfordKurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" kurs "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Kurs\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" studerande "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Studerande\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En importsats är bara nödvändig när man använder kod som är definierad någonstans utanför den aktuella filen (eller Python-tolksessionen). Detta inkluderar situationer där vi vill använda något som är definierat i Pythons standardbibliotek. Modulen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" innehåller till exempel vissa matematiska operationer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\nx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"kvadratroten av "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" är "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplet ovan antog vi att de tre klasserna definierades i var sin fil och att huvudfunktionen kördes från ytterligare en fil. Det var därför "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satserna var nödvändiga."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om all programkod skrivs i samma fil, vilket de flesta övningarna i den här kursen rekommenderar, behöver du "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"inte"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"-satser för att använda de klasser du har definierat."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall du märker dig själv skriva något i stil med"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Person\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"är det sannolikt att du förstått nånting felaktigt. Ifall du behöver en uppfriskare så introducerades "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" deklarationen för första gången i "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-7/1-moduler","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 7"}]},{"type":"text","value":" av kursmaterialet."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Husdjur","tmcname":"osa09-06_husdjur"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns två klasser, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Husdjur"}]},{"type":"text","value":". Varje person har ett djur. Ändra "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metoden i "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" klassen, så att metoden returnerar en sträng som berättar personens namn plus husdjurets namn och ras enligt exemplet nedan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strängen som returneras av metoden "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"måste följa det format som anges nedan exakt"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"hulda "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Husdjur"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hulda\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"blandrasig hund\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nludde "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ludde\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hulda"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ludde"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ludde, vars kompis är Hulda, en blandrasig hund"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" alla klassdefinitioner finns i samma textfil. Du ska inte behöva "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":"era något."}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-lista-med-objekt-som-attribut-till-ett-objekt","style":"position:relative;"},"children":[{"type":"text","value":"En lista med objekt som attribut till ett objekt"},{"type":"element","tagName":"a","properties":{"href":"#en-lista-med-objekt-som-attribut-till-ett-objekt","ariaLabel":"en lista med objekt som attribut till ett objekt permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I exemplen ovan använde vi enstaka instanser av andra klasser som attribut: en Person har ett enda Husdjur som attribut, och en SlutfordKurs har en Studerande och en Kurs som attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I objektorienterad programmering är det ofta så att vi vill ha en samling objekt som attribut. Till exempel följer relationen mellan ett idrottslag och dess spelare detta mönster:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" mal\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (mål "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Lag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sammanfattning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" spelare "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lag:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Spelare:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Spelarnas målmängd:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett exempel på hur vår klass fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gumboll "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkts boll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lag: Gumtäkts boll\nSpelare: 3\nSpelarnas målmängd: [10, 22, 1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Paketpackning","tmcname":"osa09-07_paketpackning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här övningen får du öva på att slå in presenter. Du kommer att skriva två klasser: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Present"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lada"}]},{"type":"text","value":". En present har ett namn och en vikt, och en låda innehåller presenter."}]},{"type":"element","tagName":"h2","properties":{"id":"presentklassen","style":"position:relative;"},"children":[{"type":"text","value":"Presentklassen"},{"type":"element","tagName":"a","properties":{"href":"#presentklassen","ariaLabel":"presentklassen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Present"}]},{"type":"text","value":" som kan användas för att representera olika typer av presenter. Klassdefinitionen bör innehålla attribut för presentens namn och vikt (i kg). Instanser av klassen ska fungera på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Presentens namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Presentens vikt:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Present:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften borde vara"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Presentens namn: ABC bok\nPresentens vikt: 2\nPresent: ABC bok (2 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ladklassen","style":"position:relative;"},"children":[{"type":"text","value":"Lådklassen"},{"type":"element","tagName":"a","properties":{"href":"#ladklassen","ariaLabel":"ladklassen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lada"}]},{"type":"text","value":". Du ska kunna lägga presenter i lådan och lådan ska hålla reda på den sammanlagda vikten av presenterna i den. Klassdefinitionen bör innehålla dessa metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_present(self, present: Present)"}]},{"type":"text","value":", som tillsätter present givet som argument till lådan. Metoden returnerar inget värde."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"totalvikt(self)"}]},{"type":"text","value":", som returnerar den totala vikten av presenterna i lådan."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande är ett exempel av koden i användning:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlada "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalvikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncd_skiva "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pink Floyd: Dark side of the moon\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cd_skiva"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lada"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalvikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"none-en-referens-till-ingenting","style":"position:relative;"},"children":[{"type":"text","value":"None: en referens till ingenting"},{"type":"element","tagName":"a","properties":{"href":"#none-en-referens-till-ingenting","ariaLabel":"none en referens till ingenting permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python-programmering refererar alla initialiserade variabler till ett objekt. Det finns dock oundvikligen situationer där vi måste referera till något som inte existerar, utan att orsaka fel. Nyckelordet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" representerar just ett sådant \"tomt\" objekt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss fortsätta från exemplet med lag och spelare ovan och anta att vi vill lägga till en metod för att söka efter spelare i laget med hjälp av spelarens namn. Om ingen sådan spelare hittas kan det vara vettigt att returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" mal\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (mål "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Lag"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" spelare "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" spelare\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss testa vår funktion:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gumboll "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkts boll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emilia\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nspelare1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anton\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nspelare2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Johan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anton (mål 1)\nNone"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Var dock försiktig med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":". Det kan ibland orsaka mer problem än det löser. Det är ett vanligt programmeringsfel att försöka komma åt en metod eller ett attribut via en referens som utvärderas till "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gumboll "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkts boll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nspelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Johan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Johans målmängd "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exekverande av ovanstående skulle orsaka ett fel:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nAttributeError: 'NoneType' object has no attribute 'mal'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det är en god idé att kontrollera om det finns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" innan du försöker komma åt några attribut eller metoder för returvärden:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"gumboll "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkts boll\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Erik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nspelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gumboll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Johan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spelare "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Johans målmängd "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"p"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Johan spelar inte i Gumtäkts boll :(\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Johan spelar inte i Gumtäkts boll :("}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Rummets kortaste","tmcname":"osa09-08_rummets_kortaste"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Övningsmallen innehåller klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":". En person har ett namn och en höjd. I denna övning kommer du att implementera klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rum"}]},{"type":"text","value":". Du kan lägga till valfritt antal personer i ett rum, och du kan också söka efter och ta bort den kortaste personen i rummet."}]},{"type":"element","tagName":"h2","properties":{"id":"rum","style":"position:relative;"},"children":[{"type":"text","value":"Rum"},{"type":"element","tagName":"a","properties":{"href":"#rum","ariaLabel":"rum permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rum"}]},{"type":"text","value":", som har en lista på personer som attribut och har följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt(person: Person)"}]},{"type":"text","value":" lägger till personen som anges som argument till rummet."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ar_tomt()"}]},{"type":"text","value":" - Teturnerar värdet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", beroende på om rummet är tomt."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_personer()"}]},{"type":"text","value":" skriver ut innehållet av listan på personerna i rummet"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande är ett exempel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rummet tomt?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_tomt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"183"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"182"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alex\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"186"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tom\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"185"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rummet tomt?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_tomt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rummet tomt? True\nRummet tomt? False\nI rummet finns 5 personer, totallängd 908 cm\nLena (183 cm)\nKenya (182 cm)\nAlex (186 cm)\nNina (172 cm)\nTom (185 cm)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"kortaste-personen","style":"position:relative;"},"children":[{"type":"text","value":"Kortaste personen"},{"type":"element","tagName":"a","properties":{"href":"#kortaste-personen","ariaLabel":"kortaste personen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera metoden kortast() inom klassdefinitionen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rum"}]},{"type":"text","value":". Metoden ska returnera den kortaste personen i det rum som den anropas i. Om rummet är tomt ska metoden returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":". Metoden ska "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inte"}]},{"type":"text","value":" ta bort personen från rummet."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rummet tomt?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_tomt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kortast:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"183"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"182"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alex\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"186"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rummet tomt?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ar_tomt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kortast:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rummet tomt? True\nKortast: None"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rummet tomt? False\nKortast: Nina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I rummet finns 4 personer, totallängd 723 cm\nLena (183 cm)\nKenya (182 cm)\nNina (172 cm)\nAlex (186 cm)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"ta-bort-en-person-fran-rummet","style":"position:relative;"},"children":[{"type":"text","value":"Ta bort en person från rummet"},{"type":"element","tagName":"a","properties":{"href":"#ta-bort-en-person-fran-rummet","ariaLabel":"ta bort en person fran rummet permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Definiera metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ta_bort_kortaste"}]},{"type":"text","value":"() inom klassdefinitionen för "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rum"}]},{"type":"text","value":". Metoden ska ta bort det kortaste "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":"-objektet från rummet och returnera referensen till objektet. Om rummet är tomt ska metoden returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"rum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"183"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"182"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alex\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"186"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nborttagen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"ta_bort_kortaste"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Borttagen från rummet: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"borttagen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_personer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I rummet finns 4 personer, totallängd 723 cm\nLena (183 cm)\nKenya (182 cm)\nNina (172 cm)\nAlex (186 cm)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Borttagen från rummet: Nina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I rummet finns 3 personer, totallängd 551 cm\nLena (183 cm)\nKenya (182 cm)\nAlex (186 cm)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips"}]},{"type":"text","value":": "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-4/3-listor","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"i modul 4"}]},{"type":"text","value":" hittar du instruktioner för att ta bort föremål från en lista"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Tips2"}]},{"type":"text","value":": det är alltid möjligt att kalla en annan metod från samma klass inuti en metod. Följande borde fungera helt bra:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"kortast"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# kod"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"ta_bort_kortaste"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n kortast_person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kortast"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du hur man använder objekt som attribut i andra objekt
  • \n
  • Kommer du vara bekant med nyckelordet None
  • \n

Vi har redan sett exempel på klasser som har listor som attribut. Eftersom det alltså inte finns något som hindrar oss från att inkludera mutabla objekt som attribut i våra klasser, kan vi lika gärna använda instanser av våra egna klasser som attribut i andra klasser som vi själva har definierat. I följande exempel kommer vi att definiera klasserna Kurs, Studerande och SlutfordKurs. En slutförd kurs använder sig av de två första klasserna. Klassdefinitionerna är mycket korta och enkla för att vi bättre ska kunna koncentrera oss på tekniken att använda instanser av våra egna klasser som attribut.

Vi kommer att anta att varje klass definieras i en separat fil.

Först definierar vi klassen Kurs i en fil med namnet kurs.py:

class Kurs:\n    def __init__(self, namn: str, kod: str, studiepoang: int):\n        self.namn = namn\n        self.kod = kod\n        self.studiepoang = studiepoang

Till näst, klassen Studerande i en fil med namnet studerande.py:

class Studerande:\n    def __init__(self, namn: str, studerandenummer: str, studiepoang: int):\n        self.namn = namn\n        self.studerandenummer = studerandenummer\n        self.studiepoang = studiepoang

Slutligen är klassen SlutfordKurs definierad i en fil med namned SlutfordKurs.py. Eftersom den använder de två andra klasserna, måste de importeras innan de kan användas:

from kurs import Kurs\nfrom studerande import Studerande\n\nclass SlutfordKurs:\n    def __init__(self, studerande: Studerande, kurs: Kurs, vitsord: int):\n        self.studerande = studerande\n        self.kurs = kurs\n        self.vitsord = vitsord

Här är ett exempel av en huvudfunktion som lägger till några slutförda kurser i en lista:

from slutfordkurs import SlutfordKurs\nfrom kurs import Kurs\nfrom studerande import Studerande\n\n# Vi skapar en lista av studeranden\nstuderanden = []\nstuderanden.append(Studerande(\"Olle\", \"1234\", 10))\nstuderanden.append(Studerande(\"Peter\", \"3210\", 23))\nstuderanden.append(Studerande(\"Lena\", \"9999\", 43))\nstuderanden.append(Studerande(\"Tina\", \"3333\", 8))\n\n# Kurssen Introduktion till Programmering\nitp = Kurs(\"Introduktion till Programmering\", \"itp1\", 5)\n\n# Vi ger prestationer för varje student, med vitsordet 3 till alla\nprestationer = []\nfor studerande in studeranden:\n    prestationer.append(SlutfordKurs(studerande, itp, 3))\n\n# Vi skriver ut studerandenas namn för varje avklarad kurs\nfor prestation in prestationer:\n    print(prestation.studerande.namn)

Olle\nPeter\nLena\nTina

Vad exakt händer med alla prickar på raden print(kurs.studerande.namn)?

    \n
  • kurs är en instans av klassen SlutfordKurs
  • \n
  • studerande refererar till ett attribut i objektet SlutfordKurs, som är ett objekt av typen Studerande
  • \n
  • attributnamnet i Studerande-objektet innehåller namnet på studenten
  • \n

När är en import nödvändig?

I exemplen ovan förekommer en import-sats ganska många gånger:

from slutfordkurs import SlutfordKurs\nfrom kurs import Kurs\nfrom studerande import Studerande\n\n# kod

En importsats är bara nödvändig när man använder kod som är definierad någonstans utanför den aktuella filen (eller Python-tolksessionen). Detta inkluderar situationer där vi vill använda något som är definierat i Pythons standardbibliotek. Modulen math innehåller till exempel vissa matematiska operationer:

import math\n\nx = 10\nprint(f\"kvadratroten av {x} är {math.sqrt(x)}\")

I exemplet ovan antog vi att de tre klasserna definierades i var sin fil och att huvudfunktionen kördes från ytterligare en fil. Det var därför import-satserna var nödvändiga.

Om all programkod skrivs i samma fil, vilket de flesta övningarna i den här kursen rekommenderar, behöver du inte import-satser för att använda de klasser du har definierat.

Ifall du märker dig själv skriva något i stil med

from person import Person\n\n# kod

är det sannolikt att du förstått nånting felaktigt. Ifall du behöver en uppfriskare så introducerades import deklarationen för första gången i modul 7 av kursmaterialet.

I uppgiftsbotten finns två klasser, Person och Husdjur. Varje person har ett djur. Ändra __str__-metoden i Person klassen, så att metoden returnerar en sträng som berättar personens namn plus husdjurets namn och ras enligt exemplet nedan.

Strängen som returneras av metoden måste följa det format som anges nedan exakt.

hulda = Husdjur(\"Hulda\", \"blandrasig hund\")\nludde = Person(\"Ludde\", hulda)\n\nprint(ludde)

Ludde, vars kompis är Hulda, en blandrasig hund

OBS: alla klassdefinitioner finns i samma textfil. Du ska inte behöva importera något.

En lista med objekt som attribut till ett objekt

I exemplen ovan använde vi enstaka instanser av andra klasser som attribut: en Person har ett enda Husdjur som attribut, och en SlutfordKurs har en Studerande och en Kurs som attribut.

I objektorienterad programmering är det ofta så att vi vill ha en samling objekt som attribut. Till exempel följer relationen mellan ett idrottslag och dess spelare detta mönster:

class Spelare:\n    def __init__(self, namn: str, mal: int):\n        self.namn = namn\n        self.mal = mal\n\n    def __str__(self):\n        return f\"{self.namn} (mål {self.mal})\"\n\nclass Lag:\n    def __init__(self, namn: str):\n        self.namn = namn\n        self.spelare = []\n\n    def tillsatt_spelare(self, spelare: Spelare):\n        self.spelare.append(spelare)\n\n    def sammanfattning(self):\n        mal = []\n        for spelare in self.spelare:\n            mal.append(spelare.mal)\n        print(\"Lag:\", self.namn)\n        print(\"Spelare:\", len(self.spelare))\n        print(\"Spelarnas målmängd:\", mal)

Ett exempel på hur vår klass fungerar:

gumboll = Lag(\"Gumtäkts boll\")\ngumboll.tillsatt_spelare(Spelare(\"Erik\", 10))\ngumboll.tillsatt_spelare(Spelare(\"Emilia\", 22))\ngumboll.tillsatt_spelare(Spelare(\"Anton\", 1))\ngumboll.sammanfattning()

Lag: Gumtäkts boll\nSpelare: 3\nSpelarnas målmängd: [10, 22, 1]

I den här övningen får du öva på att slå in presenter. Du kommer att skriva två klasser: Present och Lada. En present har ett namn och en vikt, och en låda innehåller presenter.

Presentklassen

Definiera klassen Present som kan användas för att representera olika typer av presenter. Klassdefinitionen bör innehålla attribut för presentens namn och vikt (i kg). Instanser av klassen ska fungera på följande sätt:

bok = Present(\"ABC bok\", 2)\n\nprint(\"Presentens namn:\", bok.namn)\nprint(\"Presentens vikt:\", bok.vikt)\nprint(\"Present:\", bok)

Utskriften borde vara

Presentens namn: ABC bok\nPresentens vikt: 2\nPresent: ABC bok (2 kg)

Lådklassen

Definiera klassen Lada. Du ska kunna lägga presenter i lådan och lådan ska hålla reda på den sammanlagda vikten av presenterna i den. Klassdefinitionen bör innehålla dessa metoder:

    \n
  • tillsatt_present(self, present: Present), som tillsätter present givet som argument till lådan. Metoden returnerar inget värde.
  • \n
  • totalvikt(self), som returnerar den totala vikten av presenterna i lådan.
  • \n

Följande är ett exempel av koden i användning:

bok = Present(\"ABC bok\", 2)\n\nlada = Lada()\nlada.tillsatt_present(bok)\nprint(lada.totalvikt())\n\ncd_skiva = Present(\"Pink Floyd: Dark side of the moon\", 1)\nlada.tillsatt_present(cd_skiva)\nprint(lada.totalvikt())

2\n3

None: en referens till ingenting

I Python-programmering refererar alla initialiserade variabler till ett objekt. Det finns dock oundvikligen situationer där vi måste referera till något som inte existerar, utan att orsaka fel. Nyckelordet None representerar just ett sådant \"tomt\" objekt.

Låt oss fortsätta från exemplet med lag och spelare ovan och anta att vi vill lägga till en metod för att söka efter spelare i laget med hjälp av spelarens namn. Om ingen sådan spelare hittas kan det vara vettigt att returnera None:

class Spelare:\n    def __init__(self, namn: str, mal: int):\n        self.namn = namn\n        self.mal = mal\n\n    def __str__(self):\n        return f\"{self.namn} (mål {self.mal})\"\n\nclass Lag:\n    def __init__(self, namn: str):\n        self.namn = namn\n        self.spelare = []\n\n    def tillsatt_spelare(self, spelare: Spelare):\n        self.spelare.append(spelare)\n\n    def sok(self, namn: str):\n        for spelare in self.spelare:\n            if spelare.namn == namn:\n                return spelare\n        return None

Låt oss testa vår funktion:

gumboll = Lag(\"Gumtäkts boll\")\ngumboll.tillsatt_spelare(Spelare(\"Erik\", 10))\ngumboll.tillsatt_spelare(Spelare(\"Emilia\", 22))\ngumboll.tillsatt_spelare(Spelare(\"Anton\", 1))\n\nspelare1 = gumboll.sok(\"Anton\")\nprint(spelare1)\nspelare2 = gumboll.sok(\"Johan\")\nprint(spelare2)

Anton (mål 1)\nNone

Var dock försiktig med None. Det kan ibland orsaka mer problem än det löser. Det är ett vanligt programmeringsfel att försöka komma åt en metod eller ett attribut via en referens som utvärderas till None:

gumboll = Lag(\"Gumtäkts boll\")\ngumboll.tillsatt_spelare(Spelare(\"Erik\", 10))\n\nspelare = gumboll.sok(\"Johan\")\nprint(f\"Johans målmängd {spelare.mal}\")

Exekverande av ovanstående skulle orsaka ett fel:

Traceback (most recent call last):\nFile \"\", line 1, in \nAttributeError: 'NoneType' object has no attribute 'mal'

Det är en god idé att kontrollera om det finns None innan du försöker komma åt några attribut eller metoder för returvärden:

gumboll = Lag(\"Gumtäkts boll\")\ngumboll.tillsatt_spelare(Spelare(\"Erik\", 10))\n\nspelare = gumboll.sok(\"Johan\")\nif spelare is not None:\n    print(f\"Johans målmängd {p.mal}\")\nelse:\n    print(f\"Johan spelar inte i Gumtäkts boll :(\")

Johan spelar inte i Gumtäkts boll :(

Övningsmallen innehåller klassen Person. En person har ett namn och en höjd. I denna övning kommer du att implementera klassen Rum. Du kan lägga till valfritt antal personer i ett rum, och du kan också söka efter och ta bort den kortaste personen i rummet.

Rum

Skapa klassen Rum, som har en lista på personer som attribut och har följande metoder:

    \n
  • tillsatt(person: Person) lägger till personen som anges som argument till rummet.
  • \n
  • ar_tomt() - Teturnerar värdet True eller False, beroende på om rummet är tomt.
  • \n
  • skriv_ut_personer() skriver ut innehållet av listan på personerna i rummet
  • \n

Följande är ett exempel:

rum = Rum()\nprint(\"Rummet tomt?\", rum.ar_tomt())\nrum.tillsatt(Person(\"Lena\", 183))\nrum.tillsatt(Person(\"Kenya\", 182))\nrum.tillsatt(Person(\"Alex\", 186))\nrum.tillsatt(Person(\"Nina\", 172))\nrum.tillsatt(Person(\"Tom\", 185))\nprint(\"Rummet tomt?\", rum.ar_tomt())\nrum.skriv_ut_personer()

Rummet tomt? True\nRummet tomt? False\nI rummet finns 5 personer, totallängd 908 cm\nLena (183 cm)\nKenya (182 cm)\nAlex (186 cm)\nNina (172 cm)\nTom (185 cm)

Kortaste personen

Definiera metoden kortast() inom klassdefinitionen Rum. Metoden ska returnera den kortaste personen i det rum som den anropas i. Om rummet är tomt ska metoden returnera None. Metoden ska inte ta bort personen från rummet.

rum = Rum()\n\nprint(\"Rummet tomt?\", rum.ar_tomt())\nprint(\"Kortast:\", rum.kortast())\n\nrum.tillsatt(Person(\"Lena\", 183))\nrum.tillsatt(Person(\"Kenya\", 182))\nrum.tillsatt(Person(\"Nina\", 172))\nrum.tillsatt(Person(\"Alex\", 186))\n\nprint()\n\nprint(\"Rummet tomt?\", rum.ar_tomt())\nprint(\"Kortast:\", rum.kortast())\n\nprint()\n\nrum.skriv_ut_personer()

Rummet tomt? True\nKortast: None

Rummet tomt? False\nKortast: Nina

I rummet finns 4 personer, totallängd 723 cm\nLena (183 cm)\nKenya (182 cm)\nNina (172 cm)\nAlex (186 cm)

Ta bort en person från rummet

Definiera metoden ta_bort_kortaste() inom klassdefinitionen för Rum. Metoden ska ta bort det kortaste Person-objektet från rummet och returnera referensen till objektet. Om rummet är tomt ska metoden returnera None.

rum = Rum()\n\nrum.tillsatt(Person(\"Lena\", 183))\nrum.tillsatt(Person(\"Kenya\", 182))\nrum.tillsatt(Person(\"Nina\", 172))\nrum.tillsatt(Person(\"Alex\", 186))\nrum.skriv_ut_personer()\n\nprint()\n\nborttagen = rum.ta_bort_kortaste()\nprint(f\"Borttagen från rummet: {borttagen.namn}\")\n\nprint()\n\nrum.skriv_ut_personer()

I rummet finns 4 personer, totallängd 723 cm\nLena (183 cm)\nKenya (182 cm)\nNina (172 cm)\nAlex (186 cm)

Borttagen från rummet: Nina

I rummet finns 3 personer, totallängd 551 cm\nLena (183 cm)\nKenya (182 cm)\nAlex (186 cm)

Tips: i modul 4 hittar du instruktioner för att ta bort föremål från en lista

Tips2: det är alltid möjligt att kalla en annan metod från samma klass inuti en metod. Följande borde fungera helt bra:

class Rum:\n    # ...\n    def kortast(self):\n        # kod\n\n    def ta_bort_kortaste(self):\n        kortast_person = self.kortast()\n        # ...
","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/2-objekt-som-attribut.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-9/3-inkapsling/page-data.json b/page-data/osa-9/3-inkapsling/page-data.json index 3456164ba..cb36cd0fd 100644 --- a/page-data/osa-9/3-inkapsling/page-data.json +++ b/page-data/osa-9/3-inkapsling/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-9/3-inkapsling", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad inkapsling innebär"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa privata attribut"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du skapar gettar och sättare för dina attribut"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I objektorienterad programmering avser termen klient ett program som använder en klass eller instanser av en klass. En klass erbjuder klienten tjänster genom vilka klienten kan komma åt de objekt som skapats baserat på klassen. Målen här är att"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"användningen av en klass och/eller objekt är så enkel som möjligt ur klientens synvinkel"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"integriteten för varje objekt bevaras hela tiden"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett objekts integritet innebär att objektets tillstånd alltid förblir acceptabelt. I praktiken innebär detta att värdena på objektets attribut alltid är acceptabla. Ett objekt som representerar ett datum ska till exempel aldrig ha 13 som värde för månaden, ett objekt som representerar en student ska aldrig ha ett negativt tal som värde för uppnådda studiepoäng och så vidare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en klass som heter Studerande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillagg_poang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" studiepoang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" objektet erbjuder sina klienter metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillagg_poang"}]},{"type":"text","value":", som gör det möjligt för klienten att lägga till ett angivet antal studiepoäng till studentens totala antal. Metoden säkerställer att värdet som skickas som argument är över noll. Följande kod lägger till studiepoäng vid tre tillfällen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"oskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Oskar Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studiepoäng:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studiepoäng: 20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Trots metoddefinitionen är det fortfarande möjligt att komma åt attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studiepoang"}]},{"type":"text","value":" direkt. Detta kunde resultera i ett felaktigt tillstånd där objektets integritet går förlorad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"oskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Oskar Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studiepoäng:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studiepoäng: -100"}]}]},{"type":"element","tagName":"h2","properties":{"id":"inkapsling","style":"position:relative;"},"children":[{"type":"text","value":"Inkapsling"},{"type":"element","tagName":"a","properties":{"href":"#inkapsling","ariaLabel":"inkapsling permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett vanligt inslag i objektorienterade programmeringsspråk är att klasserna kan dölja sina attribut för eventuella kunder. Dolda attribut kallas vanligtvis privata. I Python uppnås denna sekretess genom att lägga till två understreck "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":" i början av attributnamnet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Attributet nummer är gömt, attributet namn är åtkombart"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett privat attribut är inte direkt synligt för klienten. Försök att referera till det orsakar ett fel. I exemplet ovan är attributet namn lätt att komma åt och ändra:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pank\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Robert Rik\nPeter Pank"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall man provar få en utskrift av kortnumret så orsakar det däremot ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'Bankkort' object has no attribute '__nummer'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att dölja attribut från klienter kallas inkapsling. Som namnet antyder är attributet \"slutet inne i en kapsel\". Klienten erbjuds sedan ett lämpligt gränssnitt (engelska: interface) för att komma åt och bearbeta den data som finns lagrad i objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss lägga till ett annat inkapslat attribut: saldot på kreditkortet. Den här gången lägger vi också till offentligt synliga metoder som gör det möjligt för klienten att komma åt och ändra saldot:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"anvand_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"anvand_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta lyckas inte, eftersom saldot inte är tillräckligt"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"anvand_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5000\n5100\n4600\n4600"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Saldot kan inte ändras direkt eftersom attributet är privat, men vi har inkluderat metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ta_ut_pengar"}]},{"type":"text","value":" för att ändra värdet. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"returnera_saldo"}]},{"type":"text","value":" returnerar det värde som lagrats i saldo. Metoderna innehåller några rudimentära kontroller för att bibehålla objektets integritet: till exempel kan kortet inte överdras."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bil","tmcname":"osa09-09_bil"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bil"}]},{"type":"text","value":" och som har två privata, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inkapslade"}]},{"type":"text","value":" variabler: mängden bensin i tanken (0 till 60 liter) och vägmätarställningen (i kilometer). Bilen förbrukar en liter bensin per kilometer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska ha följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tanka()"}]},{"type":"text","value":", som fyller bensintanken"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kor(km:int)"}]},{"type":"text","value":", som kör bilen enligt den angivna distansen eller för så länge bensinen i tanken räcker"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":", som visar en strängrepresentation av bilen enligt exemplet nedan"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur klassen fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tanka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tanka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tanka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bil: har kört 0 km, bensin 0 liter\nBil: har kört 0 km, bensin 60 liter\nBil: har kört 20 km, bensin 40 liter\nBil: har kört 60 km, bensin 0 liter\nBil: har kört 60 km, bensin 0 liter\nBil: har kört 60 km, bensin 60 liter"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" Du ombeds att kapsla in mängden bensin som finns kvar och vägmätarställningen. Det ska inte vara möjligt att komma åt dem direkt utanför klassens egna metoder."}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-kort-notis-om-privata-attribut-python-och-objektorienterad-programmering","style":"position:relative;"},"children":[{"type":"text","value":"En kort notis om privata attribut, Python och objektorienterad programmering"},{"type":"element","tagName":"a","properties":{"href":"#en-kort-notis-om-privata-attribut-python-och-objektorienterad-programmering","ariaLabel":"en kort notis om privata attribut python och objektorienterad programmering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns sätt att kringgå understryknings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":"-notationen för att dölja attribut, som du kan stöta på om du söker efter material online. Inget Python-attribut är verkligen privat, och det är avsiktligt från skaparna av Pythons. Å andra sidan förväntas en Python-programmerare i allmänhet respektera de riktlinjer för synlighet som anges i klasser, och det krävs en särskild ansträngning för att komma runt dessa. I andra objektorienterade programmeringsspråk, till exempel Java, är privata variabler ofta verkligen dolda, och det är bäst om du tänker på privata Python-variabler som sådana också."}]},{"type":"element","tagName":"h2","properties":{"id":"getter-och-sattare","style":"position:relative;"},"children":[{"type":"text","value":"Getter och sättare"},{"type":"element","tagName":"a","properties":{"href":"#getter-och-sattare","ariaLabel":"getter och sattare permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I objektorienterad programmering kallas metoder som är avsedda för att komma åt och ändra attribut vanligtvis för getter och sättare (eng: setters). Inte alla Python-programmerare använder termerna \"getter\" och \"sättare\", men konceptet med egenskaper som beskrivs nedan är mycket liknande, vilket är varför vi kommer att använda den allmänt accepterade objektorienterade programmeringsterminologin här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovan skapade vi några offentliga metoder för att komma åt privata attribut, men det finns ett enklare, \"pythoniskt\" sätt att komma åt attribut. Låt oss ta en titt på en enkel klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Planbok"}]},{"type":"text","value":" med ett enda privat attribut "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pengar"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Planbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan tillägga getter och sättar metoder för att komma åt det privata attributet genom att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" dekoratorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Planbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Gettermetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Sättarmetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pengar"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först definierar vi en getter-metod som returnerar den summa pengar som för närvarande finns i plånboken. Sedan definierar vi en sättar-metod som sätter ett nytt värde för pengar-attributet och samtidigt ser till att det nya värdet inte är negativt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De nya metoderna kan användas på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"planbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nplanbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nplanbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n50\n50"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För klienten är det ingen skillnad att använda dessa nya metoder jämfört med att direkt komma åt ett attribut. Parenteser är inte nödvändiga, utan det är helt acceptabelt att ange "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"planbok.pengar = 50"}]},{"type":"text","value":", som om vi helt enkelt tilldelar ett värde till en variabel. Syftet var faktiskt att dölja (dvs. kapsla in) den interna implementeringen av attributet och samtidigt erbjuda ett enkelt sätt att komma åt och ändra den data som lagras i objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det föregående exemplet finns dock ett litet problem: klienten meddelas inte om att det inte går att ange ett negativt värde för attributet pengar. När ett värde som anges är uppenbart felaktigt är det vanligtvis en bra idé att skapa ett undantag och på så sätt informera klienten. I det här fallet bör undantaget förmodligen vara av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" för att visa att det angivna värdet var oacceptabelt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en förbättrad version av klassen, tillsammans med lite kod för att testa den:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Planbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Gettermetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Sättarmetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pengar\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden får inte vara under 0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ValueError: Mängden får inte vara under 0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: getter-metoden, dvs "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property-dekoratorn"}]},{"type":"text","value":", måste introduceras före sättar-metoden i koden, annars blir det fel när klassen exekveras. Detta beror på att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property-dekoratorn"}]},{"type":"text","value":" definierar namnet på det \"attribut\" som erbjuds till klienten. Sättar-metoden, som läggs till med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".setter"}]},{"type":"text","value":", lägger helt enkelt till en ny funktionalitet till den."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Inspelning","tmcname":"osa09-10_inspelning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Inspelning"}]},{"type":"text","value":" som modellerar en enda inspelning. Klassen ska ha en privat variabel: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__langd"}]},{"type":"text","value":" av typen heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera följande:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"konstruktorn, som får längden som argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en gettermetod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langd"}]},{"type":"text","value":", som returnerar längden av inspelningen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en sättarmetod, som sätter längden på inspelningen"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen används på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"the_wall "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Inspelning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"43"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"the_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nthe_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"the_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"43\n44"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om argumentet för antingen konstruktorn eller sättar-metoden är under noll, bör detta åstadkomma ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall du inte minns hur man åstadkommer undantag, kan du kolla in\n"},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/3-fel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":" av materialet."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel har en klass med två privata attribut, tillsammans med getter och sättare för båda. Prova programmet med olika värden som skickas som argument:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__spelnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spelnummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namnet kan inte vara tomt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spelnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__spelnummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spelnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spelnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__spelnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spelnummer\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Spelnumret måste vara ett positivt heltal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fredrik Fotare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fia Futis\""}]},{"type":"text","value":"\nspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fredrik Fotare\n10\nFia Futis\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som avslutning på detta avsnitt ska vi titta på en klass som modellerar en enkel dagbok. Alla attribut är privata, men de hanteras genom olika gränssnitt: dagbokens ägare har getter- och sättar-metoder, men dagboksposterna behandlas med \"traditionella\" metoder. I det här fallet är det vettigt att neka klienten all tillgång till dagbokens interna datastruktur. Endast de offentliga metoderna är direkt synliga för klienten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inkapsling säkerställer också att den interna implementeringen av klassen kan ändras när som helst, förutsatt att det offentliga gränssnittet förblir intakt. Klienten behöver inte veta eller bry sig om huruvida den interna datastrukturen är baserad på listor, ordlistor eller något helt annat."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Dagbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"agare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__agare\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"agare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ägaren kan inte vara tom\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_inlagg"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Totalt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"inlägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"- \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dagbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Dagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Idag åt jag gröt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Idag lärde jag mig objekt-orienterad programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Idag lade jag mig tidigt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Totalt 3 inlägg"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Idag åt jag gröt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Idag lärde jag mig objekt-orienterad programmering"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Idag lade jag mig tidigt"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Väderstation","tmcname":"osa09-11_vaderstation"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet Vaderstation som används för att lagra observationer om vädret. Klassen bör ha följande offentliga attribut:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en konstruktor som tar namnet på stationen som sitt argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_observation(observation: str)"}]},{"type":"text","value":", som lägger till en observation till slutet av listan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"senaste_observation()"}]},{"type":"text","value":", som returnerar den senaste observationen som lagts till i listan. Om det ännu inte finns några observationer bör metoden returnera en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tom sträng"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"observationernas_antal()"}]},{"type":"text","value":", som returnerar det totala antalet observationer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":", som returnertar stationens namn och det totala antalet observationer liksom exemplet nedan."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla attribut ska vara inkapslade, så att de inte kan nås direkt. Det är upp till dig hur du implementerar klassen, så länge som det allmänna gränssnittet är exakt som beskrivet ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur klassen ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"station "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Vaderstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Regn 10mm\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Soligt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"senaste_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Åskväder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"senaste_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"observationernas_antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Soligt\nÅskväder\n3\nGumtäkt, 3 observationer"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad inkapsling innebär
  • \n
  • Kommer du att kunna skapa privata attribut
  • \n
  • Vet du hur du skapar gettar och sättare för dina attribut
  • \n

I objektorienterad programmering avser termen klient ett program som använder en klass eller instanser av en klass. En klass erbjuder klienten tjänster genom vilka klienten kan komma åt de objekt som skapats baserat på klassen. Målen här är att

    \n
  1. användningen av en klass och/eller objekt är så enkel som möjligt ur klientens synvinkel
  2. \n
  3. integriteten för varje objekt bevaras hela tiden
  4. \n

Ett objekts integritet innebär att objektets tillstånd alltid förblir acceptabelt. I praktiken innebär detta att värdena på objektets attribut alltid är acceptabla. Ett objekt som representerar ett datum ska till exempel aldrig ha 13 som värde för månaden, ett objekt som representerar en student ska aldrig ha ett negativt tal som värde för uppnådda studiepoäng och så vidare.

Låt oss ta en titt på en klass som heter Studerande:

class Studerande:\n    def __init__(self, namn: str, studerandenummer: str):\n        self.namn = namn\n        self.studerandenummer = studerandenummer\n        self.studiepoang = 0\n\n    def tillagg_poang(self, studiepoang):\n        if studiepoang > 0:\n            self.studiepoang += studiepoang

Studerande objektet erbjuder sina klienter metoden tillagg_poang, som gör det möjligt för klienten att lägga till ett angivet antal studiepoäng till studentens totala antal. Metoden säkerställer att värdet som skickas som argument är över noll. Följande kod lägger till studiepoäng vid tre tillfällen:

oskar = Studerande(\"Oskar Studerande\", \"12345\")\noskar.tillagg_poang(5)\noskar.tillagg_poang(5)\noskar.tillagg_poang(10)\nprint(\"Studiepoäng:\", oskar.studiepoang)

Studiepoäng: 20

Trots metoddefinitionen är det fortfarande möjligt att komma åt attributet studiepoang direkt. Detta kunde resultera i ett felaktigt tillstånd där objektets integritet går förlorad:

oskar = Studerande(\"Oskar Studerande\", \"12345\")\noskar.studiepoang = -100\nprint(\"Studiepoäng:\", oskar.studiepoang)

Studiepoäng: -100

Inkapsling

Ett vanligt inslag i objektorienterade programmeringsspråk är att klasserna kan dölja sina attribut för eventuella kunder. Dolda attribut kallas vanligtvis privata. I Python uppnås denna sekretess genom att lägga till två understreck __ i början av attributnamnet:

class Bankkort:\n    # Attributet nummer är gömt, attributet namn är åtkombart\n    def __init__(self, nummer: str, namn: str):\n        self.__nummer = nummer\n        self.namn = namn

Ett privat attribut är inte direkt synligt för klienten. Försök att referera till det orsakar ett fel. I exemplet ovan är attributet namn lätt att komma åt och ändra:

kort = Bankkort(\"123456\",\"Robert Rik\")\nprint(kort.namn)\nkort.namn = \"Peter Pank\"\nprint(kort.namn)

Robert Rik\nPeter Pank

Ifall man provar få en utskrift av kortnumret så orsakar det däremot ett fel:

kort = Bankkort(\"123456\",\"Robert Rik\")\nprint(kort.__nummer)

AttributeError: 'Bankkort' object has no attribute '__nummer'

Att dölja attribut från klienter kallas inkapsling. Som namnet antyder är attributet \"slutet inne i en kapsel\". Klienten erbjuds sedan ett lämpligt gränssnitt (engelska: interface) för att komma åt och bearbeta den data som finns lagrad i objektet.

Låt oss lägga till ett annat inkapslat attribut: saldot på kreditkortet. Den här gången lägger vi också till offentligt synliga metoder som gör det möjligt för klienten att komma åt och ändra saldot:

class Bankkort:\n    def __init__(self, nummer: str, namn: str, saldo: float):\n        self.__nummer = nummer\n        self.namn = namn\n        self.__saldo = saldo\n\n    def tillsatt_pengar(self, mangd: float):\n        if mangd > 0:\n            self.__saldo += mangd\n\n    def anvand_pengar(self, mangd: float):\n        if mangd > 0 and mangd <= self.__saldo:\n            self.__saldo -= mangd\n\n    def hamta_saldo(self):\n        return self.__saldo
kort = Bankkort(\"123456\", \"Robert Rik\", 5000)\nprint(kort.hamta_saldo())\nkort.tillsatt_pengar(100)\nprint(kort.hamta_saldo())\nkort.anvand_pengar(500)\nprint(kort.hamta_saldo())\n# Detta lyckas inte, eftersom saldot inte är tillräckligt\nkort.anvand_pengar(10000)\nprint(kort.hamta_saldo())

5000\n5100\n4600\n4600

Saldot kan inte ändras direkt eftersom attributet är privat, men vi har inkluderat metoderna tillsatt_pengar och ta_ut_pengar för att ändra värdet. Metoden returnera_saldo returnerar det värde som lagrats i saldo. Metoderna innehåller några rudimentära kontroller för att bibehålla objektets integritet: till exempel kan kortet inte överdras.

Implementera en klass som heter Bil och som har två privata, inkapslade variabler: mängden bensin i tanken (0 till 60 liter) och vägmätarställningen (i kilometer). Bilen förbrukar en liter bensin per kilometer.

Klassen ska ha följande metoder:

    \n
  • tanka(), som fyller bensintanken
  • \n
  • kor(km:int), som kör bilen enligt den angivna distansen eller för så länge bensinen i tanken räcker
  • \n
  • __str__, som visar en strängrepresentation av bilen enligt exemplet nedan
  • \n

Exempel på hur klassen fungerar:

bil = Bil()\nprint(bil)\nbil.tanka()\nprint(bil)\nbil.kor(20)\nprint(bil)\nbil.kor(50)\nprint(bil)\nbil.kor(10)\nprint(bil)\nbil.tanka()\nbil.tanka()\nprint(bil)

Bil: har kört 0 km, bensin 0 liter\nBil: har kört 0 km, bensin 60 liter\nBil: har kört 20 km, bensin 40 liter\nBil: har kört 60 km, bensin 0 liter\nBil: har kört 60 km, bensin 0 liter\nBil: har kört 60 km, bensin 60 liter

OBS: Du ombeds att kapsla in mängden bensin som finns kvar och vägmätarställningen. Det ska inte vara möjligt att komma åt dem direkt utanför klassens egna metoder.

En kort notis om privata attribut, Python och objektorienterad programmering

Det finns sätt att kringgå understryknings __-notationen för att dölja attribut, som du kan stöta på om du söker efter material online. Inget Python-attribut är verkligen privat, och det är avsiktligt från skaparna av Pythons. Å andra sidan förväntas en Python-programmerare i allmänhet respektera de riktlinjer för synlighet som anges i klasser, och det krävs en särskild ansträngning för att komma runt dessa. I andra objektorienterade programmeringsspråk, till exempel Java, är privata variabler ofta verkligen dolda, och det är bäst om du tänker på privata Python-variabler som sådana också.

Getter och sättare

I objektorienterad programmering kallas metoder som är avsedda för att komma åt och ändra attribut vanligtvis för getter och sättare (eng: setters). Inte alla Python-programmerare använder termerna \"getter\" och \"sättare\", men konceptet med egenskaper som beskrivs nedan är mycket liknande, vilket är varför vi kommer att använda den allmänt accepterade objektorienterade programmeringsterminologin här.

Ovan skapade vi några offentliga metoder för att komma åt privata attribut, men det finns ett enklare, \"pythoniskt\" sätt att komma åt attribut. Låt oss ta en titt på en enkel klass som heter Planbok med ett enda privat attribut pengar:

class Planbok:\n    def __init__(self):\n        self.__pengar = 0

Vi kan tillägga getter och sättar metoder för att komma åt det privata attributet genom att använda @property dekoratorn:

class Planbok:\n    def __init__(self):\n        self.__pengar = 0\n\n    # Gettermetod\n    @property\n    def pengar(self):\n        return self.__pengar\n\n    # Sättarmetod\n    @pengar.setter\n    def pengar(self, pengar):\n        if pengar >= 0:\n            self.__pengar = pengar

Först definierar vi en getter-metod som returnerar den summa pengar som för närvarande finns i plånboken. Sedan definierar vi en sättar-metod som sätter ett nytt värde för pengar-attributet och samtidigt ser till att det nya värdet inte är negativt.

De nya metoderna kan användas på följande sätt:

planbok = Planbok()\nprint(planbok.pengar)\n\nplanbok.pengar = 50\nprint(planbok.pengar)\n\nplanbok.pengar = -30\nprint(planbok.pengar)

0\n50\n50

För klienten är det ingen skillnad att använda dessa nya metoder jämfört med att direkt komma åt ett attribut. Parenteser är inte nödvändiga, utan det är helt acceptabelt att ange planbok.pengar = 50, som om vi helt enkelt tilldelar ett värde till en variabel. Syftet var faktiskt att dölja (dvs. kapsla in) den interna implementeringen av attributet och samtidigt erbjuda ett enkelt sätt att komma åt och ändra den data som lagras i objektet.

I det föregående exemplet finns dock ett litet problem: klienten meddelas inte om att det inte går att ange ett negativt värde för attributet pengar. När ett värde som anges är uppenbart felaktigt är det vanligtvis en bra idé att skapa ett undantag och på så sätt informera klienten. I det här fallet bör undantaget förmodligen vara av typen ValueError för att visa att det angivna värdet var oacceptabelt.

Här har vi en förbättrad version av klassen, tillsammans med lite kod för att testa den:

class Planbok:\n    def __init__(self):\n        self.__pengar = 0\n\n    # Gettermetod\n    @property\n    def pengar(self):\n        return self.__pengar\n\n    # Sättarmetod\n    @pengar.setter\n    def pengar(self, pengar):\n        if pengar >= 0:\n            self.__pengar = pengar\n        else:\n            raise ValueError(\"Mängden får inte vara under 0\")
planbok.pengar = -30\nprint(planbok.pengar)

ValueError: Mängden får inte vara under 0

OBS: getter-metoden, dvs @property-dekoratorn, måste introduceras före sättar-metoden i koden, annars blir det fel när klassen exekveras. Detta beror på att @property-dekoratorn definierar namnet på det \"attribut\" som erbjuds till klienten. Sättar-metoden, som läggs till med .setter, lägger helt enkelt till en ny funktionalitet till den.

Skapa en klass med namnet Inspelning som modellerar en enda inspelning. Klassen ska ha en privat variabel: __langd av typen heltal.

Implementera följande:

    \n
  • konstruktorn, som får längden som argument
  • \n
  • en gettermetod langd, som returnerar längden av inspelningen
  • \n
  • en sättarmetod, som sätter längden på inspelningen
  • \n

Klassen används på följande sätt:

the_wall = Inspelning(43)\nprint(the_wall.langd)\nthe_wall.langd = 44\nprint(the_wall.langd)

43\n44

Om argumentet för antingen konstruktorn eller sättar-metoden är under noll, bör detta åstadkomma ett ValueError.

Ifall du inte minns hur man åstadkommer undantag, kan du kolla in\nmodul 6 av materialet.

Följande exempel har en klass med två privata attribut, tillsammans med getter och sättare för båda. Prova programmet med olika värden som skickas som argument:

class Spelare:\n    def __init__(self, namn: str, spelnummer: int):\n        self.__namn = namn\n        self.__spelnummer = spelnummer\n\n    @property\n    def namn(self):\n        return self.__namn\n\n    @namn.setter\n    def namn(self, namn: str):\n        if namn != \"\":\n            self.__namn = namn\n        else:\n            raise ValueError(\"Namnet kan inte vara tomt\")\n\n    @property\n    def spelnummer(self):\n        return self.__spelnummer\n\n    @spelnummer.setter\n    def spelnummer(self, spelnummer: int):\n        if spelnummer > 0:\n            self.__spelnummer = spelnummer\n        else:\n            raise ValueError(\"Spelnumret måste vara ett positivt heltal\")
spelare = Spelare(\"Fredrik Fotare\", 10)\nprint(spelare.namn)\nprint(spelare.spelnummer)\n\nspelare.namn = \"Fia Futis\"\nspelare.spelnummer = 11\nprint(spelare.namn)\nprint(spelare.spelnummer)

Fredrik Fotare\n10\nFia Futis\n11

Som avslutning på detta avsnitt ska vi titta på en klass som modellerar en enkel dagbok. Alla attribut är privata, men de hanteras genom olika gränssnitt: dagbokens ägare har getter- och sättar-metoder, men dagboksposterna behandlas med \"traditionella\" metoder. I det här fallet är det vettigt att neka klienten all tillgång till dagbokens interna datastruktur. Endast de offentliga metoderna är direkt synliga för klienten.

Inkapsling säkerställer också att den interna implementeringen av klassen kan ändras när som helst, förutsatt att det offentliga gränssnittet förblir intakt. Klienten behöver inte veta eller bry sig om huruvida den interna datastrukturen är baserad på listor, ordlistor eller något helt annat.

class Dagbok:\n    def __init__(self, agare: str):\n        self.__agare = agare\n        self.__inlagg = []\n\n    @property\n    def agare(self):\n        return self.__agare\n\n    @agare.setter\n    def agare(self, agare):\n        if agare != \"\":\n            self.__agare = agare\n        else:\n            raise ValueError(\"Ägaren kan inte vara tom\")\n\n    def tillsatt_inlagg(self, inlagg: str):\n        self.__inlagg.append(inlagg)\n\n    def skriv_ut(self):\n        print(\"Totalt\", len(self.__inlagg), \"inlägg\")\n        for inlagg in self.__inlagg:\n            print(\"- \" + inlagg)
dagbok = Dagbok(\"Peter\")\ndagbok.tillsatt_inlagg(\"Idag åt jag gröt\")\ndagbok.tillsatt_inlagg(\"Idag lärde jag mig objekt-orienterad programmering\")\ndagbok.tillsatt_inlagg(\"Idag lade jag mig tidigt\")\ndagbok.skriv_ut()

Totalt 3 inlägg

    \n
  • Idag åt jag gröt
  • \n
  • Idag lärde jag mig objekt-orienterad programmering
  • \n
  • Idag lade jag mig tidigt
  • \n

Skapa en klass med namnet Vaderstation som används för att lagra observationer om vädret. Klassen bör ha följande offentliga attribut:

    \n
  • en konstruktor som tar namnet på stationen som sitt argument
  • \n
  • metoden tillsatt_observation(observation: str), som lägger till en observation till slutet av listan
  • \n
  • metoden senaste_observation(), som returnerar den senaste observationen som lagts till i listan. Om det ännu inte finns några observationer bör metoden returnera en tom sträng.
  • \n
  • metoden observationernas_antal(), som returnerar det totala antalet observationer
  • \n
  • metoden __str__, som returnertar stationens namn och det totala antalet observationer liksom exemplet nedan.
  • \n

Alla attribut ska vara inkapslade, så att de inte kan nås direkt. Det är upp till dig hur du implementerar klassen, så länge som det allmänna gränssnittet är exakt som beskrivet ovan.

Exempel på hur klassen ska fungera:

station = Vaderstation(\"Gumtäkt\")\nstation.tillsatt_observation(\"Regn 10mm\")\nstation.tillsatt_observation(\"Soligt\")\nprint(station.senaste_observation())\n\nstation.tillsatt_observation(\"Åskväder\")\nprint(station.senaste_observation())\n\nprint(station.observationernas_antal())\nprint(station)

Soligt\nÅskväder\n3\nGumtäkt, 3 observationer

","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/3-inkapsling.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du vad inkapsling innebär"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skapa privata attribut"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du skapar gettar och sättare för dina attribut"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I objektorienterad programmering avser termen klient ett program som använder en klass eller instanser av en klass. En klass erbjuder klienten tjänster genom vilka klienten kan komma åt de objekt som skapats baserat på klassen. Målen här är att"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"användningen av en klass och/eller objekt är så enkel som möjligt ur klientens synvinkel"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"integriteten för varje objekt bevaras hela tiden"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett objekts integritet innebär att objektets tillstånd alltid förblir acceptabelt. I praktiken innebär detta att värdena på objektets attribut alltid är acceptabla. Ett objekt som representerar ett datum ska till exempel aldrig ha 13 som värde för månaden, ett objekt som representerar en student ska aldrig ha ett negativt tal som värde för uppnådda studiepoäng och så vidare."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på en klass som heter Studerande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillagg_poang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" studiepoang"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":" objektet erbjuder sina klienter metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillagg_poang"}]},{"type":"text","value":", som gör det möjligt för klienten att lägga till ett angivet antal studiepoäng till studentens totala antal. Metoden säkerställer att värdet som skickas som argument är över noll. Följande kod lägger till studiepoäng vid tre tillfällen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"oskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Oskar Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillagg_poang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studiepoäng:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studiepoäng: 20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Trots metoddefinitionen är det fortfarande möjligt att komma åt attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studiepoang"}]},{"type":"text","value":" direkt. Detta kunde resultera i ett felaktigt tillstånd där objektets integritet går förlorad:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"oskar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Oskar Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\noskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studiepoäng:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oskar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studiepoäng: -100"}]}]},{"type":"element","tagName":"h2","properties":{"id":"inkapsling","style":"position:relative;"},"children":[{"type":"text","value":"Inkapsling"},{"type":"element","tagName":"a","properties":{"href":"#inkapsling","ariaLabel":"inkapsling permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett vanligt inslag i objektorienterade programmeringsspråk är att klasserna kan dölja sina attribut för eventuella kunder. Dolda attribut kallas vanligtvis privata. I Python uppnås denna sekretess genom att lägga till två understreck "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":" i början av attributnamnet:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Attributet nummer är gömt, attributet namn är åtkombart"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ett privat attribut är inte direkt synligt för klienten. Försök att referera till det orsakar ett fel. I exemplet ovan är attributet namn lätt att komma åt och ändra:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pank\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Robert Rik\nPeter Pank"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall man provar få en utskrift av kortnumret så orsakar det däremot ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'Bankkort' object has no attribute '__nummer'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Att dölja attribut från klienter kallas inkapsling. Som namnet antyder är attributet \"slutet inne i en kapsel\". Klienten erbjuds sedan ett lämpligt gränssnitt (engelska: interface) för att komma åt och bearbeta den data som finns lagrad i objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss lägga till ett annat inkapslat attribut: saldot på kreditkortet. Den här gången lägger vi också till offentligt synliga metoder som gör det möjligt för klienten att komma åt och ändra saldot:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bankkort"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"anvand_pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mangd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" mangd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" mangd\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hamta_saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kort "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Robert Rik\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"anvand_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Detta lyckas inte, eftersom saldot inte är tillräckligt"}]},{"type":"text","value":"\nkort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"anvand_pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hamta_saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5000\n5100\n4600\n4600"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Saldot kan inte ändras direkt eftersom attributet är privat, men vi har inkluderat metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_pengar"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ta_ut_pengar"}]},{"type":"text","value":" för att ändra värdet. Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"returnera_saldo"}]},{"type":"text","value":" returnerar det värde som lagrats i saldo. Metoderna innehåller några rudimentära kontroller för att bibehålla objektets integritet: till exempel kan kortet inte överdras."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bil","tmcname":"osa09-09_bil"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera en klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bil"}]},{"type":"text","value":" och som har två privata, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inkapslade"}]},{"type":"text","value":" variabler: mängden bensin i tanken (0 till 60 liter) och vägmätarställningen (i kilometer). Bilen förbrukar en liter bensin per kilometer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska ha följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tanka()"}]},{"type":"text","value":", som fyller bensintanken"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"kor(km:int)"}]},{"type":"text","value":", som kör bilen enligt den angivna distansen eller för så länge bensinen i tanken räcker"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":", som visar en strängrepresentation av bilen enligt exemplet nedan"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur klassen fungerar:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bil "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tanka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"kor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tanka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tanka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bil"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bil: har kört 0 km, bensin 0 liter\nBil: har kört 0 km, bensin 60 liter\nBil: har kört 20 km, bensin 40 liter\nBil: har kört 60 km, bensin 0 liter\nBil: har kört 60 km, bensin 0 liter\nBil: har kört 60 km, bensin 60 liter"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OBS:"}]},{"type":"text","value":" Du ombeds att kapsla in mängden bensin som finns kvar och vägmätarställningen. Det ska inte vara möjligt att komma åt dem direkt utanför klassens egna metoder."}]}]},{"type":"element","tagName":"h2","properties":{"id":"en-kort-notis-om-privata-attribut-python-och-objektorienterad-programmering","style":"position:relative;"},"children":[{"type":"text","value":"En kort notis om privata attribut, Python och objektorienterad programmering"},{"type":"element","tagName":"a","properties":{"href":"#en-kort-notis-om-privata-attribut-python-och-objektorienterad-programmering","ariaLabel":"en kort notis om privata attribut python och objektorienterad programmering permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns sätt att kringgå understryknings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":"-notationen för att dölja attribut, som du kan stöta på om du söker efter material online. Inget Python-attribut är verkligen privat, och det är avsiktligt från skaparna av Pythons. Å andra sidan förväntas en Python-programmerare i allmänhet respektera de riktlinjer för synlighet som anges i klasser, och det krävs en särskild ansträngning för att komma runt dessa. I andra objektorienterade programmeringsspråk, till exempel Java, är privata variabler ofta verkligen dolda, och det är bäst om du tänker på privata Python-variabler som sådana också."}]},{"type":"element","tagName":"h2","properties":{"id":"getter-och-sattare","style":"position:relative;"},"children":[{"type":"text","value":"Getter och sättare"},{"type":"element","tagName":"a","properties":{"href":"#getter-och-sattare","ariaLabel":"getter och sattare permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I objektorienterad programmering kallas metoder som är avsedda för att komma åt och ändra attribut vanligtvis för getter och sättare (eng: setters). Inte alla Python-programmerare använder termerna \"getter\" och \"sättare\", men konceptet med egenskaper som beskrivs nedan är mycket liknande, vilket är varför vi kommer att använda den allmänt accepterade objektorienterade programmeringsterminologin här."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovan skapade vi några offentliga metoder för att komma åt privata attribut, men det finns ett enklare, \"pythoniskt\" sätt att komma åt attribut. Låt oss ta en titt på en enkel klass som heter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Planbok"}]},{"type":"text","value":" med ett enda privat attribut "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pengar"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Planbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vi kan tillägga getter och sättar metoder för att komma åt det privata attributet genom att använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" dekoratorn:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Planbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Gettermetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Sättarmetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pengar"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Först definierar vi en getter-metod som returnerar den summa pengar som för närvarande finns i plånboken. Sedan definierar vi en sättar-metod som sätter ett nytt värde för pengar-attributet och samtidigt ser till att det nya värdet inte är negativt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De nya metoderna kan användas på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"planbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nplanbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nplanbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n50\n50"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"För klienten är det ingen skillnad att använda dessa nya metoder jämfört med att direkt komma åt ett attribut. Parenteser är inte nödvändiga, utan det är helt acceptabelt att ange "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"planbok.pengar = 50"}]},{"type":"text","value":", som om vi helt enkelt tilldelar ett värde till en variabel. Syftet var faktiskt att dölja (dvs. kapsla in) den interna implementeringen av attributet och samtidigt erbjuda ett enkelt sätt att komma åt och ändra den data som lagras i objektet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I det föregående exemplet finns dock ett litet problem: klienten meddelas inte om att det inte går att ange ett negativt värde för attributet pengar. När ett värde som anges är uppenbart felaktigt är det vanligtvis en bra idé att skapa ett undantag och på så sätt informera klienten. I det här fallet bör undantaget förmodligen vara av typen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" för att visa att det angivna värdet var oacceptabelt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Här har vi en förbättrad version av klassen, tillsammans med lite kod för att testa den:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Planbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Gettermetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Sättarmetod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"pengar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pengar\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mängden får inte vara under 0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"planbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pengar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"ValueError: Mängden får inte vara under 0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: getter-metoden, dvs "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property-dekoratorn"}]},{"type":"text","value":", måste introduceras före sättar-metoden i koden, annars blir det fel när klassen exekveras. Detta beror på att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property-dekoratorn"}]},{"type":"text","value":" definierar namnet på det \"attribut\" som erbjuds till klienten. Sättar-metoden, som läggs till med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".setter"}]},{"type":"text","value":", lägger helt enkelt till en ny funktionalitet till den."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Inspelning","tmcname":"osa09-10_inspelning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Inspelning"}]},{"type":"text","value":" som modellerar en enda inspelning. Klassen ska ha en privat variabel: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__langd"}]},{"type":"text","value":" av typen heltal."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementera följande:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"konstruktorn, som får längden som argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en gettermetod "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"langd"}]},{"type":"text","value":", som returnerar längden av inspelningen"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en sättarmetod, som sätter längden på inspelningen"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen används på följande sätt:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"the_wall "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Inspelning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"43"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"the_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nthe_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"the_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"43\n44"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om argumentet för antingen konstruktorn eller sättar-metoden är under noll, bör detta åstadkomma ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ifall du inte minns hur man åstadkommer undantag, kan du kolla in\n"},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-6/3-fel","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"modul 6"}]},{"type":"text","value":" av materialet."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel har en klass med två privata attribut, tillsammans med getter och sättare för båda. Prova programmet med olika värden som skickas som argument:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Spelare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__spelnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spelnummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namnet kan inte vara tomt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spelnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__spelnummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spelnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spelnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__spelnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" spelnummer\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Spelnumret måste vara ett positivt heltal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"spelare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fredrik Fotare\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fia Futis\""}]},{"type":"text","value":"\nspelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"spelare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spelnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fredrik Fotare\n10\nFia Futis\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Som avslutning på detta avsnitt ska vi titta på en klass som modellerar en enkel dagbok. Alla attribut är privata, men de hanteras genom olika gränssnitt: dagbokens ägare har getter- och sättar-metoder, men dagboksposterna behandlas med \"traditionella\" metoder. I det här fallet är det vettigt att neka klienten all tillgång till dagbokens interna datastruktur. Endast de offentliga metoderna är direkt synliga för klienten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inkapsling säkerställer också att den interna implementeringen av klassen kan ändras när som helst, förutsatt att det offentliga gränssnittet förblir intakt. Klienten behöver inte veta eller bry sig om huruvida den interna datastrukturen är baserad på listor, ordlistor eller något helt annat."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Dagbok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"agare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__agare\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"agare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ägaren kan inte vara tom\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_inlagg"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"skriv_ut"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Totalt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"inlägg\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" inlagg "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"- \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"dagbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Dagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Idag åt jag gröt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Idag lärde jag mig objekt-orienterad programmering\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_inlagg"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Idag lade jag mig tidigt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndagbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Totalt 3 inlägg"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Idag åt jag gröt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Idag lärde jag mig objekt-orienterad programmering"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Idag lade jag mig tidigt"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Väderstation","tmcname":"osa09-11_vaderstation"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet Vaderstation som används för att lagra observationer om vädret. Klassen bör ha följande offentliga attribut:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en konstruktor som tar namnet på stationen som sitt argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_observation(observation: str)"}]},{"type":"text","value":", som lägger till en observation till slutet av listan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"senaste_observation()"}]},{"type":"text","value":", som returnerar den senaste observationen som lagts till i listan. Om det ännu inte finns några observationer bör metoden returnera en "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tom sträng"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"observationernas_antal()"}]},{"type":"text","value":", som returnerar det totala antalet observationer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":", som returnertar stationens namn och det totala antalet observationer liksom exemplet nedan."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla attribut ska vara inkapslade, så att de inte kan nås direkt. Det är upp till dig hur du implementerar klassen, så länge som det allmänna gränssnittet är exakt som beskrivet ovan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på hur klassen ska fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"station "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Vaderstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Gumtäkt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Regn 10mm\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Soligt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"senaste_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Åskväder\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"senaste_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"observationernas_antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Soligt\nÅskväder\n3\nGumtäkt, 3 observationer"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du vad inkapsling innebär
  • \n
  • Kommer du att kunna skapa privata attribut
  • \n
  • Vet du hur du skapar gettar och sättare för dina attribut
  • \n

I objektorienterad programmering avser termen klient ett program som använder en klass eller instanser av en klass. En klass erbjuder klienten tjänster genom vilka klienten kan komma åt de objekt som skapats baserat på klassen. Målen här är att

    \n
  1. användningen av en klass och/eller objekt är så enkel som möjligt ur klientens synvinkel
  2. \n
  3. integriteten för varje objekt bevaras hela tiden
  4. \n

Ett objekts integritet innebär att objektets tillstånd alltid förblir acceptabelt. I praktiken innebär detta att värdena på objektets attribut alltid är acceptabla. Ett objekt som representerar ett datum ska till exempel aldrig ha 13 som värde för månaden, ett objekt som representerar en student ska aldrig ha ett negativt tal som värde för uppnådda studiepoäng och så vidare.

Låt oss ta en titt på en klass som heter Studerande:

class Studerande:\n    def __init__(self, namn: str, studerandenummer: str):\n        self.namn = namn\n        self.studerandenummer = studerandenummer\n        self.studiepoang = 0\n\n    def tillagg_poang(self, studiepoang):\n        if studiepoang > 0:\n            self.studiepoang += studiepoang

Studerande objektet erbjuder sina klienter metoden tillagg_poang, som gör det möjligt för klienten att lägga till ett angivet antal studiepoäng till studentens totala antal. Metoden säkerställer att värdet som skickas som argument är över noll. Följande kod lägger till studiepoäng vid tre tillfällen:

oskar = Studerande(\"Oskar Studerande\", \"12345\")\noskar.tillagg_poang(5)\noskar.tillagg_poang(5)\noskar.tillagg_poang(10)\nprint(\"Studiepoäng:\", oskar.studiepoang)

Studiepoäng: 20

Trots metoddefinitionen är det fortfarande möjligt att komma åt attributet studiepoang direkt. Detta kunde resultera i ett felaktigt tillstånd där objektets integritet går förlorad:

oskar = Studerande(\"Oskar Studerande\", \"12345\")\noskar.studiepoang = -100\nprint(\"Studiepoäng:\", oskar.studiepoang)

Studiepoäng: -100

Inkapsling

Ett vanligt inslag i objektorienterade programmeringsspråk är att klasserna kan dölja sina attribut för eventuella kunder. Dolda attribut kallas vanligtvis privata. I Python uppnås denna sekretess genom att lägga till två understreck __ i början av attributnamnet:

class Bankkort:\n    # Attributet nummer är gömt, attributet namn är åtkombart\n    def __init__(self, nummer: str, namn: str):\n        self.__nummer = nummer\n        self.namn = namn

Ett privat attribut är inte direkt synligt för klienten. Försök att referera till det orsakar ett fel. I exemplet ovan är attributet namn lätt att komma åt och ändra:

kort = Bankkort(\"123456\",\"Robert Rik\")\nprint(kort.namn)\nkort.namn = \"Peter Pank\"\nprint(kort.namn)

Robert Rik\nPeter Pank

Ifall man provar få en utskrift av kortnumret så orsakar det däremot ett fel:

kort = Bankkort(\"123456\",\"Robert Rik\")\nprint(kort.__nummer)

AttributeError: 'Bankkort' object has no attribute '__nummer'

Att dölja attribut från klienter kallas inkapsling. Som namnet antyder är attributet \"slutet inne i en kapsel\". Klienten erbjuds sedan ett lämpligt gränssnitt (engelska: interface) för att komma åt och bearbeta den data som finns lagrad i objektet.

Låt oss lägga till ett annat inkapslat attribut: saldot på kreditkortet. Den här gången lägger vi också till offentligt synliga metoder som gör det möjligt för klienten att komma åt och ändra saldot:

class Bankkort:\n    def __init__(self, nummer: str, namn: str, saldo: float):\n        self.__nummer = nummer\n        self.namn = namn\n        self.__saldo = saldo\n\n    def tillsatt_pengar(self, mangd: float):\n        if mangd > 0:\n            self.__saldo += mangd\n\n    def anvand_pengar(self, mangd: float):\n        if mangd > 0 and mangd <= self.__saldo:\n            self.__saldo -= mangd\n\n    def hamta_saldo(self):\n        return self.__saldo
kort = Bankkort(\"123456\", \"Robert Rik\", 5000)\nprint(kort.hamta_saldo())\nkort.tillsatt_pengar(100)\nprint(kort.hamta_saldo())\nkort.anvand_pengar(500)\nprint(kort.hamta_saldo())\n# Detta lyckas inte, eftersom saldot inte är tillräckligt\nkort.anvand_pengar(10000)\nprint(kort.hamta_saldo())

5000\n5100\n4600\n4600

Saldot kan inte ändras direkt eftersom attributet är privat, men vi har inkluderat metoderna tillsatt_pengar och ta_ut_pengar för att ändra värdet. Metoden returnera_saldo returnerar det värde som lagrats i saldo. Metoderna innehåller några rudimentära kontroller för att bibehålla objektets integritet: till exempel kan kortet inte överdras.

Implementera en klass som heter Bil och som har två privata, inkapslade variabler: mängden bensin i tanken (0 till 60 liter) och vägmätarställningen (i kilometer). Bilen förbrukar en liter bensin per kilometer.

Klassen ska ha följande metoder:

    \n
  • tanka(), som fyller bensintanken
  • \n
  • kor(km:int), som kör bilen enligt den angivna distansen eller för så länge bensinen i tanken räcker
  • \n
  • __str__, som visar en strängrepresentation av bilen enligt exemplet nedan
  • \n

Exempel på hur klassen fungerar:

bil = Bil()\nprint(bil)\nbil.tanka()\nprint(bil)\nbil.kor(20)\nprint(bil)\nbil.kor(50)\nprint(bil)\nbil.kor(10)\nprint(bil)\nbil.tanka()\nbil.tanka()\nprint(bil)

Bil: har kört 0 km, bensin 0 liter\nBil: har kört 0 km, bensin 60 liter\nBil: har kört 20 km, bensin 40 liter\nBil: har kört 60 km, bensin 0 liter\nBil: har kört 60 km, bensin 0 liter\nBil: har kört 60 km, bensin 60 liter

OBS: Du ombeds att kapsla in mängden bensin som finns kvar och vägmätarställningen. Det ska inte vara möjligt att komma åt dem direkt utanför klassens egna metoder.

En kort notis om privata attribut, Python och objektorienterad programmering

Det finns sätt att kringgå understryknings __-notationen för att dölja attribut, som du kan stöta på om du söker efter material online. Inget Python-attribut är verkligen privat, och det är avsiktligt från skaparna av Pythons. Å andra sidan förväntas en Python-programmerare i allmänhet respektera de riktlinjer för synlighet som anges i klasser, och det krävs en särskild ansträngning för att komma runt dessa. I andra objektorienterade programmeringsspråk, till exempel Java, är privata variabler ofta verkligen dolda, och det är bäst om du tänker på privata Python-variabler som sådana också.

Getter och sättare

I objektorienterad programmering kallas metoder som är avsedda för att komma åt och ändra attribut vanligtvis för getter och sättare (eng: setters). Inte alla Python-programmerare använder termerna \"getter\" och \"sättare\", men konceptet med egenskaper som beskrivs nedan är mycket liknande, vilket är varför vi kommer att använda den allmänt accepterade objektorienterade programmeringsterminologin här.

Ovan skapade vi några offentliga metoder för att komma åt privata attribut, men det finns ett enklare, \"pythoniskt\" sätt att komma åt attribut. Låt oss ta en titt på en enkel klass som heter Planbok med ett enda privat attribut pengar:

class Planbok:\n    def __init__(self):\n        self.__pengar = 0

Vi kan tillägga getter och sättar metoder för att komma åt det privata attributet genom att använda @property dekoratorn:

class Planbok:\n    def __init__(self):\n        self.__pengar = 0\n\n    # Gettermetod\n    @property\n    def pengar(self):\n        return self.__pengar\n\n    # Sättarmetod\n    @pengar.setter\n    def pengar(self, pengar):\n        if pengar >= 0:\n            self.__pengar = pengar

Först definierar vi en getter-metod som returnerar den summa pengar som för närvarande finns i plånboken. Sedan definierar vi en sättar-metod som sätter ett nytt värde för pengar-attributet och samtidigt ser till att det nya värdet inte är negativt.

De nya metoderna kan användas på följande sätt:

planbok = Planbok()\nprint(planbok.pengar)\n\nplanbok.pengar = 50\nprint(planbok.pengar)\n\nplanbok.pengar = -30\nprint(planbok.pengar)

0\n50\n50

För klienten är det ingen skillnad att använda dessa nya metoder jämfört med att direkt komma åt ett attribut. Parenteser är inte nödvändiga, utan det är helt acceptabelt att ange planbok.pengar = 50, som om vi helt enkelt tilldelar ett värde till en variabel. Syftet var faktiskt att dölja (dvs. kapsla in) den interna implementeringen av attributet och samtidigt erbjuda ett enkelt sätt att komma åt och ändra den data som lagras i objektet.

I det föregående exemplet finns dock ett litet problem: klienten meddelas inte om att det inte går att ange ett negativt värde för attributet pengar. När ett värde som anges är uppenbart felaktigt är det vanligtvis en bra idé att skapa ett undantag och på så sätt informera klienten. I det här fallet bör undantaget förmodligen vara av typen ValueError för att visa att det angivna värdet var oacceptabelt.

Här har vi en förbättrad version av klassen, tillsammans med lite kod för att testa den:

class Planbok:\n    def __init__(self):\n        self.__pengar = 0\n\n    # Gettermetod\n    @property\n    def pengar(self):\n        return self.__pengar\n\n    # Sättarmetod\n    @pengar.setter\n    def pengar(self, pengar):\n        if pengar >= 0:\n            self.__pengar = pengar\n        else:\n            raise ValueError(\"Mängden får inte vara under 0\")
planbok.pengar = -30\nprint(planbok.pengar)

ValueError: Mängden får inte vara under 0

OBS: getter-metoden, dvs @property-dekoratorn, måste introduceras före sättar-metoden i koden, annars blir det fel när klassen exekveras. Detta beror på att @property-dekoratorn definierar namnet på det \"attribut\" som erbjuds till klienten. Sättar-metoden, som läggs till med .setter, lägger helt enkelt till en ny funktionalitet till den.

Skapa en klass med namnet Inspelning som modellerar en enda inspelning. Klassen ska ha en privat variabel: __langd av typen heltal.

Implementera följande:

    \n
  • konstruktorn, som får längden som argument
  • \n
  • en gettermetod langd, som returnerar längden av inspelningen
  • \n
  • en sättarmetod, som sätter längden på inspelningen
  • \n

Klassen används på följande sätt:

the_wall = Inspelning(43)\nprint(the_wall.langd)\nthe_wall.langd = 44\nprint(the_wall.langd)

43\n44

Om argumentet för antingen konstruktorn eller sättar-metoden är under noll, bör detta åstadkomma ett ValueError.

Ifall du inte minns hur man åstadkommer undantag, kan du kolla in\nmodul 6 av materialet.

Följande exempel har en klass med två privata attribut, tillsammans med getter och sättare för båda. Prova programmet med olika värden som skickas som argument:

class Spelare:\n    def __init__(self, namn: str, spelnummer: int):\n        self.__namn = namn\n        self.__spelnummer = spelnummer\n\n    @property\n    def namn(self):\n        return self.__namn\n\n    @namn.setter\n    def namn(self, namn: str):\n        if namn != \"\":\n            self.__namn = namn\n        else:\n            raise ValueError(\"Namnet kan inte vara tomt\")\n\n    @property\n    def spelnummer(self):\n        return self.__spelnummer\n\n    @spelnummer.setter\n    def spelnummer(self, spelnummer: int):\n        if spelnummer > 0:\n            self.__spelnummer = spelnummer\n        else:\n            raise ValueError(\"Spelnumret måste vara ett positivt heltal\")
spelare = Spelare(\"Fredrik Fotare\", 10)\nprint(spelare.namn)\nprint(spelare.spelnummer)\n\nspelare.namn = \"Fia Futis\"\nspelare.spelnummer = 11\nprint(spelare.namn)\nprint(spelare.spelnummer)

Fredrik Fotare\n10\nFia Futis\n11

Som avslutning på detta avsnitt ska vi titta på en klass som modellerar en enkel dagbok. Alla attribut är privata, men de hanteras genom olika gränssnitt: dagbokens ägare har getter- och sättar-metoder, men dagboksposterna behandlas med \"traditionella\" metoder. I det här fallet är det vettigt att neka klienten all tillgång till dagbokens interna datastruktur. Endast de offentliga metoderna är direkt synliga för klienten.

Inkapsling säkerställer också att den interna implementeringen av klassen kan ändras när som helst, förutsatt att det offentliga gränssnittet förblir intakt. Klienten behöver inte veta eller bry sig om huruvida den interna datastrukturen är baserad på listor, ordlistor eller något helt annat.

class Dagbok:\n    def __init__(self, agare: str):\n        self.__agare = agare\n        self.__inlagg = []\n\n    @property\n    def agare(self):\n        return self.__agare\n\n    @agare.setter\n    def agare(self, agare):\n        if agare != \"\":\n            self.__agare = agare\n        else:\n            raise ValueError(\"Ägaren kan inte vara tom\")\n\n    def tillsatt_inlagg(self, inlagg: str):\n        self.__inlagg.append(inlagg)\n\n    def skriv_ut(self):\n        print(\"Totalt\", len(self.__inlagg), \"inlägg\")\n        for inlagg in self.__inlagg:\n            print(\"- \" + inlagg)
dagbok = Dagbok(\"Peter\")\ndagbok.tillsatt_inlagg(\"Idag åt jag gröt\")\ndagbok.tillsatt_inlagg(\"Idag lärde jag mig objekt-orienterad programmering\")\ndagbok.tillsatt_inlagg(\"Idag lade jag mig tidigt\")\ndagbok.skriv_ut()

Totalt 3 inlägg

    \n
  • Idag åt jag gröt
  • \n
  • Idag lärde jag mig objekt-orienterad programmering
  • \n
  • Idag lade jag mig tidigt
  • \n

Skapa en klass med namnet Vaderstation som används för att lagra observationer om vädret. Klassen bör ha följande offentliga attribut:

    \n
  • en konstruktor som tar namnet på stationen som sitt argument
  • \n
  • metoden tillsatt_observation(observation: str), som lägger till en observation till slutet av listan
  • \n
  • metoden senaste_observation(), som returnerar den senaste observationen som lagts till i listan. Om det ännu inte finns några observationer bör metoden returnera en tom sträng.
  • \n
  • metoden observationernas_antal(), som returnerar det totala antalet observationer
  • \n
  • metoden __str__, som returnertar stationens namn och det totala antalet observationer liksom exemplet nedan.
  • \n

Alla attribut ska vara inkapslade, så att de inte kan nås direkt. Det är upp till dig hur du implementerar klassen, så länge som det allmänna gränssnittet är exakt som beskrivet ovan.

Exempel på hur klassen ska fungera:

station = Vaderstation(\"Gumtäkt\")\nstation.tillsatt_observation(\"Regn 10mm\")\nstation.tillsatt_observation(\"Soligt\")\nprint(station.senaste_observation())\n\nstation.tillsatt_observation(\"Åskväder\")\nprint(station.senaste_observation())\n\nprint(station.observationernas_antal())\nprint(station)

Soligt\nÅskväder\n3\nGumtäkt, 3 observationer

","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/3-inkapsling.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-9/4-metodernas-rackvidd/page-data.json b/page-data/osa-9/4-metodernas-rackvidd/page-data.json index 512559070..11d6017b8 100644 --- a/page-data/osa-9/4-metodernas-rackvidd/page-data.json +++ b/page-data/osa-9/4-metodernas-rackvidd/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-9/4-metodernas-rackvidd", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du kan begränsa synligheten för en metod i Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva privata metoder"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De metoder som definieras inom en klass kan döljas på exakt samma sätt som attributen i föregående avsnitt. Om metoden börjar med två understreck "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":" är den inte direkt åtkomlig för klienten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tekniken är alltså densamma för både metoder och attribut, men användningsfallen är oftast lite annorlunda. Privata attribut kommer ofta tillsammans med getter- och sättar-metoder för att kontrollera åtkomsten till dem. Privata metoder å andra sidan är vanligtvis endast avsedda för internt bruk, som hjälpmetoder för processer som klienten inte behöver känna till."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En privat metod kan användas inom klassen precis som vilken annan metod som helst, men man måste naturligtvis komma ihåg att inkludera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"-prefixet. Följande är en enkel klass som representerar mottagaren av e-postbrev. Den innehåller en privat hjälpmetod för att kontrollera att e-postadressen är i ett giltigt format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Mottagare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__granska_epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eposten duger inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__granska_epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En enkel granksning: adressen måste vara över 5 symboler lång och innehålla en punkt och en @-symbol"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" epost "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" epost"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Försök att kalla den privata metoden direkt orsakar ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"petter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Mottagare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Keinonen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"petter@example.com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npetter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__granska_epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"naganannan@example.com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'Mottagare' object has no attribute '__granska_epost'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inom klassen kan metoden användas på normalt sätt, och det är vettigt att använda den även för att ange ett nytt värde för adressen. Låt oss lägga till getter- och sättar-metoder för e-postadressen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Mottagare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__granska_epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eposten duger inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__granska_epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En enkel granksning: adressen måste vara över 5 symboler lång och innehålla en punkt och en @-symbol"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" epost "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" epost\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__epost\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__granska_epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eposten duger inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel är klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kortlek"}]},{"type":"text","value":" en modell för en kortlek med 52 kort. Den innehåller hjälpmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__aterstall_kortlek"}]},{"type":"text","value":", som skapar en ny blandad kortlek. Den privata metoden anropas för närvarande endast i konstruktörsmetoden, så implementationen skulle kunna placeras direkt i konstruktören. Att använda en separat metod gör dock koden mer lättläst och gör det också möjligt att komma åt funktionaliteten senare i andra metoder om det behövs."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Kortlek"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__aterstall_kortlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__aterstall_kortlek"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__packe "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi lägger alla korten i packen"}]},{"type":"text","value":"\n sviter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spader\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hjarter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"klover\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ruter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" svit "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sviter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__packe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Blanda packen"}]},{"type":"text","value":"\n shuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__packe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"dela"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kortens_antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hand "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Flytta korten på toppen av packen till handen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kortens_antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__packe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" hand"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss testa klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kortlek "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Kortlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhand1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kortlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hand1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhand2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kortlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hand2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom händerna är slumpmässigt genererade, är följande endast ett exempel av det som kunde utskrivas:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[('spader', 7), ('spader', 11), ('hjarter', 7), ('ruter', 3), ('spader', 4)]\n[('klover', 8), ('spader', 12), ('ruter', 13), ('klover', 11), ('spader', 10)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Privata metoder är i allmänhet mindre vanliga än privata attribut. En tumregel är att en metod ska döljas när klienten inte har något behov av att direkt komma åt den. Detta är särskilt fallet när det är möjligt att klienten kan påverka objektets integritet negativt genom att anropa metoden."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Service avgift","tmcname":"osa09-12_service_avgift"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":", som modellerar ett bankkonto. Klassen ska innehålla"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en konstruktor, som får som argument namnet på ägaren (str), kontonumret (str) och saldot (float)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insattning(summa: float)"}]},{"type":"text","value":", som sätter till pengar på kontot"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttag(summa: float)"}]},{"type":"text","value":", som tar ut pengar från kontot"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"gettermetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":", som returnerar kontots saldo"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska också innehålla en privat metod"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__service_avgift()"}]},{"type":"text","value":", som minskar saldot på kontot med en procent. När någon av metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insattning"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttag"}]},{"type":"text","value":" anropas bör även denna metod anropas. Serviceavgiften beräknas och subtraheras först efter att den faktiska åtgärden har slutförts (det vill säga efter att det angivna beloppet har lagts till eller subtraherats från saldot)."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla dataattribut inom klassdefinitionen ska vara privata."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på användning av klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ragnar Rikedom\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-6789\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uttag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"891.0\n981.09"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du hur du kan begränsa synligheten för en metod i Python
  • \n
  • Kommer du att kunna skriva privata metoder
  • \n

De metoder som definieras inom en klass kan döljas på exakt samma sätt som attributen i föregående avsnitt. Om metoden börjar med två understreck __ är den inte direkt åtkomlig för klienten.

Tekniken är alltså densamma för både metoder och attribut, men användningsfallen är oftast lite annorlunda. Privata attribut kommer ofta tillsammans med getter- och sättar-metoder för att kontrollera åtkomsten till dem. Privata metoder å andra sidan är vanligtvis endast avsedda för internt bruk, som hjälpmetoder för processer som klienten inte behöver känna till.

En privat metod kan användas inom klassen precis som vilken annan metod som helst, men man måste naturligtvis komma ihåg att inkludera self-prefixet. Följande är en enkel klass som representerar mottagaren av e-postbrev. Den innehåller en privat hjälpmetod för att kontrollera att e-postadressen är i ett giltigt format:

class Mottagare:\n    def __init__(self, namn: str, epost: str):\n        self.__namn = namn\n        if self.__granska_epost(epost):\n            self.__epost = epost\n        else:\n            raise ValueError(\"Eposten duger inte\")\n\n    def __granska_epost(self, epost: str):\n        # En enkel granksning: adressen måste vara över 5 symboler lång och innehålla en punkt och en @-symbol\n        return len(epost) > 5 and \".\" in epost and \"@\" in epost

Försök att kalla den privata metoden direkt orsakar ett fel:

petter = Mottagare(\"Petter Keinonen\", \"petter@example.com\")\npetter.__granska_epost(\"naganannan@example.com\")

AttributeError: 'Mottagare' object has no attribute '__granska_epost'

Inom klassen kan metoden användas på normalt sätt, och det är vettigt att använda den även för att ange ett nytt värde för adressen. Låt oss lägga till getter- och sättar-metoder för e-postadressen:

class Mottagare:\n    def __init__(self, namn: str, epost: str):\n        self.__namn = namn\n        if self.__granska_epost(epost):\n            self.__epost = epost\n        else:\n            raise ValueError(\"Eposten duger inte\")\n\n    def __granska_epost(self, epost: str):\n        # En enkel granksning: adressen måste vara över 5 symboler lång och innehålla en punkt och en @-symbol\n        return len(epost) > 5 and \".\" in epost and \"@\" in epost\n\n    @property\n    def epost(self):\n        return self.__epost\n\n    @epost.setter\n    def epost(self, epost: str):\n        if self.__granska_epost(epost):\n            self.__epost = epost\n        else:\n            raise ValueError(\"Eposten duger inte\")

I följande exempel är klassen Kortlek en modell för en kortlek med 52 kort. Den innehåller hjälpmetoden __aterstall_kortlek, som skapar en ny blandad kortlek. Den privata metoden anropas för närvarande endast i konstruktörsmetoden, så implementationen skulle kunna placeras direkt i konstruktören. Att använda en separat metod gör dock koden mer lättläst och gör det också möjligt att komma åt funktionaliteten senare i andra metoder om det behövs.

from random import shuffle\n\nclass Kortlek:\n    def __init__(self):\n        self.__aterstall_kortlek()\n\n    def __aterstall_kortlek(self):\n        self.__packe = []\n        # Vi lägger alla korten i packen\n        sviter = [\"spader\", \"hjarter\", \"klover\", \"ruter\"]\n        for svit in sviter:\n            for nummer in range(1, 14):\n                self.__packe.append((svit, nummer))\n        # Blanda packen\n        shuffle(self.__packe)\n\n    def dela(self, kortens_antal: int):\n        hand = []\n        # Flytta korten på toppen av packen till handen\n        for i in range(kortens_antal):\n            hand.append(self.__packe.pop())\n        return hand

Låt oss testa klassen:

kortlek = Kortlek()\nhand1 = kortlek.dela(5)\nprint(hand1)\nhand2 = kortlek.dela(5)\nprint(hand2)

Eftersom händerna är slumpmässigt genererade, är följande endast ett exempel av det som kunde utskrivas:

[('spader', 7), ('spader', 11), ('hjarter', 7), ('ruter', 3), ('spader', 4)]\n[('klover', 8), ('spader', 12), ('ruter', 13), ('klover', 11), ('spader', 10)]

Privata metoder är i allmänhet mindre vanliga än privata attribut. En tumregel är att en metod ska döljas när klienten inte har något behov av att direkt komma åt den. Detta är särskilt fallet när det är möjligt att klienten kan påverka objektets integritet negativt genom att anropa metoden.

Skapa en klass Bankkonto, som modellerar ett bankkonto. Klassen ska innehålla

    \n
  • en konstruktor, som får som argument namnet på ägaren (str), kontonumret (str) och saldot (float)
  • \n
  • metoden insattning(summa: float), som sätter till pengar på kontot
  • \n
  • metoden uttag(summa: float), som tar ut pengar från kontot
  • \n
  • gettermetoden saldo, som returnerar kontots saldo
  • \n

Klassen ska också innehålla en privat metod

    \n
  • __service_avgift(), som minskar saldot på kontot med en procent. När någon av metoderna insattning eller uttag anropas bör även denna metod anropas. Serviceavgiften beräknas och subtraheras först efter att den faktiska åtgärden har slutförts (det vill säga efter att det angivna beloppet har lagts till eller subtraherats från saldot).
  • \n

Alla dataattribut inom klassdefinitionen ska vara privata.

Exempel på användning av klassen:

konto = Bankkonto(\"Ragnar Rikedom\", \"12345-6789\", 1000)\nkonto.uttag(100)\nprint(konto.saldo)\nkonto.tillsatt(100)\nprint(konto.saldo)\n

891.0\n981.09

","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/4-metodernas-rackvidd.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur du kan begränsa synligheten för en metod i Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna skriva privata metoder"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"De metoder som definieras inom en klass kan döljas på exakt samma sätt som attributen i föregående avsnitt. Om metoden börjar med två understreck "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":" är den inte direkt åtkomlig för klienten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tekniken är alltså densamma för både metoder och attribut, men användningsfallen är oftast lite annorlunda. Privata attribut kommer ofta tillsammans med getter- och sättar-metoder för att kontrollera åtkomsten till dem. Privata metoder å andra sidan är vanligtvis endast avsedda för internt bruk, som hjälpmetoder för processer som klienten inte behöver känna till."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En privat metod kan användas inom klassen precis som vilken annan metod som helst, men man måste naturligtvis komma ihåg att inkludera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"-prefixet. Följande är en enkel klass som representerar mottagaren av e-postbrev. Den innehåller en privat hjälpmetod för att kontrollera att e-postadressen är i ett giltigt format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Mottagare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__granska_epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eposten duger inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__granska_epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En enkel granksning: adressen måste vara över 5 symboler lång och innehålla en punkt och en @-symbol"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" epost "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" epost"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Försök att kalla den privata metoden direkt orsakar ett fel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"petter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Mottagare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Keinonen\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"petter@example.com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npetter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__granska_epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"naganannan@example.com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'Mottagare' object has no attribute '__granska_epost'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Inom klassen kan metoden användas på normalt sätt, och det är vettigt att använda den även för att ange ett nytt värde för adressen. Låt oss lägga till getter- och sättar-metoder för e-postadressen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Mottagare"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__granska_epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eposten duger inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__granska_epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# En enkel granksning: adressen måste vara över 5 symboler lång och innehålla en punkt och en @-symbol"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" epost "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" epost\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__epost\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"epost"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__granska_epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"epost"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__epost "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" epost\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eposten duger inte\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel är klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Kortlek"}]},{"type":"text","value":" en modell för en kortlek med 52 kort. Den innehåller hjälpmetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__aterstall_kortlek"}]},{"type":"text","value":", som skapar en ny blandad kortlek. Den privata metoden anropas för närvarande endast i konstruktörsmetoden, så implementationen skulle kunna placeras direkt i konstruktören. Att använda en separat metod gör dock koden mer lättläst och gör det också möjligt att komma åt funktionaliteten senare i andra metoder om det behövs."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Kortlek"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__aterstall_kortlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__aterstall_kortlek"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__packe "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi lägger alla korten i packen"}]},{"type":"text","value":"\n sviter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"spader\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hjarter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"klover\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ruter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" svit "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sviter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" nummer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__packe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"svit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Blanda packen"}]},{"type":"text","value":"\n shuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__packe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"dela"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kortens_antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hand "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Flytta korten på toppen av packen till handen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" i "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kortens_antal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n hand"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__packe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" hand"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss testa klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kortlek "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Kortlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhand1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kortlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hand1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhand2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kortlek"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dela"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"hand2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom händerna är slumpmässigt genererade, är följande endast ett exempel av det som kunde utskrivas:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[('spader', 7), ('spader', 11), ('hjarter', 7), ('ruter', 3), ('spader', 4)]\n[('klover', 8), ('spader', 12), ('ruter', 13), ('klover', 11), ('spader', 10)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Privata metoder är i allmänhet mindre vanliga än privata attribut. En tumregel är att en metod ska döljas när klienten inte har något behov av att direkt komma åt den. Detta är särskilt fallet när det är möjligt att klienten kan påverka objektets integritet negativt genom att anropa metoden."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Service avgift","tmcname":"osa09-12_service_avgift"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bankkonto"}]},{"type":"text","value":", som modellerar ett bankkonto. Klassen ska innehålla"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en konstruktor, som får som argument namnet på ägaren (str), kontonumret (str) och saldot (float)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insattning(summa: float)"}]},{"type":"text","value":", som sätter till pengar på kontot"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttag(summa: float)"}]},{"type":"text","value":", som tar ut pengar från kontot"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"gettermetoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"saldo"}]},{"type":"text","value":", som returnerar kontots saldo"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska också innehålla en privat metod"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__service_avgift()"}]},{"type":"text","value":", som minskar saldot på kontot med en procent. När någon av metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insattning"}]},{"type":"text","value":" eller "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"uttag"}]},{"type":"text","value":" anropas bör även denna metod anropas. Serviceavgiften beräknas och subtraheras först efter att den faktiska åtgärden har slutförts (det vill säga efter att det angivna beloppet har lagts till eller subtraherats från saldot)."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Alla dataattribut inom klassdefinitionen ska vara privata."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exempel på användning av klassen:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"konto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bankkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ragnar Rikedom\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-6789\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"uttag"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"891.0\n981.09"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Vet du hur du kan begränsa synligheten för en metod i Python
  • \n
  • Kommer du att kunna skriva privata metoder
  • \n

De metoder som definieras inom en klass kan döljas på exakt samma sätt som attributen i föregående avsnitt. Om metoden börjar med två understreck __ är den inte direkt åtkomlig för klienten.

Tekniken är alltså densamma för både metoder och attribut, men användningsfallen är oftast lite annorlunda. Privata attribut kommer ofta tillsammans med getter- och sättar-metoder för att kontrollera åtkomsten till dem. Privata metoder å andra sidan är vanligtvis endast avsedda för internt bruk, som hjälpmetoder för processer som klienten inte behöver känna till.

En privat metod kan användas inom klassen precis som vilken annan metod som helst, men man måste naturligtvis komma ihåg att inkludera self-prefixet. Följande är en enkel klass som representerar mottagaren av e-postbrev. Den innehåller en privat hjälpmetod för att kontrollera att e-postadressen är i ett giltigt format:

class Mottagare:\n    def __init__(self, namn: str, epost: str):\n        self.__namn = namn\n        if self.__granska_epost(epost):\n            self.__epost = epost\n        else:\n            raise ValueError(\"Eposten duger inte\")\n\n    def __granska_epost(self, epost: str):\n        # En enkel granksning: adressen måste vara över 5 symboler lång och innehålla en punkt och en @-symbol\n        return len(epost) > 5 and \".\" in epost and \"@\" in epost

Försök att kalla den privata metoden direkt orsakar ett fel:

petter = Mottagare(\"Petter Keinonen\", \"petter@example.com\")\npetter.__granska_epost(\"naganannan@example.com\")

AttributeError: 'Mottagare' object has no attribute '__granska_epost'

Inom klassen kan metoden användas på normalt sätt, och det är vettigt att använda den även för att ange ett nytt värde för adressen. Låt oss lägga till getter- och sättar-metoder för e-postadressen:

class Mottagare:\n    def __init__(self, namn: str, epost: str):\n        self.__namn = namn\n        if self.__granska_epost(epost):\n            self.__epost = epost\n        else:\n            raise ValueError(\"Eposten duger inte\")\n\n    def __granska_epost(self, epost: str):\n        # En enkel granksning: adressen måste vara över 5 symboler lång och innehålla en punkt och en @-symbol\n        return len(epost) > 5 and \".\" in epost and \"@\" in epost\n\n    @property\n    def epost(self):\n        return self.__epost\n\n    @epost.setter\n    def epost(self, epost: str):\n        if self.__granska_epost(epost):\n            self.__epost = epost\n        else:\n            raise ValueError(\"Eposten duger inte\")

I följande exempel är klassen Kortlek en modell för en kortlek med 52 kort. Den innehåller hjälpmetoden __aterstall_kortlek, som skapar en ny blandad kortlek. Den privata metoden anropas för närvarande endast i konstruktörsmetoden, så implementationen skulle kunna placeras direkt i konstruktören. Att använda en separat metod gör dock koden mer lättläst och gör det också möjligt att komma åt funktionaliteten senare i andra metoder om det behövs.

from random import shuffle\n\nclass Kortlek:\n    def __init__(self):\n        self.__aterstall_kortlek()\n\n    def __aterstall_kortlek(self):\n        self.__packe = []\n        # Vi lägger alla korten i packen\n        sviter = [\"spader\", \"hjarter\", \"klover\", \"ruter\"]\n        for svit in sviter:\n            for nummer in range(1, 14):\n                self.__packe.append((svit, nummer))\n        # Blanda packen\n        shuffle(self.__packe)\n\n    def dela(self, kortens_antal: int):\n        hand = []\n        # Flytta korten på toppen av packen till handen\n        for i in range(kortens_antal):\n            hand.append(self.__packe.pop())\n        return hand

Låt oss testa klassen:

kortlek = Kortlek()\nhand1 = kortlek.dela(5)\nprint(hand1)\nhand2 = kortlek.dela(5)\nprint(hand2)

Eftersom händerna är slumpmässigt genererade, är följande endast ett exempel av det som kunde utskrivas:

[('spader', 7), ('spader', 11), ('hjarter', 7), ('ruter', 3), ('spader', 4)]\n[('klover', 8), ('spader', 12), ('ruter', 13), ('klover', 11), ('spader', 10)]

Privata metoder är i allmänhet mindre vanliga än privata attribut. En tumregel är att en metod ska döljas när klienten inte har något behov av att direkt komma åt den. Detta är särskilt fallet när det är möjligt att klienten kan påverka objektets integritet negativt genom att anropa metoden.

Skapa en klass Bankkonto, som modellerar ett bankkonto. Klassen ska innehålla

    \n
  • en konstruktor, som får som argument namnet på ägaren (str), kontonumret (str) och saldot (float)
  • \n
  • metoden insattning(summa: float), som sätter till pengar på kontot
  • \n
  • metoden uttag(summa: float), som tar ut pengar från kontot
  • \n
  • gettermetoden saldo, som returnerar kontots saldo
  • \n

Klassen ska också innehålla en privat metod

    \n
  • __service_avgift(), som minskar saldot på kontot med en procent. När någon av metoderna insattning eller uttag anropas bör även denna metod anropas. Serviceavgiften beräknas och subtraheras först efter att den faktiska åtgärden har slutförts (det vill säga efter att det angivna beloppet har lagts till eller subtraherats från saldot).
  • \n

Alla dataattribut inom klassdefinitionen ska vara privata.

Exempel på användning av klassen:

konto = Bankkonto(\"Ragnar Rikedom\", \"12345-6789\", 1000)\nkonto.uttag(100)\nprint(konto.saldo)\nkonto.tillsatt(100)\nprint(konto.saldo)\n

891.0\n981.09

","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/4-metodernas-rackvidd.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-9/5-klassattribut/page-data.json b/page-data/osa-9/5-klassattribut/page-data.json index 0e2888a75..44779d60f 100644 --- a/page-data/osa-9/5-klassattribut/page-data.json +++ b/page-data/osa-9/5-klassattribut/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-9/5-klassattribut", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är du bekant med begreppen klassvariabel och klassmetod"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur statiska egenskaper skiljer sig från egenskaper hos instanser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna lägga till statiska egenskaper i dina egna klasser"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objektens egenskaper är ett centralt begrepp inom objektorienterad programmering. Termen egenskaper (eng. traits) omfattar de metoder och variabler som definieras i klassdefinitionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi mest behandlat egenskaper hos objekt. Dessa inkluderar de metoder och attribut som är tillgängliga i alla instanser av en klass. Faktum är att klasser i sig också kan ha egenskaper, som ibland kallas statiska egenskaper, eller mer specifikt klassvariabler och klassmetoder."}]},{"type":"element","tagName":"h2","properties":{"id":"klassvariabler","style":"position:relative;"},"children":[{"type":"text","value":"Klassvariabler"},{"type":"element","tagName":"a","properties":{"href":"#klassvariabler","ariaLabel":"klassvariabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje instans av en klass har sina egna specifika värden för varje attribut som definieras i klassen, som vi har sett i exemplen i de tidigare avsnitten. Men vad händer om vi vill ha data som delas mellan de olika instanserna? Här kommer klassvariablerna in i bilden, även kallade statiska variabler. En klassvariabel är en variabel som nås via själva klassen, inte via de instanser som skapas baserat på klassen. Vid varje given tidpunkt under exekveringen av programmet har en klassvariabel ett enda värde, oavsett hur många instanser av klassen som skapas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassvariabel deklareras utan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"-prefixet och vanligtvis utanför varje metoddefinition eftersom den ska kunna nås var som helst inom klassen eller till och med utanför klassen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Sparkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.03"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Räntan är allmänna räntan + kontots ränta"}]},{"type":"text","value":"\n ranta_totalt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" ranta_totalt\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"allman_ranta"}]},{"type":"text","value":" definieras inom klassen men utanför alla metoddefinitioner och inte använder prefixet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":", är det en klassvariabel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassvariabel nås via klassens namn, till exempel så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den allmänna räntan är oberoende av andra instanser av objektet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Allmänna räntan är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkonto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.05"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Lägg till totala räntant till kontots saldo"}]},{"type":"text","value":"\nkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Allmänna räntan är 0.03\n1080.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassvariablerna nås alltså via klassens namn, till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sparkonto.allman_ranta"}]},{"type":"text","value":", medan instansvariablerna nås via objektvariabelns namn, till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"konto.saldo"}]},{"type":"text","value":". En instansvariabel existerar naturligtvis bara när en instans av klassen har skapats, men en klassvariabel är tillgänglig överallt och vid varje tidpunkt då klassen i sig är tillgänglig."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassvariabler är användbara när det finns behov av värden som delas av alla instanser av klassen. I exemplet ovan antog vi att den totala räntan för alla sparkonton består av två komponenter: den allmänna räntan delas av alla konton, men varje konto har också sin egen ränta i en instansvariabel. Den allmänna räntan kan också förändras, men förändringen kommer då att påverka alla instanser av klassen lika mycket."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Sparkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.03"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Räntan är allmänna räntan + kontots ränta"}]},{"type":"text","value":"\n ranta_totalt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" ranta_totalt\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"totalranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"konto1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.03"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkonto2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.06"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Allmänna räntan:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi ökar den allmänna räntan till 10 procent"}]},{"type":"text","value":"\nSparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.10"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Allmänna räntan:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Allmänna räntan: 0.03\n0.06\n0.09\nAllmänna räntan: 0.1\n0.13\n0.16"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När den allmänna räntan ändras, ändras den totala räntan för alla instanser av klassen. Som du kan se ovan är det möjligt att lägga till en getter-metod med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"-dekoratorn även om det inte finns ett attribut med samma namn i klassen. Denna metod returnerar summan av den allmänna räntan och den kontospecifika räntan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ett annat exempel. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonnummer"}]},{"type":"text","value":" används för att definiera ett enda telefonnummer, men den innehåller också några landskoder i en ordbok. Denna ordbok är en klassvariabel och delas därför av alla instanser av klassen, eftersom landskoden för telefonnummer från ett och samma land alltid är densamma."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n landkoder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+358\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+46\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"USA\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" telefonnummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" land\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den första nollan tas bort då landskoden läggs till"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"landkoder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"paulan_nro "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"050 1234 567\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"paulan_nro"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"+358 50 1234 567"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje Telefonnummer-objekt innehåller namnet på ägaren, själva numret och det land där telefonnumret finns. När attributet som innehåller telefonnumret nås med getter-metoden hämtas lämplig landskod från klassvariabelns ordbok baserat på landattributet, och resultatet prefixeras till numret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementeringsexemplet ovan är inte särskilt funktionellt i övrigt. I följande exempel har vi lagt till getter och sättare för alla attribut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n landkoder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+358\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+46\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"USA\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Det här kallar metoden telefonnummer.setter"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"telefonnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" telefonnummer\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Det här kallar metoden land.setter"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" land\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Getter-metoden för telefonnummer kombinerar landskoden och attributet telefonnummer"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den första nollan tas bort då landskoden läggs till"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"landkoder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi granksar att numret endast innehåller siffror och mellanslag"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234567890 \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Telefonnummer får innehålla endast siffror och mellanslag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Endast telefonnummer utan landskod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"lokalt_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"land"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"land"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi granskar att landet är en nyckel i ordlistan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"landkoder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det angivna landet hittades inte på listan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" land\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n tlfnnmr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"040 111 1111\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tlfnnmr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tlfnnmr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tlfnnmr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lokalt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"+46 40 111 1111 (Petter Python)\n+46 40 111 1111\n040 111 1111"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Postnummer","tmcname":"osa09-13_postnummer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stad"}]},{"type":"text","value":" definierad, som modellerar en enskild stad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tillsätt en klassvariabel med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"postnummer"}]},{"type":"text","value":", som refererar till en ordlista. I ordlistan används städernas namn som nycklar, och värdena som fås är städernas postnummer. Båda är strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I ordlistan ska åtminstone följande postnummer hittas:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Helsingfors 00100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Åbo 20100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Tammerfors 33100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Jyväskylä 40100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Uleåborg 90100"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Annan funktionalitet behövs inte implementeras."}]}]},{"type":"element","tagName":"h2","properties":{"id":"klassmetoder","style":"position:relative;"},"children":[{"type":"text","value":"Klassmetoder"},{"type":"element","tagName":"a","properties":{"href":"#klassmetoder","ariaLabel":"klassmetoder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassmetod, även kallad statisk metod, är en metod som inte är knuten till någon enskild instans av klassen. En klassmetod kan anropas utan att några instanser av klassen skapas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassmetoder är vanligen verktyg som har något att göra med klassens syfte, men som är fristående i den meningen att det inte ska vara nödvändigt att skapa instanser av klassen för att kunna anropa dem. Klassmetoder är vanligtvis offentliga, så att de kan anropas både utanför klassen och inom klassen, inklusive inom instanser av klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassmetod definieras med annotationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":". Den första parametern är alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":". Variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":" liknar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"-parametern. Skillnaden är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":" pekar på klassen medan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" pekar på en instans av klassen. Ingen av parametrarna ingår i argumentlistan när funktionen anropas, utan Python fyller i lämpligt värde automatiskt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel har vi en klass som modellerar fordonsregistreringar. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Registration"}]},{"type":"text","value":" innehåller en statisk metod för att kontrollera om en registernummer är giltig. Metoden är en statisk klassmetod eftersom det är användbart att kunna kontrollera om en registernummer är giltig redan innan ett enda Registration-objekt har skapats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Registration"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" marke"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" registernummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__marke "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" marke\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi anropar metoden registernummer.setter"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"registernummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" registernummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"registernummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__registernummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@registernummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"registernummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"registernummer_giltigt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__registernummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Registernummer inte giltigt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Klassmetod som validerar registernumret"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"registernummer_giltigt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Granska början och slutet av numret skilt"}]},{"type":"text","value":"\n borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" slutet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Börjandelen får endast innehålla bokstäver"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" marke"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lower"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefghijklmnopqrstuvwxyzåäö\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Slutdelen får endast innehålla siffror"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" slutet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234567890\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"reg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bertil Bilist\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Volvo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1992\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abc-123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"registernummer_giltigt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"xyz-789\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Detta är ett giltigt nummer!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta är ett giltigt nummer!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Giltigheten för en registernummer kan kontrolleras även utan att skapa en enda instans av klassen, t.ex. med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Registration.nummer_plat_giltig(\"xyz-789\")"}]},{"type":"text","value":". Samma metod anropas i klassens konstruktor. OBS: även inom konstruktören är denna metod åtkomlig via klassens namn, inte "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List-hjälpare","tmcname":"osa09-14_listhjalpare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ListHjalpare"}]},{"type":"text","value":" som innehåller följande två klassmetoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storsta_frekvensen(lista: list)"}]},{"type":"text","value":" returnerar det mest förekommande föremålet på listan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dubbletter(lista: list)"}]},{"type":"text","value":" returnerar antalet unika tal som förekommer åtminstone två gånger på listan"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det ska vara möjligt att använda dessa metoder utan att skapa en instans av klassen. Ett exempel på hur metoderna kan användas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ListHjalpare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"storsta_frekvensen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ListHjalpare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dubbletter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Är du bekant med begreppen klassvariabel och klassmetod
  • \n
  • Vet du hur statiska egenskaper skiljer sig från egenskaper hos instanser
  • \n
  • Kommer du att kunna lägga till statiska egenskaper i dina egna klasser
  • \n

Objektens egenskaper är ett centralt begrepp inom objektorienterad programmering. Termen egenskaper (eng. traits) omfattar de metoder och variabler som definieras i klassdefinitionen.

Hittills har vi mest behandlat egenskaper hos objekt. Dessa inkluderar de metoder och attribut som är tillgängliga i alla instanser av en klass. Faktum är att klasser i sig också kan ha egenskaper, som ibland kallas statiska egenskaper, eller mer specifikt klassvariabler och klassmetoder.

Klassvariabler

Varje instans av en klass har sina egna specifika värden för varje attribut som definieras i klassen, som vi har sett i exemplen i de tidigare avsnitten. Men vad händer om vi vill ha data som delas mellan de olika instanserna? Här kommer klassvariablerna in i bilden, även kallade statiska variabler. En klassvariabel är en variabel som nås via själva klassen, inte via de instanser som skapas baserat på klassen. Vid varje given tidpunkt under exekveringen av programmet har en klassvariabel ett enda värde, oavsett hur många instanser av klassen som skapas.

En klassvariabel deklareras utan self-prefixet och vanligtvis utanför varje metoddefinition eftersom den ska kunna nås var som helst inom klassen eller till och med utanför klassen.

class Sparkonto:\n    allman_ranta = 0.03\n\n    def __init__(self, kontonummer: str, saldo: float, ranta: float):\n        self.__kontonummer = kontonummer\n        self.__saldo = saldo\n        self.__ranta = ranta\n\n    def tillsatt_ranta(self):\n        # Räntan är allmänna räntan + kontots ränta\n        ranta_totalt = Sparkonto.allman_ranta + self.__ranta\n        self.__saldo += self.__saldo * ranta_totalt\n\n    @property\n    def saldo(self):\n        return self.__saldo

Eftersom variabeln allman_ranta definieras inom klassen men utanför alla metoddefinitioner och inte använder prefixet self, är det en klassvariabel.

En klassvariabel nås via klassens namn, till exempel så här:

# Den allmänna räntan är oberoende av andra instanser av objektet\nprint(\"Allmänna räntan är\", Sparkonto.allman_ranta)\n\nkonto = Sparkonto(\"12345\", 1000, 0.05)\n# Lägg till totala räntant till kontots saldo\nkonto.tillsatt_ranta()\nprint(konto.saldo)

Allmänna räntan är 0.03\n1080.0

Klassvariablerna nås alltså via klassens namn, till exempel Sparkonto.allman_ranta, medan instansvariablerna nås via objektvariabelns namn, till exempel konto.saldo. En instansvariabel existerar naturligtvis bara när en instans av klassen har skapats, men en klassvariabel är tillgänglig överallt och vid varje tidpunkt då klassen i sig är tillgänglig.

Klassvariabler är användbara när det finns behov av värden som delas av alla instanser av klassen. I exemplet ovan antog vi att den totala räntan för alla sparkonton består av två komponenter: den allmänna räntan delas av alla konton, men varje konto har också sin egen ränta i en instansvariabel. Den allmänna räntan kan också förändras, men förändringen kommer då att påverka alla instanser av klassen lika mycket.

class Sparkonto:\n    allman_ranta = 0.03\n\n    def __init__(self, kontonummer: str, saldo: float, ranta: float):\n        self.__kontonummer = kontonummer\n        self.__saldo = saldo\n        self.__ranta = ranta\n\n    def tillsatt_ranta(self):\n        # Räntan är allmänna räntan + kontots ränta\n        ranta_totalt = Sparkonto.allman_ranta + self.__ranta\n        self.__saldo += self.__saldo * ranta_totalt\n\n    @property\n    def saldo(self):\n        return self.__saldo\n\n    @property\n    def totalranta(self):\n        return self.__ranta + Sparkonto.allman_ranta
konto1 = Sparkonto(\"12345\", 100, 0.03)\nkonto2 = Sparkonto(\"54321\", 200, 0.06)\n\nprint(\"Allmänna räntan:\", Sparkonto.allman_ranta)\nprint(konto1.totalranta)\nprint(konto2.totalranta)\n\n# Vi ökar den allmänna räntan till 10 procent\nSparkonto.allman_ranta = 0.10\n\nprint(\"Allmänna räntan:\", Sparkonto.allman_ranta)\nprint(konto1.totalranta)\nprint(konto2.totalranta)

Allmänna räntan: 0.03\n0.06\n0.09\nAllmänna räntan: 0.1\n0.13\n0.16

När den allmänna räntan ändras, ändras den totala räntan för alla instanser av klassen. Som du kan se ovan är det möjligt att lägga till en getter-metod med @property-dekoratorn även om det inte finns ett attribut med samma namn i klassen. Denna metod returnerar summan av den allmänna räntan och den kontospecifika räntan.

Låt oss ta en titt på ett annat exempel. Klassen Telefonnummer används för att definiera ett enda telefonnummer, men den innehåller också några landskoder i en ordbok. Denna ordbok är en klassvariabel och delas därför av alla instanser av klassen, eftersom landskoden för telefonnummer från ett och samma land alltid är densamma.

class Telefonnummer:\n    landkoder = {\"Finland\": \"+358\", \"Sverige\": \"+46\", \"USA\": \"+1\"}\n\n    def __init__(self, namn: str, telefonnummer: str, land: str):\n        self.__namn = namn\n        self.__telefonnummer = telefonnummer\n        self.__land = land\n\n    @property\n    def telefonnummer(self):\n        # Den första nollan tas bort då landskoden läggs till\n        return Telefonnummer.landkoder[self.__land] + \" \" + self.__telefonnummer[1:]
paulan_nro = Telefonnummer(\"Pernilla Pythonson\", \"050 1234 567\", \"Finland\")\nprint(paulan_nro.telefonnummer)

+358 50 1234 567

Varje Telefonnummer-objekt innehåller namnet på ägaren, själva numret och det land där telefonnumret finns. När attributet som innehåller telefonnumret nås med getter-metoden hämtas lämplig landskod från klassvariabelns ordbok baserat på landattributet, och resultatet prefixeras till numret.

Implementeringsexemplet ovan är inte särskilt funktionellt i övrigt. I följande exempel har vi lagt till getter och sättare för alla attribut:

class Telefonnummer:\n    landkoder = {\"Finland\": \"+358\", \"Sverige\": \"+46\", \"USA\": \"+1\"}\n\n    def __init__(self, namn: str, telefonnummer: str, land: str):\n        self.__namn = namn\n        # Det här kallar metoden telefonnummer.setter\n        self.telefonnummer = telefonnummer\n        # Det här kallar metoden land.setter\n        self.land = land\n\n    # Getter-metoden för telefonnummer kombinerar landskoden och attributet telefonnummer\n    @property\n    def telefonnummer(self):\n        # Den första nollan tas bort då landskoden läggs till\n        return Telefonnummer.landkoder[self.__land] + \" \" + self.__telefonnummer[1:]\n\n    @telefonnummer.setter\n    def telefonnummer(self, nummer):\n        # Vi granksar att numret endast innehåller siffror och mellanslag\n        for marke in nummer:\n            if marke not in \"1234567890 \":\n                raise ValueError(\"Telefonnummer får innehålla endast siffror och mellanslag\")\n        self.__telefonnummer = nummer\n\n    # Endast telefonnummer utan landskod\n    @property\n    def lokalt_nummer(self):\n        return self.__telefonnummer\n\n    @property\n    def land(self):\n        return self.__land\n\n    @land.setter\n    def land(self, land):\n        # Vi granskar att landet är en nyckel i ordlistan\n        if land not in Telefonnummer.landkoder:\n            raise ValueError(\"Det angivna landet hittades inte på listan\")\n        self.__land = land\n\n    @property\n    def namn(self):\n        return self.__namn\n\n    @namn.setter\n    def namn(self, namn):\n        self.__namn = namn\n\n    def __str__(self):\n        return f\"{self.telefonnummer} ({self.__namn})\"
if __name__ == \"__main__\":\n    tlfnnmr = Telefonnummer(\"Petter Python\", \"040 111 1111\", \"Sverige\")\n    print(tlfnnmr)\n    print(tlfnnmr.telefonnummer)\n    print(tlfnnmr.lokalt_nummer)

+46 40 111 1111 (Petter Python)\n+46 40 111 1111\n040 111 1111

I uppgiftsbotten finns klassen Stad definierad, som modellerar en enskild stad.

Tillsätt en klassvariabel med namnet postnummer, som refererar till en ordlista. I ordlistan används städernas namn som nycklar, och värdena som fås är städernas postnummer. Båda är strängar.

I ordlistan ska åtminstone följande postnummer hittas:

    \n
  • Helsingfors 00100
  • \n
  • Åbo 20100
  • \n
  • Tammerfors 33100
  • \n
  • Jyväskylä 40100
  • \n
  • Uleåborg 90100
  • \n

Annan funktionalitet behövs inte implementeras.

Klassmetoder

En klassmetod, även kallad statisk metod, är en metod som inte är knuten till någon enskild instans av klassen. En klassmetod kan anropas utan att några instanser av klassen skapas.

Klassmetoder är vanligen verktyg som har något att göra med klassens syfte, men som är fristående i den meningen att det inte ska vara nödvändigt att skapa instanser av klassen för att kunna anropa dem. Klassmetoder är vanligtvis offentliga, så att de kan anropas både utanför klassen och inom klassen, inklusive inom instanser av klassen.

En klassmetod definieras med annotationen @classmethod. Den första parametern är alltid cls. Variabelnamnet cls liknar self-parametern. Skillnaden är att cls pekar på klassen medan self pekar på en instans av klassen. Ingen av parametrarna ingår i argumentlistan när funktionen anropas, utan Python fyller i lämpligt värde automatiskt.

I följande exempel har vi en klass som modellerar fordonsregistreringar. Klassen Registration innehåller en statisk metod för att kontrollera om en registernummer är giltig. Metoden är en statisk klassmetod eftersom det är användbart att kunna kontrollera om en registernummer är giltig redan innan ett enda Registration-objekt har skapats:

class Registration:\n    def __init__(self, agare: str, marke: str, ar: int, registernummer: str):\n        self.__agare = agare\n        self.__marke = marke\n        self.__ar = ar\n\n        # Vi anropar metoden registernummer.setter\n        self.registernummer = registernummer\n\n    @property\n    def registernummer(self):\n        return self.__registernummer\n\n    @registernummer.setter\n    def registernummer(self, nummer):\n        if Registration.registernummer_giltigt(nummer):\n            self.__registernummer = nummer\n        else:\n            raise ValueError(\"Registernummer inte giltigt\")\n\n    # Klassmetod som validerar registernumret\n    @classmethod\n    def registernummer_giltigt(cls, nummer: str):\n        if len(nummer) < 3 or \"-\" not in nummer:\n            return False\n\n        # Granska början och slutet av numret skilt\n        borjan, slutet = nummer.split(\"-\")\n\n        # Börjandelen får endast innehålla bokstäver\n        for marke in borjan:\n            if marke.lower() not in \"abcdefghijklmnopqrstuvwxyzåäö\":\n                return False\n\n        # Slutdelen får endast innehålla siffror\n        for marke in slutet:\n            if marke not in \"1234567890\":\n                return False\n\n        return True
reg = Registration(\"Bertil Bilist\", \"Volvo\", \"1992\", \"abc-123\")\n\nif Registration.registernummer_giltigt(\"xyz-789\"):\n    print(\"Detta är ett giltigt nummer!\")

Detta är ett giltigt nummer!

Giltigheten för en registernummer kan kontrolleras även utan att skapa en enda instans av klassen, t.ex. med Registration.nummer_plat_giltig(\"xyz-789\"). Samma metod anropas i klassens konstruktor. OBS: även inom konstruktören är denna metod åtkomlig via klassens namn, inte self!

Skapa en klass med namnet ListHjalpare som innehåller följande två klassmetoder:

    \n
  • Metoden storsta_frekvensen(lista: list) returnerar det mest förekommande föremålet på listan
  • \n
  • Metoden dubbletter(lista: list) returnerar antalet unika tal som förekommer åtminstone två gånger på listan
  • \n

Det ska vara möjligt att använda dessa metoder utan att skapa en instans av klassen. Ett exempel på hur metoderna kan användas:

tal = [1, 1, 2, 1, 3, 3, 4, 5, 5, 5, 6, 5, 5, 5]\nprint(ListHjalpare.storsta_frekvensen(tal))\nprint(ListHjalpare.dubbletter(tal))

5\n3

","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/5-klassattribut.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är du bekant med begreppen klassvariabel och klassmetod"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Vet du hur statiska egenskaper skiljer sig från egenskaper hos instanser"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kommer du att kunna lägga till statiska egenskaper i dina egna klasser"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Objektens egenskaper är ett centralt begrepp inom objektorienterad programmering. Termen egenskaper (eng. traits) omfattar de metoder och variabler som definieras i klassdefinitionen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hittills har vi mest behandlat egenskaper hos objekt. Dessa inkluderar de metoder och attribut som är tillgängliga i alla instanser av en klass. Faktum är att klasser i sig också kan ha egenskaper, som ibland kallas statiska egenskaper, eller mer specifikt klassvariabler och klassmetoder."}]},{"type":"element","tagName":"h2","properties":{"id":"klassvariabler","style":"position:relative;"},"children":[{"type":"text","value":"Klassvariabler"},{"type":"element","tagName":"a","properties":{"href":"#klassvariabler","ariaLabel":"klassvariabler permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje instans av en klass har sina egna specifika värden för varje attribut som definieras i klassen, som vi har sett i exemplen i de tidigare avsnitten. Men vad händer om vi vill ha data som delas mellan de olika instanserna? Här kommer klassvariablerna in i bilden, även kallade statiska variabler. En klassvariabel är en variabel som nås via själva klassen, inte via de instanser som skapas baserat på klassen. Vid varje given tidpunkt under exekveringen av programmet har en klassvariabel ett enda värde, oavsett hur många instanser av klassen som skapas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassvariabel deklareras utan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"-prefixet och vanligtvis utanför varje metoddefinition eftersom den ska kunna nås var som helst inom klassen eller till och med utanför klassen."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Sparkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.03"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Räntan är allmänna räntan + kontots ränta"}]},{"type":"text","value":"\n ranta_totalt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" ranta_totalt\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eftersom variabeln "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"allman_ranta"}]},{"type":"text","value":" definieras inom klassen men utanför alla metoddefinitioner och inte använder prefixet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":", är det en klassvariabel."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassvariabel nås via klassens namn, till exempel så här:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den allmänna räntan är oberoende av andra instanser av objektet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Allmänna räntan är\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nkonto "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.05"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Lägg till totala räntant till kontots saldo"}]},{"type":"text","value":"\nkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Allmänna räntan är 0.03\n1080.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassvariablerna nås alltså via klassens namn, till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sparkonto.allman_ranta"}]},{"type":"text","value":", medan instansvariablerna nås via objektvariabelns namn, till exempel "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"konto.saldo"}]},{"type":"text","value":". En instansvariabel existerar naturligtvis bara när en instans av klassen har skapats, men en klassvariabel är tillgänglig överallt och vid varje tidpunkt då klassen i sig är tillgänglig."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassvariabler är användbara när det finns behov av värden som delas av alla instanser av klassen. I exemplet ovan antog vi att den totala räntan för alla sparkonton består av två komponenter: den allmänna räntan delas av alla konton, men varje konto har också sin egen ränta i en instansvariabel. Den allmänna räntan kan också förändras, men förändringen kommer då att påverka alla instanser av klassen lika mycket."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Sparkonto"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.03"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kontonummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" saldo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__kontonummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" kontonummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" saldo\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ranta\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_ranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Räntan är allmänna räntan + kontots ränta"}]},{"type":"text","value":"\n ranta_totalt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" ranta_totalt\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"saldo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__saldo\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"totalranta"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta"}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"konto1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.03"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nkonto2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.06"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Allmänna räntan:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi ökar den allmänna räntan till 10 procent"}]},{"type":"text","value":"\nSparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.10"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Allmänna räntan:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Sparkonto"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"allman_ranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"konto2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"totalranta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Allmänna räntan: 0.03\n0.06\n0.09\nAllmänna räntan: 0.1\n0.13\n0.16"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"När den allmänna räntan ändras, ändras den totala räntan för alla instanser av klassen. Som du kan se ovan är det möjligt att lägga till en getter-metod med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"-dekoratorn även om det inte finns ett attribut med samma namn i klassen. Denna metod returnerar summan av den allmänna räntan och den kontospecifika räntan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ett annat exempel. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Telefonnummer"}]},{"type":"text","value":" används för att definiera ett enda telefonnummer, men den innehåller också några landskoder i en ordbok. Denna ordbok är en klassvariabel och delas därför av alla instanser av klassen, eftersom landskoden för telefonnummer från ett och samma land alltid är densamma."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n landkoder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+358\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+46\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"USA\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" telefonnummer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" land\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den första nollan tas bort då landskoden läggs till"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"landkoder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"paulan_nro "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pernilla Pythonson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"050 1234 567\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"paulan_nro"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"+358 50 1234 567"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Varje Telefonnummer-objekt innehåller namnet på ägaren, själva numret och det land där telefonnumret finns. När attributet som innehåller telefonnumret nås med getter-metoden hämtas lämplig landskod från klassvariabelns ordbok baserat på landattributet, och resultatet prefixeras till numret."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Implementeringsexemplet ovan är inte särskilt funktionellt i övrigt. I följande exempel har vi lagt till getter och sättare för alla attribut:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n landkoder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+358\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+46\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"USA\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"+1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Det här kallar metoden telefonnummer.setter"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"telefonnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" telefonnummer\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Det här kallar metoden land.setter"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" land\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Getter-metoden för telefonnummer kombinerar landskoden och attributet telefonnummer"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den första nollan tas bort då landskoden läggs till"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"landkoder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"telefonnummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi granksar att numret endast innehåller siffror och mellanslag"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234567890 \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Telefonnummer får innehålla endast siffror och mellanslag\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Endast telefonnummer utan landskod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"lokalt_nummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__telefonnummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"land"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"land"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" land"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi granskar att landet är en nyckel i ordlistan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" land "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"landkoder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Det angivna landet hittades inte på listan\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__land "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" land\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" __name__ "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"__main__\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n tlfnnmr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Petter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"040 111 1111\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sverige\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tlfnnmr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tlfnnmr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"telefonnummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tlfnnmr"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lokalt_nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"+46 40 111 1111 (Petter Python)\n+46 40 111 1111\n040 111 1111"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Postnummer","tmcname":"osa09-13_postnummer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I uppgiftsbotten finns klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stad"}]},{"type":"text","value":" definierad, som modellerar en enskild stad."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tillsätt en klassvariabel med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"postnummer"}]},{"type":"text","value":", som refererar till en ordlista. I ordlistan används städernas namn som nycklar, och värdena som fås är städernas postnummer. Båda är strängar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I ordlistan ska åtminstone följande postnummer hittas:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Helsingfors 00100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Åbo 20100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Tammerfors 33100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Jyväskylä 40100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Uleåborg 90100"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Annan funktionalitet behövs inte implementeras."}]}]},{"type":"element","tagName":"h2","properties":{"id":"klassmetoder","style":"position:relative;"},"children":[{"type":"text","value":"Klassmetoder"},{"type":"element","tagName":"a","properties":{"href":"#klassmetoder","ariaLabel":"klassmetoder permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassmetod, även kallad statisk metod, är en metod som inte är knuten till någon enskild instans av klassen. En klassmetod kan anropas utan att några instanser av klassen skapas."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassmetoder är vanligen verktyg som har något att göra med klassens syfte, men som är fristående i den meningen att det inte ska vara nödvändigt att skapa instanser av klassen för att kunna anropa dem. Klassmetoder är vanligtvis offentliga, så att de kan anropas både utanför klassen och inom klassen, inklusive inom instanser av klassen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En klassmetod definieras med annotationen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":". Den första parametern är alltid "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":". Variabelnamnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":" liknar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"-parametern. Skillnaden är att "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":" pekar på klassen medan "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" pekar på en instans av klassen. Ingen av parametrarna ingår i argumentlistan när funktionen anropas, utan Python fyller i lämpligt värde automatiskt."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I följande exempel har vi en klass som modellerar fordonsregistreringar. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Registration"}]},{"type":"text","value":" innehåller en statisk metod för att kontrollera om en registernummer är giltig. Metoden är en statisk klassmetod eftersom det är användbart att kunna kontrollera om en registernummer är giltig redan innan ett enda Registration-objekt har skapats:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Registration"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" agare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" marke"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" registernummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__agare "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" agare\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__marke "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" marke\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__ar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Vi anropar metoden registernummer.setter"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"registernummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" registernummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"registernummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__registernummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@registernummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"registernummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"registernummer_giltigt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__registernummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Registernummer inte giltigt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Klassmetod som validerar registernumret"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"registernummer_giltigt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Granska början och slutet av numret skilt"}]},{"type":"text","value":"\n borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" slutet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" nummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Börjandelen får endast innehålla bokstäver"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" marke"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lower"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefghijklmnopqrstuvwxyzåäö\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Slutdelen får endast innehålla siffror"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" slutet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" marke "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234567890\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"reg "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bertil Bilist\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Volvo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1992\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abc-123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"registernummer_giltigt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"xyz-789\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Detta är ett giltigt nummer!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Detta är ett giltigt nummer!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Giltigheten för en registernummer kan kontrolleras även utan att skapa en enda instans av klassen, t.ex. med "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Registration.nummer_plat_giltig(\"xyz-789\")"}]},{"type":"text","value":". Samma metod anropas i klassens konstruktor. OBS: även inom konstruktören är denna metod åtkomlig via klassens namn, inte "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List-hjälpare","tmcname":"osa09-14_listhjalpare"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa en klass med namnet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ListHjalpare"}]},{"type":"text","value":" som innehåller följande två klassmetoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"storsta_frekvensen(lista: list)"}]},{"type":"text","value":" returnerar det mest förekommande föremålet på listan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dubbletter(lista: list)"}]},{"type":"text","value":" returnerar antalet unika tal som förekommer åtminstone två gånger på listan"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det ska vara möjligt att använda dessa metoder utan att skapa en instans av klassen. Ett exempel på hur metoderna kan användas:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"tal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ListHjalpare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"storsta_frekvensen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ListHjalpare"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"dubbletter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]}]}]},"html":"

Efter den här delen

    \n
  • Är du bekant med begreppen klassvariabel och klassmetod
  • \n
  • Vet du hur statiska egenskaper skiljer sig från egenskaper hos instanser
  • \n
  • Kommer du att kunna lägga till statiska egenskaper i dina egna klasser
  • \n

Objektens egenskaper är ett centralt begrepp inom objektorienterad programmering. Termen egenskaper (eng. traits) omfattar de metoder och variabler som definieras i klassdefinitionen.

Hittills har vi mest behandlat egenskaper hos objekt. Dessa inkluderar de metoder och attribut som är tillgängliga i alla instanser av en klass. Faktum är att klasser i sig också kan ha egenskaper, som ibland kallas statiska egenskaper, eller mer specifikt klassvariabler och klassmetoder.

Klassvariabler

Varje instans av en klass har sina egna specifika värden för varje attribut som definieras i klassen, som vi har sett i exemplen i de tidigare avsnitten. Men vad händer om vi vill ha data som delas mellan de olika instanserna? Här kommer klassvariablerna in i bilden, även kallade statiska variabler. En klassvariabel är en variabel som nås via själva klassen, inte via de instanser som skapas baserat på klassen. Vid varje given tidpunkt under exekveringen av programmet har en klassvariabel ett enda värde, oavsett hur många instanser av klassen som skapas.

En klassvariabel deklareras utan self-prefixet och vanligtvis utanför varje metoddefinition eftersom den ska kunna nås var som helst inom klassen eller till och med utanför klassen.

class Sparkonto:\n    allman_ranta = 0.03\n\n    def __init__(self, kontonummer: str, saldo: float, ranta: float):\n        self.__kontonummer = kontonummer\n        self.__saldo = saldo\n        self.__ranta = ranta\n\n    def tillsatt_ranta(self):\n        # Räntan är allmänna räntan + kontots ränta\n        ranta_totalt = Sparkonto.allman_ranta + self.__ranta\n        self.__saldo += self.__saldo * ranta_totalt\n\n    @property\n    def saldo(self):\n        return self.__saldo

Eftersom variabeln allman_ranta definieras inom klassen men utanför alla metoddefinitioner och inte använder prefixet self, är det en klassvariabel.

En klassvariabel nås via klassens namn, till exempel så här:

# Den allmänna räntan är oberoende av andra instanser av objektet\nprint(\"Allmänna räntan är\", Sparkonto.allman_ranta)\n\nkonto = Sparkonto(\"12345\", 1000, 0.05)\n# Lägg till totala räntant till kontots saldo\nkonto.tillsatt_ranta()\nprint(konto.saldo)

Allmänna räntan är 0.03\n1080.0

Klassvariablerna nås alltså via klassens namn, till exempel Sparkonto.allman_ranta, medan instansvariablerna nås via objektvariabelns namn, till exempel konto.saldo. En instansvariabel existerar naturligtvis bara när en instans av klassen har skapats, men en klassvariabel är tillgänglig överallt och vid varje tidpunkt då klassen i sig är tillgänglig.

Klassvariabler är användbara när det finns behov av värden som delas av alla instanser av klassen. I exemplet ovan antog vi att den totala räntan för alla sparkonton består av två komponenter: den allmänna räntan delas av alla konton, men varje konto har också sin egen ränta i en instansvariabel. Den allmänna räntan kan också förändras, men förändringen kommer då att påverka alla instanser av klassen lika mycket.

class Sparkonto:\n    allman_ranta = 0.03\n\n    def __init__(self, kontonummer: str, saldo: float, ranta: float):\n        self.__kontonummer = kontonummer\n        self.__saldo = saldo\n        self.__ranta = ranta\n\n    def tillsatt_ranta(self):\n        # Räntan är allmänna räntan + kontots ränta\n        ranta_totalt = Sparkonto.allman_ranta + self.__ranta\n        self.__saldo += self.__saldo * ranta_totalt\n\n    @property\n    def saldo(self):\n        return self.__saldo\n\n    @property\n    def totalranta(self):\n        return self.__ranta + Sparkonto.allman_ranta
konto1 = Sparkonto(\"12345\", 100, 0.03)\nkonto2 = Sparkonto(\"54321\", 200, 0.06)\n\nprint(\"Allmänna räntan:\", Sparkonto.allman_ranta)\nprint(konto1.totalranta)\nprint(konto2.totalranta)\n\n# Vi ökar den allmänna räntan till 10 procent\nSparkonto.allman_ranta = 0.10\n\nprint(\"Allmänna räntan:\", Sparkonto.allman_ranta)\nprint(konto1.totalranta)\nprint(konto2.totalranta)

Allmänna räntan: 0.03\n0.06\n0.09\nAllmänna räntan: 0.1\n0.13\n0.16

När den allmänna räntan ändras, ändras den totala räntan för alla instanser av klassen. Som du kan se ovan är det möjligt att lägga till en getter-metod med @property-dekoratorn även om det inte finns ett attribut med samma namn i klassen. Denna metod returnerar summan av den allmänna räntan och den kontospecifika räntan.

Låt oss ta en titt på ett annat exempel. Klassen Telefonnummer används för att definiera ett enda telefonnummer, men den innehåller också några landskoder i en ordbok. Denna ordbok är en klassvariabel och delas därför av alla instanser av klassen, eftersom landskoden för telefonnummer från ett och samma land alltid är densamma.

class Telefonnummer:\n    landkoder = {\"Finland\": \"+358\", \"Sverige\": \"+46\", \"USA\": \"+1\"}\n\n    def __init__(self, namn: str, telefonnummer: str, land: str):\n        self.__namn = namn\n        self.__telefonnummer = telefonnummer\n        self.__land = land\n\n    @property\n    def telefonnummer(self):\n        # Den första nollan tas bort då landskoden läggs till\n        return Telefonnummer.landkoder[self.__land] + \" \" + self.__telefonnummer[1:]
paulan_nro = Telefonnummer(\"Pernilla Pythonson\", \"050 1234 567\", \"Finland\")\nprint(paulan_nro.telefonnummer)

+358 50 1234 567

Varje Telefonnummer-objekt innehåller namnet på ägaren, själva numret och det land där telefonnumret finns. När attributet som innehåller telefonnumret nås med getter-metoden hämtas lämplig landskod från klassvariabelns ordbok baserat på landattributet, och resultatet prefixeras till numret.

Implementeringsexemplet ovan är inte särskilt funktionellt i övrigt. I följande exempel har vi lagt till getter och sättare för alla attribut:

class Telefonnummer:\n    landkoder = {\"Finland\": \"+358\", \"Sverige\": \"+46\", \"USA\": \"+1\"}\n\n    def __init__(self, namn: str, telefonnummer: str, land: str):\n        self.__namn = namn\n        # Det här kallar metoden telefonnummer.setter\n        self.telefonnummer = telefonnummer\n        # Det här kallar metoden land.setter\n        self.land = land\n\n    # Getter-metoden för telefonnummer kombinerar landskoden och attributet telefonnummer\n    @property\n    def telefonnummer(self):\n        # Den första nollan tas bort då landskoden läggs till\n        return Telefonnummer.landkoder[self.__land] + \" \" + self.__telefonnummer[1:]\n\n    @telefonnummer.setter\n    def telefonnummer(self, nummer):\n        # Vi granksar att numret endast innehåller siffror och mellanslag\n        for marke in nummer:\n            if marke not in \"1234567890 \":\n                raise ValueError(\"Telefonnummer får innehålla endast siffror och mellanslag\")\n        self.__telefonnummer = nummer\n\n    # Endast telefonnummer utan landskod\n    @property\n    def lokalt_nummer(self):\n        return self.__telefonnummer\n\n    @property\n    def land(self):\n        return self.__land\n\n    @land.setter\n    def land(self, land):\n        # Vi granskar att landet är en nyckel i ordlistan\n        if land not in Telefonnummer.landkoder:\n            raise ValueError(\"Det angivna landet hittades inte på listan\")\n        self.__land = land\n\n    @property\n    def namn(self):\n        return self.__namn\n\n    @namn.setter\n    def namn(self, namn):\n        self.__namn = namn\n\n    def __str__(self):\n        return f\"{self.telefonnummer} ({self.__namn})\"
if __name__ == \"__main__\":\n    tlfnnmr = Telefonnummer(\"Petter Python\", \"040 111 1111\", \"Sverige\")\n    print(tlfnnmr)\n    print(tlfnnmr.telefonnummer)\n    print(tlfnnmr.lokalt_nummer)

+46 40 111 1111 (Petter Python)\n+46 40 111 1111\n040 111 1111

I uppgiftsbotten finns klassen Stad definierad, som modellerar en enskild stad.

Tillsätt en klassvariabel med namnet postnummer, som refererar till en ordlista. I ordlistan används städernas namn som nycklar, och värdena som fås är städernas postnummer. Båda är strängar.

I ordlistan ska åtminstone följande postnummer hittas:

    \n
  • Helsingfors 00100
  • \n
  • Åbo 20100
  • \n
  • Tammerfors 33100
  • \n
  • Jyväskylä 40100
  • \n
  • Uleåborg 90100
  • \n

Annan funktionalitet behövs inte implementeras.

Klassmetoder

En klassmetod, även kallad statisk metod, är en metod som inte är knuten till någon enskild instans av klassen. En klassmetod kan anropas utan att några instanser av klassen skapas.

Klassmetoder är vanligen verktyg som har något att göra med klassens syfte, men som är fristående i den meningen att det inte ska vara nödvändigt att skapa instanser av klassen för att kunna anropa dem. Klassmetoder är vanligtvis offentliga, så att de kan anropas både utanför klassen och inom klassen, inklusive inom instanser av klassen.

En klassmetod definieras med annotationen @classmethod. Den första parametern är alltid cls. Variabelnamnet cls liknar self-parametern. Skillnaden är att cls pekar på klassen medan self pekar på en instans av klassen. Ingen av parametrarna ingår i argumentlistan när funktionen anropas, utan Python fyller i lämpligt värde automatiskt.

I följande exempel har vi en klass som modellerar fordonsregistreringar. Klassen Registration innehåller en statisk metod för att kontrollera om en registernummer är giltig. Metoden är en statisk klassmetod eftersom det är användbart att kunna kontrollera om en registernummer är giltig redan innan ett enda Registration-objekt har skapats:

class Registration:\n    def __init__(self, agare: str, marke: str, ar: int, registernummer: str):\n        self.__agare = agare\n        self.__marke = marke\n        self.__ar = ar\n\n        # Vi anropar metoden registernummer.setter\n        self.registernummer = registernummer\n\n    @property\n    def registernummer(self):\n        return self.__registernummer\n\n    @registernummer.setter\n    def registernummer(self, nummer):\n        if Registration.registernummer_giltigt(nummer):\n            self.__registernummer = nummer\n        else:\n            raise ValueError(\"Registernummer inte giltigt\")\n\n    # Klassmetod som validerar registernumret\n    @classmethod\n    def registernummer_giltigt(cls, nummer: str):\n        if len(nummer) < 3 or \"-\" not in nummer:\n            return False\n\n        # Granska början och slutet av numret skilt\n        borjan, slutet = nummer.split(\"-\")\n\n        # Börjandelen får endast innehålla bokstäver\n        for marke in borjan:\n            if marke.lower() not in \"abcdefghijklmnopqrstuvwxyzåäö\":\n                return False\n\n        # Slutdelen får endast innehålla siffror\n        for marke in slutet:\n            if marke not in \"1234567890\":\n                return False\n\n        return True
reg = Registration(\"Bertil Bilist\", \"Volvo\", \"1992\", \"abc-123\")\n\nif Registration.registernummer_giltigt(\"xyz-789\"):\n    print(\"Detta är ett giltigt nummer!\")

Detta är ett giltigt nummer!

Giltigheten för en registernummer kan kontrolleras även utan att skapa en enda instans av klassen, t.ex. med Registration.nummer_plat_giltig(\"xyz-789\"). Samma metod anropas i klassens konstruktor. OBS: även inom konstruktören är denna metod åtkomlig via klassens namn, inte self!

Skapa en klass med namnet ListHjalpare som innehåller följande två klassmetoder:

    \n
  • Metoden storsta_frekvensen(lista: list) returnerar det mest förekommande föremålet på listan
  • \n
  • Metoden dubbletter(lista: list) returnerar antalet unika tal som förekommer åtminstone två gånger på listan
  • \n

Det ska vara möjligt att använda dessa metoder utan att skapa en instans av klassen. Ett exempel på hur metoderna kan användas:

tal = [1, 1, 2, 1, 3, 3, 4, 5, 5, 5, 6, 5, 5, 5]\nprint(ListHjalpare.storsta_frekvensen(tal))\nprint(ListHjalpare.dubbletter(tal))

5\n3

","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/5-klassattribut.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-9/6-fler-exempel-med-klasser/page-data.json b/page-data/osa-9/6-fler-exempel-med-klasser/page-data.json index 49badae42..1b9dabd28 100644 --- a/page-data/osa-9/6-fler-exempel-med-klasser/page-data.json +++ b/page-data/osa-9/6-fler-exempel-med-klasser/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/osa-9/6-fler-exempel-med-klasser", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är du bekant med några fler exempel på klasser och objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kan du använda standardvärden för parametrar i dina metoder"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel består av två klasser. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Punkt"}]},{"type":"text","value":" är en modell för en punkt i ett tvådimensionellt rum. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stracka"}]},{"type":"text","value":" är en modell för ett linjesegment mellan två punkter. Koden nedan är kommenterad, läs gärna kommentarerna för att förstå hur klasserna fungerar."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Punkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen representerar en punkt i ett tvådimensionellt rum \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Attributen är offentliga, eftersom vilket värde som helst kan användas som värde för x och y."}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" y\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna klassmetod returnerar en ny punkt vid origo (0, 0)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Det är möjligt att returnera en ny instans av klassen inifrån klassen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"origo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Klassmetoden skapar en ny punkt baserad på den givna punkten"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den nya punkten är en spegelbild av den givna punkten på en eller båda axlarna."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Till exempel är punkten (1, 3) speglad på x-axeln (1, -3)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spegelbild"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spegla_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"bool"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spegla_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"bool"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spegla_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spegla_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Stracka"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en sträcka i ett tvådimensionellt rum \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Dessa attribut är offentliga eftersom två valfria punkter kan accepteras"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" slut\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna metod använder Pythagoras sats för att beräkna längden på sträckan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langd"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar mitten av sträckan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"medelpunkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n medelx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n medely "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"medelx"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" medely"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ... "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\norigo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"origo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"origo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npunkt2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spegelbild"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkt2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstracka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" punkt2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"medelpunkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(1, 3)\n(0, 0)\n(-1, -3)\n6.324555320336759\n(0.0, 0.0)\n(1, 3) ... (-1, -3)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"standardvarden-for-parametrar","style":"position:relative;"},"children":[{"type":"text","value":"Standardvärden för parametrar"},{"type":"element","tagName":"a","properties":{"href":"#standardvarden-for-parametrar","ariaLabel":"standardvarden for parametrar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python-programmering kan du i allmänhet ange ett standardvärde för alla parametrar. Standardvärden kan användas i både funktioner och metoder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en parameter har ett standardvärde behöver du inte inkludera ett värde som ett argument när du anropar funktionen. Om ett argument anges ignoreras standardvärdet. Om inte, används standardvärdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Default-värden används ofta i konstruktörer. Om man kan förvänta sig att all information inte är tillgänglig när ett objekt skapas är det bättre att inkludera ett standardvärde i definitionen av konstruktörsmetoden än att tvinga klienten att ta hand om problemet. Detta gör det enklare att använda klassen ur klientens synvinkel, men det säkerställer också objektets integritet. Med ett fastställt standardvärde kan vi t.ex. vara säkra på att ett \"tomt\" värde alltid är detsamma, såvida inte klienten specifikt vill ange något annat. Om ett standardvärde inte anges är det upp till kunden att tillhandahålla ett \"tomt\" värde. Det kan t.ex. vara en tom sträng "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":", det speciella tomma objektet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" eller strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"inte angivet\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ännu en klass som representerar en studerande. När ett nytt Studerande-objekt skapas måste klienten ange ett namn och ett studerandenummer. Studerandenumret är privat och ska inte ändras i efterhand. Dessutom har ett Studerande-objekt attribut för studiepoäng och anteckningar, vilka har standardvärden som anges i konstruktorn. Nya värden kan skickas som argument till konstruktören, men de kan också utelämnas så att standardvärdena används istället. Titta gärna på kommentarerna i koden för att bättre förstå vad varje metod gör."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Modellerar en endaste studerande \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kallar sättar-metoden"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studerandenumret ska ha minst 5 tecken\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kallar sättar-metoden"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" anteckningar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namnet kan inte vara tomt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"studerandenummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studerandenummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"studiepoang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studiepoang\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"studiepoang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" sp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sp\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studiepoäng kan inte vara ett negativt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" anteckningar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sammanfattning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Studerande "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"):\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"- studiepoäng "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"- anteckningar: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skickar endast namnet och studerandenumret"}]},{"type":"text","value":"\nstuderande1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skickar namnet, studerandenummer och studiepoäng"}]},{"type":"text","value":"\nstuderande2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skickar alla uppgifter"}]},{"type":"text","value":"\nstuderande3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sara Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"99999\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"140"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tillägstid i tenter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skickar anteckningar, men inte studiepoäng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Obs: parametern måste nu bli namngiven när argumenten inte är i ordning"}]},{"type":"text","value":"\nstuderande4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saga Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"98765\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"avlägsen studieår 20-21\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande Sam Studerande (12345):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"studiepoäng 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"anteckningar:"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande Saul Studerande (54321):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"studiepoäng 25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"anteckningar:"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande Sara Studerande (99999):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"studiepoäng 140"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"anteckningar: tillägstid i tenter"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande Saga Studerande (98765):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"studiepoäng 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"anteckningar: avlägsen studieår 20-21"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Det finns ingen sättar-metod för attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande_nummer"}]},{"type":"text","value":" eftersom det inte är meningen att studerandenumret ska ändras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns en ganska betydande hake när man använder standardvärden för parametrar. Följande exempel som modellerar ännu en typ av studerande kommer att belysa detta mer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gjorda_kurser\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_prestation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ItP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tira\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['ItP', 'Tira']\n['ItP', 'Tira']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du lägger till slutförda kurser i Sams lista läggs dessa kurser också till i Sauls lista. Faktum är att dessa två är exakt samma lista, eftersom Python återanvänder referensen som lagras i standardvärdet. Att skapa de två nya Studerande-objekten i exemplet ovan är likvärdigt med följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstuderande1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Standardvärdena för parametrar bör aldrig vara instanser av mer komplicerade, föränderliga datastrukturer, t.ex. listor. Problemet kan kringgås genom att göra följande ändringar i konstruktorn för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" gjorda_kurser "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gjorda_kurser\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_prestation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ItP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tira\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['ItP', 'Tira']\n[]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"den-stora-finalen","style":"position:relative;"},"children":[{"type":"text","value":"Den stora finalen"},{"type":"element","tagName":"a","properties":{"href":"#den-stora-finalen","ariaLabel":"den stora finalen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fastän följande övning avslutar den här delen av materialet, så har de tekniker som krävs för att lösa den redan behandlats i avsnittet som heter "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/2-objekt-som-attribut","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Objekt som attribut"}]},{"type":"text","value":". Du behöver inte använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"-dekoratorn eller standardvärden för parametrar i den här övningen. Den här övningen är mycket lik övningarna \"en presentask\" och \"den kortaste personen i rummet\"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sak, Resväska och Lastutrymme","tmcname":"osa09-15_sak_resvaska_lastutrymme"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiftsserien kommer du skapa klasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sak"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lastutrymme"}]},{"type":"text","value":", vilka låter dig öva vidare på att jobba på objekt som innehåller referenser till andra objekt."}]},{"type":"element","tagName":"h2","properties":{"id":"sak","style":"position:relative;"},"children":[{"type":"text","value":"Sak"},{"type":"element","tagName":"a","properties":{"href":"#sak","ariaLabel":"sak permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sak"}]},{"type":"text","value":", som används för att skapa saker av olika sorter. Varje sak har ett namn och en vikt (i kg)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bokens namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bokens vikt:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bok:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Telefon:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets utskrift borde vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bokens namn: ABC bok\nBokens vikt: 2\nBok: ABC bok (2 kg)\nTelefon: Nokia 3210 (1 kg)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sak"}]},{"type":"text","value":" ska alltså innehålla metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vikt"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", som returnerar värden som lagras i dessa attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Namnet och vikten ska vara inkapslade inom klassen. Följande ska inte fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"resvaska","style":"position:relative;"},"children":[{"type":"text","value":"Resväska"},{"type":"element","tagName":"a","properties":{"href":"#resvaska","ariaLabel":"resvaska permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":". Det ska vara möjligt att packa saker i resväskan. En räsväska har dessutom en maximal vikt för saker som lagras i den."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till följande till klassen:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en konstruktor, som tar maximal vikt som ett argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_sak"}]},{"type":"text","value":", som lägger till saken givet som argument till resväskan. Metoden har inget returvärde."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":", som returnerar en sträng i formatet \"x saker (y kg)\""}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska se till att den sammanlagda vikten av de föremål som förvaras i en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":" inte överstiger den maximala vikt som har ställts in för den instansen. Om den maximala vikten skulle överskridas när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_sak"}]},{"type":"text","value":"-metoden anropas, ska det nya föremålet inte läggas till i resväskan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets utskrift borde vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 saker (0 kg)\n1 saker (2 kg)\n2 saker (3 kg)\n2 saker (3 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sprakvard","style":"position:relative;"},"children":[{"type":"text","value":"Språkvård"},{"type":"element","tagName":"a","properties":{"href":"#sprakvard","ariaLabel":"sprakvard permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Meddelandet ”1 saker” är inte särskilt grammatiskt. Istället borde det stå ”1 sak”. Gör de ändringar som krävs i din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Föregående exempel borde nu skriva ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 saker (0 kg)\n1 sak (2 kg)\n2 saker (3 kg)\n2 saker (3 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"alla-saker","style":"position:relative;"},"children":[{"type":"text","value":"Alla saker"},{"type":"element","tagName":"a","properties":{"href":"#alla-saker","ariaLabel":"alla saker permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tillägg följande metoder till definitionen av din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":"-klass:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_saker"}]},{"type":"text","value":", som skriver ut alla saker som lagras i resväskan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vikt"}]},{"type":"text","value":", som returnerar ett heltalsnummer som representerar den totala vikten av sakerna i resväskan"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen borde nu fungera med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"I resväskan finns följande saker:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_saker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvikt_tot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Totalvikt: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"vikt_tot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovanstående programs utskrift borde ge följande resultat:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I resväskan finns följande saker:\nABC bok (2 kg)\nNokia 3210 (1 kg)\nTegelsten (4 kg)\nTotalvikt: 7 kg"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du har implementerat din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":"-klass med fler än två instansvariabler, gör de ändringar som krävs så att varje instans endast har två dataattribut: den maximala vikten och en lista över föremålen i den."}]},{"type":"element","tagName":"h2","properties":{"id":"den-tyngsta-saken","style":"position:relative;"},"children":[{"type":"text","value":"Den tyngsta saken"},{"type":"element","tagName":"a","properties":{"href":"#den-tyngsta-saken","ariaLabel":"den tyngsta saken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till en ny metod i din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":"-klass: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tyngsta_saken"}]},{"type":"text","value":" ska returnera den "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sak"}]},{"type":"text","value":" som är tyngst. Om det finns två eller flera föremål med samma vikt kan metoden returnera vilket som helst av dessa. Metoden ska returnera en referens till ett objekt. Om resväskan är tom ska metoden returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din klass borde nu fungera med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntyngsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tyngsta_saken"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Tyngsta saken: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"tyngsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets utskrift borde nu vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tyngsta saken: Tegelsten (4 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lastutrymme","style":"position:relative;"},"children":[{"type":"text","value":"Lastutrymme"},{"type":"element","tagName":"a","properties":{"href":"#lastutrymme","ariaLabel":"lastutrymme permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lastutrymme"}]},{"type":"text","value":", som har följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en konstruktor, som får en maximalvikt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_resvaska"}]},{"type":"text","value":", som lägger till resväskan den får som argument till lastutrymmet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":", som returnerar en sträng i formatet \"x resväskor, rum för y kg\""}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen bör se till att den sammanlagda vikten av de föremål som lagras i ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lastutrymme"}]},{"type":"text","value":" inte överstiger den maximala vikt som har ställts in för den instansen. Om den maximala vikten skulle överskridas när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_resvaska"}]},{"type":"text","value":"-metoden anropas, ska den nya resväskan inte läggas till i lastutrymmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din klass borde nu fungera med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lastutrymme "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nadas_vaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npeters_vaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 resväskor, rum för 1000 kg\n1 resväska, rum för 997 kg\n2 resväskor, rum för 993 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lastutrymmets-innehall","style":"position:relative;"},"children":[{"type":"text","value":"Lastutrymmets innehåll"},{"type":"element","tagName":"a","properties":{"href":"#lastutrymmets-innehall","ariaLabel":"lastutrymmets innehall permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_saker"}]},{"type":"text","value":", till din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lastutrymme"}]},{"type":"text","value":"-klass. Metoden ska skriva ut alla saker i alla resväskor i lastutrymmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din klass borde nu fungera med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nadas_vaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npeters_vaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlastutrymme "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Väskorna i lastutrymmet innehåller följande saker:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_saker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften för ovanstående program borde vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Väskorna i lastutrymmet innehåller följande saker:\nABC bok (2 kg)\nNokia 3210 (1 kg)\nTegelsten (4 kg)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara avslutningsvis på följande frågeformulär:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"34723602-dd55-54df-914d-bdd2037722f8"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Är du bekant med några fler exempel på klasser och objekt
  • \n
  • Kan du använda standardvärden för parametrar i dina metoder
  • \n

Följande exempel består av två klasser. Klassen Punkt är en modell för en punkt i ett tvådimensionellt rum. Klassen Stracka är en modell för ett linjesegment mellan två punkter. Koden nedan är kommenterad, läs gärna kommentarerna för att förstå hur klasserna fungerar.

import math\n\nclass Punkt:\n    \"\"\" Klassen representerar en punkt i ett tvådimensionellt rum \"\"\"\n\n    def __init__(self, x: float, y: float):\n        # Attributen är offentliga, eftersom vilket värde som helst kan användas som värde för x och y.\n        self.x = x\n        self.y = y\n\n    # Denna klassmetod returnerar en ny punkt vid origo (0, 0)\n    # Det är möjligt att returnera en ny instans av klassen inifrån klassen\n    @classmethod\n    def origo(cls):\n        return Punkt(0, 0)\n\n    # Klassmetoden skapar en ny punkt baserad på den givna punkten\n    # Den nya punkten är en spegelbild av den givna punkten på en eller båda axlarna.\n    # Till exempel är punkten (1, 3) speglad på x-axeln (1, -3)\n    @classmethod\n    def spegelbild(cls, punkt, spegla_x: bool, spegla_y: bool):\n        x = punkt.x\n        y = punkt.y\n        if spegla_x:\n            y = -y\n        if spegla_y:\n            x = -x\n\n        return Punkt(x, y)\n\n    def __str__(self):\n        return f\"({self.x}, {self.y})\"\n\nclass Stracka:\n    \"\"\" Klassen modellerar en sträcka i ett tvådimensionellt rum \"\"\"\n\n    def __init__(self, borjan: Punkt, slut: Punkt):\n        # Dessa attribut är offentliga eftersom två valfria punkter kan accepteras\n        self.borjan = borjan\n        self.slut = slut\n\n    # Denna metod använder Pythagoras sats för att beräkna längden på sträckan\n    def langd(self):\n        summa = (self.slut.x - self.borjan.x) ** 2 + (self.slut.y - self.borjan.y) ** 2\n        return math.sqrt(summa)\n\n    # Metoden returnerar mitten av sträckan\n    def medelpunkt(self):\n        medelx = (self.borjan.x + self.slut.x) / 2\n        medely = (self.borjan.y + self.slut.y) / 2\n        return Punkt(medelx, medely)\n\n    def __str__(self):\n        return f\"{self.borjan} ... {self.slut}\"
punkt = Punkt(1,3)\nprint(punkt)\n\norigo = Punkt.origo()\nprint(origo)\n\npunkt2 = Punkt.spegelbild(punkt, True, True)\nprint(punkt2)\n\nstracka = Stracka(punkt, punkt2)\nprint(stracka.langd())\nprint(stracka.medelpunkt())\nprint(stracka)

(1, 3)\n(0, 0)\n(-1, -3)\n6.324555320336759\n(0.0, 0.0)\n(1, 3) ... (-1, -3)

Standardvärden för parametrar

I Python-programmering kan du i allmänhet ange ett standardvärde för alla parametrar. Standardvärden kan användas i både funktioner och metoder.

Om en parameter har ett standardvärde behöver du inte inkludera ett värde som ett argument när du anropar funktionen. Om ett argument anges ignoreras standardvärdet. Om inte, används standardvärdet.

Default-värden används ofta i konstruktörer. Om man kan förvänta sig att all information inte är tillgänglig när ett objekt skapas är det bättre att inkludera ett standardvärde i definitionen av konstruktörsmetoden än att tvinga klienten att ta hand om problemet. Detta gör det enklare att använda klassen ur klientens synvinkel, men det säkerställer också objektets integritet. Med ett fastställt standardvärde kan vi t.ex. vara säkra på att ett \"tomt\" värde alltid är detsamma, såvida inte klienten specifikt vill ange något annat. Om ett standardvärde inte anges är det upp till kunden att tillhandahålla ett \"tomt\" värde. Det kan t.ex. vara en tom sträng \"\", det speciella tomma objektet None eller strängen \"inte angivet\".

Låt oss ta en titt på ännu en klass som representerar en studerande. När ett nytt Studerande-objekt skapas måste klienten ange ett namn och ett studerandenummer. Studerandenumret är privat och ska inte ändras i efterhand. Dessutom har ett Studerande-objekt attribut för studiepoäng och anteckningar, vilka har standardvärden som anges i konstruktorn. Nya värden kan skickas som argument till konstruktören, men de kan också utelämnas så att standardvärdena används istället. Titta gärna på kommentarerna i koden för att bättre förstå vad varje metod gör.

class Studerande:\n    \"\"\" Modellerar en endaste studerande \"\"\"\n\n    def __init__(self, namn: str, studerandenummer: str, studiepoang:int = 0, anteckningar:str = \"\"):\n        # Kallar sättar-metoden\n        self.namn = namn\n\n        if len(studerandenummer) < 5:\n            raise ValueError(\"Studerandenumret ska ha minst 5 tecken\")\n\n        self.__studerandenummer = studerandenummer\n\n        # Kallar sättar-metoden\n        self.studiepoang = studiepoang\n\n        self.__anteckningar = anteckningar\n\n    @property\n    def namn(self):\n        return self.__namn\n\n    @namn.setter\n    def namn(self, namn):\n        if namn != \"\":\n            self.__namn = namn\n        else:\n            raise ValueError(\"Namnet kan inte vara tomt\")\n\n    @property\n    def studerandenummer(self):\n        return self.__studerandenummer\n\n    @property\n    def studiepoang(self):\n        return self.__studiepoang\n\n    @studiepoang.setter\n    def studiepoang(self, sp):\n        if sp >= 0:\n            self.__studiepoang = sp\n        else:\n            raise ValueError(\"Studiepoäng kan inte vara ett negativt tal\")\n\n    @property\n    def anteckningar(self):\n        return self.__anteckningar\n\n    @anteckningar.setter\n    def anteckningar(self, anteckningar):\n        self.__anteckningar = anteckningar\n\n    def sammanfattning(self):\n        print(f\"Studerande {self.__namn} ({self.studerandenummer}):\")\n        print(f\"- studiepoäng {self.__studiepoang}\")\n        print(f\"- anteckningar: {self.anteckningar}\")
# Skickar endast namnet och studerandenumret\nstuderande1 = Studerande(\"Sam Studerande\", \"12345\")\nstuderande1.sammanfattning()\n\n# Skickar namnet, studerandenummer och studiepoäng\nstuderande2 = Studerande(\"Saul Studerande\", \"54321\", 25)\nstuderande2.sammanfattning()\n\n# Skickar alla uppgifter\nstuderande3 = Studerande(\"Sara Studerande\", \"99999\", 140, \"tillägstid i tenter\")\nstuderande3.sammanfattning()\n\n# Skickar anteckningar, men inte studiepoäng\n# Obs: parametern måste nu bli namngiven när argumenten inte är i ordning\nstuderande4 = Studerande(\"Saga Studerande\", \"98765\", anteckningar=\"avlägsen studieår 20-21\")\nstuderande4.sammanfattning()

Studerande Sam Studerande (12345):

    \n
  • studiepoäng 0
  • \n
  • anteckningar:
  • \n

Studerande Saul Studerande (54321):

    \n
  • studiepoäng 25
  • \n
  • anteckningar:
  • \n

Studerande Sara Studerande (99999):

    \n
  • studiepoäng 140
  • \n
  • anteckningar: tillägstid i tenter
  • \n

Studerande Saga Studerande (98765):

    \n
  • studiepoäng 0
  • \n
  • anteckningar: avlägsen studieår 20-21
  • \n

OBS: Det finns ingen sättar-metod för attributet studerande_nummer eftersom det inte är meningen att studerandenumret ska ändras.

Det finns en ganska betydande hake när man använder standardvärden för parametrar. Följande exempel som modellerar ännu en typ av studerande kommer att belysa detta mer:

class Studerande:\n    def __init__(self, namn, gjorda_kurser=[]):\n        self.namn = namn\n        self.gjorda_kurser = gjorda_kurser\n\n    def tillsatt_prestation(self, kurs):\n        self.gjorda_kurser.append(kurs)
studerande1 = Studerande(\"Sam Studerande\")\nstuderande2 = Studerande(\"Saul Studerande\")\n\nstuderande1.tillsatt_prestation(\"ItP\")\nstuderande1.tillsatt_prestation(\"Tira\")\n\nprint(studerande1.gjorda_kurser)\nprint(studerande2.gjorda_kurser)

['ItP', 'Tira']\n['ItP', 'Tira']

Om du lägger till slutförda kurser i Sams lista läggs dessa kurser också till i Sauls lista. Faktum är att dessa två är exakt samma lista, eftersom Python återanvänder referensen som lagras i standardvärdet. Att skapa de två nya Studerande-objekten i exemplet ovan är likvärdigt med följande:

kurser = []\nstuderande1 = Studerande(\"Sam Studerande\", kurser)\nstuderande2 = Studerande(\"Saul Studerande\", kurser)

Standardvärdena för parametrar bör aldrig vara instanser av mer komplicerade, föränderliga datastrukturer, t.ex. listor. Problemet kan kringgås genom att göra följande ändringar i konstruktorn för klassen Studerande:

class Studerande:\n    def __init__(self, namn, gjorda_kurser=None):\n        self.namn = namn\n        if gjorda_kurser is None:\n            self.gjorda_kurser = []\n        else:\n            self.gjorda_kurser = gjorda_kurser\n\n    def tillsatt_prestation(self, kurs):\n        self.gjorda_kurser.append(kurs)
studerande1 = Studerande(\"Sam Studerande\")\nstuderande2 = Studerande(\"Saul Studerande\")\n\nstuderande1.tillsatt_prestation(\"ItP\")\nstuderande1.tillsatt_prestation(\"Tira\")\n\nprint(studerande1.gjorda_kurser)\nprint(studerande2.gjorda_kurser)

['ItP', 'Tira']\n[]

Den stora finalen

Fastän följande övning avslutar den här delen av materialet, så har de tekniker som krävs för att lösa den redan behandlats i avsnittet som heter Objekt som attribut. Du behöver inte använda @property-dekoratorn eller standardvärden för parametrar i den här övningen. Den här övningen är mycket lik övningarna \"en presentask\" och \"den kortaste personen i rummet\".

I den här uppgiftsserien kommer du skapa klasserna Sak, Resvaska och Lastutrymme, vilka låter dig öva vidare på att jobba på objekt som innehåller referenser till andra objekt.

Sak

Skapa klassen Sak, som används för att skapa saker av olika sorter. Varje sak har ett namn och en vikt (i kg).

Klassen ska fungera enligt följande:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\n\nprint(\"Bokens namn:\", bok.namn())\nprint(\"Bokens vikt:\", bok.vikt())\n\nprint(\"Bok:\", bok)\nprint(\"Telefon:\", telefon)

Programmets utskrift borde vara följande:

Bokens namn: ABC bok\nBokens vikt: 2\nBok: ABC bok (2 kg)\nTelefon: Nokia 3210 (1 kg)

En Sak ska alltså innehålla metoderna vikt och namn, som returnerar värden som lagras i dessa attribut.

Namnet och vikten ska vara inkapslade inom klassen. Följande ska inte fungera:

bok = Sak(\"ABC bok\", 2)\nbok.vikt = 10

Resväska

Skapa klassen Resvaska. Det ska vara möjligt att packa saker i resväskan. En räsväska har dessutom en maximal vikt för saker som lagras i den.

Lägg till följande till klassen:

    \n
  • en konstruktor, som tar maximal vikt som ett argument
  • \n
  • metoden tillsatt_sak, som lägger till saken givet som argument till resväskan. Metoden har inget returvärde.
  • \n
  • metoden __str__, som returnerar en sträng i formatet \"x saker (y kg)\"
  • \n

Klassen ska se till att den sammanlagda vikten av de föremål som förvaras i en Resvaska inte överstiger den maximala vikt som har ställts in för den instansen. Om den maximala vikten skulle överskridas när tillsatt_sak-metoden anropas, ska det nya föremålet inte läggas till i resväskan.

Klassen ska fungera enligt följande:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nresvaska = Resvaska(5)\nprint(resvaska)\n\nresvaska.tillsatt_sak(bok)\nprint(resvaska)\n\nresvaska.tillsatt_sak(telefon)\nprint(resvaska)\n\nresvaska.tillsatt_sak(tegelsten)\nprint(resvaska)

Programmets utskrift borde vara följande:

0 saker (0 kg)\n1 saker (2 kg)\n2 saker (3 kg)\n2 saker (3 kg)

Språkvård

Meddelandet ”1 saker” är inte särskilt grammatiskt. Istället borde det stå ”1 sak”. Gör de ändringar som krävs i din __str__-metod.

Föregående exempel borde nu skriva ut:

0 saker (0 kg)\n1 sak (2 kg)\n2 saker (3 kg)\n2 saker (3 kg)

Alla saker

Tillägg följande metoder till definitionen av din Resvaska-klass:

    \n
  • skriv_ut_saker, som skriver ut alla saker som lagras i resväskan
  • \n
  • vikt, som returnerar ett heltalsnummer som representerar den totala vikten av sakerna i resväskan
  • \n

Klassen borde nu fungera med följande program:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nresvaska = Resvaska(10)\nresvaska.tillsatt_sak(bok)\nresvaska.tillsatt_sak(telefon)\nresvaska.tillsatt_sak(tegelsten)\n\nprint(\"I resväskan finns följande saker:\")\nresvaska.skriv_ut_saker()\nvikt_tot = resvaska.vikt()\nprint(f\"Totalvikt: {vikt_tot} kg\")

Ovanstående programs utskrift borde ge följande resultat:

I resväskan finns följande saker:\nABC bok (2 kg)\nNokia 3210 (1 kg)\nTegelsten (4 kg)\nTotalvikt: 7 kg

Om du har implementerat din Resvaska-klass med fler än två instansvariabler, gör de ändringar som krävs så att varje instans endast har två dataattribut: den maximala vikten och en lista över föremålen i den.

Den tyngsta saken

Lägg till en ny metod i din Resvaska-klass: tyngsta_saken ska returnera den sak som är tyngst. Om det finns två eller flera föremål med samma vikt kan metoden returnera vilket som helst av dessa. Metoden ska returnera en referens till ett objekt. Om resväskan är tom ska metoden returnera None.

Din klass borde nu fungera med följande program:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nresvaska = Resvaska(10)\nresvaska.tillsatt_sak(bok)\nresvaska.tillsatt_sak(telefon)\nresvaska.tillsatt_sak(tegelsten)\n\ntyngsta = resvaska.tyngsta_saken()\nprint(f\"Tyngsta saken: {tyngsta}\")

Programmets utskrift borde nu vara följande:

Tyngsta saken: Tegelsten (4 kg)

Lastutrymme

Skapa klassen Lastutrymme, som har följande metoder:

    \n
  • en konstruktor, som får en maximalvikt
  • \n
  • metoden tillsatt_resvaska, som lägger till resväskan den får som argument till lastutrymmet
  • \n
  • metoden __str__, som returnerar en sträng i formatet \"x resväskor, rum för y kg\"
  • \n

Klassen bör se till att den sammanlagda vikten av de föremål som lagras i ett Lastutrymme inte överstiger den maximala vikt som har ställts in för den instansen. Om den maximala vikten skulle överskridas när tillsatt_resvaska-metoden anropas, ska den nya resväskan inte läggas till i lastutrymmet.

Din klass borde nu fungera med följande program:

lastutrymme = Lastutrymme(1000)\nprint(lastutrymme)\n\nbok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nadas_vaska = Resvaska(10)\nadas_vaska.tillsatt_sak(bok)\nadas_vaska.tillsatt_sak(telefon)\n\npeters_vaska = Resvaska(10)\npeters_vaska.tillsatt_sak(tegelsten)\n\nlastutrymme.tillsatt_resvaska(adas_vaska)\nprint(lastutrymme)\n\nlastutrymme.tillsatt_resvaska(peters_vaska)\nprint(lastutrymme)

0 resväskor, rum för 1000 kg\n1 resväska, rum för 997 kg\n2 resväskor, rum för 993 kg

Lastutrymmets innehåll

Lägg till metoden skriv_ut_saker, till din Lastutrymme-klass. Metoden ska skriva ut alla saker i alla resväskor i lastutrymmet.

Din klass borde nu fungera med följande program:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nadas_vaska = Resvaska(10)\nadas_vaska.tillsatt_sak(bok)\nadas_vaska.tillsatt_sak(telefon)\n\npeters_vaska = Resvaska(10)\npeters_vaska.tillsatt_sak(tegelsten)\n\nlastutrymme = Lastutrymme(1000)\nlastutrymme.tillsatt_resvaska(adas_vaska)\nlastutrymme.tillsatt_resvaska(peters_vaska)\n\nprint(\"Väskorna i lastutrymmet innehåller följande saker:\")\nlastutrymme.skriv_ut_saker()

Utskriften för ovanstående program borde vara följande:

Väskorna i lastutrymmet innehåller följande saker:\nABC bok (2 kg)\nNokia 3210 (1 kg)\nTegelsten (4 kg)

Svara avslutningsvis på följande frågeformulär:

","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/6-fler-exempel-med-klasser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"text-box","properties":{"variant":"learningObjectives","name":"Inlärningsmål"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Efter den här delen"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Är du bekant med några fler exempel på klasser och objekt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Kan du använda standardvärden för parametrar i dina metoder"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Följande exempel består av två klasser. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Punkt"}]},{"type":"text","value":" är en modell för en punkt i ett tvådimensionellt rum. Klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stracka"}]},{"type":"text","value":" är en modell för ett linjesegment mellan två punkter. Koden nedan är kommenterad, läs gärna kommentarerna för att förstå hur klasserna fungerar."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Punkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen representerar en punkt i ett tvådimensionellt rum \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Attributen är offentliga, eftersom vilket värde som helst kan användas som värde för x och y."}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" y\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna klassmetod returnerar en ny punkt vid origo (0, 0)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Det är möjligt att returnera en ny instans av klassen inifrån klassen"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"origo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Klassmetoden skapar en ny punkt baserad på den givna punkten"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Den nya punkten är en spegelbild av den givna punkten på en eller båda axlarna."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Till exempel är punkten (1, 3) speglad på x-axeln (1, -3)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"spegelbild"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spegla_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"bool"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" spegla_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"bool"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spegla_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" spegla_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":")\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Stracka"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Klassen modellerar en sträcka i ett tvådimensionellt rum \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Dessa attribut är offentliga eftersom två valfria punkter kan accepteras"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" borjan\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" slut\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Denna metod använder Pythagoras sats för att beräkna längden på sträckan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"langd"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n summa "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"summa"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Metoden returnerar mitten av sträckan"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"medelpunkt"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n medelx "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n medely "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"medelx"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" medely"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"borjan"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ... "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"slut"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"punkt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\norigo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"origo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"origo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npunkt2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"spegelbild"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkt2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstracka "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"punkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" punkt2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"langd"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"medelpunkt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stracka"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(1, 3)\n(0, 0)\n(-1, -3)\n6.324555320336759\n(0.0, 0.0)\n(1, 3) ... (-1, -3)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"standardvarden-for-parametrar","style":"position:relative;"},"children":[{"type":"text","value":"Standardvärden för parametrar"},{"type":"element","tagName":"a","properties":{"href":"#standardvarden-for-parametrar","ariaLabel":"standardvarden for parametrar permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I Python-programmering kan du i allmänhet ange ett standardvärde för alla parametrar. Standardvärden kan användas i både funktioner och metoder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om en parameter har ett standardvärde behöver du inte inkludera ett värde som ett argument när du anropar funktionen. Om ett argument anges ignoreras standardvärdet. Om inte, används standardvärdet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Default-värden används ofta i konstruktörer. Om man kan förvänta sig att all information inte är tillgänglig när ett objekt skapas är det bättre att inkludera ett standardvärde i definitionen av konstruktörsmetoden än att tvinga klienten att ta hand om problemet. Detta gör det enklare att använda klassen ur klientens synvinkel, men det säkerställer också objektets integritet. Med ett fastställt standardvärde kan vi t.ex. vara säkra på att ett \"tomt\" värde alltid är detsamma, såvida inte klienten specifikt vill ange något annat. Om ett standardvärde inte anges är det upp till kunden att tillhandahålla ett \"tomt\" värde. Det kan t.ex. vara en tom sträng "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":", det speciella tomma objektet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" eller strängen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"inte angivet\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Låt oss ta en titt på ännu en klass som representerar en studerande. När ett nytt Studerande-objekt skapas måste klienten ange ett namn och ett studerandenummer. Studerandenumret är privat och ska inte ändras i efterhand. Dessutom har ett Studerande-objekt attribut för studiepoäng och anteckningar, vilka har standardvärden som anges i konstruktorn. Nya värden kan skickas som argument till konstruktören, men de kan också utelämnas så att standardvärdena används istället. Titta gärna på kommentarerna i koden för att bättre förstå vad varje metod gör."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" Modellerar en endaste studerande \"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kallar sättar-metoden"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studerandenumret ska ha minst 5 tecken\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studerandenummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studerandenummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Kallar sättar-metoden"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" studiepoang\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" anteckningar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"namn"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Namnet kan inte vara tomt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"studerandenummer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studerandenummer\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"studiepoang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studiepoang\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"studiepoang"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" sp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studiepoang "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sp\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Studiepoäng kan inte vara ett negativt tal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"anteckningar"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__anteckningar "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" anteckningar\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sammanfattning"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Studerande "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"studerandenummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"):\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"- studiepoäng "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__studiepoang"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"- anteckningar: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skickar endast namnet och studerandenumret"}]},{"type":"text","value":"\nstuderande1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skickar namnet, studerandenummer och studiepoäng"}]},{"type":"text","value":"\nstuderande2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skickar alla uppgifter"}]},{"type":"text","value":"\nstuderande3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sara Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"99999\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"140"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tillägstid i tenter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Skickar anteckningar, men inte studiepoäng"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Obs: parametern måste nu bli namngiven när argumenten inte är i ordning"}]},{"type":"text","value":"\nstuderande4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saga Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"98765\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" anteckningar"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"avlägsen studieår 20-21\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sammanfattning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande Sam Studerande (12345):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"studiepoäng 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"anteckningar:"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande Saul Studerande (54321):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"studiepoäng 25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"anteckningar:"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande Sara Studerande (99999):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"studiepoäng 140"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"anteckningar: tillägstid i tenter"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Studerande Saga Studerande (98765):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"studiepoäng 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"anteckningar: avlägsen studieår 20-21"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OBS: Det finns ingen sättar-metod för attributet "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"studerande_nummer"}]},{"type":"text","value":" eftersom det inte är meningen att studerandenumret ska ändras."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Det finns en ganska betydande hake när man använder standardvärden för parametrar. Följande exempel som modellerar ännu en typ av studerande kommer att belysa detta mer:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gjorda_kurser\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_prestation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ItP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tira\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['ItP', 'Tira']\n['ItP', 'Tira']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du lägger till slutförda kurser i Sams lista läggs dessa kurser också till i Sauls lista. Faktum är att dessa två är exakt samma lista, eftersom Python återanvänder referensen som lagras i standardvärdet. Att skapa de två nya Studerande-objekten i exemplet ovan är likvärdigt med följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"kurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstuderande1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Standardvärdena för parametrar bör aldrig vara instanser av mer komplicerade, föränderliga datastrukturer, t.ex. listor. Problemet kan kringgås genom att göra följande ändringar i konstruktorn för klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Studerande"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" namn\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" gjorda_kurser "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" gjorda_kurser\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"tillsatt_prestation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"kurs"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"studerande1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sam Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Studerande"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Studerande\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ItP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstuderande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_prestation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tira\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"studerande2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"gjorda_kurser"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['ItP', 'Tira']\n[]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"den-stora-finalen","style":"position:relative;"},"children":[{"type":"text","value":"Den stora finalen"},{"type":"element","tagName":"a","properties":{"href":"#den-stora-finalen","ariaLabel":"den stora finalen permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fastän följande övning avslutar den här delen av materialet, så har de tekniker som krävs för att lösa den redan behandlats i avsnittet som heter "},{"type":"element","tagName":"a","properties":{"href":"https://rage.github.io/ohjelmointi-24-sv/osa-9/2-objekt-som-attribut","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Objekt som attribut"}]},{"type":"text","value":". Du behöver inte använda "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":"-dekoratorn eller standardvärden för parametrar i den här övningen. Den här övningen är mycket lik övningarna \"en presentask\" och \"den kortaste personen i rummet\"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sak, Resväska och Lastutrymme","tmcname":"osa09-15_sak_resvaska_lastutrymme"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I den här uppgiftsserien kommer du skapa klasserna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sak"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lastutrymme"}]},{"type":"text","value":", vilka låter dig öva vidare på att jobba på objekt som innehåller referenser till andra objekt."}]},{"type":"element","tagName":"h2","properties":{"id":"sak","style":"position:relative;"},"children":[{"type":"text","value":"Sak"},{"type":"element","tagName":"a","properties":{"href":"#sak","ariaLabel":"sak permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sak"}]},{"type":"text","value":", som används för att skapa saker av olika sorter. Varje sak har ett namn och en vikt (i kg)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bokens namn:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"namn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bokens vikt:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bok:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Telefon:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets utskrift borde vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bokens namn: ABC bok\nBokens vikt: 2\nBok: ABC bok (2 kg)\nTelefon: Nokia 3210 (1 kg)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"En "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sak"}]},{"type":"text","value":" ska alltså innehålla metoderna "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vikt"}]},{"type":"text","value":" och "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"namn"}]},{"type":"text","value":", som returnerar värden som lagras i dessa attribut."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Namnet och vikten ska vara inkapslade inom klassen. Följande ska inte fungera:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"resvaska","style":"position:relative;"},"children":[{"type":"text","value":"Resväska"},{"type":"element","tagName":"a","properties":{"href":"#resvaska","ariaLabel":"resvaska permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":". Det ska vara möjligt att packa saker i resväskan. En räsväska har dessutom en maximal vikt för saker som lagras i den."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till följande till klassen:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en konstruktor, som tar maximal vikt som ett argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_sak"}]},{"type":"text","value":", som lägger till saken givet som argument till resväskan. Metoden har inget returvärde."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":", som returnerar en sträng i formatet \"x saker (y kg)\""}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska se till att den sammanlagda vikten av de föremål som förvaras i en "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":" inte överstiger den maximala vikt som har ställts in för den instansen. Om den maximala vikten skulle överskridas när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_sak"}]},{"type":"text","value":"-metoden anropas, ska det nya föremålet inte läggas till i resväskan."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen ska fungera enligt följande:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets utskrift borde vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 saker (0 kg)\n1 saker (2 kg)\n2 saker (3 kg)\n2 saker (3 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sprakvard","style":"position:relative;"},"children":[{"type":"text","value":"Språkvård"},{"type":"element","tagName":"a","properties":{"href":"#sprakvard","ariaLabel":"sprakvard permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Meddelandet ”1 saker” är inte särskilt grammatiskt. Istället borde det stå ”1 sak”. Gör de ändringar som krävs i din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":"-metod."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Föregående exempel borde nu skriva ut:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 saker (0 kg)\n1 sak (2 kg)\n2 saker (3 kg)\n2 saker (3 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"alla-saker","style":"position:relative;"},"children":[{"type":"text","value":"Alla saker"},{"type":"element","tagName":"a","properties":{"href":"#alla-saker","ariaLabel":"alla saker permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tillägg följande metoder till definitionen av din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":"-klass:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_saker"}]},{"type":"text","value":", som skriver ut alla saker som lagras i resväskan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"vikt"}]},{"type":"text","value":", som returnerar ett heltalsnummer som representerar den totala vikten av sakerna i resväskan"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen borde nu fungera med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"I resväskan finns följande saker:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_saker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvikt_tot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"vikt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Totalvikt: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"vikt_tot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Ovanstående programs utskrift borde ge följande resultat:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"I resväskan finns följande saker:\nABC bok (2 kg)\nNokia 3210 (1 kg)\nTegelsten (4 kg)\nTotalvikt: 7 kg"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Om du har implementerat din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":"-klass med fler än två instansvariabler, gör de ändringar som krävs så att varje instans endast har två dataattribut: den maximala vikten och en lista över föremålen i den."}]},{"type":"element","tagName":"h2","properties":{"id":"den-tyngsta-saken","style":"position:relative;"},"children":[{"type":"text","value":"Den tyngsta saken"},{"type":"element","tagName":"a","properties":{"href":"#den-tyngsta-saken","ariaLabel":"den tyngsta saken permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till en ny metod i din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Resvaska"}]},{"type":"text","value":"-klass: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tyngsta_saken"}]},{"type":"text","value":" ska returnera den "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sak"}]},{"type":"text","value":" som är tyngst. Om det finns två eller flera föremål med samma vikt kan metoden returnera vilket som helst av dessa. Metoden ska returnera en referens till ett objekt. Om resväskan är tom ska metoden returnera "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din klass borde nu fungera med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresvaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntyngsta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tyngsta_saken"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Tyngsta saken: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"tyngsta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programmets utskrift borde nu vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tyngsta saken: Tegelsten (4 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lastutrymme","style":"position:relative;"},"children":[{"type":"text","value":"Lastutrymme"},{"type":"element","tagName":"a","properties":{"href":"#lastutrymme","ariaLabel":"lastutrymme permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Skapa klassen "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lastutrymme"}]},{"type":"text","value":", som har följande metoder:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"en konstruktor, som får en maximalvikt"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_resvaska"}]},{"type":"text","value":", som lägger till resväskan den får som argument till lastutrymmet"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":", som returnerar en sträng i formatet \"x resväskor, rum för y kg\""}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Klassen bör se till att den sammanlagda vikten av de föremål som lagras i ett "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lastutrymme"}]},{"type":"text","value":" inte överstiger den maximala vikt som har ställts in för den instansen. Om den maximala vikten skulle överskridas när "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tillsatt_resvaska"}]},{"type":"text","value":"-metoden anropas, ska den nya resväskan inte läggas till i lastutrymmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din klass borde nu fungera med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"lastutrymme "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nadas_vaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npeters_vaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 resväskor, rum för 1000 kg\n1 resväska, rum för 997 kg\n2 resväskor, rum för 993 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lastutrymmets-innehall","style":"position:relative;"},"children":[{"type":"text","value":"Lastutrymmets innehåll"},{"type":"element","tagName":"a","properties":{"href":"#lastutrymmets-innehall","ariaLabel":"lastutrymmets innehall permalink","className":["anchor","after"]},"children":[{"type":"element","tagName":"svg","properties":{"ariaHidden":"true","focusable":"false","height":"16","version":"1.1","viewBox":"0 0 16 16","width":"16"},"children":[{"type":"element","tagName":"path","properties":{"fillRule":"evenodd","d":"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"},"children":[]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lägg till metoden "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"skriv_ut_saker"}]},{"type":"text","value":", till din "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lastutrymme"}]},{"type":"text","value":"-klass. Metoden ska skriva ut alla saker i alla resväskor i lastutrymmet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Din klass borde nu fungera med följande program:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python"]},"children":[{"type":"text","value":"bok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ABC bok\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntelefon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntegelsten "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tegelsten\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nadas_vaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"telefon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npeters_vaska "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_sak"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tegelsten"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlastutrymme "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Lastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adas_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tillsatt_resvaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_vaska"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Väskorna i lastutrymmet innehåller följande saker:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlastutrymme"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"skriv_ut_saker"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Utskriften för ovanstående program borde vara följande:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Väskorna i lastutrymmet innehåller följande saker:\nABC bok (2 kg)\nNokia 3210 (1 kg)\nTegelsten (4 kg)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Svara avslutningsvis på följande frågeformulär:"}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"34723602-dd55-54df-914d-bdd2037722f8"},"children":[]}]}]},"html":"

Efter den här delen

    \n
  • Är du bekant med några fler exempel på klasser och objekt
  • \n
  • Kan du använda standardvärden för parametrar i dina metoder
  • \n

Följande exempel består av två klasser. Klassen Punkt är en modell för en punkt i ett tvådimensionellt rum. Klassen Stracka är en modell för ett linjesegment mellan två punkter. Koden nedan är kommenterad, läs gärna kommentarerna för att förstå hur klasserna fungerar.

import math\n\nclass Punkt:\n    \"\"\" Klassen representerar en punkt i ett tvådimensionellt rum \"\"\"\n\n    def __init__(self, x: float, y: float):\n        # Attributen är offentliga, eftersom vilket värde som helst kan användas som värde för x och y.\n        self.x = x\n        self.y = y\n\n    # Denna klassmetod returnerar en ny punkt vid origo (0, 0)\n    # Det är möjligt att returnera en ny instans av klassen inifrån klassen\n    @classmethod\n    def origo(cls):\n        return Punkt(0, 0)\n\n    # Klassmetoden skapar en ny punkt baserad på den givna punkten\n    # Den nya punkten är en spegelbild av den givna punkten på en eller båda axlarna.\n    # Till exempel är punkten (1, 3) speglad på x-axeln (1, -3)\n    @classmethod\n    def spegelbild(cls, punkt, spegla_x: bool, spegla_y: bool):\n        x = punkt.x\n        y = punkt.y\n        if spegla_x:\n            y = -y\n        if spegla_y:\n            x = -x\n\n        return Punkt(x, y)\n\n    def __str__(self):\n        return f\"({self.x}, {self.y})\"\n\nclass Stracka:\n    \"\"\" Klassen modellerar en sträcka i ett tvådimensionellt rum \"\"\"\n\n    def __init__(self, borjan: Punkt, slut: Punkt):\n        # Dessa attribut är offentliga eftersom två valfria punkter kan accepteras\n        self.borjan = borjan\n        self.slut = slut\n\n    # Denna metod använder Pythagoras sats för att beräkna längden på sträckan\n    def langd(self):\n        summa = (self.slut.x - self.borjan.x) ** 2 + (self.slut.y - self.borjan.y) ** 2\n        return math.sqrt(summa)\n\n    # Metoden returnerar mitten av sträckan\n    def medelpunkt(self):\n        medelx = (self.borjan.x + self.slut.x) / 2\n        medely = (self.borjan.y + self.slut.y) / 2\n        return Punkt(medelx, medely)\n\n    def __str__(self):\n        return f\"{self.borjan} ... {self.slut}\"
punkt = Punkt(1,3)\nprint(punkt)\n\norigo = Punkt.origo()\nprint(origo)\n\npunkt2 = Punkt.spegelbild(punkt, True, True)\nprint(punkt2)\n\nstracka = Stracka(punkt, punkt2)\nprint(stracka.langd())\nprint(stracka.medelpunkt())\nprint(stracka)

(1, 3)\n(0, 0)\n(-1, -3)\n6.324555320336759\n(0.0, 0.0)\n(1, 3) ... (-1, -3)

Standardvärden för parametrar

I Python-programmering kan du i allmänhet ange ett standardvärde för alla parametrar. Standardvärden kan användas i både funktioner och metoder.

Om en parameter har ett standardvärde behöver du inte inkludera ett värde som ett argument när du anropar funktionen. Om ett argument anges ignoreras standardvärdet. Om inte, används standardvärdet.

Default-värden används ofta i konstruktörer. Om man kan förvänta sig att all information inte är tillgänglig när ett objekt skapas är det bättre att inkludera ett standardvärde i definitionen av konstruktörsmetoden än att tvinga klienten att ta hand om problemet. Detta gör det enklare att använda klassen ur klientens synvinkel, men det säkerställer också objektets integritet. Med ett fastställt standardvärde kan vi t.ex. vara säkra på att ett \"tomt\" värde alltid är detsamma, såvida inte klienten specifikt vill ange något annat. Om ett standardvärde inte anges är det upp till kunden att tillhandahålla ett \"tomt\" värde. Det kan t.ex. vara en tom sträng \"\", det speciella tomma objektet None eller strängen \"inte angivet\".

Låt oss ta en titt på ännu en klass som representerar en studerande. När ett nytt Studerande-objekt skapas måste klienten ange ett namn och ett studerandenummer. Studerandenumret är privat och ska inte ändras i efterhand. Dessutom har ett Studerande-objekt attribut för studiepoäng och anteckningar, vilka har standardvärden som anges i konstruktorn. Nya värden kan skickas som argument till konstruktören, men de kan också utelämnas så att standardvärdena används istället. Titta gärna på kommentarerna i koden för att bättre förstå vad varje metod gör.

class Studerande:\n    \"\"\" Modellerar en endaste studerande \"\"\"\n\n    def __init__(self, namn: str, studerandenummer: str, studiepoang:int = 0, anteckningar:str = \"\"):\n        # Kallar sättar-metoden\n        self.namn = namn\n\n        if len(studerandenummer) < 5:\n            raise ValueError(\"Studerandenumret ska ha minst 5 tecken\")\n\n        self.__studerandenummer = studerandenummer\n\n        # Kallar sättar-metoden\n        self.studiepoang = studiepoang\n\n        self.__anteckningar = anteckningar\n\n    @property\n    def namn(self):\n        return self.__namn\n\n    @namn.setter\n    def namn(self, namn):\n        if namn != \"\":\n            self.__namn = namn\n        else:\n            raise ValueError(\"Namnet kan inte vara tomt\")\n\n    @property\n    def studerandenummer(self):\n        return self.__studerandenummer\n\n    @property\n    def studiepoang(self):\n        return self.__studiepoang\n\n    @studiepoang.setter\n    def studiepoang(self, sp):\n        if sp >= 0:\n            self.__studiepoang = sp\n        else:\n            raise ValueError(\"Studiepoäng kan inte vara ett negativt tal\")\n\n    @property\n    def anteckningar(self):\n        return self.__anteckningar\n\n    @anteckningar.setter\n    def anteckningar(self, anteckningar):\n        self.__anteckningar = anteckningar\n\n    def sammanfattning(self):\n        print(f\"Studerande {self.__namn} ({self.studerandenummer}):\")\n        print(f\"- studiepoäng {self.__studiepoang}\")\n        print(f\"- anteckningar: {self.anteckningar}\")
# Skickar endast namnet och studerandenumret\nstuderande1 = Studerande(\"Sam Studerande\", \"12345\")\nstuderande1.sammanfattning()\n\n# Skickar namnet, studerandenummer och studiepoäng\nstuderande2 = Studerande(\"Saul Studerande\", \"54321\", 25)\nstuderande2.sammanfattning()\n\n# Skickar alla uppgifter\nstuderande3 = Studerande(\"Sara Studerande\", \"99999\", 140, \"tillägstid i tenter\")\nstuderande3.sammanfattning()\n\n# Skickar anteckningar, men inte studiepoäng\n# Obs: parametern måste nu bli namngiven när argumenten inte är i ordning\nstuderande4 = Studerande(\"Saga Studerande\", \"98765\", anteckningar=\"avlägsen studieår 20-21\")\nstuderande4.sammanfattning()

Studerande Sam Studerande (12345):

    \n
  • studiepoäng 0
  • \n
  • anteckningar:
  • \n

Studerande Saul Studerande (54321):

    \n
  • studiepoäng 25
  • \n
  • anteckningar:
  • \n

Studerande Sara Studerande (99999):

    \n
  • studiepoäng 140
  • \n
  • anteckningar: tillägstid i tenter
  • \n

Studerande Saga Studerande (98765):

    \n
  • studiepoäng 0
  • \n
  • anteckningar: avlägsen studieår 20-21
  • \n

OBS: Det finns ingen sättar-metod för attributet studerande_nummer eftersom det inte är meningen att studerandenumret ska ändras.

Det finns en ganska betydande hake när man använder standardvärden för parametrar. Följande exempel som modellerar ännu en typ av studerande kommer att belysa detta mer:

class Studerande:\n    def __init__(self, namn, gjorda_kurser=[]):\n        self.namn = namn\n        self.gjorda_kurser = gjorda_kurser\n\n    def tillsatt_prestation(self, kurs):\n        self.gjorda_kurser.append(kurs)
studerande1 = Studerande(\"Sam Studerande\")\nstuderande2 = Studerande(\"Saul Studerande\")\n\nstuderande1.tillsatt_prestation(\"ItP\")\nstuderande1.tillsatt_prestation(\"Tira\")\n\nprint(studerande1.gjorda_kurser)\nprint(studerande2.gjorda_kurser)

['ItP', 'Tira']\n['ItP', 'Tira']

Om du lägger till slutförda kurser i Sams lista läggs dessa kurser också till i Sauls lista. Faktum är att dessa två är exakt samma lista, eftersom Python återanvänder referensen som lagras i standardvärdet. Att skapa de två nya Studerande-objekten i exemplet ovan är likvärdigt med följande:

kurser = []\nstuderande1 = Studerande(\"Sam Studerande\", kurser)\nstuderande2 = Studerande(\"Saul Studerande\", kurser)

Standardvärdena för parametrar bör aldrig vara instanser av mer komplicerade, föränderliga datastrukturer, t.ex. listor. Problemet kan kringgås genom att göra följande ändringar i konstruktorn för klassen Studerande:

class Studerande:\n    def __init__(self, namn, gjorda_kurser=None):\n        self.namn = namn\n        if gjorda_kurser is None:\n            self.gjorda_kurser = []\n        else:\n            self.gjorda_kurser = gjorda_kurser\n\n    def tillsatt_prestation(self, kurs):\n        self.gjorda_kurser.append(kurs)
studerande1 = Studerande(\"Sam Studerande\")\nstuderande2 = Studerande(\"Saul Studerande\")\n\nstuderande1.tillsatt_prestation(\"ItP\")\nstuderande1.tillsatt_prestation(\"Tira\")\n\nprint(studerande1.gjorda_kurser)\nprint(studerande2.gjorda_kurser)

['ItP', 'Tira']\n[]

Den stora finalen

Fastän följande övning avslutar den här delen av materialet, så har de tekniker som krävs för att lösa den redan behandlats i avsnittet som heter Objekt som attribut. Du behöver inte använda @property-dekoratorn eller standardvärden för parametrar i den här övningen. Den här övningen är mycket lik övningarna \"en presentask\" och \"den kortaste personen i rummet\".

I den här uppgiftsserien kommer du skapa klasserna Sak, Resvaska och Lastutrymme, vilka låter dig öva vidare på att jobba på objekt som innehåller referenser till andra objekt.

Sak

Skapa klassen Sak, som används för att skapa saker av olika sorter. Varje sak har ett namn och en vikt (i kg).

Klassen ska fungera enligt följande:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\n\nprint(\"Bokens namn:\", bok.namn())\nprint(\"Bokens vikt:\", bok.vikt())\n\nprint(\"Bok:\", bok)\nprint(\"Telefon:\", telefon)

Programmets utskrift borde vara följande:

Bokens namn: ABC bok\nBokens vikt: 2\nBok: ABC bok (2 kg)\nTelefon: Nokia 3210 (1 kg)

En Sak ska alltså innehålla metoderna vikt och namn, som returnerar värden som lagras i dessa attribut.

Namnet och vikten ska vara inkapslade inom klassen. Följande ska inte fungera:

bok = Sak(\"ABC bok\", 2)\nbok.vikt = 10

Resväska

Skapa klassen Resvaska. Det ska vara möjligt att packa saker i resväskan. En räsväska har dessutom en maximal vikt för saker som lagras i den.

Lägg till följande till klassen:

    \n
  • en konstruktor, som tar maximal vikt som ett argument
  • \n
  • metoden tillsatt_sak, som lägger till saken givet som argument till resväskan. Metoden har inget returvärde.
  • \n
  • metoden __str__, som returnerar en sträng i formatet \"x saker (y kg)\"
  • \n

Klassen ska se till att den sammanlagda vikten av de föremål som förvaras i en Resvaska inte överstiger den maximala vikt som har ställts in för den instansen. Om den maximala vikten skulle överskridas när tillsatt_sak-metoden anropas, ska det nya föremålet inte läggas till i resväskan.

Klassen ska fungera enligt följande:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nresvaska = Resvaska(5)\nprint(resvaska)\n\nresvaska.tillsatt_sak(bok)\nprint(resvaska)\n\nresvaska.tillsatt_sak(telefon)\nprint(resvaska)\n\nresvaska.tillsatt_sak(tegelsten)\nprint(resvaska)

Programmets utskrift borde vara följande:

0 saker (0 kg)\n1 saker (2 kg)\n2 saker (3 kg)\n2 saker (3 kg)

Språkvård

Meddelandet ”1 saker” är inte särskilt grammatiskt. Istället borde det stå ”1 sak”. Gör de ändringar som krävs i din __str__-metod.

Föregående exempel borde nu skriva ut:

0 saker (0 kg)\n1 sak (2 kg)\n2 saker (3 kg)\n2 saker (3 kg)

Alla saker

Tillägg följande metoder till definitionen av din Resvaska-klass:

    \n
  • skriv_ut_saker, som skriver ut alla saker som lagras i resväskan
  • \n
  • vikt, som returnerar ett heltalsnummer som representerar den totala vikten av sakerna i resväskan
  • \n

Klassen borde nu fungera med följande program:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nresvaska = Resvaska(10)\nresvaska.tillsatt_sak(bok)\nresvaska.tillsatt_sak(telefon)\nresvaska.tillsatt_sak(tegelsten)\n\nprint(\"I resväskan finns följande saker:\")\nresvaska.skriv_ut_saker()\nvikt_tot = resvaska.vikt()\nprint(f\"Totalvikt: {vikt_tot} kg\")

Ovanstående programs utskrift borde ge följande resultat:

I resväskan finns följande saker:\nABC bok (2 kg)\nNokia 3210 (1 kg)\nTegelsten (4 kg)\nTotalvikt: 7 kg

Om du har implementerat din Resvaska-klass med fler än två instansvariabler, gör de ändringar som krävs så att varje instans endast har två dataattribut: den maximala vikten och en lista över föremålen i den.

Den tyngsta saken

Lägg till en ny metod i din Resvaska-klass: tyngsta_saken ska returnera den sak som är tyngst. Om det finns två eller flera föremål med samma vikt kan metoden returnera vilket som helst av dessa. Metoden ska returnera en referens till ett objekt. Om resväskan är tom ska metoden returnera None.

Din klass borde nu fungera med följande program:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nresvaska = Resvaska(10)\nresvaska.tillsatt_sak(bok)\nresvaska.tillsatt_sak(telefon)\nresvaska.tillsatt_sak(tegelsten)\n\ntyngsta = resvaska.tyngsta_saken()\nprint(f\"Tyngsta saken: {tyngsta}\")

Programmets utskrift borde nu vara följande:

Tyngsta saken: Tegelsten (4 kg)

Lastutrymme

Skapa klassen Lastutrymme, som har följande metoder:

    \n
  • en konstruktor, som får en maximalvikt
  • \n
  • metoden tillsatt_resvaska, som lägger till resväskan den får som argument till lastutrymmet
  • \n
  • metoden __str__, som returnerar en sträng i formatet \"x resväskor, rum för y kg\"
  • \n

Klassen bör se till att den sammanlagda vikten av de föremål som lagras i ett Lastutrymme inte överstiger den maximala vikt som har ställts in för den instansen. Om den maximala vikten skulle överskridas när tillsatt_resvaska-metoden anropas, ska den nya resväskan inte läggas till i lastutrymmet.

Din klass borde nu fungera med följande program:

lastutrymme = Lastutrymme(1000)\nprint(lastutrymme)\n\nbok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nadas_vaska = Resvaska(10)\nadas_vaska.tillsatt_sak(bok)\nadas_vaska.tillsatt_sak(telefon)\n\npeters_vaska = Resvaska(10)\npeters_vaska.tillsatt_sak(tegelsten)\n\nlastutrymme.tillsatt_resvaska(adas_vaska)\nprint(lastutrymme)\n\nlastutrymme.tillsatt_resvaska(peters_vaska)\nprint(lastutrymme)

0 resväskor, rum för 1000 kg\n1 resväska, rum för 997 kg\n2 resväskor, rum för 993 kg

Lastutrymmets innehåll

Lägg till metoden skriv_ut_saker, till din Lastutrymme-klass. Metoden ska skriva ut alla saker i alla resväskor i lastutrymmet.

Din klass borde nu fungera med följande program:

bok = Sak(\"ABC bok\", 2)\ntelefon = Sak(\"Nokia 3210\", 1)\ntegelsten = Sak(\"Tegelsten\", 4)\n\nadas_vaska = Resvaska(10)\nadas_vaska.tillsatt_sak(bok)\nadas_vaska.tillsatt_sak(telefon)\n\npeters_vaska = Resvaska(10)\npeters_vaska.tillsatt_sak(tegelsten)\n\nlastutrymme = Lastutrymme(1000)\nlastutrymme.tillsatt_resvaska(adas_vaska)\nlastutrymme.tillsatt_resvaska(peters_vaska)\n\nprint(\"Väskorna i lastutrymmet innehåller följande saker:\")\nlastutrymme.skriv_ut_saker()

Utskriften för ovanstående program borde vara följande:

Väskorna i lastutrymmet innehåller följande saker:\nABC bok (2 kg)\nNokia 3210 (1 kg)\nTegelsten (4 kg)

Svara avslutningsvis på följande frågeformulär:

","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/6-fler-exempel-med-klasser.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"}}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"}}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"}}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"}}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"}}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"}}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"}}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"}}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"}}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"}}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"}}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"}}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"}}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"}}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"}}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"}}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"}}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"}}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"}}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"}}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"}}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"}}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"}}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"}}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"}}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"}}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"}}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"}}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"}}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"}}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"}}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"}}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"}}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"}}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"}}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"}}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"}}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"}}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"}}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"}}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"}}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"}}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"}}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"}}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"}}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"}}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"}}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"}}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"}}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"}}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"}}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"}}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"}}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"}}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"}}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"}}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"}}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"}}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"}}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"}}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "}}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"}}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"}}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"}}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/osa-9/page-data.json b/page-data/osa-9/page-data.json index 4e3200144..171438663 100644 --- a/page-data/osa-9/page-data.json +++ b/page-data/osa-9/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/osa-9", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-9","title":"Modul 9"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
\n
","frontmatter":{"path":"/osa-9","title":"Modul 9"},"fileAbsolutePath":"/home/runner/work/ohjelmointi-24-sv/ohjelmointi-24-sv/data/osa-9/index.md"},"allPages":{"edges":[{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"path":"/bedomningar-och-prov","title":"Bedömning och prov"},"moocfiExercises":[]}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"path":"/","title":"Info om kursen"},"moocfiExercises":[]}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"path":"/alla-uppgifter","title":"Alla uppgifter"},"moocfiExercises":[]}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"path":"/stod","title":"Handledning och stöd"},"moocfiExercises":[]}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"path":"/ofta-stallda-fragar","title":"Ofta ställda frågor"},"moocfiExercises":[]}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"path":"/onlinetent","title":"Nättentamen 4.6."},"moocfiExercises":[]}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"path":"/onlinetent-ohja","title":"Fortsättningskurs i programmerings nättentamen 16.12."},"moocfiExercises":[]}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"path":"/onlinetent-ohpe","title":"Introduktionskurs i programmerings nättentamen 17.08."},"moocfiExercises":[]}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"path":"/felanmalningar","title":"Vanliga felmeddelanden"},"moocfiExercises":[]}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"path":"/sanasto","title":"Sanasto"},"moocfiExercises":[]}},{"node":{"id":"9910673b-d62d-5d8d-a756-5267160d891f","frontmatter":{"path":"/osa-1/1-introduktion","title":"Introduktion"},"moocfiExercises":[{"id":"Leende","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Fixa programmet: Sju bröder","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Gubben Noak","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Minuter i ett år","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"Kod som skriver ut kod","type":"programming-exercise","parentPagePath":"/osa-1/1-introduktion"},{"id":"4b736942-71b8-5171-8427-508b57d1776b","type":"quiz","parentPagePath":"/osa-1/1-introduktion"}]}},{"node":{"id":"0b17a28e-f939-57cc-aa5b-4db407cdf34a","frontmatter":{"path":"/osa-1/2-info-fran-anvandaren","title":"Information från användaren"},"moocfiExercises":[{"id":"Namn, två gånger","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn med utropstecken","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Namn och adress","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Fixa programmet: Uttryck","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-1/2-info-fran-anvandaren"},{"id":"96aa989d-0b30-57f6-8bbf-4b7f462e8f14","type":"quiz","parentPagePath":"/osa-1/2-info-fran-anvandaren"}]}},{"node":{"id":"b95794f2-f3b3-597c-a4ec-6f023b584f81","frontmatter":{"path":"/osa-1/3-mer-om-variabler","title":"Mer om variabler"},"moocfiExercises":[{"id":"64c34f03-2a59-5f9e-8850-e2112625b111","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Med mellanslag eller utan","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Räkneoperationer","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"Fixa programmet: Utskrifterna på samma rad","type":"programming-exercise","parentPagePath":"/osa-1/3-mer-om-variabler"},{"id":"5e01aa77-89a6-5ccd-ba2d-6890180680e2","type":"quiz","parentPagePath":"/osa-1/3-mer-om-variabler"}]}},{"node":{"id":"02cd0849-e7a5-5fd1-bf66-35f0197d3e75","frontmatter":{"path":"/osa-1/4-rakneoperationer","title":"Räkneoperationer"},"moocfiExercises":[{"id":"Siffra gånger fem","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Namn och ålder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Dygn i sekunder","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Fixa programmet: Produkten av talen","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Talens summa och produkt","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Summa och medeltal","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Matkostnader","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"Gruppindelning","type":"programming-exercise","parentPagePath":"/osa-1/4-rakneoperationer"},{"id":"7722d5cd-4b10-5b39-80cc-834d09f01ff2","type":"quiz","parentPagePath":"/osa-1/4-rakneoperationer"}]}},{"node":{"id":"42690a69-3997-5b84-b999-15d9c3212884","frontmatter":{"path":"/osa-1/5-villkorssatser","title":"If-satser"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Absolutbelopp","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Soppa. Eller inte.","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Storleksklass","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Räknare","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Temperaturer","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Lön","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Fixa programmet: Ränta","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Morgondagens klädsel","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"Andra gradens ekvation","type":"programming-exercise","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"cb6eff3b-2fb8-506d-9019-e97de6834d7e","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"},{"id":"5f238c10-5e09-5a35-94f5-d9a0b56d9e79","type":"quiz","parentPagePath":"/osa-1/5-villkorssatser"}]}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"path":"/osa-1","title":"Modul 1"},"moocfiExercises":[]}},{"node":{"id":"e7a4d0c8-b8c1-53fa-ab51-9cc06fd417a3","frontmatter":{"path":"/osa-10/1-klasshierarkier","title":"Klasshierarkier"},"moocfiExercises":[{"id":"Bärbar dator","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Spelmuseum","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Arean","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"},{"id":"Ordspel","type":"programming-exercise","parentPagePath":"/osa-10/1-klasshierarkier"}]}},{"node":{"id":"5f6d4b8b-7d05-5c79-9f2f-9e30b397167b","frontmatter":{"path":"/osa-10/2-atkamstmodifierare","title":"Åtkomstmodifierare"},"moocfiExercises":[{"id":"Supergrupp","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"},{"id":"Hemlig trolldryck","type":"programming-exercise","parentPagePath":"/osa-10/2-atkamstmodifierare"}]}},{"node":{"id":"bb896c90-aabc-5a9d-8d4b-50839da286db","frontmatter":{"path":"/osa-10/3-objektorienterade-programmeringstekniker","title":"Objektorienterade programmeringstekniker"},"moocfiExercises":[{"id":"Pengar","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"SimpelDatum","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"},{"id":"Itererbar affärslista","type":"programming-exercise","parentPagePath":"/osa-10/3-objektorienterade-programmeringstekniker"}]}},{"node":{"id":"a2dca4f1-d64d-5e77-a3ca-8b2a208e9afa","frontmatter":{"path":"/osa-10/4-storre-applikation","title":"Att utveckla en större applikation"},"moocfiExercises":[{"id":"Utveckling av telefonkatalogen, del 1","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Utveckling av telefonkatalogen, del 2","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"Studieregister","type":"programming-exercise","parentPagePath":"/osa-10/4-storre-applikation"},{"id":"4d7c8be7-105b-5bd2-87dd-7da65ff664e7","type":"quiz","parentPagePath":"/osa-10/4-storre-applikation"}]}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"path":"/osa-10","title":"Modul 10"},"moocfiExercises":[]}},{"node":{"id":"f6688d26-5502-5c17-a8ee-db66f650f1f1","frontmatter":{"path":"/osa-11/1-list-comprehension","title":"List comprehension"},"moocfiExercises":[{"id":"Kvadratrötter","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Stjärnrader","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Bäst provresultat","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Längder","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Ta bort mindre","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Börjar med vokal","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"},{"id":"Lotterinummer","type":"programming-exercise","parentPagePath":"/osa-11/1-list-comprehension"}]}},{"node":{"id":"1eac8489-72f2-5d53-904b-c960e2622018","frontmatter":{"path":"/osa-11/2-mer-om-comprehensions","title":"Mer om comprehensions"},"moocfiExercises":[{"id":"Filtrera förbjudna","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Affärslistans produkter","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Billigare prisskillnad","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Strängarnas längder","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"},{"id":"Vanligaste orden","type":"programming-exercise","parentPagePath":"/osa-11/2-mer-om-comprehensions"}]}},{"node":{"id":"09630fca-7768-53f3-8aca-6e5ac638fc68","frontmatter":{"path":"/osa-11/3-rekursion","title":"Rekursion"},"moocfiExercises":[{"id":"Större tal","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Rekursiv summa","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"},{"id":"Balanserade parenteser","type":"programming-exercise","parentPagePath":"/osa-11/3-rekursion"}]}},{"node":{"id":"70f6008e-a762-574c-a92a-c5c585d9677e","frontmatter":{"path":"/osa-11/4-lisaa-esimerkkeja","title":"Fler exempel på rekursion"},"moocfiExercises":[{"id":"Största noden","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Chefer och underordnade","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsbok","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"Uppgiftsboksapplikation","type":"programming-exercise","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"},{"id":"3435ec4c-3236-59f6-82c1-664967213776","type":"quiz","parentPagePath":"/osa-11/4-lisaa-esimerkkeja"}]}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"path":"/osa-11","title":"Modul 11"},"moocfiExercises":[]}},{"node":{"id":"9bba31dc-e14c-5400-a25b-7339107e9ba7","frontmatter":{"path":"/osa-12/1-funktioner-som-argument","title":"Funktioner som argument"},"moocfiExercises":[{"id":"Sortering enligt återstående lager","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt produktionssäsonger","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Sortering enligt poäng","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsrutt","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Klättringsområde","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Bollspelare","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"},{"id":"Söking av produkter","type":"programming-exercise","parentPagePath":"/osa-12/1-funktioner-som-argument"}]}},{"node":{"id":"5b9e4584-7234-5216-a118-d2a1bc617e13","frontmatter":{"path":"/osa-12/2-generatorer","title":"Generatorer"},"moocfiExercises":[{"id":"Jämna tal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Primtal","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-12/2-generatorer"}]}},{"node":{"id":"1d27636e-74f4-5c30-a1d8-08a303684945","frontmatter":{"path":"/osa-12/3-funktionell-programmering","title":"Funktionell programmering"},"moocfiExercises":[{"id":"Prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Begränsade prestationer","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"},{"id":"Studiepoäng","type":"programming-exercise","parentPagePath":"/osa-12/3-funktionell-programmering"}]}},{"node":{"id":"6785750f-f145-5b88-81e9-b52e36ba53c6","frontmatter":{"path":"/osa-12/4-reguljara-uttryck","title":"Reguljära uttryck"},"moocfiExercises":[{"id":"Reguljära uttryck","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"Statistik i ordning","type":"programming-exercise","parentPagePath":"/osa-12/4-reguljara-uttryck"},{"id":"9b73dc6b-2a11-59c5-a0aa-12e4250211a8","type":"quiz","parentPagePath":"/osa-12/4-reguljara-uttryck"}]}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"path":"/osa-12","title":"Modul 12"},"moocfiExercises":[]}},{"node":{"id":"14e3c129-50ec-581f-8679-8132248d41c1","frontmatter":{"path":"/osa-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Fyra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Robotar i rad","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Hundra robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"},{"id":"Slumpmässiga robotar","type":"programming-exercise","parentPagePath":"/osa-13/1-pygame"}]}},{"node":{"id":"01bc3b9b-4f80-5561-8b56-3a7f09082263","frontmatter":{"path":"/osa-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertikal rörelse","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Längs med kanten","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Två robotar","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Ringlek","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Studsande boll","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/osa-13/2-animation"}]}},{"node":{"id":"dd68786f-e2fc-5957-b47a-52b74c5199ef","frontmatter":{"path":"/osa-13/3-handelser","title":"Händelser"},"moocfiExercises":[{"id":"Fyra riktningar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Fyra väggar","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Två spelare","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Roboten och musen","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"},{"id":"Robotens plats","type":"programming-exercise","parentPagePath":"/osa-13/3-handelser"}]}},{"node":{"id":"4a1aaa41-d6f7-51cd-8b89-95842cb9e02d","frontmatter":{"path":"/osa-13/4-fler-pygame-tekniker","title":"Fler pygame-tekniker"},"moocfiExercises":[{"id":"Kello","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"Asteroider","type":"programming-exercise","parentPagePath":"/osa-13/4-fler-pygame-tekniker"},{"id":"7ac40922-75c5-5306-96b6-029dec86aad9","type":"quiz","parentPagePath":"/osa-13/4-fler-pygame-tekniker"}]}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"path":"/osa-13","title":"Modul 13"},"moocfiExercises":[]}},{"node":{"id":"0ab28435-7654-5a8e-88d6-daa17d78103b","frontmatter":{"path":"/osa-14/1-spelprojekt","title":"Spelprojekt"},"moocfiExercises":[]}},{"node":{"id":"b7a0628c-e4ba-583e-a52a-dd0ad78028d8","frontmatter":{"path":"/osa-14/2-robot-och-ladar","title":"Robot och lådor"},"moocfiExercises":[]}},{"node":{"id":"6ccdbdff-7539-5a23-b23c-2ad48a99386f","frontmatter":{"path":"/osa-14/3-fardigstallande-av-spelet","title":"Färdigställande av spelet"},"moocfiExercises":[]}},{"node":{"id":"1b505c9f-9b1b-58bd-8df7-553fef49cc8d","frontmatter":{"path":"/osa-14/4-ditt-eget-sepl","title":"Ditt eget spel"},"moocfiExercises":[{"id":"037bc92c-a684-5a29-979a-85fa23e0882b","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"5632d8a9-3add-5ce1-8a69-c2ef4351ca5d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"},{"id":"fa951288-f547-5603-8572-55f5359c209d","type":"quiz","parentPagePath":"/osa-14/4-ditt-eget-sepl"}]}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"path":"/osa-14","title":"Modul 14"},"moocfiExercises":[]}},{"node":{"id":"b5c346d1-d175-5824-98bb-4a4c3b45204d","frontmatter":{"path":"/osa-2/1-terminologi","title":"Programmeringsterminologi"},"moocfiExercises":[{"id":"Fixa felen","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Antalet tecken","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"Konvertera värden","type":"programming-exercise","parentPagePath":"/osa-2/1-terminologi"},{"id":"02c9e0da-7357-5476-a478-ec1b0cad2b1f","type":"quiz","parentPagePath":"/osa-2/1-terminologi"}]}},{"node":{"id":"db7188b0-0253-5c30-b8d2-0e115e235fb3","frontmatter":{"path":"/osa-2/2-else-elif","title":"Mera om if-satser"},"moocfiExercises":[{"id":"Myndig eller inte?","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Större eller lika med","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"Sist i alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/2-else-elif"},{"id":"908fdd39-c6bc-5365-9731-442a7877a47d","type":"quiz","parentPagePath":"/osa-2/2-else-elif"}]}},{"node":{"id":"49d15928-f8b9-5883-9f29-86c307f6929e","frontmatter":{"path":"/osa-2/3-kombinera-villkor","title":"Kombinera villkor"},"moocfiExercises":[{"id":"Kontroll av ålder","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Brorsöner","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Vitsord och poäng","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Skottår","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"I mitten av alfabetet","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"Gåvoskatt","type":"programming-exercise","parentPagePath":"/osa-2/3-kombinera-villkor"},{"id":"8a61b73e-243e-518f-8409-e209dbdd05ba","type":"quiz","parentPagePath":"/osa-2/3-kombinera-villkor"}]}},{"node":{"id":"93f1f9c8-6b27-5892-8bd4-7d1d008bf07a","frontmatter":{"path":"/osa-2/4-loopar","title":"Enkla loopar"},"moocfiExercises":[{"id":"Fortsätter vi?","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Valida värden","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Lösenord på nytt","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Pin och antal försök","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Nästa skottår","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Berättelse","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"Behandling av tal","type":"programming-exercise","parentPagePath":"/osa-2/4-loopar"},{"id":"fc9f8642-2465-55f3-8f3a-2d649a0849d3","type":"quiz","parentPagePath":"/osa-2/4-loopar"},{"id":"dde69083-2fe5-5493-ae10-9cfa75a04aa4","type":"quiz","parentPagePath":"/osa-2/4-loopar"}]}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"path":"/osa-2","title":"Modul 2"},"moocfiExercises":[]}},{"node":{"id":"02012307-c2a6-5cf2-9105-c10933c14b37","frontmatter":{"path":"/osa-3/1-loopar-med-villkor","title":"Loopar med villkor"},"moocfiExercises":[{"id":"Skriv ut siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Nedräkning","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Siffror","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Potenser av två","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"n:te potensen","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 1","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"Summa av varandra följande tal, version 2","type":"programming-exercise","parentPagePath":"/osa-3/1-loopar-med-villkor"},{"id":"7a997c9b-0897-524d-9725-07def37b0272","type":"quiz","parentPagePath":"/osa-3/1-loopar-med-villkor"}]}},{"node":{"id":"efb889f4-4a19-515e-a0f6-4297d76a2757","frontmatter":{"path":"/osa-3/2-behandla-strangar","title":"Behandla strängar"},"moocfiExercises":[{"id":"Flera strängar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Den längre strängen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Omvänt","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra och näst sista","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Speciellt streck","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Understrykning","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Högerjusterat","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Ordlåda","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 1","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Delsträngar 2","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hittas vokaler?","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta första delsträngen","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Hitta alla delsträngar","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"Andra förekomsten","type":"programming-exercise","parentPagePath":"/osa-3/2-behandla-strangar"},{"id":"90c17193-9a86-56c4-a51c-1ae816a05a12","type":"quiz","parentPagePath":"/osa-3/2-behandla-strangar"}]}},{"node":{"id":"a602077d-bbae-5eb7-a354-cc44b29ae96a","frontmatter":{"path":"/osa-3/3-mer-om-loopar","title":"Mera om loopar"},"moocfiExercises":[{"id":"Multiplikationstabell","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Ordens första bokstäver","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Sväng paren","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"Turvis","type":"programming-exercise","parentPagePath":"/osa-3/3-mer-om-loopar"},{"id":"1aadeb85-a3b9-513e-87f5-a12b9d762f0b","type":"quiz","parentPagePath":"/osa-3/3-mer-om-loopar"}]}},{"node":{"id":"6d878692-b9c7-57e6-ae58-044e5a6bef6f","frontmatter":{"path":"/osa-3/4-definiera-funktioner","title":"Definiera funktioner"},"moocfiExercises":[{"id":"Sju bröder","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Första tecknet","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Medelvärde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Flera utskrifter","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Fyrkant","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Schackbräde","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"Ordkvadrat","type":"programming-exercise","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"2b3067e7-08e8-500f-985e-4edc7cfd5fd6","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"},{"id":"bb18ac32-1984-5ea8-acb6-a51f695e2542","type":"quiz","parentPagePath":"/osa-3/4-definiera-funktioner"}]}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"path":"/osa-3","title":"Modul 3"},"moocfiExercises":[]}},{"node":{"id":"3de95518-6b44-54db-9bcd-0c90a41d2fd5","frontmatter":{"path":"/osa-4/1-vscode","title":"Editorn Visual Studio Code, Pythontolken och det inbyggda debuggningsverktyget"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/osa-4/1-vscode"}]}},{"node":{"id":"7de9921c-7f94-58fe-84b9-7676dddb3b94","frontmatter":{"path":"/osa-4/2-mer-om-funktioner","title":"Mer om funktioner"},"moocfiExercises":[{"id":"Streck","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Fyrkant (igen)","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Speciell kvadrat","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Triangel","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Figur","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Julgran","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Störst av talen","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Lika tecken","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"Första, andra och sista","type":"programming-exercise","parentPagePath":"/osa-4/2-mer-om-funktioner"},{"id":"5702d07b-676c-5ec2-ac2e-3d0f3b1b7d4e","type":"quiz","parentPagePath":"/osa-4/2-mer-om-funktioner"}]}},{"node":{"id":"29e29c69-360c-5f5c-8f34-5aef22eb327f","frontmatter":{"path":"/osa-4/3-listor","title":"Listor"},"moocfiExercises":[{"id":"Ändra på elementens värden","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till element i en lista","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Lägg till, ta bort","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Samma ord två gånger","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"En lista, två varianter","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Listans längd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Medeltal","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"Variationsbredd","type":"programming-exercise","parentPagePath":"/osa-4/3-listor"},{"id":"28b681c8-dcad-56bd-a01d-21e0c947134d","type":"quiz","parentPagePath":"/osa-4/3-listor"}]}},{"node":{"id":"bfe57880-0cbb-54e4-be9c-8fa180eafe6b","frontmatter":{"path":"/osa-4/4-iteration","title":"Iteration"},"moocfiExercises":[{"id":"Utskrift med asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Från negativ till positiv","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Asterisker","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Anagram","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Palindrom","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summan av de positiva","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Jämna","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Summalista","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Unika","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Längden av den längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans kortaste","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"Listans längsta","type":"programming-exercise","parentPagePath":"/osa-4/4-iteration"},{"id":"f0f76ddb-47d3-56ff-8bf4-2556f3f57af6","type":"quiz","parentPagePath":"/osa-4/4-iteration"}]}},{"node":{"id":"2eab46f0-5373-5cbe-b2a0-1d071854a053","frontmatter":{"path":"/osa-4/5-utskrift","title":"Formatera utskrift"},"moocfiExercises":[{"id":"Från sifferlista till stränglista","type":"programming-exercise","parentPagePath":"/osa-4/5-utskrift"},{"id":"c06c0359-d590-5b81-ba81-0faedf42b216","type":"quiz","parentPagePath":"/osa-4/5-utskrift"}]}},{"node":{"id":"a5974fee-f2eb-5b89-88a5-6dd3754baaad","frontmatter":{"path":"/osa-4/6-strangar-listor","title":"Mera strängar och listor"},"moocfiExercises":[{"id":"Allt omvänt","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vanligaste bokstaven","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Utan vokaler","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Versaler bort","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Grannar i en lista","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"Vitsordsstatistik","type":"programming-exercise","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"810e59c2-b9f9-51cc-b8e9-e716b3247515","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"},{"id":"90c165f5-711a-5b1a-a5e3-8e029a2245fb","type":"quiz","parentPagePath":"/osa-4/6-strangar-listor"}]}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"path":"/osa-4","title":"Modul 4"},"moocfiExercises":[]}},{"node":{"id":"2558444a-be1d-5bc6-95ab-e091da2443fc","frontmatter":{"path":"/osa-5/1-mer-om-listor","title":"Mer om listor"},"moocfiExercises":[{"id":"Längsta strängen","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Antalet element","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Go","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Rader korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kolumner korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Kvadrater korrekt","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"Sudoku: Korrekt?","type":"programming-exercise","parentPagePath":"/osa-5/1-mer-om-listor"},{"id":"9157a211-3209-56ba-8c9f-be456d3b1af8","type":"quiz","parentPagePath":"/osa-5/1-mer-om-listor"}]}},{"node":{"id":"b452d9be-0569-56c2-ba89-a040d4cb40dc","frontmatter":{"path":"/osa-5/2-referenser","title":"Referenser"},"moocfiExercises":[{"id":"Elementen fördubblade","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Bort med minsta","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Utskrift och att lägga till en siffra","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Sudoku: Lägga till en siffra i en kopia av sudokut","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Tre i rad","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"Transponera","type":"programming-exercise","parentPagePath":"/osa-5/2-referenser"},{"id":"21ff9f90-0aed-591d-8ff0-60e790724739","type":"quiz","parentPagePath":"/osa-5/2-referenser"}]}},{"node":{"id":"ebe4ca3f-e657-5907-bfcc-ae84d9947aff","frontmatter":{"path":"/osa-5/3-lexikon","title":"Lexikon"},"moocfiExercises":[{"id":"Gånger tio","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Fakulteter","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 1","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Telefonkatalog, version 2","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Att vända ett lexikon","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Siffror som ord","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Filmregister","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"Hitta film","type":"programming-exercise","parentPagePath":"/osa-5/3-lexikon"},{"id":"7228e663-3942-5c8c-b00e-49e194a6d394","type":"quiz","parentPagePath":"/osa-5/3-lexikon"}]}},{"node":{"id":"f1dd5d66-ac87-5e53-92a0-0b7e3ece6ed8","frontmatter":{"path":"/osa-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Skapa tuple","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldst","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Äldre","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Studeranderegister","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"Bokstavsruta","type":"programming-exercise","parentPagePath":"/osa-5/4-tuple"},{"id":"7317f5ec-99ed-5641-9829-092aa4ef6c04","type":"quiz","parentPagePath":"/osa-5/4-tuple"},{"id":"d0fa423f-3772-5385-ae9d-b1f6ad9d2e79","type":"quiz","parentPagePath":"/osa-5/4-tuple"}]}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"path":"/osa-5","title":"Modul 5"},"moocfiExercises":[]}},{"node":{"id":"e6cfa6e1-e99b-50e0-bc6a-3c0f153420e8","frontmatter":{"path":"/osa-6/1-lasa-filer","title":"Läsa filer"},"moocfiExercises":[{"id":"Största siffran","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Fruktaffär","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Matris","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 1","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 2","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Kursresultat, del 3","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Receptsök","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"Stadscyklar","type":"programming-exercise","parentPagePath":"/osa-6/1-lasa-filer"},{"id":"61d39363-2bed-504e-8181-41badf9b7e57","type":"quiz","parentPagePath":"/osa-6/1-lasa-filer"}]}},{"node":{"id":"ce8d90a4-ca7c-5797-abc1-3fe0094bfa4b","frontmatter":{"path":"/osa-6/2-skriva-filer","title":"Skriva filer"},"moocfiExercises":[{"id":"Tillägnad för...","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Dagbok","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Filtrera data","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Lagra personer","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Kursresultat, del 4","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordsök","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"Ordbok som minns","type":"programming-exercise","parentPagePath":"/osa-6/2-skriva-filer"},{"id":"33ad7375-10df-5850-9744-813bb01ac2b4","type":"quiz","parentPagePath":"/osa-6/2-skriva-filer"}]}},{"node":{"id":"519d5330-277a-59ba-be50-c2f63080df94","frontmatter":{"path":"/osa-6/3-fel","title":"Förbered dig på fel"},"moocfiExercises":[{"id":"Läsa indata","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Validera givna argument","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"Felaktiga lottorader","type":"programming-exercise","parentPagePath":"/osa-6/3-fel"},{"id":"e9f4deb4-df09-5ff3-9407-d3d6140568de","type":"quiz","parentPagePath":"/osa-6/3-fel"}]}},{"node":{"id":"82393756-3ef8-5d07-a8da-fed818ca43f0","frontmatter":{"path":"/osa-6/4-lokala-globala-variabler","title":"Lokala och globala variabler"},"moocfiExercises":[{"id":"1d2219f9-7421-553f-9e49-b31f2ee8526e","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"},{"id":"115f1983-91d3-5389-9022-31fa5e06fcf1","type":"quiz","parentPagePath":"/osa-6/4-lokala-globala-variabler"}]}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"path":"/osa-6","title":"Modul 6"},"moocfiExercises":[]}},{"node":{"id":"bbb2346d-b1ee-551d-89c1-516f119b0eed","frontmatter":{"path":"/osa-7/1-moduler","title":"Moduler"},"moocfiExercises":[{"id":"Hypotenusa","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Specialtecken","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"Bråk","type":"programming-exercise","parentPagePath":"/osa-7/1-moduler"},{"id":"bd59e044-7533-5711-839f-e065dedd666d","type":"quiz","parentPagePath":"/osa-7/1-moduler"}]}},{"node":{"id":"ecb9af64-49f9-5e28-a544-3934e95233cf","frontmatter":{"path":"/osa-7/2-slumpmassighet","title":"Slumpmässighet"},"moocfiExercises":[{"id":"Lottorader","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 1","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Lösenordsgenerator, del 2","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Tärningssimulation","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"Slumpmässiga ord","type":"programming-exercise","parentPagePath":"/osa-7/2-slumpmassighet"},{"id":"8b15ae92-4b54-5fb8-a03b-2aadc4bb789d","type":"quiz","parentPagePath":"/osa-7/2-slumpmassighet"}]}},{"node":{"id":"ebe469ef-c486-523a-9621-5dc4040c5cd8","frontmatter":{"path":"/osa-7/3-tid-datum","title":"Tid och datum"},"moocfiExercises":[{"id":"Hur gammal?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Personbeteckning rätt?","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"Skärmtid","type":"programming-exercise","parentPagePath":"/osa-7/3-tid-datum"},{"id":"802c4053-2c0b-5efb-9502-75c09c5f3a39","type":"quiz","parentPagePath":"/osa-7/3-tid-datum"}]}},{"node":{"id":"b9cf2d99-08f6-5de5-8425-c9b483241567","frontmatter":{"path":"/osa-7/4-behandla-data","title":"Behandla data"},"moocfiExercises":[{"id":"JSON-fil","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Kursstatistik","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Vem lurade, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"Spell checker, andra versionen","type":"programming-exercise","parentPagePath":"/osa-7/4-behandla-data"},{"id":"3388732e-5324-53ce-8908-865a5fbf47c8","type":"quiz","parentPagePath":"/osa-7/4-behandla-data"}]}},{"node":{"id":"27075b37-8a6a-5554-9079-465a36d2da2d","frontmatter":{"path":"/osa-7/5-egna-moduler","title":"Skapa dina egna moduler"},"moocfiExercises":[{"id":"Teckenverktyg","type":"programming-exercise","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"dcddffa9-9c51-5831-85f3-990d16b717d5","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"},{"id":"b0d3884e-afb8-57a5-a50e-39f5e0c5d73c","type":"quiz","parentPagePath":"/osa-7/5-egna-moduler"}]}},{"node":{"id":"69492225-4d69-59ea-b33d-5301d35cf4ed","frontmatter":{"path":"/osa-7/6-funktionaliteter-python","title":"Flera funktionaliteter i Python"},"moocfiExercises":[{"id":"Eget programmeringsspråk","type":"programming-exercise","parentPagePath":"/osa-7/6-funktionaliteter-python"},{"id":"21de019f-ba83-56f0-a68b-31c5c62dcf9a","type":"quiz","parentPagePath":"/osa-7/6-funktionaliteter-python"}]}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"path":"/osa-7","title":"Modul 7"},"moocfiExercises":[]}},{"node":{"id":"4afc4486-3cfe-53d1-b4c0-6db26484acfa","frontmatter":{"path":"/osa-8/1-objekt-och-metoder","title":"Objekt och metoder"},"moocfiExercises":[{"id":"Minsta medeltalet","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"},{"id":"Radernas summor","type":"programming-exercise","parentPagePath":"/osa-8/1-objekt-och-metoder"}]}},{"node":{"id":"6be37772-e94b-5b36-8a02-fffa7eef9feb","frontmatter":{"path":"/osa-8/2-klasser-och-objekt","title":"Klasser och objekt"},"moocfiExercises":[{"id":"Lista år","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"},{"id":"Affarslista","type":"programming-exercise","parentPagePath":"/osa-8/2-klasser-och-objekt"}]}},{"node":{"id":"8b6aec39-f8b9-525c-9f25-d8e73389fa42","frontmatter":{"path":"/osa-8/3-egna-klasser","title":"Egna klasser"},"moocfiExercises":[{"id":"Bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skriv klasser","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Skapa husdjur","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Äldre bok","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"},{"id":"Genrens böcker","type":"programming-exercise","parentPagePath":"/osa-8/3-egna-klasser"}]}},{"node":{"id":"85db460a-77c6-5087-8e97-a3c772186fd9","frontmatter":{"path":"/osa-8/4-metoder-i-egna-klasser","title":"Metoder i egna klasser"},"moocfiExercises":[{"id":"Minskande räknare","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"För- och efternamn","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"},{"id":"Nummerstatistik","type":"programming-exercise","parentPagePath":"/osa-8/4-metoder-i-egna-klasser"}]}},{"node":{"id":"0e59dee0-5dd9-5827-83b9-a55e7d585c31","frontmatter":{"path":"/osa-8/5-fler-exempel","title":"Fler exempel"},"moocfiExercises":[{"id":"Stoppur","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Klocka","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Lunchkort","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"Sarja","type":"programming-exercise","parentPagePath":"/osa-8/5-fler-exempel"},{"id":"a02d084c-8f0c-5918-8740-6282910a2b33","type":"quiz","parentPagePath":"/osa-8/5-fler-exempel"}]}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"path":"/osa-8","title":"Modul 8"},"moocfiExercises":[]}},{"node":{"id":"f7457509-a9a6-5420-8d03-ff1c61df8dc4","frontmatter":{"path":"/osa-9/1-objekt-och-referenser","title":"Objekt och referenser"},"moocfiExercises":[{"id":"Snabbaste bilen","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Godkända prestationer","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bebiscenter","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Lunchkort och betalterminal","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"},{"id":"Bostadsjämförelse","type":"programming-exercise","parentPagePath":"/osa-9/1-objekt-och-referenser"}]}},{"node":{"id":"16689ce4-b6aa-5bc4-9a30-44820994d6ea","frontmatter":{"path":"/osa-9/2-objekt-som-attribut","title":"Objekt som attribut "},"moocfiExercises":[{"id":"Husdjur","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Paketpackning","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"},{"id":"Rummets kortaste","type":"programming-exercise","parentPagePath":"/osa-9/2-objekt-som-attribut"}]}},{"node":{"id":"07985959-e85a-5f45-8923-babacb943c04","frontmatter":{"path":"/osa-9/3-inkapsling","title":"Inkapsling"},"moocfiExercises":[{"id":"Bil","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Inspelning","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"},{"id":"Väderstation","type":"programming-exercise","parentPagePath":"/osa-9/3-inkapsling"}]}},{"node":{"id":"83108600-9a37-5fd1-9a2b-13492879a9d6","frontmatter":{"path":"/osa-9/4-metodernas-rackvidd","title":"Metodernas räckvidd"},"moocfiExercises":[{"id":"Service avgift","type":"programming-exercise","parentPagePath":"/osa-9/4-metodernas-rackvidd"}]}},{"node":{"id":"a553cdc9-d6ac-5a14-ab43-d44811b407a2","frontmatter":{"path":"/osa-9/5-klassattribut","title":"Klassattribut"},"moocfiExercises":[{"id":"Postnummer","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"},{"id":"List-hjälpare","type":"programming-exercise","parentPagePath":"/osa-9/5-klassattribut"}]}},{"node":{"id":"f7d7c9f1-6b54-5268-a308-995d49dd913d","frontmatter":{"path":"/osa-9/6-fler-exempel-med-klasser","title":"Fler exempel med klasser"},"moocfiExercises":[{"id":"Sak, Resväska och Lastutrymme","type":"programming-exercise","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"},{"id":"34723602-dd55-54df-914d-bdd2037722f8","type":"quiz","parentPagePath":"/osa-9/6-fler-exempel-med-klasser"}]}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"path":"/osa-9","title":"Modul 9"},"moocfiExercises":[]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/sq/d/1357757885.json b/page-data/sq/d/1357757885.json index 04f256fd3..fb45924e1 100644 --- a/page-data/sq/d/1357757885.json +++ b/page-data/sq/d/1357757885.json @@ -1 +1 @@ -{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"title":"Info om kursen","information_page":true,"course_info_page":null,"path":"/","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":5000}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"title":"Alla uppgifter","information_page":null,"course_info_page":true,"path":"/alla-uppgifter","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":false,"sidebar_priority":3500}}},{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"title":"Bedömning och prov","information_page":true,"course_info_page":null,"path":"/bedomningar-och-prov","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":4000}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"title":"Vanliga felmeddelanden","information_page":true,"course_info_page":null,"path":"/felanmalningar","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"title":"Frontmatter-guide","information_page":false,"course_info_page":null,"path":"/frontmatter-guide","hidden":true,"separator_after":"derp","upcoming":"1.1.","hide_in_sidebar":true,"sidebar_priority":1337}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"title":"Ofta ställda frågor","information_page":true,"course_info_page":null,"path":"/ofta-stallda-fragar","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"title":"Nättentamen 4.6.","information_page":true,"course_info_page":null,"path":"/onlinetent","hidden":null,"separator_after":null,"upcoming":null,"hide_in_sidebar":true,"sidebar_priority":3000}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"title":"Fortsättningskurs i programmerings nättentamen 16.12.","information_page":true,"course_info_page":null,"path":"/onlinetent-ohja","hidden":null,"separator_after":null,"upcoming":null,"hide_in_sidebar":true,"sidebar_priority":2800}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"title":"Introduktionskurs i programmerings nättentamen 17.08.","information_page":true,"course_info_page":null,"path":"/onlinetent-ohpe","hidden":null,"separator_after":null,"upcoming":null,"hide_in_sidebar":true,"sidebar_priority":2900}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"title":"Modul 1","information_page":null,"course_info_page":null,"path":"/osa-1","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"title":"Modul 10","information_page":null,"course_info_page":null,"path":"/osa-10","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"title":"Modul 11","information_page":null,"course_info_page":null,"path":"/osa-11","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"title":"Modul 12","information_page":null,"course_info_page":null,"path":"/osa-12","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"title":"Modul 13","information_page":null,"course_info_page":null,"path":"/osa-13","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"title":"Modul 14","information_page":null,"course_info_page":null,"path":"/osa-14","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"title":"Modul 2","information_page":null,"course_info_page":null,"path":"/osa-2","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"title":"Modul 3","information_page":null,"course_info_page":null,"path":"/osa-3","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"title":"Modul 4","information_page":null,"course_info_page":null,"path":"/osa-4","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"title":"Modul 5","information_page":null,"course_info_page":null,"path":"/osa-5","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"title":"Modul 6","information_page":null,"course_info_page":null,"path":"/osa-6","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"title":"Modul 7","information_page":null,"course_info_page":null,"path":"/osa-7","hidden":false,"separator_after":"Ohjelmoinnin jatkokurssi","upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"title":"Modul 8","information_page":null,"course_info_page":null,"path":"/osa-8","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"title":"Modul 9","information_page":null,"course_info_page":null,"path":"/osa-9","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"title":"Sanasto","information_page":true,"course_info_page":null,"path":"/sanasto","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":true,"sidebar_priority":null}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"title":"Handledning och stöd","information_page":true,"course_info_page":null,"path":"/stod","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}}]}}} \ No newline at end of file +{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"56769133-4836-5fbb-a857-8fb0c6803e6d","frontmatter":{"title":"Info om kursen","information_page":true,"course_info_page":null,"path":"/","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":5000}}},{"node":{"id":"e86762f0-c2a9-5598-aba3-2b8d7db7807c","frontmatter":{"title":"Alla uppgifter","information_page":null,"course_info_page":true,"path":"/alla-uppgifter","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":false,"sidebar_priority":3500}}},{"node":{"id":"b5e8ae41-d492-55f7-88a7-1c7292333ab1","frontmatter":{"title":"Bedömning och prov","information_page":true,"course_info_page":null,"path":"/bedomningar-och-prov","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":4000}}},{"node":{"id":"347bdde4-ba21-5f22-8a88-0287dfe29a45","frontmatter":{"title":"Vanliga felmeddelanden","information_page":true,"course_info_page":null,"path":"/felanmalningar","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"af4df6be-211d-5ba1-acea-9edbefa06c49","frontmatter":{"title":"Frontmatter-guide","information_page":false,"course_info_page":null,"path":"/frontmatter-guide","hidden":true,"separator_after":"derp","upcoming":"1.1.","hide_in_sidebar":true,"sidebar_priority":1337}}},{"node":{"id":"ac195ee0-ad04-5cc9-9643-963f93d29ad2","frontmatter":{"title":"Ofta ställda frågor","information_page":true,"course_info_page":null,"path":"/ofta-stallda-fragar","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"51a165d7-8ce5-5d53-9c15-5321b6adef8c","frontmatter":{"title":"Nättentamen 4.6.","information_page":true,"course_info_page":null,"path":"/onlinetent","hidden":null,"separator_after":null,"upcoming":null,"hide_in_sidebar":false,"sidebar_priority":3000}}},{"node":{"id":"4d9cf293-01e9-5e6c-a88f-599560e33c9a","frontmatter":{"title":"Fortsättningskurs i programmerings nättentamen 16.12.","information_page":true,"course_info_page":null,"path":"/onlinetent-ohja","hidden":null,"separator_after":null,"upcoming":null,"hide_in_sidebar":true,"sidebar_priority":2800}}},{"node":{"id":"279e1cca-fb0b-5d3f-b527-be05243d138d","frontmatter":{"title":"Introduktionskurs i programmerings nättentamen 17.08.","information_page":true,"course_info_page":null,"path":"/onlinetent-ohpe","hidden":null,"separator_after":null,"upcoming":null,"hide_in_sidebar":true,"sidebar_priority":2900}}},{"node":{"id":"96a0ec65-c0a3-5150-95c5-48c260f88162","frontmatter":{"title":"Modul 1","information_page":null,"course_info_page":null,"path":"/osa-1","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"8c9cf5a7-f02e-5ccf-ab8f-f078c3ccecb6","frontmatter":{"title":"Modul 10","information_page":null,"course_info_page":null,"path":"/osa-10","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"eb8ed7d7-c39d-52db-949d-f75649498c67","frontmatter":{"title":"Modul 11","information_page":null,"course_info_page":null,"path":"/osa-11","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"d21df33a-4bf4-5ee3-9e2a-431d57561f09","frontmatter":{"title":"Modul 12","information_page":null,"course_info_page":null,"path":"/osa-12","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"cdcf0ce1-e881-5ca6-8fa9-d0d343ec4e15","frontmatter":{"title":"Modul 13","information_page":null,"course_info_page":null,"path":"/osa-13","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"55c54859-8c7f-5fd9-8a8d-2c54ee677577","frontmatter":{"title":"Modul 14","information_page":null,"course_info_page":null,"path":"/osa-14","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"5523b5b8-61f8-506d-bfe0-c6cc774390ce","frontmatter":{"title":"Modul 2","information_page":null,"course_info_page":null,"path":"/osa-2","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"682b5a18-73c5-56e2-8374-237baccbab40","frontmatter":{"title":"Modul 3","information_page":null,"course_info_page":null,"path":"/osa-3","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"e125c469-ee19-505e-9a41-83da6143ecd8","frontmatter":{"title":"Modul 4","information_page":null,"course_info_page":null,"path":"/osa-4","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"161c0109-5c00-5bed-b7af-90dc7ddf5713","frontmatter":{"title":"Modul 5","information_page":null,"course_info_page":null,"path":"/osa-5","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"007d85ae-a118-5b10-8a7e-18302b15eba8","frontmatter":{"title":"Modul 6","information_page":null,"course_info_page":null,"path":"/osa-6","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"70ae841a-e6e9-5252-afae-6329fb67bcad","frontmatter":{"title":"Modul 7","information_page":null,"course_info_page":null,"path":"/osa-7","hidden":false,"separator_after":"Ohjelmoinnin jatkokurssi","upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"64715d0c-54f1-5aa7-a566-d2c5be6523ea","frontmatter":{"title":"Modul 8","information_page":null,"course_info_page":null,"path":"/osa-8","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"c1fef3f7-8ca3-5780-9dec-19bd672b4627","frontmatter":{"title":"Modul 9","information_page":null,"course_info_page":null,"path":"/osa-9","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}},{"node":{"id":"3bf12e3a-1542-57f4-812d-dd32cebc99e2","frontmatter":{"title":"Sanasto","information_page":true,"course_info_page":null,"path":"/sanasto","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":true,"sidebar_priority":null}}},{"node":{"id":"5d065309-85e8-5823-9527-ec1c3cf38d4d","frontmatter":{"title":"Handledning och stöd","information_page":true,"course_info_page":null,"path":"/stod","hidden":false,"separator_after":null,"upcoming":null,"hide_in_sidebar":null,"sidebar_priority":null}}}]}}} \ No newline at end of file diff --git a/report-issue/index.html b/report-issue/index.html index 95af06fac..84ecea761 100644 --- a/report-issue/index.html +++ b/report-issue/index.html @@ -134,4 +134,4 @@ } } }) -

Hur meddela om ett problem i materialet


Du kan rapportera ett problem på kursens GitHub-sida.

Det här kräver ett GitHub-konto som du kan skapa här

När du öppnar en issue, kolla här om det redan finns en öppen issue som gäller samma sak


Upplägget för en issue

  1. Klicka på knappen ovan.
  2. Skriv en kort och koncis rubrik.
  3. Beskriv problemet tillräckligt noggrannt.
  4. Välj "Submit new issue".

om vi svarar på din issue, vi kan behöva ytterligare information av dig.

Tack för att du hjälper oss förbättra materialet!

\ No newline at end of file +

Hur meddela om ett problem i materialet


Du kan rapportera ett problem på kursens GitHub-sida.

Det här kräver ett GitHub-konto som du kan skapa här

När du öppnar en issue, kolla här om det redan finns en öppen issue som gäller samma sak


Upplägget för en issue

  1. Klicka på knappen ovan.
  2. Skriv en kort och koncis rubrik.
  3. Beskriv problemet tillräckligt noggrannt.
  4. Välj "Submit new issue".

om vi svarar på din issue, vi kan behöva ytterligare information av dig.

Tack för att du hjälper oss förbättra materialet!

\ No newline at end of file diff --git a/sanasto/index.html b/sanasto/index.html index aa180888e..2bfa873a5 100644 --- a/sanasto/index.html +++ b/sanasto/index.html @@ -137,4 +137,4 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/sign-in/index.html b/sign-in/index.html index a712aefb1..2a02bfee3 100644 --- a/sign-in/index.html +++ b/sign-in/index.html @@ -140,4 +140,4 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/sign-up/index.html b/sign-up/index.html index 0bf2bdca0..a92d65203 100644 --- a/sign-up/index.html +++ b/sign-up/index.html @@ -140,4 +140,4 @@ } } }) -

Skapa konto

Kursen använder mooc.fi -konton. Om du tidigare har gjort Mooc.fi-kurser, logga in. På den här sidan kan du skapa ett konto som fungerar för majoriteten av Mooc.fi-kurserna.
\ No newline at end of file +

Skapa konto

Kursen använder mooc.fi -konton. Om du tidigare har gjort Mooc.fi-kurser, logga in. På den här sidan kan du skapa ett konto som fungerar för majoriteten av Mooc.fi-kurserna.
\ No newline at end of file diff --git a/stod/index.html b/stod/index.html index 0998b0843..112eedebd 100644 --- a/stod/index.html +++ b/stod/index.html @@ -138,7 +138,7 @@ } } }) -

Handledning och stöd

Höstens stödtider

+

Handledning och stöd

Höstens stödtider

Under hösten 2024 ordnas handledning både på Gumtäkts campus såväl som på Discord. På campus ordnas stödtillfällen i Exactum BK107. Handledningen ordnas enligt tidtabellen nedan:

@@ -223,4 +223,4 @@

Discord

Discord är en meddelandetjänst, som du kan hitta mer info om här.

Du hittar kursens kanaler här.

-

Under handledningstiden svarar handledaren på frågor i kurskanalen och via privata meddelanden på Discord. Discord används även för diskussioner och information utanför handledningstiderna, så det lönar sig att gå med!

\ No newline at end of file +

Under handledningstiden svarar handledaren på frågor i kurskanalen och via privata meddelanden på Discord. Discord används även för diskussioner och information utanför handledningstiderna, så det lönar sig att gå med!

\ No newline at end of file